forked from 5ire-tech/wasm-contract-deployment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.js
72 lines (55 loc) · 2.11 KB
/
deploy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { CodePromise, Abi, ContractPromise } from '@polkadot/api-contract';
import { ApiPromise, WsProvider, Keyring} from '@polkadot/api';
// import .contract file as json string
import { json } from "./abi.js"
import {
mnemonicToMiniSecret,
naclKeypairFromSeed,
} from "@polkadot/util-crypto";
try {
let address; // variable for storing the address of the deployed contract
// API creation for connection to the chain
const wsProvider = new WsProvider('ws://127.0.0.1:9944');
const api = await ApiPromise.create({ provider: wsProvider });
// convert json into usable contract ABI
let contractAbi = new Abi(json, api?.registry?.getChainProperties());
// instantiating wasm blob on the blockchain
const code = new CodePromise(api, json, json.source.wasm);
// gas limit for deployment
const gasLimit = 100000n * 1000000n
// endoement
const value = 100n * 1000n
// adding fire account for paying the gas fee
const PHRASE = 'solar avocado consider wide hurdle civil message curious despair bicycle sleep live';
const seedUser = mnemonicToMiniSecret(PHRASE);
const keyring = new Keyring({ type: "ed25519" });
const userKeyring = keyring.addFromPair(naclKeypairFromSeed(seedUser));
// parameters for constructor function inside the contract
let params = [];
params.push(userKeyring.publicKey);
params.push(userKeyring.publicKey);
let constructorIndex = 0;
try {
// upload wasm blob
let transferMethod = code && contractAbi?.constructors[constructorIndex]?.method && value
? code.tx[contractAbi.constructors[constructorIndex].method]({
gasLimit: gasLimit,
storageDepositLimit: null,
value: value
}, ...params)
: null;
// code deploy
const unsub = await transferMethod.signAndSend(userKeyring, async (response) => {
if (response.status.isInBlock || response.status.isFinalized) {
address = response.contract.address.toString();
console.log("address ====== ", address);
unsub();
}
});
} catch (e) {
console.log("error catch", e);
}
}
catch(err){
console.log("error",err.toString())
}