This repository has been archived by the owner on Jan 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
getData.js
73 lines (66 loc) · 2.42 KB
/
getData.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
const fs = require("fs");
const { exec } = require("child_process");
const alphaShaData = require('./data/alpha-sha');
const { formatNEAR, getContract, getDataSet } = require("./services/utils");
// maximum amount of gas you can attach to a single contract call
// all unused gas will be refunded to your account
const MAX_GAS = "300000000000000";
async function getKeyValuePair(contract, contractMethodString, key) {
const result = await contract.account.functionCall(
contract.contractId,
contractMethodString,
{ key },
MAX_GAS
);
return result;
}
async function calculateGas(contract, contractMethod, key) {
let gasBurnt = [];
let tokensBurnt = [];
const result = await getKeyValuePair(contract, contractMethod, key);
gasBurnt.push(result.transaction_outcome.outcome.gas_burnt);
tokensBurnt.push(formatNEAR(result.transaction_outcome.outcome.tokens_burnt));
for (let i = 0; i < result.receipts_outcome.length; i++) {
gasBurnt.push(result.receipts_outcome[i].outcome.gas_burnt);
tokensBurnt.push(
formatNEAR(result.receipts_outcome[i].outcome.tokens_burnt)
);
}
return {
gas_burnt: gasBurnt.reduce((acc, cur) => acc + cur, 0),
tokens_burnt: tokensBurnt.reduce((acc, curr) => acc + curr, 0),
};
}
async function recordGasResults(contract, contractMethod, dataArr) {
console.log(
`Calling [ ${contract.contractId} ] using [ ${contractMethod} ]...`
);
let resultArr = [];
for (let i = 0; i < dataArr.length; i++) {
console.time("call_duration");
console.log("call placed at:", new Date());
const results = await calculateGas(contract, contractMethod, dataArr[i].key);
console.log(dataArr[i].key);
console.timeEnd("call_duration");
console.log(results);
// gas_burnt converts results into TGas (terra gas)
resultArr.push({
key: dataArr[i].key,
gas_burnt: Number((results.gas_burnt / 1e12).toFixed(4)),
tokens_burnt: results.tokens_burnt,
});
fs.writeFileSync(
`results/user-results/get-data/${contractMethod}_results.js`,
`const ${contractMethod}_data = ${JSON.stringify(resultArr)}`
);
}
}
async function getData(data) {
const contract = await getContract();
await recordGasResults(contract, "get_map", data);
await recordGasResults(contract, "get_unordered_map", data);
await recordGasResults(contract, "get_tree_map", data);
exec('yarn getCharts');
}
const data = getDataSet(alphaShaData, 30);
getData(data);