Skip to content

Commit 63216fc

Browse files
committed
Stub out simulations. Clean up helpers to remove old cli helpers
1 parent 9601498 commit 63216fc

File tree

4 files changed

+86
-165
lines changed

4 files changed

+86
-165
lines changed

scripts/cli/commands/simulations/baseSimulation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@
322322
// await executeTransaction(networkContracts.staking.setThawingPeriod(defaultThawingPeriod), network)
323323
// }
324324

325-
// const populateAll = async (mnemonic: string, provider: string, network: string): Promise<void> => {
325+
// const baseSimulation = async (mnemonic: string, provider: string, network: string): Promise<void> => {
326326
// const users = userAccounts(mnemonic, provider)
327327
// const proxies = proxyAccounts(mnemonic, provider)
328328
// const channels = channelAccounts(mnemonic, provider)
@@ -336,4 +336,5 @@
336336
// await populateStaking(network, users, proxies)
337337
// }
338338

339-
// export default populateAll
339+
const baseSimulation = async (): Promise<void> => {}
340+
export default baseSimulation
Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,39 @@
1-
#!/usr/bin/env ts-node
2-
import * as path from 'path'
3-
import * as minimist from 'minimist'
1+
import consola from 'consola'
2+
import yargs, { Argv } from 'yargs'
43

5-
import baseScenario from './baseSimulation'
6-
import { buildNetworkEndpoint, DEFAULT_MNEMONIC } from '../../../contracts/helpers'
4+
import { loadEnv, CLIArgs, CLIEnvironment } from '../../env'
5+
import baseSimulation from './baseSimulation'
76

8-
const { scenario, network, mnemonic } = minimist.default(process.argv.slice(2), {
9-
string: ['scenario', 'provider', 'wallet'],
10-
})
7+
const logger = consola.create({})
118

12-
if (!scenario || !network || !mnemonic) {
13-
console.error(
14-
`
15-
Usage: ${path.basename(process.argv[1])}
16-
--scenario <string> - options: default (our only scenario for now)
17-
--network <string> - options: ganache, kovan, rinkeby
18-
--mnemonic <string> - options: ganache, env
19-
`,
20-
)
21-
process.exit(1)
9+
export const runBaseSimulation = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
10+
logger.log(`Running the base simulation...`)
11+
await baseSimulation()
2212
}
2313

24-
const main = async () => {
25-
try {
26-
let getMnemonic
27-
mnemonic == 'ganache' ? (getMnemonic = DEFAULT_MNEMONIC) : (getMnemonic = process.env.MNEMONIC)
28-
if (scenario == 'default') {
29-
const provider = buildNetworkEndpoint(network, 'infura')
30-
console.log(`Running default scenario on ${network} with mnemonic ${getMnemonic}`)
31-
populateData(getMnemonic, provider, network)
32-
} else {
33-
console.log('Wrong scenario name')
34-
}
35-
} catch (e) {
36-
console.log(` ..failed within scenario.ts: ${e.message}`)
37-
process.exit(1)
38-
}
14+
export const curationSimulator = async (cli: CLIEnvironment, cliArgs: CLIArgs): Promise<void> => {
15+
// todo
3916
}
4017

41-
main()
18+
export const simulationCommand = {
19+
command: 'simulation',
20+
describe: 'Run a simulation',
21+
builder: (yargs: Argv): yargs.Argv => {
22+
return yargs
23+
.command({
24+
command: 'baseSimulation',
25+
describe:
26+
'Run the base simulation, which just tests all function calls to get data into new network contracts',
27+
handler: async (argv: CLIArgs): Promise<void> => {
28+
return runBaseSimulation(await loadEnv(argv), argv)
29+
},
30+
})
31+
.command({
32+
command: 'curationSimulator',
33+
describe: 'Run a simulator that sends curator signals on many subgraphs',
34+
handler: async (argv: CLIArgs): Promise<void> => {
35+
return curationSimulator(await loadEnv(argv), argv)
36+
},
37+
})
38+
},
39+
}

scripts/cli/helpers.ts

Lines changed: 39 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,18 @@
1-
import * as fs from 'fs'
2-
import * as path from 'path'
31
import * as dotenv from 'dotenv'
42

5-
import { ContractTransaction, utils, ContractReceipt, providers, Wallet, Overrides } from 'ethers'
3+
import { utils, providers, Wallet, Overrides } from 'ethers'
64
import ipfsHttpClient from 'ipfs-http-client'
75
import * as bs58 from 'bs58'
86

97
dotenv.config()
108

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-
969
export const defaultOverrides = (): Overrides => {
9710
return {
9811
gasPrice: utils.parseUnits('25', 'gwei'),
9912
gasLimit: 2000000,
10013
}
10114
}
10215

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-
13216
export class IPFS {
13317
static createIpfsClient(node: string): ipfsHttpClient {
13418
let url: URL
@@ -193,6 +77,44 @@ export const mockChannelPubKeys: Array<string> = [
19377
'0x0456708870bfd5d8fc956fe33285dcf59b075cd7a25a21ee00834e480d3754bcda180e670145a290bb4bebca8e105ea7776a7b39e16c4df7d4d1083260c6f05d59',
19478
]
19579

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+
196118
// User accounts used are always 0 to 10
197119
export const userAccounts = (mnemonic: string, provider: string): Array<Wallet> => {
198120
return configureWallets(mnemonic, provider, 10)

scripts/cli/network.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,24 @@ export const sendTransaction = async (
5757
fn: string,
5858
...params
5959
): Promise<providers.TransactionReceipt> => {
60+
let tx: ContractTransaction
6061
try {
61-
const tx: ContractTransaction = await contract.functions[fn](...params)
62-
logger.log(`> Sent transaction ${fn}: ${params}, txHash: ${tx.hash}`)
63-
const receipt = await wallet.provider.waitForTransaction(tx.hash)
64-
logger.success(`Transaction mined ${tx.hash}`)
65-
return receipt
62+
tx = await contract.functions[fn](...params)
6663
} catch (e) {
6764
if (e.code == 'UNPREDICTABLE_GAS_LIMIT') {
6865
logger.warn(`Gas could not be estimated - trying defaultOverrides`)
69-
const tx: ContractTransaction = await contract.functions[fn](...params, defaultOverrides())
70-
logger.log(`> Sent transaction ${fn}: ${params}, txHash: ${tx.hash}`)
71-
const receipt = await wallet.provider.waitForTransaction(tx.hash)
72-
logger.success(`Transaction mined ${tx.hash}`)
73-
return receipt
74-
} else {
75-
logger.error(`send transaction failed: ${e}`)
66+
tx = await contract.functions[fn](...params, defaultOverrides())
7667
}
7768
}
69+
logger.log(`> Sent transaction ${fn}: ${params}, txHash: ${tx.hash}`)
70+
const receipt = await wallet.provider.waitForTransaction(tx.hash)
71+
const networkName = (await wallet.provider.getNetwork()).name
72+
if (networkName == 'kovan' || networkName == 'rinkeby') {
73+
logger.success(`Transaction mined 'https://${networkName}.etherscan.io/tx/${tx.hash}'`)
74+
} else {
75+
logger.success(`Transaction mined ${tx.hash}`)
76+
}
77+
return receipt
7878
}
7979

8080
export const getContractFactory = (name: string): ContractFactory => {

0 commit comments

Comments
 (0)