|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package tone2e |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + |
| 11 | + "github.com/ethereum/go-ethereum/common" |
| 12 | + |
| 13 | + "github.com/xssnick/tonutils-go/address" |
| 14 | + "github.com/xssnick/tonutils-go/tlb" |
| 15 | + "github.com/xssnick/tonutils-go/ton" |
| 16 | + "github.com/xssnick/tonutils-go/ton/wallet" |
| 17 | + "github.com/xssnick/tonutils-go/tvm/cell" |
| 18 | + |
| 19 | + "github.com/smartcontractkit/chainlink-ton/pkg/bindings/mcms/mcms" |
| 20 | + "github.com/smartcontractkit/chainlink-ton/pkg/bindings/mcms/timelock" |
| 21 | + "github.com/smartcontractkit/chainlink-ton/pkg/ton/tracetracking" |
| 22 | + "github.com/smartcontractkit/chainlink-ton/pkg/ton/wrappers" |
| 23 | + |
| 24 | + "github.com/smartcontractkit/mcms/internal/testutils" |
| 25 | + "github.com/smartcontractkit/mcms/types" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + EnvPathContracts = "PATH_CONTRACTS_TON" |
| 30 | + |
| 31 | + PathContractsMCMS = "mcms.MCMS.compiled.json" |
| 32 | + PathContractsTimelock = "mcms.RBACTimelock.compiled.json" |
| 33 | +) |
| 34 | + |
| 35 | +func must[E any](out E, err error) E { |
| 36 | + if err != nil { |
| 37 | + panic(err) |
| 38 | + } |
| 39 | + |
| 40 | + return out |
| 41 | +} |
| 42 | + |
| 43 | +type DeployOpts struct { |
| 44 | + // Connection |
| 45 | + Client *ton.APIClient |
| 46 | + Wallet *wallet.Wallet |
| 47 | + |
| 48 | + // Deployment info |
| 49 | + ContractPath string |
| 50 | + |
| 51 | + Amount tlb.Coins |
| 52 | + Data any |
| 53 | + Body any |
| 54 | +} |
| 55 | + |
| 56 | +func DeployContract(ctx context.Context, opts DeployOpts) (*address.Address, error) { |
| 57 | + contractCode, err := wrappers.ParseCompiledContract(opts.ContractPath) |
| 58 | + if err != nil { |
| 59 | + return nil, fmt.Errorf("failed to parse compiled contract: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + contractData, ok := opts.Data.(*cell.Cell) // Cell or we try to decode |
| 63 | + if !ok { |
| 64 | + contractData, err = tlb.ToCell(opts.Data) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to create contract data cell: %w", err) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + bodyCell, ok := opts.Body.(*cell.Cell) // Cell or we try to decode |
| 71 | + if !ok { |
| 72 | + bodyCell, err = tlb.ToCell(opts.Body) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("failed to create contract body cell: %w", err) |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + _client := tracetracking.NewSignedAPIClient(opts.Client, *opts.Wallet) |
| 79 | + contract, _, err := wrappers.Deploy(ctx, &_client, contractCode, contractData, opts.Amount, bodyCell) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("failed to deploy contract: %w", err) |
| 82 | + } |
| 83 | + |
| 84 | + return contract.Address, nil |
| 85 | +} |
| 86 | + |
| 87 | +func DeployMCMSContract(ctx context.Context, client *ton.APIClient, w *wallet.Wallet, amount tlb.Coins, data mcms.Data) (*address.Address, error) { |
| 88 | + return DeployContract(ctx, DeployOpts{ |
| 89 | + Client: client, |
| 90 | + Wallet: w, |
| 91 | + ContractPath: filepath.Join(os.Getenv(EnvPathContracts), PathContractsMCMS), |
| 92 | + Amount: amount, |
| 93 | + Data: data, |
| 94 | + Body: cell.BeginCell().EndCell(), // empty cell, top up |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func DeployTimelockContract(ctx context.Context, client *ton.APIClient, w *wallet.Wallet, amount tlb.Coins, data timelock.Data, body timelock.Init) (*address.Address, error) { |
| 99 | + return DeployContract(ctx, DeployOpts{ |
| 100 | + Client: client, |
| 101 | + Wallet: w, |
| 102 | + ContractPath: filepath.Join(os.Getenv(EnvPathContracts), PathContractsTimelock), |
| 103 | + Amount: amount, |
| 104 | + Data: data, |
| 105 | + Body: body, |
| 106 | + }) |
| 107 | +} |
| 108 | + |
| 109 | +// GenSimpleTestMCMSConfig generates a simple test configuration that's used in e2e tests. |
| 110 | +func GenSimpleTestMCMSConfig(signers []testutils.ECDSASigner) *types.Config { |
| 111 | + return &types.Config{ |
| 112 | + Quorum: 1, |
| 113 | + Signers: []common.Address{signers[0].Address()}, |
| 114 | + GroupSigners: []types.Config{ |
| 115 | + { |
| 116 | + Quorum: 1, |
| 117 | + Signers: []common.Address{signers[1].Address()}, |
| 118 | + GroupSigners: []types.Config{}, |
| 119 | + }, |
| 120 | + }, |
| 121 | + } |
| 122 | +} |
0 commit comments