diff options
author | Fabio Berger <me@fabioberger.com> | 2017-12-16 02:37:03 +0800 |
---|---|---|
committer | Fabio Berger <me@fabioberger.com> | 2017-12-16 02:37:03 +0800 |
commit | b46dd2e0a277340c19fa137a436981cc53c8cb80 (patch) | |
tree | d2814176fe8ffc5ec6a61534aa77487aae6bcc13 /packages/dev-utils/src/rpc.ts | |
parent | 484dd4c33aa5786def1f0b159f5b51f99a585aa8 (diff) | |
parent | 3eb08735d4d0fe4b046dd6b74f5ed503cf5c64e3 (diff) | |
download | dexon-sol-tools-b46dd2e0a277340c19fa137a436981cc53c8cb80.tar.gz dexon-sol-tools-b46dd2e0a277340c19fa137a436981cc53c8cb80.tar.zst dexon-sol-tools-b46dd2e0a277340c19fa137a436981cc53c8cb80.zip |
Merge branch 'development' into createWethPage
* development: (54 commits)
Fix redundant spaces
Fix tests
Fix website unused vars
Fix connect unused vars
Fix 0x.js unused vars
Dissallow unused vars/imports
Implement first custom linter rule async-suffix
Reuse intervalutils in website
Add a newline
Name a variable
Add a comment
Fix a conditional
Make migrations deterministic
Fix linter error
Fix linter errors
Add a function to init token balances
Rename tokenUtils.getNonProtocolTokens to tokenUtils.getDummyTokens
Add DummyToken to gitignore
Add DummyToken to artifacts list
Increase mocha timeout
...
Diffstat (limited to 'packages/dev-utils/src/rpc.ts')
-rw-r--r-- | packages/dev-utils/src/rpc.ts | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/packages/dev-utils/src/rpc.ts b/packages/dev-utils/src/rpc.ts new file mode 100644 index 000000000..19834dbb4 --- /dev/null +++ b/packages/dev-utils/src/rpc.ts @@ -0,0 +1,60 @@ +import * as ethUtil from 'ethereumjs-util'; +import * as request from 'request-promise-native'; + +export class RPC { + private url: string; + private id: number; + constructor(url: string) { + this.url = url; + this.id = 0; + } + public async takeSnapshotAsync(): Promise<number> { + const method = 'evm_snapshot'; + const params: any[] = []; + const payload = this.toPayload(method, params); + const snapshotIdHex = await this.sendAsync(payload); + const snapshotId = ethUtil.bufferToInt(ethUtil.toBuffer(snapshotIdHex)); + return snapshotId; + } + public async revertSnapshotAsync(snapshotId: number): Promise<boolean> { + const method = 'evm_revert'; + const params = [snapshotId]; + const payload = this.toPayload(method, params); + const didRevert = await this.sendAsync(payload); + return didRevert; + } + public async increaseTimeAsync(time: number) { + const method = 'evm_increaseTime'; + const params = [time]; + const payload = this.toPayload(method, params); + return this.sendAsync(payload); + } + public async mineBlockAsync(): Promise<void> { + const method = 'evm_mine'; + const params: any[] = []; + const payload = this.toPayload(method, params); + await this.sendAsync(payload); + } + private toPayload(method: string, params: any[] = []): string { + const payload = JSON.stringify({ + id: this.id, + method, + params, + }); + this.id += 1; + return payload; + } + private async sendAsync(payload: string): Promise<any> { + const opts = { + method: 'POST', + uri: this.url, + body: payload, + headers: { + 'content-type': 'application/json', + }, + }; + const bodyString = await request(opts); + const body = JSON.parse(bodyString); + return body.result; + } +} |