Skip to content

Commit ef225fb

Browse files
committed
Add sdk/ton/configurer_test.go
1 parent ab58189 commit ef225fb

File tree

11 files changed

+1790
-32
lines changed

11 files changed

+1790
-32
lines changed

.mockery.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ packages:
2323
config:
2424
dir: "./sdk/ton/mocks"
2525
filename: "wallet.go"
26+
github.com/xssnick/tonutils-go/ton:
27+
config:
28+
all: false
29+
outpkg: "mock_ton"
30+
interfaces:
31+
APIClientWrapped:
32+
config:
33+
dir: "./sdk/ton/mocks"
34+
filename: "api.go"
2635
github.com/smartcontractkit/mcms/sdk:
2736
github.com/smartcontractkit/mcms/sdk/evm:
2837
github.com/smartcontractkit/mcms/sdk/evm/bindings:

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
github.com/smartcontractkit/chainlink-ccip/chains/solana/gobindings v0.0.0-20250805210128-7f8a0f403c3a
2121
github.com/smartcontractkit/chainlink-sui v0.0.0-20251016153021-3d4a0208cf70
2222
github.com/smartcontractkit/chainlink-testing-framework/framework v0.11.2
23-
github.com/smartcontractkit/chainlink-ton v0.0.0-20251102111607-ca45c30b0924
23+
github.com/smartcontractkit/chainlink-ton v0.0.0-20251102125826-96306f144e3c
2424
github.com/smartcontractkit/freeport v0.1.3-0.20250716200817-cb5dfd0e369e
2525
github.com/spf13/cast v1.10.0
2626
github.com/stretchr/testify v1.11.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,8 @@ github.com/smartcontractkit/chainlink-ton v0.0.0-20251030120805-3cc729d99e11 h1:
637637
github.com/smartcontractkit/chainlink-ton v0.0.0-20251030120805-3cc729d99e11/go.mod h1:2tLeF9Rgnbp+aMH+/9vwxpSCc4I+8eyBn9sJzLbMaLc=
638638
github.com/smartcontractkit/chainlink-ton v0.0.0-20251102111607-ca45c30b0924 h1:1GayyMDxwVtApmDrvNlzrawkuhxsHIiUQE6l8nlVc1A=
639639
github.com/smartcontractkit/chainlink-ton v0.0.0-20251102111607-ca45c30b0924/go.mod h1:2tLeF9Rgnbp+aMH+/9vwxpSCc4I+8eyBn9sJzLbMaLc=
640+
github.com/smartcontractkit/chainlink-ton v0.0.0-20251102125826-96306f144e3c h1:5Ay0tAj+vXwG90LY2dAQ/8YX3Dp0naapA6rO6AYoBl4=
641+
github.com/smartcontractkit/chainlink-ton v0.0.0-20251102125826-96306f144e3c/go.mod h1:2tLeF9Rgnbp+aMH+/9vwxpSCc4I+8eyBn9sJzLbMaLc=
640642
github.com/smartcontractkit/freeport v0.1.3-0.20250716200817-cb5dfd0e369e h1:Hv9Mww35LrufCdM9wtS9yVi/rEWGI1UnjHbcKKU0nVY=
641643
github.com/smartcontractkit/freeport v0.1.3-0.20250716200817-cb5dfd0e369e/go.mod h1:T4zH9R8R8lVWKfU7tUvYz2o2jMv1OpGCdpY2j2QZXzU=
642644
github.com/smartcontractkit/grpc-proxy v0.0.0-20240830132753-a7e17fec5ab7 h1:12ijqMM9tvYVEm+nR826WsrNi6zCKpwBhuApq127wHs=

sdk/ton/config_transformer_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ton
1+
package ton_test
22

33
import (
44
"crypto"
@@ -21,15 +21,17 @@ import (
2121
"github.com/xssnick/tonutils-go/ton/wallet"
2222
"github.com/xssnick/tonutils-go/tvm/cell"
2323

24+
tonmcms "github.com/smartcontractkit/mcms/sdk/ton"
25+
2426
"github.com/smartcontractkit/chainlink-ton/pkg/bindings/mcms/mcms"
2527
)
2628

27-
func makeRandomTestWallet(client *ton.APIClient, networkGlobalID int32) (*wallet.Wallet, error) {
29+
func makeRandomTestWallet(api wallet.TonAPI, networkGlobalID int32) (*wallet.Wallet, error) {
2830
v5r1Config := wallet.ConfigV5R1Final{
2931
NetworkGlobalID: networkGlobalID,
3032
Workchain: 0,
3133
}
32-
return wallet.FromSeed(client, wallet.NewSeed(), v5r1Config)
34+
return wallet.FromSeed(api, wallet.NewSeed(), v5r1Config)
3335
}
3436

3537
const KEY_UINT8 = 8
@@ -265,7 +267,7 @@ func Test_ConfigTransformer_ToConfig(t *testing.T) {
265267
t.Run(tt.name, func(t *testing.T) {
266268
t.Parallel()
267269

268-
transformer := NewConfigTransformer()
270+
transformer := tonmcms.NewConfigTransformer()
269271
got, err := transformer.ToConfig(tt.give)
270272

271273
if tt.wantErr != "" {
@@ -524,7 +526,7 @@ func Test_SetConfigInputs(t *testing.T) {
524526
t.Run(tt.name, func(t *testing.T) {
525527
t.Parallel()
526528

527-
transformer := NewConfigTransformer()
529+
transformer := tonmcms.NewConfigTransformer()
528530
got, err := transformer.ToChainConfig(tt.giveConfig, nil)
529531

530532
if tt.wantErr != "" {

sdk/ton/configurer.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,19 @@ func (c configurer) SetConfig(ctx context.Context, mcmsAddr string, cfg *types.C
6868
return types.TransactionResult{}, fmt.Errorf("invalid mcms address: %w", err)
6969
}
7070

71-
groupQuorum, groupParents, signerAddresses, signerGroups, err := evm.ExtractSetConfigInputs(cfg)
71+
groupQuorum, groupParents, signerAddresses, _signerGroups, err := evm.ExtractSetConfigInputs(cfg)
7272
if err != nil {
7373
return types.TransactionResult{}, fmt.Errorf("unable to extract set config inputs: %w", err)
7474
}
7575

7676
signerKeys := make([]mcms.SignerKey, len(signerAddresses))
7777
for i, addr := range signerAddresses {
78-
signerKeys[i] = mcms.SignerKey{
79-
Value: addr.Big(),
80-
}
78+
signerKeys[i] = mcms.SignerKey{Value: addr.Big()}
79+
}
80+
81+
signerGroups := make([]mcms.SignerGroup, len(_signerGroups))
82+
for i, g := range _signerGroups {
83+
signerGroups[i] = mcms.SignerGroup{Value: g}
8184
}
8285

8386
// Encode SetConfig message
@@ -96,7 +99,7 @@ func (c configurer) SetConfig(ctx context.Context, mcmsAddr string, cfg *types.C
9699
QueryID: rand.Uint64(),
97100

98101
SignerKeys: common.SnakeData[mcms.SignerKey](signerKeys),
99-
SignerGroups: common.SnakeData[uint8](signerGroups),
102+
SignerGroups: common.SnakeData[mcms.SignerGroup](signerGroups),
100103
GroupQuorums: gqDict,
101104
GroupParents: gpDict,
102105

sdk/ton/configurer_test.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
package ton_test
2+
3+
import (
4+
"context"
5+
"errors"
6+
"math/big"
7+
"testing"
8+
9+
"github.com/ethereum/go-ethereum/common"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/mock"
13+
"github.com/stretchr/testify/require"
14+
"github.com/xssnick/tonutils-go/tlb"
15+
"github.com/xssnick/tonutils-go/ton"
16+
"github.com/xssnick/tonutils-go/ton/wallet"
17+
18+
"github.com/smartcontractkit/mcms/internal/testutils/chaintest"
19+
tonmcms "github.com/smartcontractkit/mcms/sdk/ton"
20+
ton_mocks "github.com/smartcontractkit/mcms/sdk/ton/mocks"
21+
"github.com/smartcontractkit/mcms/types"
22+
)
23+
24+
// TestConfigurer_SetConfig tests the SetConfig method of the Configurer.
25+
func TestConfigurer_SetConfig(t *testing.T) {
26+
t.Parallel()
27+
28+
ctx := context.Background()
29+
30+
// Initialize the mock
31+
chainID := chaintest.Chain7ToniID
32+
api := ton_mocks.NewTonAPI(t)
33+
wallets := []*wallet.Wallet{
34+
must(makeRandomTestWallet(api, chainID)),
35+
must(makeRandomTestWallet(api, chainID)),
36+
must(makeRandomTestWallet(api, chainID)),
37+
must(makeRandomTestWallet(api, chainID)),
38+
}
39+
40+
tests := []struct {
41+
name string
42+
mcmAddr string
43+
cfg *types.Config
44+
clearRoot bool
45+
mockSetup func(m *ton_mocks.TonAPI)
46+
want string
47+
wantErr error
48+
}{
49+
{
50+
name: "success",
51+
mcmAddr: "EQADa3W6G0nSiTV4a6euRA42fU9QxSEnb-WeDpcrtWzA2jM8",
52+
cfg: &types.Config{
53+
Quorum: 2,
54+
Signers: []common.Address{
55+
common.Address(mustKey(wallets[1]).Bytes()),
56+
common.Address(mustKey(wallets[2]).Bytes()),
57+
},
58+
GroupSigners: []types.Config{
59+
{
60+
Quorum: 1,
61+
Signers: []common.Address{
62+
common.Address(mustKey(wallets[3]).Bytes()),
63+
},
64+
GroupSigners: nil,
65+
},
66+
},
67+
},
68+
clearRoot: true,
69+
mockSetup: func(m *ton_mocks.TonAPI) {
70+
// Mock CurrentMasterchainInfo
71+
m.EXPECT().CurrentMasterchainInfo(mock.Anything).
72+
Return(&ton.BlockIDExt{}, nil)
73+
74+
// Mock WaitForBlock
75+
apiw := ton_mocks.NewAPIClientWrapped(t)
76+
apiw.EXPECT().GetAccount(mock.Anything, mock.Anything, mock.Anything).
77+
Return(&tlb.Account{}, nil)
78+
79+
apiw.EXPECT().RunGetMethod(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
80+
Return(ton.NewExecutionResult([]any{big.NewInt(5)}), nil)
81+
82+
m.EXPECT().WaitForBlock(mock.Anything).
83+
Return(apiw)
84+
85+
// Mock SendTransaction to return an error
86+
m.EXPECT().SendExternalMessageWaitTransaction(mock.Anything, mock.Anything).
87+
Return(&tlb.Transaction{Hash: []byte{1, 2, 3, 4, 14}}, &ton.BlockIDExt{}, []byte{}, nil)
88+
},
89+
want: "010203040e",
90+
wantErr: nil,
91+
},
92+
{
93+
name: "failure - SendTransaction fails",
94+
mcmAddr: "EQADa3W6G0nSiTV4a6euRA42fU9QxSEnb-WeDpcrtWzA2jM8",
95+
cfg: &types.Config{
96+
Quorum: 2,
97+
Signers: []common.Address{
98+
common.Address(mustKey(wallets[1]).Bytes()),
99+
common.Address(mustKey(wallets[2]).Bytes()),
100+
},
101+
GroupSigners: []types.Config{
102+
{
103+
Quorum: 1,
104+
Signers: []common.Address{
105+
common.Address(mustKey(wallets[3]).Bytes()),
106+
},
107+
GroupSigners: nil,
108+
},
109+
},
110+
},
111+
clearRoot: false,
112+
mockSetup: func(m *ton_mocks.TonAPI) {
113+
// Mock CurrentMasterchainInfo
114+
m.EXPECT().CurrentMasterchainInfo(mock.Anything).
115+
Return(&ton.BlockIDExt{}, nil)
116+
117+
// Mock WaitForBlock
118+
apiw := ton_mocks.NewAPIClientWrapped(t)
119+
apiw.EXPECT().GetAccount(mock.Anything, mock.Anything, mock.Anything).
120+
Return(&tlb.Account{}, nil)
121+
122+
apiw.EXPECT().RunGetMethod(mock.Anything, mock.Anything, mock.Anything, mock.Anything).
123+
Return(ton.NewExecutionResult([]any{big.NewInt(5)}), nil)
124+
125+
m.EXPECT().WaitForBlock(mock.Anything).
126+
Return(apiw)
127+
128+
// Mock SendTransaction to return an error
129+
m.EXPECT().SendExternalMessageWaitTransaction(mock.Anything, mock.Anything).
130+
Return(&tlb.Transaction{Hash: []byte{1, 2, 3, 4, 15}}, &ton.BlockIDExt{}, []byte{}, errors.New("transaction failed"))
131+
},
132+
want: "",
133+
wantErr: errors.New("transaction failed"),
134+
},
135+
}
136+
137+
for _, tt := range tests {
138+
t.Run(tt.name, func(t *testing.T) {
139+
t.Parallel()
140+
141+
_api := ton_mocks.NewTonAPI(t)
142+
walletOperator := must(makeRandomTestWallet(_api, chainID))
143+
144+
// Apply the mock setup for the ContractDeployBackend
145+
if tt.mockSetup != nil {
146+
tt.mockSetup(_api)
147+
}
148+
149+
// Create the Configurer instance
150+
configurer, err := tonmcms.NewConfigurer(walletOperator, tlb.MustFromTON("0"))
151+
require.NoError(t, err)
152+
153+
// Call SetConfig
154+
tx, err := configurer.SetConfig(ctx, tt.mcmAddr, tt.cfg, tt.clearRoot)
155+
156+
// Assert the results
157+
if tt.wantErr != nil {
158+
require.Error(t, err)
159+
assert.Contains(t, err.Error(), tt.wantErr.Error())
160+
assert.Empty(t, tx.Hash)
161+
} else {
162+
require.NoError(t, err)
163+
assert.Equal(t, tt.want, tx.Hash)
164+
}
165+
})
166+
}
167+
}

sdk/ton/decoded_operation_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
package ton
1+
package ton_test
22

33
import (
44
"fmt"
55
"testing"
66

77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
9+
10+
"github.com/smartcontractkit/mcms/sdk/ton"
911
)
1012

1113
func TestNewDecodedOperation(t *testing.T) {
@@ -65,7 +67,7 @@ func TestNewDecodedOperation(t *testing.T) {
6567
t.Run(tt.name, func(t *testing.T) {
6668
t.Parallel()
6769

68-
got, err := NewDecodedOperation(tt.contractType, tt.msgType, tt.msgOpcode, tt.msgDecoded, tt.inputKeys, tt.inputArgs)
70+
got, err := ton.NewDecodedOperation(tt.contractType, tt.msgType, tt.msgOpcode, tt.msgDecoded, tt.inputKeys, tt.inputArgs)
6971
if tt.wantErr != "" {
7072
require.Error(t, err)
7173
assert.EqualError(t, err, tt.wantErr)

sdk/ton/decoder_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ton
1+
package ton_test
22

33
import (
44
"math/big"
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/smartcontractkit/chainlink-ton/pkg/bindings/lib/access/rbac"
1515

16+
"github.com/smartcontractkit/mcms/sdk/ton"
1617
"github.com/smartcontractkit/mcms/types"
1718
)
1819

@@ -48,14 +49,14 @@ func TestDecoder(t *testing.T) {
4849
name string
4950
give types.Operation
5051
contractInterfaces string
51-
want *DecodedOperation
52+
want *ton.DecodedOperation
5253
wantErr string
5354
}{
5455
{
5556
name: "success",
5657
give: types.Operation{
5758
ChainSelector: 1,
58-
Transaction: must(NewTransaction(
59+
Transaction: must(ton.NewTransaction(
5960
address.MustParseAddr("EQADa3W6G0nSiTV4a6euRA42fU9QxSEnb-WeDpcrtWzA2jM8"),
6061
grantRoleData.ToBuilder().ToSlice(),
6162
big.NewInt(0),
@@ -64,7 +65,7 @@ func TestDecoder(t *testing.T) {
6465
)),
6566
},
6667
contractInterfaces: "com.chainlink.ton.lib.access.RBAC",
67-
want: &DecodedOperation{
68+
want: &ton.DecodedOperation{
6869
ContractType: "com.chainlink.ton.lib.access.RBAC",
6970
MsgType: "GrantRole",
7071
MsgDecoded: map[string]interface{}{
@@ -83,7 +84,7 @@ func TestDecoder(t *testing.T) {
8384
t.Run(tt.name, func(t *testing.T) {
8485
t.Parallel()
8586

86-
d := NewDecoder()
87+
d := ton.NewDecoder()
8788
got, err := d.Decode(tt.give.Transaction, tt.contractInterfaces)
8889
if tt.wantErr != "" {
8990
require.Error(t, err)

0 commit comments

Comments
 (0)