-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.ts
85 lines (80 loc) · 2.32 KB
/
utils.ts
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
73
74
75
76
77
78
79
80
81
82
83
84
85
import fs from "fs";
import { ApiPromise } from "@polkadot/api";
import { Abi } from "@polkadot/api-contract";
import { KeyringPair } from "@polkadot/keyring/types";
import { HexString } from "@polkadot/util/types";
import {
ContractInstantiateResult,
WeightV2,
} from "@polkadot/types/interfaces";
export type Addresses = {
adder: string;
};
/**
* Uploads the contract to the chain.
* @param api - The api instance.
* @param deployer - The deployer keyring pair.
* @param contractName - The file name of the contract to upload.
* @returns code hash of the deployed contract.
*/
export async function uploadCode(
api: ApiPromise,
deployer: KeyringPair,
contractName: string,
): Promise<HexString> {
const tokenContractRaw = JSON.parse(
fs.readFileSync(__dirname + `/artifacts/` + contractName, "utf8"),
);
const tokenAbi = new Abi(tokenContractRaw);
const _txHash = await new Promise(async (resolve, reject) => {
const unsub = await api.tx.contracts
.uploadCode(tokenAbi.info.source.wasm, null, 0)
.signAndSend(deployer, (result) => {
if (result.isFinalized) {
unsub();
resolve(result.txHash);
}
if (result.isError) {
unsub();
reject(result);
}
});
});
return tokenAbi.info.source.wasmHash.toHex();
}
/**
* Stores addresses in a JSON file.
* @param addresses - The addresses to store.
*/
export function storeAddresses(addresses: Addresses): void {
fs.writeFileSync(
__dirname + "/addresses.json",
JSON.stringify(addresses, null, 2),
);
}
/**
* Estimates gas required to create a new instance of the contract.
*
* NOTE: This shouldn't be necessary but `Contract::new()` doesn't estimate gas and uses a hardcoded value.
*/
export async function estimateContractInit(
api: ApiPromise,
deployer: KeyringPair,
contractName: string,
sampleArgs: unknown[],
): Promise<WeightV2> {
const contractRaw = JSON.parse(
fs.readFileSync(__dirname + `/artifacts/` + contractName, "utf8"),
);
const contractAbi = new Abi(contractRaw);
const { gasRequired } = (await api.call.contractsApi.instantiate(
deployer.address,
0,
null,
null,
{ Upload: contractAbi.info.source.wasm },
contractAbi.constructors[0].toU8a(sampleArgs),
"",
)) as unknown as ContractInstantiateResult;
return gasRequired;
}