-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.js
100 lines (88 loc) · 3.98 KB
/
setup.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import 'zx/globals'
import { decodeIcrcAccount } from '@dfinity/ledger';
import dotenv from 'dotenv';
import {
checkFilesExists,
zxRaw,
restartDfxBackgroundClean,
getCurrentDfxIdPrincipal,
getCanisterId,
getICRC1TokenCanisterDfxDeploymentCmd,
getICRC1TransferDfxCmd,
} from './script-utils.js';
// Load the environmental variables from the .env file:
dotenv.config();
async function run(testing = false) {
await restartDfxBackgroundClean();
// Check that the ICRC1 token canister wasm and did are downloaded, if not do so:
if (!checkFilesExists([ './src/icrc1-token-canister/icrc1.wasm', './src/icrc1-token-canister/icrc1.did' ])) {
await $`./src/icrc1-token-canister/install.sh`;
};
// Create all the canisters in order so ids are recreated the same:
await $`dfx canister create icrc1_token_canister`;
await $`dfx canister create internet_identity`;
await $`dfx canister create backend`;
await $`dfx canister create frontend`;
// Deploy the ICRC1 token canister (using current dfx identity as principal):
const currentPrincipal = await getCurrentDfxIdPrincipal();
try {
await zxRaw(getICRC1TokenCanisterDfxDeploymentCmd({
mintersPrincipal: currentPrincipal,
dfxJsonIcrc1CanisterName: "icrc1_token_canister",
tokenName: "CVC ICRC1 Mock Token",
tokenSymbol: "CVCMICRC1"
}));
} catch (p) {
throw new Error(`While deploying icrc1 token canister caught error ${p.toString()}`);
};
// Deploy the testnet Internet Identity canister:
try {
await $`dfx deploy internet_identity`
} catch (p) {
throw new Error(`While deploying internet_identity canister caught error ${p.toString()}`);
};
// Get the canister id of the ICRC1 token canister just deployed (could be hard-coded, this is for demonstration):
const icrc1TokenCanisterId = await getCanisterId("icrc1_token_canister");
// Deploy the backend ("payment processing") canister and setting the ICRC1 token canister id it uses:
try {
await $`dfx deploy backend`
await $`dfx generate backend`
await $`dfx canister call backend set_icrc1_token_canister '( "${icrc1TokenCanisterId}" )'`;
} catch (p) {
throw new Error(`While deploying and generating backend canister caught error ${p.toString()}`);
};
// Now deploy the frontend canister, note that it has all the other canisters as a dependency so the .env
// generated by dfx will be sure to include all the canister ids:
try {
await $`dfx deploy frontend`
} catch (p) {
throw new Error(`While deploying frontend canister caught error ${p.toString()}`);
};
// Credit initial balance of current dfx identity's subaccount:
try {
// Get the address, returned through stdout with zx.
// Good example of limits of this approach (easier to just use agent-js at some point).
const { stdout } = await $`dfx canister call backend get_account_address`;
// Parse the address's literal value:
const accountAddress = stdout.substring(stdout.indexOf(`= "`) + 3, stdout.lastIndexOf(`";`))
// Decode the ICRC1 account and initiate the transfer, crediting the account:
zxRaw(getICRC1TransferDfxCmd({ account: decodeIcrcAccount(accountAddress) }));
} catch (e) {
throw new Error(`While transferring to current user's subaccount, caught error ${e.toString()}`);
};
if (testing) {
// nns ed25519:
const testAccountAddressA = "be2us-64aaa-aaaaa-qaabq-cai-mnnzrpq.4e9ece1d5903f7a012e4d6e98ec262de481149dfa156812985b6362b1795b69a";
// nns secp256k:
const testAccountAddressB = "be2us-64aaa-aaaaa-qaabq-cai-dwuxpki.ad051cbaf8f18cdce6e4fae39791a0415182fdf25387f099f180228c08c9cb0d";
const accountA = decodeIcrcAccount(testAccountAddressA)
const accountB = decodeIcrcAccount(testAccountAddressB);
try {
zxRaw(getICRC1TransferDfxCmd({ account: accountA }));
zxRaw(getICRC1TransferDfxCmd({ account: accountB }));
} catch (p) {
throw new Error(`While transfering funds to test accounts ${p.toString()}`);
};
};
};
await run(argv.testing === true);