-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
84 lines (67 loc) · 8.54 KB
/
deploy.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
const fs = require("fs");
const ethers = require("ethers");
require("dotenv").config();
const main = async () => {
const provider = new ethers.providers.JsonRpcProvider(process.env.GOERLI_JSON_RPC_URL);
// const wallet = new ethers.Wallet(process.env.GANACHE_PRIVATE_KEY, provider);
const encryptedJsonKey = fs.readFileSync("./.encryptedJson.json", "utf8");
const walletFromJson = ethers.Wallet.fromEncryptedJsonSync(encryptedJsonKey, process.env.WALLET_PASSWORD);
const wallet = walletFromJson.connect(provider);
const contractABI = fs.readFileSync("./contracts_SimpleStorage_sol_SimpleStorage.abi", "utf8");
const contractBIN = fs.readFileSync("./contracts_SimpleStorage_sol_SimpleStorage.bin", "utf8");
const contractFactory = new ethers.ContractFactory(contractABI, contractBIN, wallet);
console.log("\x1b[33mDeploying Contract, Please wait...\x1b[0m");
const contract = await contractFactory.deploy();
await contract.deployTransaction.wait(1);
console.log("\x1b[32mContract Deployed!\x1b[0m");
console.log("\x1b[34mContract Interactions...\x1b[0m");
console.log("\x1b[33mFetching Favorite Number, Please wait...\x1b[0m");
const currentFavoriteNumber = await contract.retrieve();
console.log(`\x1b[32mCurrent Favorite Number: \x1b[36m${currentFavoriteNumber.toString()}\x1b[0m`);
console.log("\x1b[33mUpdating Favorite Number, Please wait...\x1b[0m");
const updateFavoriteNumberTxResponse = await contract.store("9");
await updateFavoriteNumberTxResponse.wait(1);
console.log("\x1b[32mFavorite Number Updated!\x1b[0m");
const updatedFavoriteNumber = await contract.retrieve();
console.log(`\x1b[32mUpdated Favorite Number: \x1b[36m${updatedFavoriteNumber.toString()}\x1b[0m`);
console.log("\x1b[33mAdding New Person and his/her Favorite Number, Please wait...\x1b[0m");
const addPersonTxResponse = await contract.addPerson("theirrationalone", "333");
await addPersonTxResponse.wait(1);
console.log("\x1b[32mPerson Added Successfully!\x1b[0m");
console.log("\x1b[33mFetching Recently Added Person, Please wait...\x1b[0m");
const [favoriteNumber, name] = await contract.getPerson("0");
console.log(
`\x1b[32mRecently Added Person: \x1b[36m${name.toString()} \x1b[32mand his/her Favorite Number is: \x1b[36m${favoriteNumber.toString()}\x1b[32m!\x1b[0m`
);
console.log(
`\x1b[33mFetching Favorite Number of Person: \x1b[36m${name.toString()}\x1b[33m, Please wait...\x1b[0m`
);
const favoriteNumberOfPerson = await contract.getFavoriteNumberOfPerson(name.toString());
console.log(
`\x1b[32mFavorite Number of \x1b[36m${name.toString()} \x1b[32mis \x1b[36m${favoriteNumberOfPerson.toString()}\x1b[32m!\x1b[0m`
);
console.log("\x1b[36mContract Interaction Completed!\x1b[0m");
// bare transaction...
// const nonce = await wallet.getTransactionCount();
// const tx = {
// nonce: nonce,
// to: null,
// value: 0,
// gasLimit: 1000000,
// gasPrice: 20000000000,
// data: "0x608060405234801561001057600080fd5b506109d8806100206000396000f3fe608060405234801561001057600080fd5b50600436106100575760003560e01c8063246982c41461005c5780632e64cec11461008c5780636057361d146100aa5780636f760f41146100c6578063cc06855f146100e2575b600080fd5b61007660048036038101906100719190610313565b610112565b604051610083919061041c565b60405180910390f35b6100946101ea565b6040516100a1919061044d565b60405180910390f35b6100c460048036038101906100bf9190610313565b6101f4565b005b6100e060048036038101906100db919061059d565b6101fe565b005b6100fc60048036038101906100f791906105f9565b610287565b604051610109919061044d565b60405180910390f35b61011a6102af565b6000828154811061012e5761012d610642565b5b906000526020600020906002020160405180604001604052908160008201548152602001600182018054610161906106a0565b80601f016020809104026020016040519081016040528092919081815260200182805461018d906106a0565b80156101da5780601f106101af576101008083540402835291602001916101da565b820191906000526020600020905b8154815290600101906020018083116101bd57829003601f168201915b5050505050815250509050919050565b6000600254905090565b8060028190555050565b6000604051806040016040528083815260200184815250908060018154018082558091505060019003906000526020600020906002020160009091909190915060008201518160000155602082015181600101908161025d919061087d565b50505080600183604051610271919061098b565b9081526020016040518091039020819055505050565b6000600182604051610299919061098b565b9081526020016040518091039020549050919050565b604051806040016040528060008152602001606081525090565b6000604051905090565b600080fd5b600080fd5b6000819050919050565b6102f0816102dd565b81146102fb57600080fd5b50565b60008135905061030d816102e7565b92915050565b600060208284031215610329576103286102d3565b5b6000610337848285016102fe565b91505092915050565b610349816102dd565b82525050565b600081519050919050565b600082825260208201905092915050565b60005b8381101561038957808201518184015260208101905061036e565b60008484015250505050565b6000601f19601f8301169050919050565b60006103b18261034f565b6103bb818561035a565b93506103cb81856020860161036b565b6103d481610395565b840191505092915050565b60006040830160008301516103f76000860182610340565b506020830151848203602086015261040f82826103a6565b9150508091505092915050565b6000602082019050818103600083015261043681846103df565b905092915050565b610447816102dd565b82525050565b6000602082019050610462600083018461043e565b92915050565b600080fd5b600080fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b6104aa82610395565b810181811067ffffffffffffffff821117156104c9576104c8610472565b5b80604052505050565b60006104dc6102c9565b90506104e882826104a1565b919050565b600067ffffffffffffffff82111561050857610507610472565b5b61051182610395565b9050602081019050919050565b82818337600083830152505050565b600061054061053b846104ed565b6104d2565b90508281526020810184848401111561055c5761055b61046d565b5b61056784828561051e565b509392505050565b600082601f83011261058457610583610468565b5b813561059484826020860161052d565b91505092915050565b600080604083850312156105b4576105b36102d3565b5b600083013567ffffffffffffffff8111156105d2576105d16102d8565b5b6105de8582860161056f565b92505060206105ef858286016102fe565b9150509250929050565b60006020828403121561060f5761060e6102d3565b5b600082013567ffffffffffffffff81111561062d5761062c6102d8565b5b6106398482850161056f565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806106b857607f821691505b6020821081036106cb576106ca610671565b5b50919050565b60008190508160005260206000209050919050565b60006020601f8301049050919050565b600082821b905092915050565b6000600883026107337fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff826106f6565b61073d86836106f6565b95508019841693508086168417925050509392505050565b6000819050919050565b600061077a610775610770846102dd565b610755565b6102dd565b9050919050565b6000819050919050565b6107948361075f565b6107a86107a082610781565b848454610703565b825550505050565b600090565b6107bd6107b0565b6107c881848461078b565b505050565b5b818110156107ec576107e16000826107b5565b6001810190506107ce565b5050565b601f82111561083157610802816106d1565b61080b846106e6565b8101602085101561081a578190505b61082e610826856106e6565b8301826107cd565b50505b505050565b600082821c905092915050565b600061085460001984600802610836565b1980831691505092915050565b600061086d8383610843565b9150826002028217905092915050565b6108868261034f565b67ffffffffffffffff81111561089f5761089e610472565b5b6108a982546106a0565b6108b48282856107f0565b600060209050601f8311600181146108e757600084156108d5578287015190505b6108df8582610861565b865550610947565b601f1984166108f5866106d1565b60005b8281101561091d578489015182556001820191506020850194506020810190506108f8565b8683101561093a5784890151610936601f891682610843565b8355505b6001600288020188555050505b505050505050565b600081905092915050565b60006109658261034f565b61096f818561094f565b935061097f81856020860161036b565b80840191505092915050565b6000610997828461095a565b91508190509291505056fea264697066735822122049a28ab1f9a886ec0e1535af7b394e086c4869e4eac2b1ebe1b584de8e5c68fc64736f6c63430008110033",
// chainId: 1337,
// };
// console.log("\x1b[33mSigning and Sending Transaction, Please wait...");
// const txResponse = await wallet.sendTransaction(tx);
// const txReceipt = await txResponse.wait(1);
// console.log("\x1b[36mTransaction Receipt: \x1b[0m");
// console.log(txReceipt);
// console.log("\x1b[32mTransaction Sent!\x1b[0m");
};
main()
.then(() => process.exit(0))
.catch((err) => {
console.log(`\x1b[31mdeploy.js -- ERROR: ${err} \x1b[0m`);
process.exit(1);
});