Skip to content

Commit

Permalink
support dsa v1 spells
Browse files Browse the repository at this point in the history
  • Loading branch information
KABBOUCHI committed Jul 4, 2022
1 parent 195b865 commit 3f45eab
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@instadapp/utils",
"version": "0.1.4",
"version": "0.1.5",
"description": "",
"repository": "instadapp/utils",
"license": "MIT",
Expand Down
13 changes: 11 additions & 2 deletions src/cast/CastDecoder.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Interface, defaultAbiCoder } from '@ethersproject/abi'
import { isAddress } from '@ethersproject/address'
import { Contract } from '@ethersproject/contracts'
import { JsonRpcProvider } from '@ethersproject/providers'
import { AbiFetcher } from '../abi'
import { Network } from '../types'
import { ICastDecoderOptions } from './types'
const DSA_V1_CAST_ABI = ['function cast(address[] _targets, bytes[] _datas, address _origin)']
const DSA_V2_CAST_ABI = ['function cast(string[] calldata _targetNames, bytes[] calldata _datas, address _origin)']
const dsaV1Interface = new Interface(DSA_V1_CAST_ABI)
const dsaV2Interface = new Interface(DSA_V2_CAST_ABI)
const DEFAULTS: ICastDecoderOptions = {
instaConnectorsAddresses: {
Expand All @@ -30,9 +33,11 @@ export class CastDecoder {

getEncodedSpells (data: string) {
try {
const tx = dsaV2Interface.parseTransaction({ data })
const isDsaV2 = data.startsWith('0x9304c934')
const tx = (isDsaV2 ? dsaV2Interface : dsaV1Interface).parseTransaction({ data })

return {
targets: tx.args._targetNames,
targets: isDsaV2 ? tx.args._targetNames : tx.args._targets,
spells: tx.args._datas
}
} catch (error) {
Expand All @@ -41,6 +46,10 @@ export class CastDecoder {
}

async getConnectorAbi (connectorName: string, network: Network = 'mainnet') {
if (isAddress(connectorName)) {
return await this.options.abiFetcher.get(connectorName, network)
}

const instaConnectorsAddress = this.options.instaConnectorsAddresses[network]

const contract = new Contract(instaConnectorsAddress, [
Expand Down
27 changes: 27 additions & 0 deletions test/cast.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,31 @@ describe('cast', () => {
erc20: '0x03ab458634910AaD20eF5f1C8ee96F1D6ac54919'
})
})

test('can decode dsa v1 spells', async () => {
const input = '0xe0e90acf000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a000000000000000000000000003d70891b8994feb6cca7022b25c32be92ee3725000000000000000000000000000000000000000000000000000000000000000100000000000000000000000094dfafcc80b8460acf1cbc5cac17bd83c95e99920000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c47008dc750000000000000000000000000000000000000000000000000000000000000080ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055a52582d4100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

const spells = await castDecoder.getSpells(input, 'mainnet')

const expectedDecodedSpell =
{
connector: '0x94dFafCc80B8460AcF1cbC5cAc17bd83C95e9992',
data: '0x7008dc750000000000000000000000000000000000000000000000000000000000000080ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000055a52582d41000000000000000000000000000000000000000000000000000000',
method: 'withdraw',
args: [
'ZRX-A',
'115792089237316195423570985008687907853269984665640564039457584007913129639935',
'0',
'0'
],
namedArgs: {
tokenId: 'ZRX-A',
amt: '115792089237316195423570985008687907853269984665640564039457584007913129639935',
getId: '0',
setId: '0'
}
}

expect(spells).toEqual([expectedDecodedSpell])
})
})

0 comments on commit 3f45eab

Please sign in to comment.