-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate account:register to anvil (#243)
- Loading branch information
Showing
14 changed files
with
323 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@celo/dev-utils': patch | ||
--- | ||
|
||
Introduces testWithAnvil that allows testing against a local anvil instance |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,30 @@ | ||
import { testWithGanache } from '@celo/dev-utils/lib/ganache-test' | ||
import { newKitFromWeb3 } from '@celo/contractkit' | ||
import { testWithAnvil } from '@celo/dev-utils/lib/anvil-test' | ||
import Web3 from 'web3' | ||
import { testLocally } from '../../test-utils/cliUtils' | ||
import { testLocallyWithWeb3Node } from '../../test-utils/cliUtils' | ||
import Register from './register' | ||
|
||
process.env.NO_SYNCCHECK = 'true' | ||
|
||
testWithGanache('account:register cmd', (web3: Web3) => { | ||
testWithAnvil('account:register cmd', (web3: Web3) => { | ||
test('can register account', async () => { | ||
const accounts = await web3.eth.getAccounts() | ||
|
||
await testLocally(Register, ['--from', accounts[0], '--name', 'Chapulin Colorado']) | ||
await testLocallyWithWeb3Node( | ||
Register, | ||
['--from', accounts[0], '--name', 'Chapulin Colorado'], | ||
web3 | ||
) | ||
|
||
const kit = newKitFromWeb3(web3) | ||
const account = await kit.contracts.getAccounts() | ||
|
||
expect(await account.getName(accounts[0])).toMatchInlineSnapshot(`"Chapulin Colorado"`) | ||
}) | ||
|
||
test('fails if from is missing', async () => { | ||
// const accounts = await web3.eth.getAccounts() | ||
|
||
await expect(testLocally(Register, [])).rejects.toThrow('Missing required flag') | ||
await expect(testLocallyWithWeb3Node(Register, [], web3)).rejects.toThrow( | ||
'Missing required flag' | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import { getInstance } from '@celo/dev-utils/lib/anvil-test' | ||
import teardown from '@celo/dev-utils/lib/ganache-teardown' | ||
|
||
export default async function globalTeardown() { | ||
await teardown() | ||
const anvil = getInstance() | ||
|
||
await Promise.all([teardown(), anvil.stop()]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
"prepack": "yarn build" | ||
}, | ||
"dependencies": { | ||
"@viem/anvil": "^0.0.9", | ||
"bignumber.js": "^9.0.0", | ||
"fs-extra": "^8.1.0", | ||
"ganache": "npm:@celo/[email protected]", | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Anvil, createAnvil } from '@viem/anvil' | ||
import { join } from 'path' | ||
import Web3 from 'web3' | ||
import { testWithWeb3 } from './test-utils' | ||
|
||
let instance: null | Anvil = null | ||
|
||
const ANVIL_PORT = 8546 | ||
/* | ||
* This file is generated by running the script at | ||
* https://github.com/celo-org/celo-monorepo/blob/master/packages/protocol/migrations_sol/create_and_migrate_anvil_devchain.sh | ||
*/ | ||
const ANVIL_STATE_JSON = 'anvil-state.json' | ||
|
||
export function getInstance(): Anvil { | ||
if (instance === null) { | ||
instance = createAnvil({ | ||
port: ANVIL_PORT, | ||
loadState: join(__dirname, ANVIL_STATE_JSON), | ||
}) | ||
} | ||
|
||
return instance | ||
} | ||
|
||
export function testWithAnvil(name: string, fn: (web3: Web3) => void) { | ||
return testWithWeb3(name, `http://127.0.0.1:${ANVIL_PORT}`, fn) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import Web3 from 'web3' | ||
import { JsonRpcResponse } from 'web3-core-helpers' | ||
import migrationOverride from './migration-override.json' | ||
|
||
export const NetworkConfig = migrationOverride | ||
|
||
export function jsonRpcCall<O>(web3: Web3, method: string, params: any[]): Promise<O> { | ||
return new Promise<O>((resolve, reject) => { | ||
if (web3.currentProvider && typeof web3.currentProvider !== 'string') { | ||
web3.currentProvider.send( | ||
{ | ||
id: new Date().getTime(), | ||
jsonrpc: '2.0', | ||
method, | ||
params, | ||
}, | ||
(err: Error | null, res?: JsonRpcResponse) => { | ||
if (err) { | ||
reject(err) | ||
} else if (!res) { | ||
reject(new Error('no response')) | ||
} else if (res.error) { | ||
reject( | ||
new Error( | ||
`Failed JsonRpcResponse: method: ${method} params: ${JSON.stringify( | ||
params | ||
)} error: ${JSON.stringify(res.error)}` | ||
) | ||
) | ||
} else { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument | ||
resolve(res.result) | ||
} | ||
} | ||
) | ||
} else { | ||
reject(new Error('Invalid provider')) | ||
} | ||
}) | ||
} | ||
|
||
export function evmRevert(web3: Web3, snapId: string): Promise<void> { | ||
return jsonRpcCall(web3, 'evm_revert', [snapId]) | ||
} | ||
|
||
export function evmSnapshot(web3: Web3) { | ||
return jsonRpcCall<string>(web3, 'evm_snapshot', []) | ||
} | ||
|
||
export function testWithWeb3(name: string, rpcUrl: string, fn: (web3: Web3) => void) { | ||
const web3 = new Web3(rpcUrl) | ||
|
||
describe(name, () => { | ||
let snapId: string | null = null | ||
|
||
beforeEach(async () => { | ||
if (snapId != null) { | ||
await evmRevert(web3, snapId) | ||
} | ||
snapId = await evmSnapshot(web3) | ||
}) | ||
|
||
afterAll(async () => { | ||
if (snapId != null) { | ||
await evmRevert(web3, snapId) | ||
} | ||
}) | ||
|
||
fn(web3) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.