forked from dogethereum/dogethereum-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
170 lines (152 loc) · 4.59 KB
/
utils.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const ethers = require("ethers");
const path = require("path");
const fs = require("fs");
const pkg = require("./package.json");
function completeYargs(yargs) {
return yargs
.option("n", {
group: "Connection:",
alias: "ethnetwork",
describe: "Eth network to be used. This option is ignored.",
deprecated: true,
type: "string",
choices: ["ganacheDogeRegtest", "ganacheDogeMainnet", "rinkeby"],
demandOption: false,
})
.option("t", {
group: "Connection:",
alias: "host",
default: "127.0.0.1",
type: "string",
describe: "host of the ethereum node",
})
.option("p", {
group: "Connection:",
alias: "port",
default: 8545,
type: "number",
describe: "port of the ethereum node",
check: (val) => val >= 1 && val <= 65535,
})
.option("g", {
group: "Connection:",
alias: "gasPrice",
describe: "The price of gas in wei",
type: "number",
default: 20000000000,
})
.option("j", {
group: "Connection:",
alias: "deployment",
describe:
"Location of the deployment artifact json. Useful during development on 'ganacheDogeRegtest' or 'ganacheDogeMainnet' networks.",
type: "string",
demandOption: false,
})
.showHelpOnFail(false, "Specify -h, -? or --help for available options")
.help("h")
.alias("h", ["?", "help"])
.version(`Dogethereum tools ${pkg.version}`);
}
async function init(argv) {
const provider = ethers.getDefaultProvider(
`http://${argv.host}:${argv.port}`
);
const deploymentPath = argv.deployment
? argv.deployment
: path.resolve(__dirname, "deployment/deployment.json");
const deployment = JSON.parse(fs.readFileSync(deploymentPath));
const contracts = await loadDeployment(provider, deployment);
return { provider, argv, ...contracts };
}
async function loadDeployment(provider, deployment) {
const { chainId } = await provider.getNetwork();
if (chainId !== deployment.chainId) {
throw new Error(
`Expected a deployment for network with chainId ${chainId} but found chainId ${deployment.chainId} instead.`
);
}
const dogeTokenArtifact = deployment.contracts.dogeToken;
return {
dogeToken: new ethers.Contract(
dogeTokenArtifact.address,
dogeTokenArtifact.abi,
provider
),
};
}
async function checkSignerBalance(signer) {
// Make sure sender has some eth to pay for txs
const senderEthBalance = await signer.getBalance();
if (senderEthBalance.isZero()) {
throw new Error("Sender address has no eth balance, aborting.");
} else {
console.log(
`Sender eth balance: ${ethers.utils.formatEther(
senderEthBalance
)} eth. Please, make sure that is enough to pay for the tx fee.`
);
}
}
async function printDogeTokenBalances(dogeToken, sender, receiver) {
// Print sender DogeToken balance
const senderDogeTokenBalance = await dogeToken.callStatic.balanceOf(sender);
console.log(
`Sender doge token balance: ${satoshiToDoge(
senderDogeTokenBalance
)} doge tokens.`
);
if (receiver !== undefined) {
// Print receiver DogeToken balance
const receiverDogeTokenBalance = await dogeToken.callStatic.balanceOf(
receiver
);
console.log(
`Receiver doge token balance: ${satoshiToDoge(
receiverDogeTokenBalance
)} doge tokens.`
);
}
}
function satoshiToDoge(dogeSatoshis) {
const dogeDecimals = 8;
return ethers.utils.formatUnits(dogeSatoshis, dogeDecimals);
}
function printTxResult(txReceipt, operation) {
if (txReceipt.events === undefined) {
throw new Error(
"No events in tx {txReceipt.transactionHash} for operation ${operation}"
);
}
const errorEvents = txReceipt.events.filter((event) => {
event.event === "ErrorDogeToken";
});
if (errorEvents.length === 0) {
console.log(`${operation} done. Tx hash: ${txReceipt.transactionHash}`);
return;
}
console.error(`Doge token error events in tx: ${txReceipt.transactionHash}`);
for (const event of errorEvents) {
// TODO: This is actually an error code. We want to provide a human readable error message here.
console.error(
`Doge token error event index ${event.logIndex}: ${event.args}`
);
}
throw new Error(`Operation ${operation} failed!`);
}
function fromHex(data) {
return Buffer.from(remove0x(data), "hex");
}
function remove0x(str) {
return str.startsWith("0x") ? str.substring(2) : str;
}
module.exports = {
completeYargs,
init,
checkSignerBalance,
printDogeTokenBalances,
satoshiToDoge,
printTxResult,
fromHex,
remove0x,
};