-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy-encrypted.js
62 lines (48 loc) · 2.1 KB
/
deploy-encrypted.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
const ethers = require("ethers");
require("dotenv").config();
const fs = require("fs");
const PASSWORD = process.env.PASSWORD;
const RPC_URL = process.env.RPC_URL;
async function main() {
const provider = new ethers.providers.JsonRpcProvider(RPC_URL);
// create a wallet object from json file
const encryptedJson = fs.readFileSync("./.encryptedKey.json", "utf-8");
let wallet = new ethers.Wallet.fromEncryptedJsonSync(
encryptedJson,
PASSWORD
);
wallet = await wallet.connect(provider);
const abi = fs.readFileSync(
"./SimpleStorage_sol_SimpleStorage.abi",
"utf-8"
);
const binary = fs.readFileSync(
"./SimpleStorage_sol_SimpleStorage.bin",
"utf-8"
);
// deploying the contract
const contractFactory = new ethers.ContractFactory(abi, binary, wallet);
console.log("Deploying contract...");
const contract = await contractFactory.deploy(); // deploy function accept an overrides object to set gasPrice, gasLimit, etc...
console.log("Contract deployed!");
const transactionReceipt = await contract.deployTransaction.wait(1);
// interacting with the contract
// get initial value for the stored number
// this is a view function, thus it does not alter the state of the blockchain and it does not cost any gas
const initialFavouriteNumber = (await contract.retrieve()).toString();
console.log(`Current favourite number: ${initialFavouriteNumber}`);
// change stored number
// this time we change the blockchain state, then we'll use gas
console.log("Setting new favourite number...");
const changeNumberTxResponse = await contract.store("23"); // use string format when passing int (for small numbers would work even passing int)
const changeNumberTxReceipt = await changeNumberTxResponse.wait(1);
console.log("Favourite number set!");
const updatedNumber = await contract.retrieve();
console.log(`The new favourite number is: ${updatedNumber}`);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});