Skip to content

Commit

Permalink
feat: add toPayload to contract functions (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
nachomazzara authored Apr 23, 2021
1 parent 2254a2d commit c0074e3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/SolidityFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,11 @@ export class SolidityFunction {
let displayName = this.displayName()

const execute = Object.assign(
(...args: any[]) => {
return this.execute(contract.requestManager, contract.address, ...args)
},
{ estimateGas: this.estimateGas.bind(this, contract.requestManager, contract.address) }
(...args: any[]) => this.execute(contract.requestManager, contract.address, ...args),
{
estimateGas: this.estimateGas.bind(this, contract.requestManager, contract.address),
toPayload: (...args: any[]) => this.toPayload(args)
}
)

if (!(contract as any)[displayName]) {
Expand Down
15 changes: 15 additions & 0 deletions test/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,21 @@ describe('contract', function () {
await didCall
})

it('should return function payload', async function () {
const provider = new FakeHttpProvider()
const rm = new RequestManager(provider)
const address = '0x1234567890123456789012345678901234567891'
const contract: any = await new ContractFactory(rm, desc).at(address)

const { data } = contract.buy.toPayload([
['0x6b80863d347a7bde24ee9317f04baaa9259a5d6a', [0], ['10000000000000000000'], ['0x14dC79964da2C08b23698B3D3cc7Ca32193d9955']]
])

expect(data).toEqual(
'0xa4fdc78a0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000006b80863d347a7bde24ee9317f04baaa9259a5d6a000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000014dc79964da2c08b23698b3d3cc7ca32193d9955',
)
})

it('should make a call with optional params', async function () {
const provider = new FakeHttpProvider()
const rm = new RequestManager(provider)
Expand Down
48 changes: 48 additions & 0 deletions test/solidityfunction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,52 @@ describe('solidityfunction', () => {
from: undefined
})
})

it('works to produce an output with tuples', async () => {
const fn = new SolidityFunction({
"inputs": [
{
"components": [
{
"internalType": "contract IERC721CollectionV2",
"name": "collection",
"type": "address"
},
{
"internalType": "uint256[]",
"name": "ids",
"type": "uint256[]"
},
{
"internalType": "uint256[]",
"name": "prices",
"type": "uint256[]"
},
{
"internalType": "address[]",
"name": "beneficiaries",
"type": "address[]"
}
],
"internalType": "struct CollectionStore.ItemToBuy[]",
"name": "_itemsToBuy",
"type": "tuple[]"
}
],
"name": "buy",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
})

expect(fn.toPayload([[
['0x6b80863d347a7bde24ee9317f04baaa9259a5d6a', [0], ['10000000000000000000'], ['0x14dC79964da2C08b23698B3D3cc7Ca32193d9955']]
]])).toEqual({
to: undefined,
data:
'0xa4fdc78a0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000200000000000000000000000006b80863d347a7bde24ee9317f04baaa9259a5d6a000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000008ac7230489e80000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000014dc79964da2c08b23698b3d3cc7ca32193d9955',
value: undefined,
from: undefined
})
})
})

0 comments on commit c0074e3

Please sign in to comment.