Skip to content

Commit

Permalink
Merge pull request #33 from lifinance/better-dest-calldata
Browse files Browse the repository at this point in the history
feat: names for destination calldata fields
  • Loading branch information
H3xept authored Aug 10, 2023
2 parents 55f8540 + baac1b9 commit f77d6b6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
17 changes: 16 additions & 1 deletion src/parser/build-parameter-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ParamType, Result } from 'ethers'

import { AbiCache } from '../abi-cache/abi-cache'

import { ParameterMap } from './parameter-map'
import { ParameterMap, SwapDataStruct } from './parameter-map'
import { parseParameterValue } from './parse-parameter-value'

export const buildParameterMap = (
Expand All @@ -23,3 +23,18 @@ export const buildParameterMap = (

return parameterMap
}, {})

export const listToSwapData = (list: unknown[]): SwapDataStruct => {
if (!(list.length === 7))
throw new Error('Cannot parse swap data. Incorrect amount of fields.')

return {
callTo: list[0],
approveTo: list[1],
sendingAssetId: list[2],
receivingAssetId: list[3],
fromAmount: (list[4] as bigint).toString(),
callData: list[5],
requiresDeposit: list[6],
} as unknown as SwapDataStruct
}
11 changes: 10 additions & 1 deletion src/parser/calldata-parsers/parse-amarok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AbiCoder } from 'ethers'

import { hexify } from '../hexify'
import { CallDataInformation } from '../parameter-map'
import { listToSwapData } from '..'

import { AMAROK_PAYLOAD_ABI } from './abis/amarok'

Expand Down Expand Up @@ -35,11 +36,19 @@ export const parseAmarok = (
return []
}

// We know that amaroks first param is a list of SwapData[] so we can treat it as such
const swapDatas = functionParameters[0].map(listToSwapData)
const receiver = functionParameters[1] as string
const massagedFunctionParams = {
swaps: swapDatas,
receiver,
}

return [
{
functionName: 'unnamed (amarok)',
rawCallData: encodedCallData,
functionParameters,
functionParameters: massagedFunctionParams,
} as CallDataInformation,
]
} catch (e) {
Expand Down
16 changes: 15 additions & 1 deletion src/parser/calldata-parsers/parse-stargate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AbiCoder } from 'ethers'

import { hexify } from '../hexify'
import { CallDataInformation } from '../parameter-map'
import { listToSwapData } from '../build-parameter-map'

import { STARGATE_PAYLOAD_ABI } from './abis/stargate'

Expand Down Expand Up @@ -34,11 +35,24 @@ export const parseStargate = (
return []
}

const txId = functionParameters[0] as string
// We know that stargates second param is a list of SwapData[] so we can treat it as such
const swapDatas = functionParameters[1].map(listToSwapData)

const assetId = functionParameters[2] as string
const receiver = functionParameters[3] as string
const massagedFunctionParams = {
transactionId: txId,
swaps: swapDatas,
assetId,
receiver,
}

return [
{
functionName: 'unnamed (stargate)',
rawCallData: encodedCallData,
functionParameters,
functionParameters: massagedFunctionParams,
} as CallDataInformation,
]
} catch (e) {
Expand Down
13 changes: 11 additions & 2 deletions src/parser/parser.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
StargateDataStruct,
SwapDataStruct,
} from './parameter-map'
import { listToSwapData } from './build-parameter-map'

const validateAndExtract = (
results: CallDataInformation[]
Expand Down Expand Up @@ -140,7 +141,12 @@ describe('Acceptance tests', () => {
rawCallData
)

expect(toolCallData.functionParameters).toStrictEqual(parsed)
expect(toolCallData.functionParameters).toStrictEqual({
transactionId: parsed[0],
swaps: parsed[1].map(listToSwapData),
assetId: parsed[2],
receiver: parsed[3],
})
})

it('swap + amarok + swap transfer', () => {
Expand All @@ -161,6 +167,9 @@ describe('Acceptance tests', () => {
rawCallData
)

expect(toolCallData.functionParameters).toStrictEqual(parsed)
expect(toolCallData.functionParameters).toStrictEqual({
swaps: parsed[0].map(listToSwapData),
receiver: parsed[1],
})
})
})

0 comments on commit f77d6b6

Please sign in to comment.