|
1 |
| -import * as fs from 'fs' |
2 |
| -import * as path from 'path' |
3 | 1 | import * as dotenv from 'dotenv'
|
4 | 2 |
|
5 |
| -import { ContractTransaction, utils, ContractReceipt, providers, Wallet, Overrides } from 'ethers' |
| 3 | +import { utils, providers, Wallet, Overrides } from 'ethers' |
6 | 4 | import ipfsHttpClient from 'ipfs-http-client'
|
7 | 5 | import * as bs58 from 'bs58'
|
8 | 6 |
|
9 | 7 | dotenv.config()
|
10 | 8 |
|
11 |
| -export const DEFAULT_MNEMONIC = |
12 |
| - 'myth like bonus scare over problem client lizard pioneer submit female collect' |
13 |
| -export const GANACHE_ENDPOINT = 'http://localhost:8545' |
14 |
| - |
15 |
| -export const buildNetworkEndpoint = ( |
16 |
| - network: string, |
17 |
| - provider?: string, |
18 |
| - providerAPIKey?: string, |
19 |
| -): string => { |
20 |
| - if (network == 'ganache') return GANACHE_ENDPOINT |
21 |
| - if (provider == 'infura') { |
22 |
| - if (providerAPIKey == undefined) { |
23 |
| - if (process.env.INFURA_KEY == undefined) { |
24 |
| - throw new Error( |
25 |
| - `Please create a .env file at the root of this project, and set INFURA_KEY=<YOUR_INFURA_KEY>, or pass in an API key`, |
26 |
| - ) |
27 |
| - } else { |
28 |
| - return `https://${network}.infura.io/v3/${process.env.INFURA_KEY}` |
29 |
| - } |
30 |
| - } else { |
31 |
| - return `https://${network}.infura.io/v3/${providerAPIKey}` |
32 |
| - } |
33 |
| - } else { |
34 |
| - throw new Error(`Only infura or local with ganache works for provider endpoint`) |
35 |
| - } |
36 |
| -} |
37 |
| - |
38 |
| -// Creates an array of wallets connected to a provider |
39 |
| -export const configureWallets = ( |
40 |
| - mnemonic: string, |
41 |
| - providerEndpoint: string, |
42 |
| - count: number, |
43 |
| -): Array<Wallet> => { |
44 |
| - const signers: Array<Wallet> = [] |
45 |
| - for (let i = 0; i < count; i++) { |
46 |
| - signers.push(configureWallet(mnemonic, providerEndpoint, i.toString())) |
47 |
| - } |
48 |
| - console.log(`Created ${count} wallets!`) |
49 |
| - return signers |
50 |
| -} |
51 |
| - |
52 |
| -export const configureGanacheWallet = (): Wallet => { |
53 |
| - return configureWallet(DEFAULT_MNEMONIC, GANACHE_ENDPOINT) |
54 |
| -} |
55 |
| - |
56 |
| -// Create a single wallet connected to a provider |
57 |
| -export const configureWallet = ( |
58 |
| - mnemonic: string, |
59 |
| - providerEndpoint: string, |
60 |
| - index = '0', |
61 |
| -): Wallet => { |
62 |
| - if (mnemonic == undefined) { |
63 |
| - throw new Error(`Please set a mnemonic in a .env file at the root of the project`) |
64 |
| - } |
65 |
| - const wallet = Wallet.fromMnemonic(mnemonic, `m/44'/60'/0'/0/${index}`) |
66 |
| - const eth = new providers.JsonRpcProvider(providerEndpoint) |
67 |
| - return wallet.connect(eth) |
68 |
| -} |
69 |
| - |
70 |
| -// Check governor for the network |
71 |
| -export const checkGovernor = (address: string, network: string): void => { |
72 |
| - const networkAddresses = getNetworkAddresses(network) |
73 |
| - const governor = networkAddresses.generatedAddresses.GraphToken.constructorArgs[0].value |
74 |
| - console.log(governor) |
75 |
| - if (address == governor) { |
76 |
| - return |
77 |
| - } else { |
78 |
| - throw new Error('You are trying to call a governor function from the wrong account') |
79 |
| - } |
80 |
| -} |
81 |
| - |
82 |
| -// TODO - return address book type, not any |
83 |
| -export const getNetworkAddresses = (network: string): any => { |
84 |
| - const addresses = JSON.parse( |
85 |
| - fs.readFileSync(path.join(__dirname, '../..', 'addresses.json'), 'utf-8'), |
86 |
| - ) |
87 |
| - if (network == 'kovan') { |
88 |
| - return addresses['42'] |
89 |
| - } else if (network == 'rinkeby') { |
90 |
| - return addresses['4'] |
91 |
| - } else if (network == 'ganache') { |
92 |
| - return addresses['1337'] |
93 |
| - } |
94 |
| -} |
95 |
| - |
96 | 9 | export const defaultOverrides = (): Overrides => {
|
97 | 10 | return {
|
98 | 11 | gasPrice: utils.parseUnits('25', 'gwei'),
|
99 | 12 | gasLimit: 2000000,
|
100 | 13 | }
|
101 | 14 | }
|
102 | 15 |
|
103 |
| -export const executeTransaction = async ( |
104 |
| - transaction: Promise<ContractTransaction>, |
105 |
| - network: string, |
106 |
| -): Promise<ContractReceipt> => { |
107 |
| - try { |
108 |
| - const tx = await transaction |
109 |
| - console.log(` Transaction pending: 'https://${network}.etherscan.io/tx/${tx.hash}'`) |
110 |
| - const receipt = await tx.wait(1) |
111 |
| - console.log(` Transaction successfully included in block #${receipt.blockNumber}`) |
112 |
| - return receipt |
113 |
| - } catch (e) { |
114 |
| - console.log(` ..executeTransaction failed: ${e.message}`) |
115 |
| - process.exit(1) |
116 |
| - } |
117 |
| -} |
118 |
| - |
119 |
| -export const checkFuncInputs = ( |
120 |
| - userInputs: Array<string | undefined>, |
121 |
| - inputNames: Array<string>, |
122 |
| - functionName: string, |
123 |
| -): void => { |
124 |
| - userInputs.forEach((input, i) => { |
125 |
| - if (input == undefined) { |
126 |
| - console.error(`ERROR: ${inputNames[i]} was not provided for ${functionName}()`) |
127 |
| - process.exit(1) |
128 |
| - } |
129 |
| - }) |
130 |
| -} |
131 |
| - |
132 | 16 | export class IPFS {
|
133 | 17 | static createIpfsClient(node: string): ipfsHttpClient {
|
134 | 18 | let url: URL
|
@@ -193,6 +77,44 @@ export const mockChannelPubKeys: Array<string> = [
|
193 | 77 | '0x0456708870bfd5d8fc956fe33285dcf59b075cd7a25a21ee00834e480d3754bcda180e670145a290bb4bebca8e105ea7776a7b39e16c4df7d4d1083260c6f05d59',
|
194 | 78 | ]
|
195 | 79 |
|
| 80 | +////////////////// WALLET HELPERS BELOW ///////////////// |
| 81 | + |
| 82 | +// Creates an array of wallets connected to a provider |
| 83 | +const configureWallets = ( |
| 84 | + mnemonic: string, |
| 85 | + providerEndpoint: string, |
| 86 | + count: number, |
| 87 | +): Array<Wallet> => { |
| 88 | + const signers: Array<Wallet> = [] |
| 89 | + for (let i = 0; i < count; i++) { |
| 90 | + signers.push(configureWallet(mnemonic, providerEndpoint, i.toString())) |
| 91 | + } |
| 92 | + console.log(`Created ${count} wallets!`) |
| 93 | + return signers |
| 94 | +} |
| 95 | + |
| 96 | +const DEFAULT_MNEMONIC = |
| 97 | + 'myth like bonus scare over problem client lizard pioneer submit female collect' |
| 98 | +const GANACHE_ENDPOINT = 'http://localhost:8545' |
| 99 | + |
| 100 | +export const configureGanacheWallet = (): Wallet => { |
| 101 | + return configureWallet(DEFAULT_MNEMONIC, GANACHE_ENDPOINT) |
| 102 | +} |
| 103 | + |
| 104 | +// Create a single wallet connected to a provider |
| 105 | +export const configureWallet = ( |
| 106 | + mnemonic: string, |
| 107 | + providerEndpoint: string, |
| 108 | + index = '0', |
| 109 | +): Wallet => { |
| 110 | + if (mnemonic == undefined) { |
| 111 | + throw new Error(`Please set a mnemonic in a .env file at the root of the project`) |
| 112 | + } |
| 113 | + const wallet = Wallet.fromMnemonic(mnemonic, `m/44'/60'/0'/0/${index}`) |
| 114 | + const eth = new providers.JsonRpcProvider(providerEndpoint) |
| 115 | + return wallet.connect(eth) |
| 116 | +} |
| 117 | + |
196 | 118 | // User accounts used are always 0 to 10
|
197 | 119 | export const userAccounts = (mnemonic: string, provider: string): Array<Wallet> => {
|
198 | 120 | return configureWallets(mnemonic, provider, 10)
|
|
0 commit comments