|
| 1 | +import { promises } from "node:fs"; |
| 2 | +import { |
| 3 | + TransactionComputer, |
| 4 | + TransactionsFactoryConfig, |
| 5 | + SmartContractTransactionsFactory, |
| 6 | + Code, |
| 7 | + Address, |
| 8 | + TransactionWatcher, |
| 9 | + SmartContractTransactionsOutcomeParser, |
| 10 | + TransactionsConverter, |
| 11 | +} from "@multiversx/sdk-core"; |
| 12 | +import { |
| 13 | + syncAndGetAccount, |
| 14 | + senderAddress, |
| 15 | + getSigner, |
| 16 | + apiNetworkProvider, |
| 17 | +} from "./setup.js"; |
| 18 | + |
| 19 | +const deploySmartContract = async () => { |
| 20 | + const user = await syncAndGetAccount(); |
| 21 | + const computer = new TransactionComputer(); |
| 22 | + const signer = await getSigner(); |
| 23 | + |
| 24 | + // Load smart contract code |
| 25 | + // For source code check: https://github.com/xdevguild/piggy-bank-sc/tree/master |
| 26 | + const codeBuffer = await promises.readFile("./piggybank.wasm"); |
| 27 | + const code = Code.fromBuffer(codeBuffer); |
| 28 | + |
| 29 | + // Load ABI file (not required for now, but will be useful when interacting with the SC) |
| 30 | + // Although it would be helpful if we had initial arguments to pass |
| 31 | + const abiFile = await promises.readFile("./piggybank.abi.json", "UTF-8"); |
| 32 | + |
| 33 | + // Prepare transfer transactions factory |
| 34 | + const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" }); |
| 35 | + let scFactory = new SmartContractTransactionsFactory({ |
| 36 | + config: factoryConfig, |
| 37 | + abi: abiFile, |
| 38 | + }); |
| 39 | + |
| 40 | + // Prepare deploy transaction |
| 41 | + const deployTransaction = scFactory.createTransactionForDeploy({ |
| 42 | + sender: new Address(senderAddress), |
| 43 | + bytecode: code.valueOf(), |
| 44 | + gasLimit: 10000000n, |
| 45 | + arguments: [], // Pass arguments for init function on SC, we don't have any on this smart contract |
| 46 | + // Below ones are optional with default values |
| 47 | + nativeTransferAmount: 0, // Sometimes you need to send EGLD to the init function on SC |
| 48 | + isUpgradeable: true, // You will be able to upgrade the contract |
| 49 | + isReadable: false, // You will be able to read its state through another contract |
| 50 | + isPayable: false, // You will be able to send funds to it |
| 51 | + isPayableBySmartContract: false, // Only smart contract can send funds to it |
| 52 | + }); |
| 53 | + |
| 54 | + // Increase the nonce |
| 55 | + deployTransaction.nonce = user.getNonceThenIncrement(); |
| 56 | + |
| 57 | + // Serialize the transaction for signing |
| 58 | + const serializedDeployTransaction = |
| 59 | + computer.computeBytesForSigning(deployTransaction); |
| 60 | + |
| 61 | + // Sign the transaction with out signer |
| 62 | + deployTransaction.signature = await signer.sign(serializedDeployTransaction); |
| 63 | + |
| 64 | + // Broadcast the transaction |
| 65 | + const txHash = await apiNetworkProvider.sendTransaction(deployTransaction); |
| 66 | + |
| 67 | + // You can compute the smart contract addres before broadcasting the transaction |
| 68 | + // https://docs.multiversx.com/sdk-and-tools/sdk-js/sdk-js-cookbook-v13#computing-the-contract-address |
| 69 | + // But let's see how to get it from the network after deployment |
| 70 | + |
| 71 | + console.log("Pending..."); |
| 72 | + |
| 73 | + // Get the transaction on network, we need to wait for results here, we use TransactionWatcher for that |
| 74 | + const transactionOnNetwork = await new TransactionWatcher( |
| 75 | + apiNetworkProvider |
| 76 | + ).awaitCompleted(txHash); |
| 77 | + |
| 78 | + // Now let's parse the results with TransactionsConverter and SmartContractTransactionsOutcomeParser |
| 79 | + const converter = new TransactionsConverter(); |
| 80 | + const parser = new SmartContractTransactionsOutcomeParser(); |
| 81 | + const transactionOutcome = |
| 82 | + converter.transactionOnNetworkToOutcome(transactionOnNetwork); |
| 83 | + const parsedOutcome = parser.parseDeploy({ transactionOutcome }); |
| 84 | + |
| 85 | + console.log(parsedOutcome); |
| 86 | + |
| 87 | + console.log( |
| 88 | + `Smart Contract deployed. Here it is:\nhttps://devnet-explorer.multiversx.com/accounts/${parsedOutcome.contracts[0].address}\n\nCheck the transaction in the Explorer:\nhttps://devnet-explorer.multiversx.com/transactions/${txHash}` |
| 89 | + ); |
| 90 | +}; |
| 91 | + |
| 92 | +deploySmartContract(); |
0 commit comments