-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathutil.ts
174 lines (153 loc) · 4.86 KB
/
util.ts
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
171
172
173
174
require('dotenv').config();
const IPFSClient = require('ipfs-http-client');
process.env = {
ethereum: 'http://127.0.0.1:8545',
ipfs: '/ip4/127.0.0.1/tcp/5001',
node_http: 'http://127.0.0.1:8000/subgraphs/name/daostack',
node_ws: 'http://127.0.0.1:8001/subgraphs/name/daostack',
test_mnemonic:
'myth like bonus scare over problem client lizard pioneer submit female collect',
...process.env,
};
const { execute } = require('apollo-link');
const { WebSocketLink } = require('apollo-link-ws');
const { SubscriptionClient } = require('subscriptions-transport-ws');
const ws = require('ws');
import axios from 'axios';
import * as HDWallet from 'hdwallet-accounts';
const Web3 = require('web3');
const { node_ws, node_http, ethereum, ipfs, test_mnemonic } = process.env;
export async function sendQuery(q: string, maxDelay = 1000, url = node_http) {
await new Promise((res) => setTimeout(res, maxDelay));
const {
data: { data },
} = await axios.post(url, {
query: q,
});
return data;
}
export const addressLength = 40;
export const hashLength = 64;
export const nullAddress = '0x0000000000000000000000000000000000000000';
export const nullParamsHash = '0x' + padZeros('', 64);
export async function getWeb3() {
const web3 = new Web3(ethereum);
const hdwallet = HDWallet(10, test_mnemonic);
Array(10)
.fill(10)
.map((_, i) => i)
.forEach((i) => {
const pk = hdwallet.accounts[i].privateKey;
const account = web3.eth.accounts.privateKeyToAccount(pk);
web3.eth.accounts.wallet.add(account);
});
web3.eth.defaultAccount = web3.eth.accounts.wallet[0].address;
return web3;
}
export function getContractAddresses() {
const addresses = require(`@daostack/migration/migration.json`);
let arcVersion = '0.0.1-rc.33';
return {
...addresses.private.test[arcVersion],
...addresses.private.dao[arcVersion],
...addresses.private.base[arcVersion],
...addresses.private.test[arcVersion].organs,
TestAvatar: addresses.private.test[arcVersion].Avatar,
NativeToken: addresses.private.dao[arcVersion].DAOToken,
NativeReputation: addresses.private.dao[arcVersion].Reputation,
};
}
export function getArcVersion() {
return '0.0.1-rc.33';
}
export function getOrgName() {
return require(`@daostack/migration/migration.json`).private.dao['0.0.1-rc.32'].name;
}
export async function getOptions(web3) {
const block = await web3.eth.getBlock('latest');
return {
from: web3.eth.defaultAccount,
gas: block.gasLimit - 100000,
};
}
export async function writeProposalIPFS(data: any) {
const ipfsClient = IPFSClient(ipfs);
const ipfsResponse = await ipfsClient.add(new Buffer(JSON.stringify(data)));
return ipfsResponse[0].path;
}
export function padZeros(str: string, max = 36) {
str = str.toString();
return str.length < max ? padZeros('0' + str, max) : str;
}
export const createSubscriptionObservable = (
query: string,
variables = 0,
wsurl = node_ws,
) => {
const client = new SubscriptionClient(wsurl, { reconnect: true }, ws);
const link = new WebSocketLink(client);
return execute(link, { query, variables });
};
export async function waitUntilTrue(test: () => Promise<boolean> | boolean) {
return new Promise((resolve, reject) => {
(async function waitForIt(): Promise<void> {
if (await test()) { return resolve(); }
setTimeout(waitForIt, 30);
})();
});
}
export async function waitUntilSynced() {
const getGraphsSynced = `{
subgraphDeployments {
synced
}
}`;
const graphIsSynced = async () => {
let result = await sendQuery(
getGraphsSynced,
1000,
'http://127.0.0.1:8000/subgraphs');
return ((result.subgraphDeployments.length > 0) && result.subgraphDeployments[0].synced);
};
await waitUntilTrue(graphIsSynced);
}
export const increaseTime = async function(duration, web3) {
const id = await Date.now();
web3.providers.HttpProvider.prototype.sendAsync = web3.providers.HttpProvider.prototype.send;
return new Promise((resolve, reject) => {
web3.currentProvider.sendAsync({
jsonrpc: '2.0',
method: 'evm_increaseTime',
params: [duration],
id,
}, (err1) => {
if (err1) { return reject(err1); }
web3.currentProvider.sendAsync({
jsonrpc: '2.0',
method: 'evm_mine',
id: id + 1,
}, (err2, res) => {
return err2 ? reject(err2) : resolve(res);
});
});
});
};
export function toFixed(x) {
if (Math.abs(x) < 1.0) {
// tslint:disable-next-line: radix
let e = parseInt(x.toString().split('e-')[1]);
if (e) {
x *= Math.pow(10, e - 1);
x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
}
} else {
// tslint:disable-next-line: radix
let e = parseInt(x.toString().split('+')[1]);
if (e > 20) {
e -= 20;
x /= Math.pow(10, e);
x += (new Array(e + 1)).join('0');
}
}
return x;
}