Skip to content

Commit 4cd1a8f

Browse files
committed
add smart contract deployment
1 parent afc622f commit 4cd1a8f

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed

deploy-smart-contract.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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();

piggybank.abi.json

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
{
2+
"buildInfo": {
3+
"rustc": {
4+
"version": "1.78.0",
5+
"commitHash": "9b00956e56009bab2aa15d7bff10916599e3d6d6",
6+
"commitDate": "2024-04-29",
7+
"channel": "Stable",
8+
"short": "rustc 1.78.0 (9b00956e5 2024-04-29)"
9+
},
10+
"contractCrate": {
11+
"name": "piggybank",
12+
"version": "0.7.0"
13+
},
14+
"framework": {
15+
"name": "multiversx-sc",
16+
"version": "0.50.3"
17+
}
18+
},
19+
"name": "PiggyBank",
20+
"constructor": {
21+
"inputs": [],
22+
"outputs": []
23+
},
24+
"upgradeConstructor": {
25+
"inputs": [],
26+
"outputs": []
27+
},
28+
"endpoints": [
29+
{
30+
"name": "createPiggy",
31+
"mutability": "mutable",
32+
"inputs": [
33+
{
34+
"name": "lock_time",
35+
"type": "u64"
36+
}
37+
],
38+
"outputs": []
39+
},
40+
{
41+
"name": "addAmount",
42+
"mutability": "mutable",
43+
"payableInTokens": [
44+
"EGLD"
45+
],
46+
"inputs": [],
47+
"outputs": []
48+
},
49+
{
50+
"name": "payOut",
51+
"mutability": "mutable",
52+
"inputs": [],
53+
"outputs": []
54+
},
55+
{
56+
"name": "getLockedAmount",
57+
"mutability": "readonly",
58+
"inputs": [
59+
{
60+
"name": "piggy_owner",
61+
"type": "Address"
62+
}
63+
],
64+
"outputs": [
65+
{
66+
"type": "BigUint"
67+
}
68+
]
69+
},
70+
{
71+
"name": "getLockTime",
72+
"mutability": "readonly",
73+
"inputs": [
74+
{
75+
"name": "piggy_owner",
76+
"type": "Address"
77+
}
78+
],
79+
"outputs": [
80+
{
81+
"type": "u64"
82+
}
83+
]
84+
}
85+
],
86+
"esdtAttributes": [],
87+
"hasCallback": false,
88+
"types": {}
89+
}

piggybank.wasm

2.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)