-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace web3->ethers and JS->TS in RPC class #589
base: confluence
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import {ethers} from "ethers" | ||
|
||
export default class RPC { | ||
constructor(public provider: ethers.providers.JsonRpcProvider) {} | ||
|
||
sendAsync(method: string, arg: any[]) { | ||
try { | ||
return this.provider.send(method, arg) | ||
} catch (error: any) { | ||
throw error | ||
} | ||
} | ||
|
||
// Change block time using TestRPC call evm_setTimestamp | ||
// https://github.com/numerai/contract/blob/master/test/numeraire.js | ||
Comment on lines
+14
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think these comments are now outdated and can be removed |
||
increaseTime(time: number) { | ||
return this.sendAsync("evm_increaseTime", [time]) | ||
} | ||
|
||
mine() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. mark it as async and since it seems in the codebase we are not expecting any return type i guess you can just use |
||
return this.sendAsync("evm_mine", []) | ||
} | ||
|
||
async snapshot() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specify return type |
||
const id = await this.sendAsync("evm_snapshot", []) | ||
return id | ||
} | ||
|
||
revert(snapshotId: number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as the comment for the 'mine' method |
||
return this.sendAsync("evm_revert", [snapshotId]) | ||
} | ||
|
||
async wait(blocks = 1, seconds = 20) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same as above |
||
const currentBlock = await this.provider.getBlockNumber() | ||
const targetBlock = currentBlock + blocks | ||
await this.waitUntilBlock(targetBlock, seconds) | ||
} | ||
|
||
async getBlockNumberAsync() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add return type |
||
return this.provider.getBlockNumber() | ||
} | ||
|
||
async waitUntilBlock(targetBlock: number, seconds = 20) { | ||
let currentBlock = await this.provider.getBlockNumber() | ||
|
||
while (currentBlock < targetBlock) { | ||
await this.increaseTime(seconds) | ||
await this.mine() | ||
currentBlock++ | ||
} | ||
} | ||
|
||
async waitUntilNextBlockMultiple( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specify return type |
||
blockMultiple: number, | ||
multiples = 1, | ||
seconds = 20 | ||
) { | ||
const currentBlock = await this.provider.getBlockNumber() | ||
const additionalBlocks = (multiples - 1) * blockMultiple | ||
await this.waitUntilBlock( | ||
this.nextBlockMultiple(currentBlock, blockMultiple) + | ||
additionalBlocks | ||
) | ||
} | ||
|
||
nextBlockMultiple(currentBlockNum: number, blockMultiple: number) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. specify return type |
||
if (blockMultiple === 0) { | ||
return currentBlockNum | ||
} | ||
|
||
const remainder = currentBlockNum % blockMultiple | ||
|
||
if (remainder === 0) { | ||
return currentBlockNum | ||
} | ||
|
||
return currentBlockNum + blockMultiple - remainder | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we explicitly mark it as 'async' and also specify a return type? (I guess by using a generic since it is used in functions that return
Promise<void>
orPromise<someType>