|
| 1 | +import path from "path"; // md-ignore |
| 2 | +import { Account, Address, DevnetEntrypoint, TransactionWatcher } from "../src"; // md-ignore |
| 3 | +import { MultisigTransactionsOutcomeParser } from "../src/multisig/multisigTransactionsOutcomeParser"; |
| 4 | +import { loadAbiRegistry, loadContractCode } from "../src/testutils"; |
| 5 | +// md-start |
| 6 | +(async () => { |
| 7 | + // ### Multisig |
| 8 | + |
| 9 | + // The sdk contains components to interact with the [Multisig Contract](https://github.com/multiversx/mx-contracts-rs/releases/tag/v0.45.5). |
| 10 | + // We can deploy a multisig smart contract, add members, propose and execute actions and query the contract. |
| 11 | + // The same as the other components, to interact with a multisig smart contract we can use either the MultisigController or the MultisigTransactionsFactory. |
| 12 | + |
| 13 | + // These operations can be performed using both the **controller** and the **factory**. For a complete list of supported methods, please refer to the autogenerated documentation: |
| 14 | + // - `class:MultisigController` |
| 15 | + // - `class:MultisigTransactionsFactory` |
| 16 | + |
| 17 | + // #### Deploying a Multisig Smart Contract using the controller |
| 18 | + // ```js |
| 19 | + { |
| 20 | + const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json"); |
| 21 | + const bytecode = await loadContractCode("src/testdata/multisig-full.wasm"); |
| 22 | + |
| 23 | + // create the entrypoint and the multisig controller // md-as-comment |
| 24 | + const entrypoint = new DevnetEntrypoint(); |
| 25 | + const controller = entrypoint.createMultisigController(abi); |
| 26 | + |
| 27 | + const filePath = path.join("../src", "testdata", "testwallets", "alice.pem"); |
| 28 | + const alice = await Account.newFromPem(filePath); |
| 29 | + |
| 30 | + // fetch the nonce of the network // md-as-comment |
| 31 | + alice.nonce = await entrypoint.recallAccountNonce(alice.address); |
| 32 | + |
| 33 | + const transaction = await controller.createTransactionForDeploy(alice, alice.getNonceThenIncrement(), { |
| 34 | + quorum: 2, |
| 35 | + board: [ |
| 36 | + alice.address, |
| 37 | + Address.newFromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"), |
| 38 | + ], |
| 39 | + bytecode: bytecode.valueOf(), |
| 40 | + gasLimit: 100000000n, |
| 41 | + }); |
| 42 | + |
| 43 | + // sending the transaction // md-as-comment |
| 44 | + const txHash = await entrypoint.sendTransaction(transaction); |
| 45 | + |
| 46 | + // wait for transaction completion, extract multisig contract's address // md-as-comment |
| 47 | + const outcome = await controller.awaitCompletedDeploy(txHash); |
| 48 | + |
| 49 | + const contractAddress = outcome[0].contractAddress; |
| 50 | + } |
| 51 | + // ``` |
| 52 | + |
| 53 | + // #### Deploying a Multisig Smart Contract using the factory |
| 54 | + // ```js |
| 55 | + { |
| 56 | + const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json"); |
| 57 | + const bytecode = await loadContractCode("src/testdata/multisig-full.wasm"); |
| 58 | + |
| 59 | + // create the entrypoint and the multisig factory // md-as-comment |
| 60 | + const entrypoint = new DevnetEntrypoint(); |
| 61 | + const factory = entrypoint.createMultisigTransactionsFactory(abi); |
| 62 | + |
| 63 | + const filePath = path.join("../src", "testdata", "testwallets", "alice.pem"); |
| 64 | + const alice = await Account.newFromPem(filePath); |
| 65 | + |
| 66 | + // fetch the nonce of the network // md-as-comment |
| 67 | + alice.nonce = await entrypoint.recallAccountNonce(alice.address); |
| 68 | + |
| 69 | + const transaction = await factory.createTransactionForDeploy(alice.address, { |
| 70 | + quorum: 2, |
| 71 | + board: [ |
| 72 | + alice.address, |
| 73 | + Address.newFromBech32("erd1spyavw0956vq68xj8y4tenjpq2wd5a9p2c6j8gsz7ztyrnpxrruqzu66jx"), |
| 74 | + ], |
| 75 | + bytecode: bytecode.valueOf(), |
| 76 | + gasLimit: 100000000n, |
| 77 | + }); |
| 78 | + |
| 79 | + transaction.nonce = alice.getNonceThenIncrement(); |
| 80 | + transaction.signature = await alice.signTransaction(transaction); |
| 81 | + // sending the transaction // md-as-comment |
| 82 | + const txHash = await entrypoint.sendTransaction(transaction); |
| 83 | + } |
| 84 | + // ``` |
| 85 | + |
| 86 | + // #### Propose an action using the controller |
| 87 | + // We'll propose an action to send some EGLD to Carol. After we sent the proposal, we'll also parse the outcome of the transaction to get the `proposal id`. |
| 88 | + // The id can be used later for signing and performing the proposal. |
| 89 | + |
| 90 | + // ```js |
| 91 | + { |
| 92 | + // create the entrypoint and the multisig controller // md-as-comment |
| 93 | + const entrypoint = new DevnetEntrypoint(); |
| 94 | + const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json"); |
| 95 | + const controller = entrypoint.createMultisigController(abi); |
| 96 | + |
| 97 | + const filePath = path.join("../src", "testdata", "testwallets", "alice.pem"); |
| 98 | + const alice = await Account.newFromPem(filePath); |
| 99 | + |
| 100 | + // fetch the nonce of the network // md-as-comment |
| 101 | + alice.nonce = await entrypoint.recallAccountNonce(alice.address); |
| 102 | + |
| 103 | + const contract = Address.newFromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqf8llllswuedva"); |
| 104 | + |
| 105 | + const transaction = await controller.createTransactionForProposeTransferExecute( |
| 106 | + alice, |
| 107 | + alice.getNonceThenIncrement(), |
| 108 | + { |
| 109 | + multisigContract: contract, |
| 110 | + to: Address.newFromBech32("erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"), |
| 111 | + gasLimit: 10000000n, |
| 112 | + nativeTokenAmount: 1000000000000000000n, |
| 113 | + }, |
| 114 | + ); |
| 115 | + |
| 116 | + // sending the transaction // md-as-comment |
| 117 | + const txHash = await entrypoint.sendTransaction(transaction); |
| 118 | + |
| 119 | + // parse the outcome and get the proposal id |
| 120 | + const actionId = await controller.awaitCompletedPerformAction(txHash); |
| 121 | + } |
| 122 | + // ``` |
| 123 | + |
| 124 | + // #### Propose an action using the factory |
| 125 | + // Proposing an action for a multisig contract using the MultisigFactory is very similar to using the controller, but in order to get the proposal id, we need to use MultisigTransactionsOutcomeParser. |
| 126 | + |
| 127 | + // ```js |
| 128 | + { |
| 129 | + const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json"); |
| 130 | + |
| 131 | + // create the entrypoint and the multisig factory // md-as-comment |
| 132 | + const entrypoint = new DevnetEntrypoint(); |
| 133 | + const provider = entrypoint.createNetworkProvider(); |
| 134 | + const factory = entrypoint.createMultisigTransactionsFactory(abi); |
| 135 | + |
| 136 | + const filePath = path.join("../src", "testdata", "testwallets", "alice.pem"); |
| 137 | + const alice = await Account.newFromPem(filePath); |
| 138 | + |
| 139 | + const contract = Address.newFromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqf8llllswuedva"); |
| 140 | + |
| 141 | + const transaction = await factory.createTransactionForProposeTransferExecute(alice.address, { |
| 142 | + multisigContract: contract, |
| 143 | + to: Address.newFromBech32("erd1k2s324ww2g0yj38qn2ch2jwctdy8mnfxep94q9arncc6xecg3xaq6mjse8"), |
| 144 | + gasLimit: 10000000n, |
| 145 | + nativeTokenAmount: 1000000000000000000n, |
| 146 | + }); |
| 147 | + // fetch the nonce of the network // md-as-comment |
| 148 | + alice.nonce = await entrypoint.recallAccountNonce(alice.address); |
| 149 | + |
| 150 | + // set the nonce // md-as-comment |
| 151 | + transaction.nonce = alice.getNonceThenIncrement(); |
| 152 | + |
| 153 | + // sign the transaction // md-as-comment |
| 154 | + transaction.signature = await alice.signTransaction(transaction); |
| 155 | + |
| 156 | + // sending the transaction // md-as-comment |
| 157 | + const txHash = await entrypoint.sendTransaction(transaction); |
| 158 | + |
| 159 | + // wait for the transaction to execute |
| 160 | + const transactionAwaiter = new TransactionWatcher(provider); |
| 161 | + const transactionOnNetwork = await transactionAwaiter.awaitCompleted(txHash); |
| 162 | + |
| 163 | + // parse the outcome of the transaction |
| 164 | + const parser = new MultisigTransactionsOutcomeParser({ abi }); |
| 165 | + const actionId = parser.parseProposeAction(transactionOnNetwork); |
| 166 | + } |
| 167 | + // ``` |
| 168 | + |
| 169 | + // #### Querying the Multisig Smart Contract |
| 170 | + // Unlike creating transactions, querying the multisig can be performed only using the controller. |
| 171 | + // Let's query the contract to get all board members. |
| 172 | + |
| 173 | + // ```js |
| 174 | + { |
| 175 | + const abi = await loadAbiRegistry("src/testdata/multisig-full.abi.json"); |
| 176 | + |
| 177 | + // create the entrypoint and the multisig controller // md-as-comment |
| 178 | + const entrypoint = new DevnetEntrypoint(); |
| 179 | + const controller = entrypoint.createMultisigController(abi); |
| 180 | + |
| 181 | + const contract = Address.newFromBech32("erd1qqqqqqqqqqqqqqqpqqqqqqqqqqqqqqqqqqqqqqqqqqqqqf8llllswuedva"); |
| 182 | + |
| 183 | + const boardMembers = await controller.getAllBoardMembers({ multisigAddress: contract.toBech32() }); |
| 184 | + } |
| 185 | + // ``` |
| 186 | +})().catch((e) => { |
| 187 | + console.log({ e }); |
| 188 | +}); |
0 commit comments