Skip to content
This repository has been archived by the owner on Apr 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #99 from libotony/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
libotony committed Jan 19, 2020
2 parents 1b755ce + 7b72f04 commit 41036e4
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 93 deletions.
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
.vscode/
src/
test/
.github
docs/
*.tgz
tsconfig.json
tslint.json
Expand Down
69 changes: 33 additions & 36 deletions src/provider/rpc-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,16 @@ RPCMethodMap.set('eth_getTransactionReceipt', async function(rpc: JSONRPC, host:
})

RPCMethodMap.set('eth_call', async function(rpc: JSONRPC, host: string, timeout: number) {
let extraURI = ''
if (rpc.params[0].to) {
extraURI = '/' + rpc.params[0].to
}
extraURI += '?revision=' + utils.fromETHBlockNumberOrHash(rpc.params[1])
const URL = host + '/accounts' + extraURI
const extraURI = '?revision=' + utils.fromETHBlockNumberOrHash(rpc.params[1])
const URL = host + '/accounts/*' + extraURI

const reqBody: any = {
value: rpc.params[0].value || '',
data: rpc.params[0].data || '0x',
gasPrice: rpc.params[0].gasPrice || '',
clauses: [{
to: rpc.params[0].to || null,
value: rpc.params[0].value || '',
data: rpc.params[0].data || '0x',
}],
gasPrice: rpc.params[0].gasPrice || undefined,
}
if (rpc.params[0].gas) {
if (typeof rpc.params[0].gas === 'number') {
Expand All @@ -151,33 +150,33 @@ RPCMethodMap.set('eth_call', async function(rpc: JSONRPC, host: string, timeout:
const res = await HTTP.post(URL, reqBody, timeout).then(HTTPPostProcessor)

debug('eth_call returns', res)
if (!res) {
if (!res || res.length === 0) {
return rpc.makeResult(null)
} else {
if (res.reverted || res.vmError) {
if (res.data && (res.data as string).startsWith('0x08c379a0')) {
return rpc.makeError('VM reverted: ' + require('web3-eth-abi').decodeParameter('string', res.data.replace(/^0x08c379a0/i, '')))
const result = res[0]
if (result.reverted || result.vmError) {
if (result.data && (result.data as string).startsWith('0x08c379a0')) {
return rpc.makeError('VM reverted: ' + require('web3-eth-abi').decodeParameter('string', result.data.replace(/^0x08c379a0/i, '')))
} else {
return rpc.makeError('VM executing failed' + (res.vmError ? ': ' + res.vmError : ''))
return rpc.makeError('VM executing failed' + (result.vmError ? ': ' + result.vmError : ''))
}
} else {
return rpc.makeResult(res.data === '0x' ? '' : res.data)
return rpc.makeResult(result.data === '0x' ? '' : result.data)
}
}
})

RPCMethodMap.set('eth_estimateGas', async function(rpc: JSONRPC, host: string, timeout: number) {
let extraURI = ''
if (rpc.params[0].to) {
extraURI = '/' + rpc.params[0].to
}
extraURI += '?revision=' + utils.fromETHBlockNumberOrHash(rpc.params[1])
const URL = host + '/accounts' + extraURI
const extraURI = '?revision=' + utils.fromETHBlockNumberOrHash(rpc.params[1])
const URL = host + '/accounts/*' + extraURI

const reqBody: any = {
value: rpc.params[0].value || '',
data: rpc.params[0].data || '0x',
gasPrice: rpc.params[0].gasPrice || '',
clauses: [{
to: rpc.params[0].to || null,
value: rpc.params[0].value || '',
data: rpc.params[0].data || '0x',
}],
gasPrice: rpc.params[0].gasPrice || undefined,
}
if (rpc.params[0].gas) {
if (typeof rpc.params[0].gas === 'number') {
Expand All @@ -192,24 +191,22 @@ RPCMethodMap.set('eth_estimateGas', async function(rpc: JSONRPC, host: string, t

const res = await HTTP.post(URL, reqBody, timeout).then(HTTPPostProcessor)

if (!res) {
if (!res || res.length === 0) {
return rpc.makeResult(null)
} else {
if (res.reverted || res.vmError) {
if (res.data && (res.data as string).startsWith('0x08c379a0')) {
return rpc.makeError('Gas estimation failed with VM reverted: ' + require('web3-eth-abi').decodeParameter('string', res.data.replace(/^0x08c379a0/i, '')))
const result = res[0]
if (result.reverted || result.vmError) {
if (result.data && (result.data as string).startsWith('0x08c379a0')) {
return rpc.makeError('Gas estimation failed with VM reverted: ' + require('web3-eth-abi').decodeParameter('string', result.data.replace(/^0x08c379a0/i, '')))
} else {
return rpc.makeError('Gas estimation failed' + (res.vmError ? ': ' + res.vmError : ''))
return rpc.makeError('Gas estimation failed' + (result.vmError ? ': ' + result.vmError : ''))
}
} else {
debug('VM gas:', res.gasUsed)
debug('VM gas:', result.gasUsed)
// ignore the overflow since block gas limit is uint64 and JavaScript's max number is 2^53
const intrinsicGas = utils.calcIntrinsicGas(Object.assign(reqBody, { to: rpc.params[0].to }))
if (res.gasUsed === 0 && (reqBody.data === '0x')) {
return rpc.makeResult(intrinsicGas)
} else {
return rpc.makeResult(Math.floor(res.gasUsed * 1.2) + intrinsicGas) // increase vm gas with 20% for safe since it's estimated from current block state, final state for the transaction is not determined for now
}
// increase vm gas by 15000 for safe since it's estimated from current block state, final state for the transaction is not determined for now
return rpc.makeResult(intrinsicGas + (result.gasUsed ? (result.gasUsed + 15000) : 0))
}
}
})
Expand All @@ -223,7 +220,7 @@ RPCMethodMap.set('eth_getLogs', async function(rpc: JSONRPC, host: string, timeo
query += '&order=' + rpc.params[0].order.toUpperCase()
}
query = query.replace('&', '?')
const URL = host + '/logs/events' + query
const URL = host + '/logs/event' + query

const reqBody = utils.formatLogQuery(rpc.params[0])

Expand Down
12 changes: 6 additions & 6 deletions test/extend/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ describe('extend:accounts', () => {
xhrUtility.setCachedResponse('/blocks/best', {
id: '0x000000352985d99d',
})
xhrUtility.setCachedResponse('/accounts/0xd3ae78222beadb038203be21ed5ce7c9b1bff602?revision=best', {
xhrUtility.setCachedResponse('/accounts/*?revision=best', [{
gasUsed: 53000,
})
}])

const ret = await web3.eth.accounts.signTransaction({
to: '0xD3ae78222BEADB038203bE21eD5ce7C9B1BfF602',
Expand Down Expand Up @@ -133,10 +133,10 @@ describe('extend:accounts', () => {

it('sign Transaction without gas', (done) => {
// if reverted estimateGas will return null
xhrUtility.setCachedResponse('/accounts/0xd3ae78222beadb038203be21ed5ce7c9b1bff602?revision=best', {
xhrUtility.setCachedResponse('/accounts/*?revision=best', [{
reverted: true,
data: '0x08c379a00x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f736f6d657468696e672077726f6e670000000000000000000000000000000000',
})
}])
web3.eth.accounts.signTransaction({
to: '0xd3ae78222beadb038203be21ed5ce7c9b1bff602',
value: '0x3e8',
Expand Down Expand Up @@ -184,9 +184,9 @@ describe('extend:accounts', () => {
})

it('sign Transaction without gas and 0-length clause should not throw error', async () => {
xhrUtility.setCachedResponse('/accounts?revision=best', {
xhrUtility.setCachedResponse('/accounts/*?revision=best', [{
gasUsed: 53000,
})
}])

await web3.eth.accounts.signTransaction({
expiration: 720,
Expand Down
4 changes: 2 additions & 2 deletions test/extend/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('web3.contract', () => {
await contract.getPastEvents('Transfer')
const { url, body } = xhrUtility.extractRequest()

expect(url).to.be.equal('/logs/events?address=0x0000000000000000000000000000456e65726779')
expect(url).to.be.equal('/logs/event?address=0x0000000000000000000000000000456e65726779')
expect(body).to.not.have.property('range')
expect(body).to.not.have.property('options')
})
Expand All @@ -26,7 +26,7 @@ describe('web3.contract', () => {

const { url } = xhrUtility.extractRequest()

expect(url).to.be.equal('/logs/events?address=0x0000000000000000000000000000456e65726779&order=ASC')
expect(url).to.be.equal('/logs/event?address=0x0000000000000000000000000000456e65726779&order=ASC')
})

})
Loading

0 comments on commit 41036e4

Please sign in to comment.