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

Commit

Permalink
test cases: call/estimate gas
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony committed Jan 19, 2020
1 parent 0cd7584 commit 7b72f04
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 50 deletions.
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
88 changes: 61 additions & 27 deletions test/provider/rpc-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ describe('rpc methods', () => {
describe('eth_call', () => {

it('valid response', async () => {
xhrUtility.setResponse({ reverted: false, data: '0xdata' })
xhrUtility.setResponse([{ reverted: false, data: '0xdata' }])
const executor = RPCMethodMap.get('eth_call')
const rpc = makeRPCRequest('eth_call', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
Expand All @@ -236,19 +236,21 @@ describe('rpc methods', () => {
const ret = await executor(rpc, host, timeout)
const { url, body } = xhrUtility.extractRequest()

expect(url).to.be.equal('/accounts/0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed?revision=best')
expect(body).to.have.property('value', '0x64')
expect(url).to.be.equal('/accounts/*?revision=best')
expect(body).to.have.property('gas', 100)
expect(body).to.have.property('gasPrice', '0x64')
expect(body).to.have.property('caller', '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed')
expect(body).to.have.property('clauses')
expect((body as any).clauses).to.be.an('array').to.have.lengthOf(1)
expect((body as any).clauses[0]).to.have.property('value', '0x64')
expect(ret.result).to.be.equal('0xdata')
})

it('reverted should return rpc error', async () => {
xhrUtility.setResponse({
xhrUtility.setResponse([{
reverted: true,
data: '0x08c379a00x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f736f6d657468696e672077726f6e670000000000000000000000000000000000',
})
}])
const executor = RPCMethodMap.get('eth_call')
const rpc = makeRPCRequest('eth_call', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
Expand All @@ -267,7 +269,7 @@ describe('rpc methods', () => {
})

it('"0x" of data should return empty string', async () => {
xhrUtility.setResponse({ data: '0x' })
xhrUtility.setResponse([{ data: '0x' }])
const executor = RPCMethodMap.get('eth_call')
const rpc = makeRPCRequest('eth_call', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
Expand All @@ -278,7 +280,6 @@ describe('rpc methods', () => {
}])

const ret = await executor(rpc, host, timeout)
const { body } = xhrUtility.extractRequest()

expect(ret.result).to.be.equal('')
})
Expand All @@ -296,7 +297,7 @@ describe('rpc methods', () => {
describe('eth_call', () => {

it('valid response', async () => {
xhrUtility.setResponse({ reverted: false, data: '0xdata' })
xhrUtility.setResponse([{ reverted: false, data: '0xdata' }])
const executor = RPCMethodMap.get('eth_call')
const rpc = makeRPCRequest('eth_call', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
Expand All @@ -309,16 +310,18 @@ describe('rpc methods', () => {
const ret = await executor(rpc, host, timeout)
const { url, body } = xhrUtility.extractRequest()

expect(url).to.be.equal('/accounts/0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed?revision=best')
expect(body).to.have.property('value', '0x64')
expect(url).to.be.equal('/accounts/*?revision=best')
expect(body).to.have.property('gas', 100)
expect(body).to.have.property('gasPrice', '0x64')
expect(body).to.have.property('caller', '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed')
expect(body).to.have.property('clauses')
expect((body as any).clauses).to.be.an('array').to.have.lengthOf(1)
expect((body as any).clauses[0]).to.have.property('value', '0x64')
expect(ret.result).to.be.equal('0xdata')
})

it('request without from should not have caller in body', async () => {
xhrUtility.setResponse({ reverted: true })
xhrUtility.setResponse([{ reverted: true }])
const executor = RPCMethodMap.get('eth_call')
const rpc = makeRPCRequest('eth_call', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
Expand All @@ -334,15 +337,19 @@ describe('rpc methods', () => {
expect(body).to.not.have.property('caller')
})

it('deploy contract should request the right url', async () => {
it('deploy contract should set "to" to null', async () => {
const executor = RPCMethodMap.get('eth_call')
const rpc = makeRPCRequest('eth_call', [{
from: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
}])

await executor(rpc, host, timeout)
const { url } = xhrUtility.extractRequest()
expect(url).to.be.equal('/accounts?revision=best')
const { url, body } = xhrUtility.extractRequest()
expect(url).to.be.equal('/accounts/*?revision=best')

expect(body).to.have.property('clauses')
expect((body as any).clauses).to.be.an('array').to.have.lengthOf(1)
expect((body as any).clauses[0]).to.have.property('to', null)
})

it('return null', async () => {
Expand All @@ -358,7 +365,7 @@ describe('rpc methods', () => {
describe('eth_estimateGas', () => {

it('valid response', async () => {
xhrUtility.setResponse({ gasUsed: 0 })
xhrUtility.setResponse([{ gasUsed: 0 }])
const executor = RPCMethodMap.get('eth_estimateGas')
const rpc = makeRPCRequest('eth_estimateGas', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
Expand All @@ -371,16 +378,20 @@ describe('rpc methods', () => {
const ret = await executor(rpc, host, timeout)
const { url, body } = xhrUtility.extractRequest()

expect(url).to.be.equal('/accounts/0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed?revision=best')
expect(body).to.have.property('value', '0x64')
expect(url).to.be.equal('/accounts/*?revision=best')
expect(body).to.have.property('gas', 100)
expect(body).to.have.property('gasPrice', '0x64')
expect(body).to.have.property('caller', '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed')

expect(body).to.have.property('clauses')
expect((body as any).clauses).to.be.an('array').to.have.lengthOf(1)
expect((body as any).clauses[0]).to.have.property('value', '0x64')

expect(ret.result).to.be.equal(21000)
})

it('gas Used should increase 20%', async () => {
xhrUtility.setResponse({ gasUsed: 10 })
it('gas Used should increase 15000', async () => {
xhrUtility.setResponse([{ gasUsed: 10 }])
const executor = RPCMethodMap.get('eth_estimateGas')
const rpc = makeRPCRequest('eth_estimateGas', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
Expand All @@ -391,14 +402,14 @@ describe('rpc methods', () => {
}])

const ret = await executor(rpc, host, timeout)
expect(ret.result).to.be.equal(21012)
expect(ret.result).to.be.equal(21000 + 10 + 15000)
})

it('reverted should return rpc error', async () => {
xhrUtility.setResponse({
xhrUtility.setResponse([{
reverted: true,
data: '0x08c379a00x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f736f6d657468696e672077726f6e670000000000000000000000000000000000',
})
}])
const executor = RPCMethodMap.get('eth_estimateGas')
const rpc = makeRPCRequest('eth_estimateGas', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
Expand All @@ -414,11 +425,30 @@ describe('rpc methods', () => {

})

it('vmError present should return rpc error', async () => {
xhrUtility.setResponse([{
vmError: 'invalid opcode 0xfe',
}])
const executor = RPCMethodMap.get('eth_estimateGas')
const rpc = makeRPCRequest('eth_estimateGas', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
from: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
gas: '0x64',
value: '0x64',
gasPrice: '0x64',
}])

const ret = await executor(rpc, host, timeout)
expect(ret).to.have.property('error')
expect(ret.error).to.have.property('message', 'Gas estimation failed: invalid opcode 0xfe')

})

it('request without from should not have caller in body', async () => {
xhrUtility.setResponse({
xhrUtility.setResponse([{
reverted: true,
data: '0x08c379a00x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f736f6d657468696e672077726f6e670000000000000000000000000000000000',
})
}])
const executor = RPCMethodMap.get('eth_estimateGas')
const rpc = makeRPCRequest('eth_estimateGas', [{
to: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
Expand All @@ -435,15 +465,19 @@ describe('rpc methods', () => {

})

it('deploy contract should request the right url', async () => {
it('deploy contract should not set "to" to null', async () => {
const executor = RPCMethodMap.get('eth_estimateGas')
const rpc = makeRPCRequest('eth_estimateGas', [{
from: '0x7567D83b7b8d80ADdCb281A71d54Fc7B3364ffed',
}])

await executor(rpc, host, timeout)
const { url } = xhrUtility.extractRequest()
expect(url).to.be.equal('/accounts?revision=best')
const { url, body } = xhrUtility.extractRequest()
expect(url).to.be.equal('/accounts/*?revision=best')

expect(body).to.have.property('clauses')
expect((body as any).clauses).to.be.an('array').to.have.lengthOf(1)
expect((body as any).clauses[0]).to.have.property('to', null)
})

it('return null', async () => {
Expand Down
45 changes: 28 additions & 17 deletions test/web3/eth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,26 +332,29 @@ describe('web3.eth', () => {
})

it('call with valid response', async () => {
xhrUtility.setResponse({
xhrUtility.setResponse([{
data: '0x0000000000000000000000000000000000000000000000000000000000000001',
})
}])
const result = await web3.eth.call({
from: '0x7567d83b7b8d80addcb281a71d54fc7b3364ffed',
to: '0x0000000000000000000000000000456e65726779',
data: '0xa9059cbb000000000000000000000000d3ae78222beadb038203be21ed5ce7c9b1bff6020000000000000000000000000000000000000000000000000000000000000064',
})
const { url, body } = xhrUtility.extractRequest()

expect(url).to.be.equal('/accounts/0x0000000000000000000000000000456e65726779?revision=best')
expect(url).to.be.equal('/accounts/*?revision=best')
expect(result).to.be.equal('0x0000000000000000000000000000000000000000000000000000000000000001')
expect(body).to.have.property('caller', '0x7567d83b7b8d80addcb281a71d54fc7b3364ffed')
expect(body).to.have.property('data', '0xa9059cbb000000000000000000000000d3ae78222beadb038203be21ed5ce7c9b1bff6020000000000000000000000000000000000000000000000000000000000000064')
expect(body).to.have.property('clauses')
expect((body as any).clauses).to.be.an('array').to.have.lengthOf(1)
expect((body as any).clauses[0]).to.have.property('to', '0x0000000000000000000000000000456e65726779')
expect((body as any).clauses[0]).to.have.property('data', '0xa9059cbb000000000000000000000000d3ae78222beadb038203be21ed5ce7c9b1bff6020000000000000000000000000000000000000000000000000000000000000064')
})

it('estimateGas with valid response', async () => {
xhrUtility.setResponse({
xhrUtility.setResponse([{
gasUsed: 0,
})
}])
const result = await web3.eth.estimateGas({
from: '0x7567d83b7b8d80addcb281a71d54fc7b3364ffed',
to: '0x0000000000000000000000000000456e65726779',
Expand All @@ -360,10 +363,12 @@ describe('web3.eth', () => {
})
const { url, body } = xhrUtility.extractRequest()

expect(url).to.be.equal('/accounts/0x0000000000000000000000000000456e65726779?revision=best')
expect(url).to.be.equal('/accounts/*?revision=best')
expect(result).to.be.equal(21000)
expect(body).to.have.property('caller', '0x7567d83b7b8d80addcb281a71d54fc7b3364ffed')
expect(body).to.have.property('value', '0x64')
expect(body).to.have.property('clauses')
expect((body as any).clauses).to.be.an('array').to.have.lengthOf(1)
expect((body as any).clauses[0]).to.have.property('value', '0x64')
})

it('getPastLogs with valid response', async () => {
Expand Down Expand Up @@ -434,27 +439,33 @@ describe('web3.eth', () => {
describe('web3.eth.Contract', () => {

it('call method', async () => {
xhrUtility.setResponse({
xhrUtility.setResponse([{
data: '0x000000000000000000000000000000000000000003663fde3f5cc2921e0d7593',
})
}])
const result = await contract.methods.balanceOf('0xd3ae78222beadb038203be21ed5ce7c9b1bff602').call()
const { url, body } = xhrUtility.extractRequest()

expect(url).to.be.equal('/accounts/0x0000000000000000000000000000456e65726779?revision=best')
expect(url).to.be.equal('/accounts/*?revision=best')
expect(result).to.be.equal('1052067071896070588235347347')
expect(body).to.have.property('data', '0x70a08231000000000000000000000000d3ae78222beadb038203be21ed5ce7c9b1bff602')

expect(body).to.have.property('clauses')
expect((body as any).clauses).to.be.an('array').to.have.lengthOf(1)
expect((body as any).clauses[0]).to.have.property('data', '0x70a08231000000000000000000000000d3ae78222beadb038203be21ed5ce7c9b1bff602')
})

it('estimateGas method', async () => {
xhrUtility.setResponse({
xhrUtility.setResponse([{
gasUsed: 870,
})
}])
const result = await contract.methods.balanceOf('0xd3ae78222beadb038203be21ed5ce7c9b1bff602').estimateGas()
const { url, body } = xhrUtility.extractRequest()

expect(url).to.be.equal('/accounts/0x0000000000000000000000000000456e65726779?revision=best')
expect(body).to.have.property('data', '0x70a08231000000000000000000000000d3ae78222beadb038203be21ed5ce7c9b1bff602')
expect(result).to.be.equal(23724)
expect(url).to.be.equal('/accounts/*?revision=best')
expect(result).to.be.equal(36870)

expect(body).to.have.property('clauses')
expect((body as any).clauses).to.be.an('array').to.have.lengthOf(1)
expect((body as any).clauses[0]).to.have.property('data', '0x70a08231000000000000000000000000d3ae78222beadb038203be21ed5ce7c9b1bff602')
})

it('getPastLogs', async () => {
Expand Down

0 comments on commit 7b72f04

Please sign in to comment.