-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathrun-deploy-contract.js
More file actions
executable file
·88 lines (78 loc) · 2.8 KB
/
Copy pathrun-deploy-contract.js
File metadata and controls
executable file
·88 lines (78 loc) · 2.8 KB
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
#!/usr/bin/env node
/* To deploy the faucet contract we must take two steps:
1. We need to fund the faucet account, if it doesn't already exist.
2. We need to use the faucet account to deploy the contract, if it hasn't
already been deployed.
Let's take both steps in this short script. This script reuses the functionality
of run-request.js, so you can run these requests from the command line yourself
if you would like. Just provide the given arguments to the ./run-request.js
script directly.
*/
const { parseArgs } = require("./internal/parse-args");
const { runRequest } = require("./run-request");
// Our first function will fund the faucet account, if necessary.
const fundFaucetIfNeeded = async () => {
const detailsArgs = ["--local", "faucet-details"];
const faucetDetails = await parseArgs(detailsArgs).then(runRequest);
if (
faucetDetails.status === "failure" &&
faucetDetails.error.message.includes("row not found: goliath-faucet")
) {
console.log(
"Faucet account not found on local Chainweb node. Funding faucet account..."
);
const fundArgs = ["--send", "fund-faucet-account", "--signers", "sender00"];
const result = await parseArgs(fundArgs).then(runRequest);
if (result.status === "success") {
console.log(`Funded! Cost ${result.gas} gas.`);
} else {
throw new Error(`Failed to fund account: ${result.error}`);
}
} else if (faucetDetails.status === "failure") {
throw new Error(
`Unexpected error getting faucet account details: ${faucetDetails.error.message}`
);
} else {
console.log(
`Faucet account found with ${faucetDetails.data.balance} in funds.`
);
}
};
const deployFaucetIfNeeded = async () => {
const detailArgs = ["--local", "faucet-contract-details"];
const contractDetails = await parseArgs(detailArgs).then(runRequest);
if (contractDetails.status === "failure") {
console.log(
"Faucet contract not found on local Chainweb node. Deploying contract..."
);
const deployArgs = [
"--send",
"deploy-faucet-contract",
"--signers",
"goliath-faucet",
];
const deployResult = await parseArgs(deployArgs).then(runRequest);
if (deployResult.status === "success") {
console.log(`Deployed! Cost: ${deployResult.gas} gas.`);
} else {
throw new Error(
`Failed to deploy contract: ${JSON.stringify(
deployResult.error,
null,
2
)}`
);
}
} else {
console.log(
`Faucet contract exists with the name ${contractDetails.data.name} and hash ${contractDetails.data.hash}`
);
}
};
// Finally, we can bring the two together to fund our faucet and then deploy
// the contract using the faucet account.
const main = async () => {
await fundFaucetIfNeeded();
await deployFaucetIfNeeded();
};
main();