77 "fmt"
88 "math/big"
99 "math/rand/v2"
10+ "reflect"
1011
1112 "github.com/ethereum/go-ethereum/common"
1213
@@ -38,13 +39,25 @@ type executor struct {
3839}
3940
4041// NewExecutor creates a new Executor for TON chains
41- func NewExecutor (encoder sdk.Encoder , client * ton.APIClient , wallet * wallet.Wallet , amount tlb.Coins ) sdk.Executor {
42+ func NewExecutor (encoder sdk.Encoder , client ton.APIClientWrapped , wallet * wallet.Wallet , amount tlb.Coins ) (sdk.Executor , error ) {
43+ if IsNil (encoder ) {
44+ return nil , errors .New ("failed to create sdk.Executor - encoder (sdk.Encoder) is nil" )
45+ }
46+
47+ if IsNil (client ) {
48+ return nil , errors .New ("failed to create sdk.Executor - client (ton.APIClientWrapped) is nil" )
49+ }
50+
51+ if wallet == nil {
52+ return nil , errors .New ("failed to create sdk.Executor - wallet (*wallet.Wallet) is nil" )
53+ }
54+
4255 return & executor {
4356 Encoder : encoder ,
4457 Inspector : NewInspector (client , NewConfigTransformer ()),
4558 wallet : wallet ,
4659 amount : amount ,
47- }
60+ }, nil
4861}
4962
5063func (e * executor ) ExecuteOperation (
@@ -54,10 +67,6 @@ func (e *executor) ExecuteOperation(
5467 proof []common.Hash ,
5568 op types.Operation ,
5669) (types.TransactionResult , error ) {
57- if e .Encoder == nil {
58- return types.TransactionResult {}, errors .New ("executor was created without an encoder" )
59- }
60-
6170 oe , ok := e .Encoder .(OperationEncoder [mcms.Op ])
6271 if ! ok {
6372 return types.TransactionResult {}, fmt .Errorf ("failed to assert OperationEncoder" )
@@ -109,7 +118,7 @@ func (e *executor) ExecuteOperation(
109118 // TODO: do we wait for execution trace?
110119 tx , _ , err := e .wallet .SendWaitTransaction (ctx , msg )
111120 if err != nil {
112- return types.TransactionResult {}, fmt .Errorf ("failed to set config : %w" , err )
121+ return types.TransactionResult {}, fmt .Errorf ("failed to execute op : %w" , err )
113122 }
114123
115124 return types.TransactionResult {
@@ -127,16 +136,6 @@ func (e *executor) SetRoot(
127136 validUntil uint32 ,
128137 sortedSignatures []types.Signature ,
129138) (types.TransactionResult , error ) {
130- if e .Encoder == nil {
131- return types.TransactionResult {}, errors .New ("Executor was created without an encoder" )
132- }
133-
134- // Map to Ton Address type
135- dstAddr , err := address .ParseAddr (metadata .MCMAddress )
136- if err != nil {
137- return types.TransactionResult {}, fmt .Errorf ("invalid timelock address: %w" , err )
138- }
139-
140139 rme , ok := e .Encoder .(RootMetadataEncoder [mcms.RootMetadata ])
141140 if ! ok {
142141 return types.TransactionResult {}, fmt .Errorf ("failed to assert RootMetadataEncoder" )
@@ -147,6 +146,12 @@ func (e *executor) SetRoot(
147146 return types.TransactionResult {}, fmt .Errorf ("failed to convert to root metadata: %w" , err )
148147 }
149148
149+ // Map to Ton Address type
150+ dstAddr , err := address .ParseAddr (metadata .MCMAddress )
151+ if err != nil {
152+ return types.TransactionResult {}, fmt .Errorf ("invalid timelock address: %w" , err )
153+ }
154+
150155 // Encode proofs
151156 pe , ok := e .Encoder .(ProofEncoder [mcms.Proof ])
152157 if ! ok {
@@ -197,7 +202,7 @@ func (e *executor) SetRoot(
197202 // TODO: do we wait for execution trace?
198203 tx , _ , err := e .wallet .SendWaitTransaction (ctx , msg )
199204 if err != nil {
200- return types.TransactionResult {}, fmt .Errorf ("failed to set config : %w" , err )
205+ return types.TransactionResult {}, fmt .Errorf ("failed to set root : %w" , err )
201206 }
202207
203208 return types.TransactionResult {
@@ -206,3 +211,18 @@ func (e *executor) SetRoot(
206211 RawData : tx ,
207212 }, nil
208213}
214+
215+ // IsNil checks if a value is nil or if it's a reference type with a nil underlying value.
216+ // Notice: vendoring github:samber/lo
217+ func IsNil (x any ) bool {
218+ if x == nil {
219+ return true
220+ }
221+ v := reflect .ValueOf (x )
222+ switch v .Kind () {
223+ case reflect .Chan , reflect .Func , reflect .Map , reflect .Pointer , reflect .UnsafePointer , reflect .Interface , reflect .Slice :
224+ return v .IsNil ()
225+ default :
226+ return false
227+ }
228+ }
0 commit comments