|
| 1 | +//go:build e2e |
| 2 | + |
| 3 | +package sui |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/block-vision/sui-go-sdk/signer" |
| 7 | + "github.com/block-vision/sui-go-sdk/sui" |
| 8 | + "github.com/stretchr/testify/suite" |
| 9 | + |
| 10 | + cselectors "github.com/smartcontractkit/chain-selectors" |
| 11 | + |
| 12 | + "github.com/smartcontractkit/chainlink-sui/bindings/bind" |
| 13 | + modulemcms "github.com/smartcontractkit/chainlink-sui/bindings/generated/mcms/mcms" |
| 14 | + modulemcmsaccount "github.com/smartcontractkit/chainlink-sui/bindings/generated/mcms/mcms_account" |
| 15 | + modulemcmsuser "github.com/smartcontractkit/chainlink-sui/bindings/generated/mcms/mcms_user" |
| 16 | + "github.com/smartcontractkit/chainlink-sui/bindings/packages/mcms" |
| 17 | + mcmsuser "github.com/smartcontractkit/chainlink-sui/bindings/packages/mcms/mcms_user" |
| 18 | + bindutils "github.com/smartcontractkit/chainlink-sui/bindings/utils" |
| 19 | + |
| 20 | + e2e "github.com/smartcontractkit/mcms/e2e/tests" |
| 21 | + "github.com/smartcontractkit/mcms/types" |
| 22 | +) |
| 23 | + |
| 24 | +type SuiTestSuite struct { |
| 25 | + suite.Suite |
| 26 | + e2e.TestSetup |
| 27 | + |
| 28 | + client sui.ISuiAPI |
| 29 | + signer bindutils.SuiSigner |
| 30 | + |
| 31 | + chainSelector types.ChainSelector |
| 32 | + |
| 33 | + // MCMS |
| 34 | + mcmsPackageID string |
| 35 | + mcms modulemcms.IMcms |
| 36 | + mcmsObj string |
| 37 | + timelockObj string |
| 38 | + depStateObj string |
| 39 | + registryObj string |
| 40 | + accountObj string |
| 41 | + ownerCapObj string |
| 42 | + |
| 43 | + // MCMS Account |
| 44 | + mcmsAccount modulemcmsaccount.IMcmsAccount |
| 45 | + |
| 46 | + // MCMS User |
| 47 | + mcmsUserPackageId string |
| 48 | + mcmsUser modulemcmsuser.IMcmsUser |
| 49 | + mcmsUserOwnerCapObj string |
| 50 | + |
| 51 | + // State Object passed into `mcms_entrypoint` |
| 52 | + stateObj string |
| 53 | +} |
| 54 | + |
| 55 | +func (s *SuiTestSuite) SetupSuite() { |
| 56 | + s.TestSetup = *e2e.InitializeSharedTestSetup(s.T()) |
| 57 | + |
| 58 | + account := s.SuiBlockchain.NetworkSpecificData.SuiAccount |
| 59 | + |
| 60 | + // Create a Sui signer from the mnemonic using the block-vision SDK |
| 61 | + signerAccount, err := signer.NewSignertWithMnemonic(account.Mnemonic) |
| 62 | + s.Require().NoError(err, "Failed to create signer from mnemonic") |
| 63 | + |
| 64 | + // Get the private key from the signer |
| 65 | + privateKey := signerAccount.PriKey |
| 66 | + testSigner := NewTestPrivateKeySigner(privateKey) |
| 67 | + |
| 68 | + // Set up Sui client |
| 69 | + s.client = s.SuiClient |
| 70 | + // TODO: Find funded accounts |
| 71 | + s.signer = testSigner |
| 72 | + s.chainSelector = types.ChainSelector(cselectors.SUI_TESTNET.Selector) |
| 73 | +} |
| 74 | + |
| 75 | +func (s *SuiTestSuite) DeployMCMSContract() { |
| 76 | + gasBudget := uint64(300_000_000) |
| 77 | + mcmsPackage, tx, err := mcms.PublishMCMS(s.T().Context(), &bind.CallOpts{ |
| 78 | + Signer: s.signer, |
| 79 | + GasBudget: &gasBudget, |
| 80 | + WaitForExecution: true, |
| 81 | + }, s.client) |
| 82 | + s.Require().NoError(err, "Failed to publish MCMS package") |
| 83 | + s.mcmsPackageID = mcmsPackage.Address() |
| 84 | + s.mcms = mcmsPackage.MCMS() |
| 85 | + |
| 86 | + mcmsObject, err := bind.FindObjectIdFromPublishTx(*tx, "mcms", "MultisigState") |
| 87 | + s.Require().NoError(err, "Failed to find object IDs in publish tx") |
| 88 | + timelockObj, err := bind.FindObjectIdFromPublishTx(*tx, "mcms", "Timelock") |
| 89 | + s.Require().NoError(err, "Failed to find object IDs in publish tx") |
| 90 | + depState, err := bind.FindObjectIdFromPublishTx(*tx, "mcms_deployer", "DeployerState") |
| 91 | + s.Require().NoError(err, "Failed to find object IDs in publish tx") |
| 92 | + reg, err := bind.FindObjectIdFromPublishTx(*tx, "mcms_registry", "Registry") |
| 93 | + s.Require().NoError(err, "Failed to find object IDs in publish tx") |
| 94 | + acc, err := bind.FindObjectIdFromPublishTx(*tx, "mcms_account", "AccountState") |
| 95 | + s.Require().NoError(err, "Failed to find object IDs in publish tx") |
| 96 | + ownCap, err := bind.FindObjectIdFromPublishTx(*tx, "mcms_account", "OwnerCap") |
| 97 | + s.Require().NoError(err, "Failed to find object IDs in publish tx") |
| 98 | + |
| 99 | + s.mcmsObj = mcmsObject |
| 100 | + s.timelockObj = timelockObj |
| 101 | + s.depStateObj = depState |
| 102 | + s.registryObj = reg |
| 103 | + s.accountObj = acc |
| 104 | + s.ownerCapObj = ownCap |
| 105 | + |
| 106 | + s.mcmsAccount, err = modulemcmsaccount.NewMcmsAccount(s.mcmsPackageID, s.client) |
| 107 | + s.Require().NoError(err, "Failed to create MCMS account instance") |
| 108 | +} |
| 109 | + |
| 110 | +func (s *SuiTestSuite) DeployMCMSUserContract() { |
| 111 | + gasBudget := uint64(300_000_000) |
| 112 | + signerAddress, err := s.signer.GetAddress() |
| 113 | + s.Require().NoError(err, "Failed to get address") |
| 114 | + |
| 115 | + mcmsUserPackage, tx, err := mcmsuser.PublishMCMSUser(s.T().Context(), &bind.CallOpts{ |
| 116 | + Signer: s.signer, |
| 117 | + GasBudget: &gasBudget, |
| 118 | + WaitForExecution: true, |
| 119 | + }, s.client, s.mcmsPackageID, signerAddress) |
| 120 | + s.Require().NoError(err, "Failed to publish MCMS user package") |
| 121 | + |
| 122 | + s.mcmsUserPackageId = mcmsUserPackage.Address() |
| 123 | + s.mcmsUser = mcmsUserPackage.MCMSUser() |
| 124 | + |
| 125 | + userDataObj, err := bind.FindObjectIdFromPublishTx(*tx, "mcms_user", "UserData") |
| 126 | + s.Require().NoError(err, "Failed to find object IDs in publish tx") |
| 127 | + mcmsUserOwnerCapObj, err := bind.FindObjectIdFromPublishTx(*tx, "mcms_user", "OwnerCap") |
| 128 | + s.Require().NoError(err, "Failed to find object IDs in publish tx") |
| 129 | + |
| 130 | + s.mcmsUserOwnerCapObj = mcmsUserOwnerCapObj |
| 131 | + s.stateObj = userDataObj |
| 132 | + |
| 133 | + // For executing, We need to register OwnerCap with MCMS |
| 134 | + { |
| 135 | + tx, err := s.mcmsUser.RegisterMcmsEntrypoint( |
| 136 | + s.T().Context(), |
| 137 | + &bind.CallOpts{ |
| 138 | + Signer: s.signer, |
| 139 | + WaitForExecution: true, |
| 140 | + }, |
| 141 | + bind.Object{Id: s.mcmsUserOwnerCapObj}, |
| 142 | + bind.Object{Id: s.registryObj}, |
| 143 | + bind.Object{Id: s.stateObj}, |
| 144 | + ) |
| 145 | + s.Require().NoError(err, "Failed to register with MCMS") |
| 146 | + s.Require().NotEmpty(tx, "Transaction should not be empty") |
| 147 | + |
| 148 | + s.T().Logf("✅ Registered with MCMS in tx: %s", tx.Digest) |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +func Must[T any](t T, err error) T { |
| 153 | + if err != nil { |
| 154 | + panic(err) |
| 155 | + } |
| 156 | + |
| 157 | + return t |
| 158 | +} |
0 commit comments