This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Add the ability to pick out struct fields from call response #169
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e080820
Add the ability to pick out struct fields from call response
mateuszradomski fa6a4f7
Changeset
mateuszradomski 1f07aff
Throw error on trying to pick from a scalar result
mateuszradomski 853e8d8
Tests
mateuszradomski df2d8e9
Fix formatting and liting errors
mateuszradomski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
196 changes: 196 additions & 0 deletions
196
packages/discovery/src/discovery/handlers/utils/callMethod.test.ts
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,196 @@ | ||
import { expect, mockObject } from 'earl' | ||
import { utils } from 'ethers' | ||
|
||
import { Bytes } from '../../../utils/Bytes' | ||
import { EthereumAddress } from '../../../utils/EthereumAddress' | ||
import { DiscoveryProvider } from '../../provider/DiscoveryProvider' | ||
import { callMethod } from './callMethod' | ||
|
||
describe('callMethod', () => { | ||
const ADDRESS = EthereumAddress.random() | ||
const BLOCK_NUMBER = 1234 | ||
const encoder = utils.defaultAbiCoder | ||
|
||
it('decodes struct returns', async () => { | ||
const RESULT_VALUE = EthereumAddress.random().toString() | ||
|
||
const abi = new utils.Interface([ | ||
'function testFunction() view returns (tuple(address r1, uint64 r2, address r3, uint64 r4))', | ||
]) | ||
|
||
const provider = mockObject<DiscoveryProvider>({ | ||
call: async () => | ||
Bytes.fromHex( | ||
encoder.encode( | ||
['tuple(address r1, uint64 r2, address r3, uint64 r4)'], | ||
[[RESULT_VALUE, 1234, RESULT_VALUE, 5678]], | ||
), | ||
), | ||
}) | ||
|
||
const result = await callMethod( | ||
provider, | ||
ADDRESS, | ||
abi.getFunction('testFunction'), | ||
[], | ||
BLOCK_NUMBER, | ||
['r1', 'r4'], | ||
) | ||
|
||
expect(result.value).toEqual([RESULT_VALUE, 5678]) | ||
}) | ||
|
||
it('picks from multiple return values', async () => { | ||
const RESULT_VALUE = EthereumAddress.random().toString() | ||
|
||
const abi = new utils.Interface([ | ||
'function testFunction() view returns (address r1, uint64 r2, address r3, uint64 r4)', | ||
]) | ||
|
||
const provider = mockObject<DiscoveryProvider>({ | ||
call: async () => | ||
Bytes.fromHex( | ||
encoder.encode( | ||
['address r1', 'uint64 r2', 'address r3', 'uint64 r4'], | ||
[RESULT_VALUE, 1234, RESULT_VALUE, 5678], | ||
), | ||
), | ||
}) | ||
|
||
const result = await callMethod( | ||
provider, | ||
ADDRESS, | ||
abi.getFunction('testFunction'), | ||
[], | ||
BLOCK_NUMBER, | ||
['r1', 'r4'], | ||
) | ||
|
||
expect(result.value).toEqual([RESULT_VALUE, 5678]) | ||
}) | ||
|
||
it('decodes multiple return values', async () => { | ||
const RESULT_VALUE = EthereumAddress.random().toString() | ||
|
||
const abi = new utils.Interface([ | ||
'function testFunction() view returns (address r1, uint64 r2, address r3, uint64 r4)', | ||
]) | ||
|
||
const provider = mockObject<DiscoveryProvider>({ | ||
call: async () => | ||
Bytes.fromHex( | ||
encoder.encode( | ||
['address r1', 'uint64 r2', 'address r3', 'uint64 r4'], | ||
[RESULT_VALUE, 1234, RESULT_VALUE, 5678], | ||
), | ||
), | ||
}) | ||
|
||
const result = await callMethod( | ||
provider, | ||
ADDRESS, | ||
abi.getFunction('testFunction'), | ||
[], | ||
BLOCK_NUMBER, | ||
) | ||
|
||
expect(result.value).toEqual([RESULT_VALUE, 1234, RESULT_VALUE, 5678]) | ||
}) | ||
|
||
it('picks from array return value', async () => { | ||
const RESULT_VALUES = [ | ||
EthereumAddress.random().toString(), | ||
EthereumAddress.random().toString(), | ||
] | ||
|
||
const abi = new utils.Interface([ | ||
'function testFunction() view returns (address[])', | ||
]) | ||
|
||
const provider = mockObject<DiscoveryProvider>({ | ||
call: async () => | ||
Bytes.fromHex(encoder.encode(['address[]'], [RESULT_VALUES])), | ||
}) | ||
|
||
const result = await callMethod( | ||
provider, | ||
ADDRESS, | ||
abi.getFunction('testFunction'), | ||
[], | ||
BLOCK_NUMBER, | ||
[1], | ||
) | ||
|
||
expect(result.value).toEqual(RESULT_VALUES[1]) | ||
}) | ||
|
||
it('decodes an array return value', async () => { | ||
const RESULT_VALUES = [ | ||
EthereumAddress.random().toString(), | ||
EthereumAddress.random().toString(), | ||
] | ||
|
||
const abi = new utils.Interface([ | ||
'function testFunction() view returns (address[])', | ||
]) | ||
|
||
const provider = mockObject<DiscoveryProvider>({ | ||
call: async () => | ||
Bytes.fromHex(encoder.encode(['address[]'], [RESULT_VALUES])), | ||
}) | ||
|
||
const result = await callMethod( | ||
provider, | ||
ADDRESS, | ||
abi.getFunction('testFunction'), | ||
[], | ||
BLOCK_NUMBER, | ||
) | ||
|
||
expect(result.value).toEqual(RESULT_VALUES) | ||
}) | ||
|
||
it('throws on trying to pick from scalar return value', async () => { | ||
const abi = new utils.Interface([ | ||
'function testFunction() view returns (uint256)', | ||
]) | ||
|
||
const provider = mockObject<DiscoveryProvider>({ | ||
call: async () => Bytes.randomOfLength(32), | ||
}) | ||
|
||
const result = await callMethod( | ||
provider, | ||
ADDRESS, | ||
abi.getFunction('testFunction'), | ||
[], | ||
BLOCK_NUMBER, | ||
['field'], | ||
) | ||
|
||
expect(result.error).toEqual( | ||
'Cannot pick fields from a non-struct-like return value', | ||
) | ||
}) | ||
|
||
it('decodes a scalar return value', async () => { | ||
const RETURN_VALUE = EthereumAddress.random() | ||
const abi = new utils.Interface([ | ||
'function testFunction() view returns (address)', | ||
]) | ||
|
||
const provider = mockObject<DiscoveryProvider>({ | ||
call: async () => Bytes.fromHex(RETURN_VALUE.toString()).padStart(32), | ||
}) | ||
|
||
const result = await callMethod( | ||
provider, | ||
ADDRESS, | ||
abi.getFunction('testFunction'), | ||
[], | ||
BLOCK_NUMBER, | ||
) | ||
|
||
expect(result.value).toEqual(RETURN_VALUE.toString()) | ||
}) | ||
}) |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.