|
| 1 | +import { promises } from "node:fs"; |
| 2 | +import { |
| 3 | + Address, |
| 4 | + SmartContractTransactionsFactory, |
| 5 | + TransactionsFactoryConfig, |
| 6 | + TransactionComputer, |
| 7 | + AbiRegistry, |
| 8 | + QueryRunnerAdapter, |
| 9 | + SmartContractQueriesController, |
| 10 | +} from "@multiversx/sdk-core"; |
| 11 | +import { |
| 12 | + syncAndGetAccount, |
| 13 | + senderAddress, |
| 14 | + getSigner, |
| 15 | + apiNetworkProvider, |
| 16 | +} from "./setup.js"; |
| 17 | + |
| 18 | +/** |
| 19 | + * Replace with your own deployed piggy bank smart contract |
| 20 | + * check deploy-smart-contract.js on how to deploy one |
| 21 | + */ |
| 22 | +const PIGGYBANK_CONTRACT_ADDRESS = |
| 23 | + "erd1qqqqqqqqqqqqqpgqtrajzw4vq0zxccdt9u66cvgg6vz8c6cwnegqkfqkpq"; |
| 24 | + |
| 25 | +/** |
| 26 | + * Load ABI file |
| 27 | + */ |
| 28 | +const getAbi = async () => { |
| 29 | + const abiFile = await promises.readFile("./piggybank.abi.json", "UTF-8"); |
| 30 | + return JSON.parse(abiFile); |
| 31 | +}; |
| 32 | + |
| 33 | +const scTransaction = async ({ functionName, args, amount }) => { |
| 34 | + const user = await syncAndGetAccount(); |
| 35 | + const computer = new TransactionComputer(); |
| 36 | + const signer = await getSigner(); |
| 37 | + |
| 38 | + const abiObj = await getAbi(); |
| 39 | + |
| 40 | + const factoryConfig = new TransactionsFactoryConfig({ chainID: "D" }); |
| 41 | + const factory = new SmartContractTransactionsFactory({ |
| 42 | + config: factoryConfig, |
| 43 | + abi: AbiRegistry.create(abiObj), |
| 44 | + }); |
| 45 | + |
| 46 | + const transaction = factory.createTransactionForExecute({ |
| 47 | + sender: new Address(senderAddress), |
| 48 | + contract: Address.fromBech32(PIGGYBANK_CONTRACT_ADDRESS), |
| 49 | + function: functionName, |
| 50 | + gasLimit: 5000000, |
| 51 | + arguments: args || [], |
| 52 | + nativeTransferAmount: amount, |
| 53 | + }); |
| 54 | + |
| 55 | + // Increase the nonce |
| 56 | + transaction.nonce = user.getNonceThenIncrement(); |
| 57 | + |
| 58 | + // Serialize the transaction for signing |
| 59 | + const serializedTransaction = computer.computeBytesForSigning(transaction); |
| 60 | + |
| 61 | + // Sign the transaction with our signer |
| 62 | + transaction.signature = await signer.sign(serializedTransaction); |
| 63 | + |
| 64 | + // Broadcast the transaction |
| 65 | + const txHash = await apiNetworkProvider.sendTransaction(transaction); |
| 66 | + |
| 67 | + console.log( |
| 68 | + "Check in the Explorer: ", |
| 69 | + `https://devnet-explorer.multiversx.com/transactions/${txHash}` |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +/** |
| 74 | + * Call the createPiggy endpoint on the PiggyBank smart contract |
| 75 | + * https://github.com/xdevguild/piggy-bank-sc/blob/master/src/lib.rs#L25 |
| 76 | + * We pass the unix timestamp in the future |
| 77 | + */ |
| 78 | +const createPiggy = async () => { |
| 79 | + await scTransaction({ |
| 80 | + functionName: "createPiggy", |
| 81 | + args: [1750686756], |
| 82 | + }); |
| 83 | +}; |
| 84 | + |
| 85 | +/** |
| 86 | + * Call the addAmount endpoint on the PiggyBank smart contract |
| 87 | + * https://github.com/xdevguild/piggy-bank-sc/blob/master/src/lib.rs#L42 |
| 88 | + */ |
| 89 | +const addAmount = async () => { |
| 90 | + await scTransaction({ |
| 91 | + functionName: "addAmount", |
| 92 | + amount: 1000000000000000n, |
| 93 | + }); |
| 94 | +}; |
| 95 | + |
| 96 | +/** |
| 97 | + * Query the getLockedAmount endpoint on the PiggyBank smart contract |
| 98 | + * https://github.com/xdevguild/piggy-bank-sc/blob/master/src/lib.rs#L92 |
| 99 | + */ |
| 100 | +const getLockedAmount = async () => { |
| 101 | + const abiObj = await getAbi(); |
| 102 | + |
| 103 | + const queryRunner = new QueryRunnerAdapter({ |
| 104 | + networkProvider: apiNetworkProvider, |
| 105 | + }); |
| 106 | + |
| 107 | + const controller = new SmartContractQueriesController({ |
| 108 | + queryRunner: queryRunner, |
| 109 | + abi: AbiRegistry.create(abiObj), |
| 110 | + }); |
| 111 | + |
| 112 | + const query = controller.createQuery({ |
| 113 | + contract: PIGGYBANK_CONTRACT_ADDRESS, |
| 114 | + function: "getLockedAmount", |
| 115 | + arguments: [senderAddress], |
| 116 | + }); |
| 117 | + |
| 118 | + const response = await controller.runQuery(query); |
| 119 | + |
| 120 | + const [amount] = controller.parseQueryResponse(response); |
| 121 | + |
| 122 | + // The returned amount is a BigNumber |
| 123 | + console.log("Locked amount is: ", amount.valueOf()); |
| 124 | +}; |
| 125 | + |
| 126 | +/** |
| 127 | + * Here we will manage which function to call |
| 128 | + */ |
| 129 | +const smartContractInteractions = () => { |
| 130 | + const args = process.argv.slice(2); |
| 131 | + const functionName = args[0]; |
| 132 | + |
| 133 | + const functions = { |
| 134 | + createPiggy, |
| 135 | + addAmount, |
| 136 | + getLockedAmount, |
| 137 | + }; |
| 138 | + |
| 139 | + if (functionName in functions) { |
| 140 | + functions[functionName](); |
| 141 | + } else { |
| 142 | + console.log("Function not found!"); |
| 143 | + } |
| 144 | +}; |
| 145 | + |
| 146 | +smartContractInteractions(); |
0 commit comments