From 64ffe4e916734d366d549fc5bb25fdc8e131e91a Mon Sep 17 00:00:00 2001 From: Nguyen Thuy Linh Date: Mon, 16 Dec 2024 10:42:50 +0700 Subject: [PATCH] fix kyberswap wrong taker amount (#108) * fix kyberswap wrong taker amount * add unit test --- v2/pkg/parser/kyberswap/abi.json | 108 ++++++++++++++++++++++ v2/pkg/parser/kyberswap/kyberswap.go | 52 ++++++++++- v2/pkg/parser/kyberswap/kyberswap_test.go | 19 ++++ v2/pkg/parser/kyberswap/parser.go | 47 +++++++++- 4 files changed, 218 insertions(+), 8 deletions(-) diff --git a/v2/pkg/parser/kyberswap/abi.json b/v2/pkg/parser/kyberswap/abi.json index a95f30a..7d77fb0 100644 --- a/v2/pkg/parser/kyberswap/abi.json +++ b/v2/pkg/parser/kyberswap/abi.json @@ -41,6 +41,114 @@ ], "name": "Swapped", "type": "event" + }, + { + "inputs": [ + { + "components": [ + { + "internalType": "address", + "name": "callTarget", + "type": "address" + }, + { + "internalType": "address", + "name": "approveTarget", + "type": "address" + }, + { + "internalType": "bytes", + "name": "targetData", + "type": "bytes" + }, + { + "components": [ + { + "internalType": "contract IERC20", + "name": "srcToken", + "type": "address" + }, + { + "internalType": "contract IERC20", + "name": "dstToken", + "type": "address" + }, + { + "internalType": "address[]", + "name": "srcReceivers", + "type": "address[]" + }, + { + "internalType": "uint256[]", + "name": "srcAmounts", + "type": "uint256[]" + }, + { + "internalType": "address[]", + "name": "feeReceivers", + "type": "address[]" + }, + { + "internalType": "uint256[]", + "name": "feeAmounts", + "type": "uint256[]" + }, + { + "internalType": "address", + "name": "dstReceiver", + "type": "address" + }, + { + "internalType": "uint256", + "name": "amount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "minReturnAmount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "flags", + "type": "uint256" + }, + { + "internalType": "bytes", + "name": "permit", + "type": "bytes" + } + ], + "internalType": "struct MetaAggregationRouterV2.SwapDescriptionV2", + "name": "desc", + "type": "tuple" + }, + { + "internalType": "bytes", + "name": "clientData", + "type": "bytes" + } + ], + "internalType": "struct MetaAggregationRouterV2.SwapExecutionParams", + "name": "execution", + "type": "tuple" + } + ], + "name": "swap", + "outputs": [ + { + "internalType": "uint256", + "name": "returnAmount", + "type": "uint256" + }, + { + "internalType": "uint256", + "name": "gasUsed", + "type": "uint256" + } + ], + "stateMutability": "payable", + "type": "function" } ] diff --git a/v2/pkg/parser/kyberswap/kyberswap.go b/v2/pkg/parser/kyberswap/kyberswap.go index b976d98..ff47500 100644 --- a/v2/pkg/parser/kyberswap/kyberswap.go +++ b/v2/pkg/parser/kyberswap/kyberswap.go @@ -26,11 +26,36 @@ var ( _ = common.Big1 _ = types.BloomLookup _ = event.NewSubscription + _ = abi.ConvertType ) +// MetaAggregationRouterV2SwapDescriptionV2 is an auto generated low-level Go binding around an user-defined struct. +type MetaAggregationRouterV2SwapDescriptionV2 struct { + SrcToken common.Address + DstToken common.Address + SrcReceivers []common.Address + SrcAmounts []*big.Int + FeeReceivers []common.Address + FeeAmounts []*big.Int + DstReceiver common.Address + Amount *big.Int + MinReturnAmount *big.Int + Flags *big.Int + Permit []byte +} + +// MetaAggregationRouterV2SwapExecutionParams is an auto generated low-level Go binding around an user-defined struct. +type MetaAggregationRouterV2SwapExecutionParams struct { + CallTarget common.Address + ApproveTarget common.Address + TargetData []byte + Desc MetaAggregationRouterV2SwapDescriptionV2 + ClientData []byte +} + // SwappedMetaData contains all meta data concerning the Swapped contract. var SwappedMetaData = &bind.MetaData{ - ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"contractIERC20\",\"name\":\"srcToken\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"contractIERC20\",\"name\":\"dstToken\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"dstReceiver\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"spentAmount\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"returnAmount\",\"type\":\"uint256\"}],\"name\":\"Swapped\",\"type\":\"event\"}]", + ABI: "[{\"anonymous\":false,\"inputs\":[{\"indexed\":false,\"internalType\":\"address\",\"name\":\"sender\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"contractIERC20\",\"name\":\"srcToken\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"contractIERC20\",\"name\":\"dstToken\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"address\",\"name\":\"dstReceiver\",\"type\":\"address\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"spentAmount\",\"type\":\"uint256\"},{\"indexed\":false,\"internalType\":\"uint256\",\"name\":\"returnAmount\",\"type\":\"uint256\"}],\"name\":\"Swapped\",\"type\":\"event\"},{\"inputs\":[{\"components\":[{\"internalType\":\"address\",\"name\":\"callTarget\",\"type\":\"address\"},{\"internalType\":\"address\",\"name\":\"approveTarget\",\"type\":\"address\"},{\"internalType\":\"bytes\",\"name\":\"targetData\",\"type\":\"bytes\"},{\"components\":[{\"internalType\":\"contractIERC20\",\"name\":\"srcToken\",\"type\":\"address\"},{\"internalType\":\"contractIERC20\",\"name\":\"dstToken\",\"type\":\"address\"},{\"internalType\":\"address[]\",\"name\":\"srcReceivers\",\"type\":\"address[]\"},{\"internalType\":\"uint256[]\",\"name\":\"srcAmounts\",\"type\":\"uint256[]\"},{\"internalType\":\"address[]\",\"name\":\"feeReceivers\",\"type\":\"address[]\"},{\"internalType\":\"uint256[]\",\"name\":\"feeAmounts\",\"type\":\"uint256[]\"},{\"internalType\":\"address\",\"name\":\"dstReceiver\",\"type\":\"address\"},{\"internalType\":\"uint256\",\"name\":\"amount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"minReturnAmount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"flags\",\"type\":\"uint256\"},{\"internalType\":\"bytes\",\"name\":\"permit\",\"type\":\"bytes\"}],\"internalType\":\"structMetaAggregationRouterV2.SwapDescriptionV2\",\"name\":\"desc\",\"type\":\"tuple\"},{\"internalType\":\"bytes\",\"name\":\"clientData\",\"type\":\"bytes\"}],\"internalType\":\"structMetaAggregationRouterV2.SwapExecutionParams\",\"name\":\"execution\",\"type\":\"tuple\"}],\"name\":\"swap\",\"outputs\":[{\"internalType\":\"uint256\",\"name\":\"returnAmount\",\"type\":\"uint256\"},{\"internalType\":\"uint256\",\"name\":\"gasUsed\",\"type\":\"uint256\"}],\"stateMutability\":\"payable\",\"type\":\"function\"}]", } // SwappedABI is the input ABI used to generate the binding from. @@ -134,11 +159,11 @@ func NewSwappedFilterer(address common.Address, filterer bind.ContractFilterer) // bindSwapped binds a generic wrapper to an already deployed contract. func bindSwapped(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { - parsed, err := abi.JSON(strings.NewReader(SwappedABI)) + parsed, err := SwappedMetaData.GetAbi() if err != nil { return nil, err } - return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil + return bind.NewBoundContract(address, *parsed, caller, transactor, filterer), nil } // Call invokes the (constant) contract method with params as input values and @@ -179,6 +204,27 @@ func (_Swapped *SwappedTransactorRaw) Transact(opts *bind.TransactOpts, method s return _Swapped.Contract.contract.Transact(opts, method, params...) } +// Swap is a paid mutator transaction binding the contract method 0xe21fd0e9. +// +// Solidity: function swap((address,address,bytes,(address,address,address[],uint256[],address[],uint256[],address,uint256,uint256,uint256,bytes),bytes) execution) payable returns(uint256 returnAmount, uint256 gasUsed) +func (_Swapped *SwappedTransactor) Swap(opts *bind.TransactOpts, execution MetaAggregationRouterV2SwapExecutionParams) (*types.Transaction, error) { + return _Swapped.contract.Transact(opts, "swap", execution) +} + +// Swap is a paid mutator transaction binding the contract method 0xe21fd0e9. +// +// Solidity: function swap((address,address,bytes,(address,address,address[],uint256[],address[],uint256[],address,uint256,uint256,uint256,bytes),bytes) execution) payable returns(uint256 returnAmount, uint256 gasUsed) +func (_Swapped *SwappedSession) Swap(execution MetaAggregationRouterV2SwapExecutionParams) (*types.Transaction, error) { + return _Swapped.Contract.Swap(&_Swapped.TransactOpts, execution) +} + +// Swap is a paid mutator transaction binding the contract method 0xe21fd0e9. +// +// Solidity: function swap((address,address,bytes,(address,address,address[],uint256[],address[],uint256[],address,uint256,uint256,uint256,bytes),bytes) execution) payable returns(uint256 returnAmount, uint256 gasUsed) +func (_Swapped *SwappedTransactorSession) Swap(execution MetaAggregationRouterV2SwapExecutionParams) (*types.Transaction, error) { + return _Swapped.Contract.Swap(&_Swapped.TransactOpts, execution) +} + // SwappedSwappedIterator is returned from FilterSwapped and is used to iterate over the raw logs and unpacked data for Swapped events raised by the Swapped contract. type SwappedSwappedIterator struct { Event *SwappedSwapped // Event containing the contract specifics and raw log diff --git a/v2/pkg/parser/kyberswap/kyberswap_test.go b/v2/pkg/parser/kyberswap/kyberswap_test.go index 179270e..366ab7e 100644 --- a/v2/pkg/parser/kyberswap/kyberswap_test.go +++ b/v2/pkg/parser/kyberswap/kyberswap_test.go @@ -8,6 +8,7 @@ import ( "testing" "time" + tradelogstype "github.com/KyberNetwork/tradelogs/v2/pkg/types" "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -58,3 +59,21 @@ func TestParseEvent(t *testing.T) { require.Equal(t, log[0].EventHash, p.eventHash) t.Log(log) } + +func TestParseWithCallFrameEvent(t *testing.T) { + eventRaw := `{"address":"0x6131b5fae19ea4f9d964eac0408e4408b66337b5","topics":["0xd6d4f5681c246c9f42c203e287975af1601f8df8035a9251f79aab5c8f09e2f8"],"data":"0x0000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000c904ae3a9b13e84","blockNumber":"0x14546f0","transactionHash":"0xb84d5ba76703a12ff65e7d4cd83a5e6ff533561162c922ad02695b6213d4b1c7","transactionIndex":"0x1","blockHash":"0x66806a6d24c19269a36f9b7a4a6738afc1d6a58d45811703bca1fce9e542e0be","logIndex":"0x91","removed":false}` + event := types.Log{} + err := json.Unmarshal([]byte(eventRaw), &event) + require.NoError(t, err) + + callFrameRaw := `{ "from": "0x2593154f192f46f65a4a0315a5d77556d5d3d264", "gas": "0x220b78", "gasUsed": "0x2232c", "to": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "input": "0xe21fd0e90000000000000000000000000000000000000000000000000000000000000020000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000005c000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004063407a490000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000831bf48183b999fde45294b14b55199072f0801b000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000000000000000000000016000000000000000000000000000000000000000000000000000000000000001a000000000000000000000000000000000000000000000000000000000000001e000000000000000000000000000000000000000000000000000000000000002000000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000001000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000a1456d2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "output": "0x0000000000000000000000000000000000000000000000000c904ae3a9b13e840000000000000000000000000000000000000000000000000000000000022191", "calls": [ { "from": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "gas": "0x216c96", "gasUsed": "0x2c55", "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "input": "0x23b872dd0000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd6700000000000000000000000000000000000000000000000000000000a1456d2b", "output": "0x0000000000000000000000000000000000000000000000000000000000000001", "calls": [ { "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "gas": "0x20e401", "gasUsed": "0x293a", "to": "0x43506849d7c04f9138d1a2050bbf3a0c054402dd", "input": "0x23b872dd0000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd6700000000000000000000000000000000000000000000000000000000a1456d2b", "output": "0x0000000000000000000000000000000000000000000000000000000000000001", "logs": [ { "address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x0000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264", "0x000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67" ], "data": "0x00000000000000000000000000000000000000000000000000000000a1456d2b", "position": "0x0" } ], "value": "0x0", "type": "DELEGATECALL" } ], "value": "0x0", "type": "CALL" }, { "from": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "gas": "0x213a1f", "gasUsed": "0x216", "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "input": "0x70a082310000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264", "output": "0x0000000000000000000000000000000000000000000000000000000000000000", "type": "STATICCALL" }, { "from": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "gas": "0x2135cf", "gasUsed": "0x53b", "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "input": "0x70a082310000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5", "output": "0x00000000000000000000000000000000000000000000000000000000030e798b", "calls": [ { "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "gas": "0x20ae1e", "gasUsed": "0x229", "to": "0x43506849d7c04f9138d1a2050bbf3a0c054402dd", "input": "0x70a082310000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5", "output": "0x00000000000000000000000000000000000000000000000000000000030e798b", "value": "0x0", "type": "DELEGATECALL" } ], "type": "STATICCALL" }, { "from": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "gas": "0x212e69", "gasUsed": "0x9e6", "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "input": "0x70a082310000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5", "output": "0x0000000000000000000000000000000000000000000000000029d7828ed0d0fb", "type": "STATICCALL" }, { "from": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "gas": "0x211be3", "gasUsed": "0x19736", "to": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "input": "0xd9c45357000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000002c0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000004063407a490000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000831bf48183b999fde45294b14b55199072f0801b000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000", "calls": [ { "from": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "gas": "0x206cb4", "gasUsed": "0xfc4d", "to": "0xf9e121c77caf7d8b7ac4bb58579015bc6f958b55", "input": "0x63407a490000000000000000000000000000000000000000000000000000000000000040000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000831bf48183b999fde45294b14b55199072f0801b000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001", "output": "0x0000000000000000000000000000000000000000000000000a62f485ecb317ee", "calls": [ { "from": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "gas": "0x1fe56e", "gasUsed": "0x53b", "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "input": "0x70a08231000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67", "output": "0x00000000000000000000000000000000000000000000000000000000a1456d2c", "calls": [ { "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "gas": "0x1f62fe", "gasUsed": "0x229", "to": "0x43506849d7c04f9138d1a2050bbf3a0c054402dd", "input": "0x70a08231000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67", "output": "0x00000000000000000000000000000000000000000000000000000000a1456d2c", "value": "0x0", "type": "DELEGATECALL" } ], "type": "STATICCALL" }, { "from": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "gas": "0x1fcae8", "gasUsed": "0xd2fc", "to": "0x831bf48183b999fde45294b14b55199072f0801b", "input": "0x128acb08000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000a1456d2c00000000000000000000000000000000000000000000000000000001000276a400000000000000000000000000000000000000000000000000000000000000a000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd670000000000000000000000000000000000000000000000000000000000000060000000000000000000000000831bf48183b999fde45294b14b55199072f0801b000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "output": "0x00000000000000000000000000000000000000000000000000000000a1456d2cfffffffffffffffffffffffffffffffffffffffffffffffff59d0b7a134ce812", "calls": [ { "from": "0x831bf48183b999fde45294b14b55199072f0801b", "gas": "0x1eeacb", "gasUsed": "0x229e", "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "input": "0xa9059cbb000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd670000000000000000000000000000000000000000000000000a62f485ecb317ee", "output": "0x0000000000000000000000000000000000000000000000000000000000000001", "logs": [ { "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000831bf48183b999fde45294b14b55199072f0801b", "0x000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67" ], "data": "0x0000000000000000000000000000000000000000000000000a62f485ecb317ee", "position": "0x0" } ], "value": "0x0", "type": "CALL" }, { "from": "0x831bf48183b999fde45294b14b55199072f0801b", "gas": "0x1ec551", "gasUsed": "0x53b", "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "input": "0x70a08231000000000000000000000000831bf48183b999fde45294b14b55199072f0801b", "output": "0x0000000000000000000000000000000000000000000000000000000e87487fbe", "calls": [ { "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "gas": "0x1e4762", "gasUsed": "0x229", "to": "0x43506849d7c04f9138d1a2050bbf3a0c054402dd", "input": "0x70a08231000000000000000000000000831bf48183b999fde45294b14b55199072f0801b", "output": "0x0000000000000000000000000000000000000000000000000000000e87487fbe", "value": "0x0", "type": "DELEGATECALL" } ], "type": "STATICCALL" }, { "from": "0x831bf48183b999fde45294b14b55199072f0801b", "gas": "0x1ebd10", "gasUsed": "0x376f", "to": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "input": "0x3a1c453c00000000000000000000000000000000000000000000000000000000a1456d2cfffffffffffffffffffffffffffffffffffffffffffffffff59d0b7a134ce812000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd670000000000000000000000000000000000000000000000000000000000000060000000000000000000000000831bf48183b999fde45294b14b55199072f0801b000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "calls": [ { "from": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "gas": "0x1e3756", "gasUsed": "0x2c2c", "to": "0x3fc7afe83f794bc87cad51bbad8eb974b560135e", "input": "0x3a1c453c00000000000000000000000000000000000000000000000000000000a1456d2cfffffffffffffffffffffffffffffffffffffffffffffffff59d0b7a134ce812000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000e000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000040000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd670000000000000000000000000000000000000000000000000000000000000060000000000000000000000000831bf48183b999fde45294b14b55199072f0801b000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "calls": [ { "from": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "gas": "0x1db43c", "gasUsed": "0x203c", "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "input": "0xa9059cbb000000000000000000000000831bf48183b999fde45294b14b55199072f0801b00000000000000000000000000000000000000000000000000000000a1456d2c", "output": "0x0000000000000000000000000000000000000000000000000000000000000001", "calls": [ { "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "gas": "0x1d3a8e", "gasUsed": "0x1d27", "to": "0x43506849d7c04f9138d1a2050bbf3a0c054402dd", "input": "0xa9059cbb000000000000000000000000831bf48183b999fde45294b14b55199072f0801b00000000000000000000000000000000000000000000000000000000a1456d2c", "output": "0x0000000000000000000000000000000000000000000000000000000000000001", "logs": [ { "address": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67", "0x000000000000000000000000831bf48183b999fde45294b14b55199072f0801b" ], "data": "0x00000000000000000000000000000000000000000000000000000000a1456d2c", "position": "0x0" } ], "value": "0x0", "type": "DELEGATECALL" } ], "value": "0x0", "type": "CALL" } ], "value": "0x0", "type": "DELEGATECALL" } ], "value": "0x0", "type": "CALL" }, { "from": "0x831bf48183b999fde45294b14b55199072f0801b", "gas": "0x1e8405", "gasUsed": "0x53b", "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "input": "0x70a08231000000000000000000000000831bf48183b999fde45294b14b55199072f0801b", "output": "0x0000000000000000000000000000000000000000000000000000000f288decea", "calls": [ { "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "gas": "0x1e071b", "gasUsed": "0x229", "to": "0x43506849d7c04f9138d1a2050bbf3a0c054402dd", "input": "0x70a08231000000000000000000000000831bf48183b999fde45294b14b55199072f0801b", "output": "0x0000000000000000000000000000000000000000000000000000000f288decea", "value": "0x0", "type": "DELEGATECALL" } ], "type": "STATICCALL" } ], "logs": [ { "address": "0x831bf48183b999fde45294b14b55199072f0801b", "topics": [ "0xc42079f94a6350d7e6235f29174924f928cc2ac818eb64fed8004e115fbcca67", "0x000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67", "0x000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67" ], "data": "0x00000000000000000000000000000000000000000000000000000000a1456d2cfffffffffffffffffffffffffffffffffffffffffffffffff59d0b7a134ce81200000000000000000000000000000000000040ddad43f0b67aa2fe64e6a3b09e000000000000000000000000000000000000000000000000002e8d73eccc0431000000000000000000000000000000000000000000000000000000000002f737", "position": "0x0" } ], "value": "0x0", "type": "CALL" } ], "logs": [ { "address": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "topics": [ "0xddac40937f35385a34f721af292e5a83fc5b840f722bff57c2fc71adba708c48" ], "data": "0x000000000000000000000000831bf48183b999fde45294b14b55199072f0801b0000000000000000000000000000000000000000000000000a62f485ecb317ee000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "position": "0x2" } ], "value": "0x0", "type": "DELEGATECALL" }, { "from": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "gas": "0x1f713c", "gasUsed": "0x216", "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "input": "0x70a08231000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67", "output": "0x0000000000000000000000000000000000000000000000000c904ae3a9b13e85", "type": "STATICCALL" }, { "from": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "gas": "0x1f6acb", "gasUsed": "0x53b", "to": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "input": "0x70a08231000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67", "output": "0x0000000000000000000000000000000000000000000000000000000000000000", "calls": [ { "from": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48", "gas": "0x1eea46", "gasUsed": "0x229", "to": "0x43506849d7c04f9138d1a2050bbf3a0c054402dd", "input": "0x70a08231000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67", "output": "0x0000000000000000000000000000000000000000000000000000000000000000", "value": "0x0", "type": "DELEGATECALL" } ], "type": "STATICCALL" }, { "from": "0xf081470f5c6fbccf48cc4e5b82dd926409dcdd67", "gas": "0x1f6159", "gasUsed": "0x5a7a", "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "input": "0xa9059cbb0000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d2640000000000000000000000000000000000000000000000000c904ae3a9b13e84", "output": "0x0000000000000000000000000000000000000000000000000000000000000001", "logs": [ { "address": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "topics": [ "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef", "0x000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd67", "0x0000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264" ], "data": "0x0000000000000000000000000000000000000000000000000c904ae3a9b13e84", "position": "0x0" } ], "value": "0x0", "type": "CALL" } ], "value": "0x0", "type": "CALL" }, { "from": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "gas": "0x1f892c", "gasUsed": "0x216", "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "input": "0x70a082310000000000000000000000006131b5fae19ea4f9d964eac0408e4408b66337b5", "output": "0x0000000000000000000000000000000000000000000000000029d7828ed0d0fb", "type": "STATICCALL" }, { "from": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "gas": "0x1f842f", "gasUsed": "0x216", "to": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "input": "0x70a082310000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264", "output": "0x0000000000000000000000000000000000000000000000000c904ae3a9b13e84", "type": "STATICCALL" } ], "logs": [ { "address": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "topics": [ "0xd6d4f5681c246c9f42c203e287975af1601f8df8035a9251f79aab5c8f09e2f8" ], "data": "0x0000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb48000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc20000000000000000000000002593154f192f46f65a4a0315a5d77556d5d3d264ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff0000000000000000000000000000000000000000000000000c904ae3a9b13e84", "position": "0x7" }, { "address": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "topics": [ "0xddac40937f35385a34f721af292e5a83fc5b840f722bff57c2fc71adba708c48" ], "data": "0x000000000000000000000000f081470f5c6fbccf48cc4e5b82dd926409dcdd670000000000000000000000000000000000000000000000000c904ae3a9b13e84000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", "position": "0x7" }, { "address": "0x6131b5fae19ea4f9d964eac0408e4408b66337b5", "topics": [ "0x095e66fa4dd6a6f7b43fb8444a7bd0edb870508c7abf639bc216efb0bcff9779" ], "data": "0x00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000", "position": "0x7" } ], "value": "0x0", "type": "CALL" }` + callFrame := tradelogstype.CallFrame{} + err = json.Unmarshal([]byte(callFrameRaw), &callFrame) + require.NoError(t, err) + + p := MustNewParser() + log, err := p.ParseWithCallFrame(callFrame, event, uint64(time.Now().Unix())) + require.NoError(t, err) + require.Equal(t, log[0].EventHash, p.eventHash) + t.Log(log) +} diff --git a/v2/pkg/parser/kyberswap/parser.go b/v2/pkg/parser/kyberswap/parser.go index 025cca6..5260422 100644 --- a/v2/pkg/parser/kyberswap/parser.go +++ b/v2/pkg/parser/kyberswap/parser.go @@ -1,9 +1,13 @@ package kyberswap import ( + "encoding/json" + "fmt" + "math/big" "strings" "github.com/KyberNetwork/tradelogs/v2/pkg/constant" + "github.com/KyberNetwork/tradelogs/v2/pkg/decoder" "github.com/KyberNetwork/tradelogs/v2/pkg/parser" storageTypes "github.com/KyberNetwork/tradelogs/v2/pkg/storage/tradelogs/types" "github.com/KyberNetwork/tradelogs/v2/pkg/types" @@ -49,12 +53,17 @@ func (p *Parser) Topics() []string { } func (p *Parser) Parse(log ethereumTypes.Log, blockTime uint64) ([]storageTypes.TradeLog, error) { + res, err := p.parse(log, blockTime) + return []storageTypes.TradeLog{res}, err +} + +func (p *Parser) parse(log ethereumTypes.Log, blockTime uint64) (storageTypes.TradeLog, error) { if len(log.Topics) > 0 && log.Topics[0].Hex() != p.eventHash { - return nil, parser.ErrInvalidTopic + return storageTypes.TradeLog{}, parser.ErrInvalidTopic } e, err := p.ps.ParseSwapped(log) if err != nil { - return nil, err + return storageTypes.TradeLog{}, err } res := storageTypes.TradeLog{ Exchange: p.Exchange(), @@ -70,7 +79,7 @@ func (p *Parser) Parse(log ethereumTypes.Log, blockTime uint64) ([]storageTypes. Timestamp: blockTime * 1000, EventHash: p.eventHash, } - return []storageTypes.TradeLog{res}, nil + return res, nil } func (p *Parser) Exchange() string { @@ -81,8 +90,36 @@ func (p *Parser) UseTraceCall() bool { return false } -func (p *Parser) ParseWithCallFrame(_ types.CallFrame, log ethereumTypes.Log, blockTime uint64) ([]storageTypes.TradeLog, error) { - return p.Parse(log, blockTime) +func (p *Parser) ParseWithCallFrame(callFrame types.CallFrame, log ethereumTypes.Log, blockTime uint64) ([]storageTypes.TradeLog, error) { + res, err := p.parse(log, blockTime) + if err != nil { + return nil, err + } + + contractCall, err := decoder.Decode(p.abi, callFrame.Input) + if err != nil { + return nil, fmt.Errorf("cannot decode contract call frame: %w", err) + } + for _, param := range contractCall.Params { + if param.Name != "execution" { + continue + } + executionBytes, err := json.Marshal(param.Value) + if err != nil { + return nil, fmt.Errorf("cannot marshal execution param: %w", err) + } + var execution *MetaAggregationRouterV2SwapExecutionParams + if err = json.Unmarshal(executionBytes, &execution); err != nil { + return nil, fmt.Errorf("cannot unmarshal execution param: %w", err) + } + srcAmounts := execution.Desc.SrcAmounts + total := new(big.Int) + for _, srcAmount := range srcAmounts { + total = total.Add(total, srcAmount) + } + res.TakerTokenAmount = total.String() + } + return []storageTypes.TradeLog{res}, nil } func (p *Parser) LogFromExchange(log ethereumTypes.Log) bool {