-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03-deploy.js
More file actions
executable file
·113 lines (95 loc) · 3.98 KB
/
03-deploy.js
File metadata and controls
executable file
·113 lines (95 loc) · 3.98 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env node
import node_fs from 'node:fs/promises';
import {
HDNodeWallet as EthersHDNodeWallet,
JsonRpcProvider as EthersJsonRpcProvider,
Mnemonic as EthersMnemonic,
} from 'ethers';
import dotenv from 'dotenv';
import FILE_PATHS from './util/file-paths.js';
import { Logger } from './util/logger.js';
const processEnv = {};
dotenv.config({
processEnv,
quiet: true,
});
const logger = new Logger();
await logger.init();
async function step03Deploy() {
await logger.logScriptBegin('deploy');
await logger.logSection('Check account funds');
// work out what the EVM address of the account is
const hdPath = "m/44'/60'/0'/0";
const seedPhrase = processEnv.SEED_PHRASE;
const mnemonic = EthersMnemonic.fromPhrase(seedPhrase);
const hdWalletNode = EthersHDNodeWallet.fromMnemonic(mnemonic, hdPath);
const hdWallet = hdWalletNode.derivePath('0');
const address = hdWallet.address;
logger.log('EVM address of deployer account', address);
const addressUrl = logger.f.url(`https://testnet.blockscout.injective.network/address/${address}`);
logger.log('Deployer account in explorer:', addressUrl);
// check what its funds are
const rpcUrl = processEnv.INJ_TESTNET_RPC_URL;
const rpcProvider = new EthersJsonRpcProvider(rpcUrl);
// check balance (1st time)
let balance = await rpcProvider.getBalance(address);
logger.log('Balance of account', balance);
// if funds are zero, wait for an additional 30s before checking again
if (balance <= 0n) {
logger.log('Waiting 30s before checking balance again...');
await logger.delay(30e3);
// check balance (2nd time)
balance = await rpcProvider.getBalance(address);
logger.log('Balance of account, 2nd attempt', balance);
// error if funds are zero, otherwise proceeed to next step
if (balance <= 0n) {
throw new Error(
'Account needs to be funded to continue. ' +
'Please repeat the funding step: ./00-fund.js',
);
}
}
await logger.logSection('Run deploy script', logger.f.italic('npx hardhat run script/deploy.js --network inj_testnet'));
await logger.logProcess('npx hardhat run script/deploy.js --network inj_testnet');
await logger.log('Deploy completed!');
await logger.logSection('Load deployment data');
const counterDeploymentJsonStr =
await node_fs.readFile(FILE_PATHS.counterDeploymentJson);
const counterDeploymentJson = JSON.parse(counterDeploymentJsonStr);
const scAddress = counterDeploymentJson.deployedAddress;
const explorerUrl = `https://testnet.blockscout.injective.network/address/${scAddress}?tab=contract_bytecode`;
const explorerUrlAnsi = logger.f.url(explorerUrl);
await logger.log('Smart contract', explorerUrlAnsi);
await logger.logInfoBox(
'What have we accomplished?',
`
1. Connect to Injective Testnet over JSON-RPC
- Using "JsonRpcProvider" from ethers.js
2. Perform a balance check for the account to ensure that it is funded
- Using "Mnemonic" and "HDNodeWallet" from ethers.js
3. Send a deployment transaction, via a hardhat script, for the Counter smart contract
4. Inspect the result of the deployment transaction
- Obtain the smart contract address
- Open the block explorer URL, to view the deployed EVM bytecode
`,
);
}
process.once('SIGINT', async () => {
await logger.logError('deploy', 'sigint');
});
process.once('SIGTERM', async () => {
await logger.logError('deploy', 'sigterm');
});
step03Deploy().then(async () => {
await logger.logScriptEnd('deploy');
console.log('To continue, run the following command for the next step:\n', logger.f.bold('./04-verify.js'));
}).catch(async (err) => {
if (err.stdout || err.stderr) {
await logger.logError('deploy', err.message);
console.log(err.stdout);
console.log(err.stderr);
} else {
await logger.logError('deploy', err.message);
console.log(err);
}
});