Skip to content

Commit

Permalink
Merge pull request #82 from KyberNetwork/paraswap_taker
Browse files Browse the repository at this point in the history
add paraswap_taker
  • Loading branch information
ngocthanh1389 authored Oct 8, 2024
2 parents d0767c8 + 1bc7827 commit 7b0a323
Show file tree
Hide file tree
Showing 5 changed files with 1,430 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/parser/paraswap_taker/abi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"address","name":"_weth","type":"address"},{"internalType":"uint256","name":"_partnerSharePercent","type":"uint256"},{"internalType":"uint256","name":"_maxFeePercent","type":"uint256"},{"internalType":"uint256","name":"_paraswapReferralShare","type":"uint256"},{"internalType":"uint256","name":"_paraswapSlippageShare","type":"uint256"},{"internalType":"contract IFeeClaimer","name":"_feeClaimer","type":"address"}],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes16","name":"uuid","type":"bytes16"},{"indexed":false,"internalType":"address","name":"partner","type":"address"},{"indexed":false,"internalType":"uint256","name":"feePercent","type":"uint256"},{"indexed":false,"internalType":"address","name":"initiator","type":"address"},{"indexed":true,"internalType":"address","name":"beneficiary","type":"address"},{"indexed":true,"internalType":"address","name":"srcToken","type":"address"},{"indexed":true,"internalType":"address","name":"destToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"srcAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"receivedAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"expectedAmount","type":"uint256"}],"name":"BoughtV3","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes16","name":"uuid","type":"bytes16"},{"indexed":false,"internalType":"address","name":"partner","type":"address"},{"indexed":false,"internalType":"uint256","name":"feePercent","type":"uint256"},{"indexed":false,"internalType":"address","name":"initiator","type":"address"},{"indexed":false,"internalType":"enum DirectSwap.DirectSwapKind","name":"kind","type":"uint8"},{"indexed":true,"internalType":"address","name":"beneficiary","type":"address"},{"indexed":true,"internalType":"address","name":"srcToken","type":"address"},{"indexed":true,"internalType":"address","name":"destToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"srcAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"receivedAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"expectedAmount","type":"uint256"}],"name":"SwappedDirect","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bytes16","name":"uuid","type":"bytes16"},{"indexed":false,"internalType":"address","name":"partner","type":"address"},{"indexed":false,"internalType":"uint256","name":"feePercent","type":"uint256"},{"indexed":false,"internalType":"address","name":"initiator","type":"address"},{"indexed":true,"internalType":"address","name":"beneficiary","type":"address"},{"indexed":true,"internalType":"address","name":"srcToken","type":"address"},{"indexed":true,"internalType":"address","name":"destToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"srcAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"receivedAmount","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"expectedAmount","type":"uint256"}],"name":"SwappedV3","type":"event"},{"inputs":[],"name":"ROUTER_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"WHITELISTED_ROLE","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"view","type":"function"},{"inputs":[{"components":[{"components":[{"internalType":"bytes32","name":"poolId","type":"bytes32"},{"internalType":"uint256","name":"assetInIndex","type":"uint256"},{"internalType":"uint256","name":"assetOutIndex","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes","name":"userData","type":"bytes"}],"internalType":"struct IBalancerV2Vault.BatchSwapStep[]","name":"swaps","type":"tuple[]"},{"internalType":"address[]","name":"assets","type":"address[]"},{"components":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"bool","name":"fromInternalBalance","type":"bool"},{"internalType":"address payable","name":"recipient","type":"address"},{"internalType":"bool","name":"toInternalBalance","type":"bool"}],"internalType":"struct IBalancerV2Vault.FundManagement","name":"funds","type":"tuple"},{"internalType":"int256[]","name":"limits","type":"int256[]"},{"internalType":"uint256","name":"fromAmount","type":"uint256"},{"internalType":"uint256","name":"toAmount","type":"uint256"},{"internalType":"uint256","name":"expectedAmount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"feePercent","type":"uint256"},{"internalType":"address","name":"vault","type":"address"},{"internalType":"address payable","name":"partner","type":"address"},{"internalType":"bool","name":"isApproved","type":"bool"},{"internalType":"address payable","name":"beneficiary","type":"address"},{"internalType":"bytes","name":"permit","type":"bytes"},{"internalType":"bytes16","name":"uuid","type":"bytes16"}],"internalType":"struct Utils.DirectBalancerV2","name":"data","type":"tuple"}],"name":"directBalancerV2GivenInSwap","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"components":[{"internalType":"bytes32","name":"poolId","type":"bytes32"},{"internalType":"uint256","name":"assetInIndex","type":"uint256"},{"internalType":"uint256","name":"assetOutIndex","type":"uint256"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"bytes","name":"userData","type":"bytes"}],"internalType":"struct IBalancerV2Vault.BatchSwapStep[]","name":"swaps","type":"tuple[]"},{"internalType":"address[]","name":"assets","type":"address[]"},{"components":[{"internalType":"address","name":"sender","type":"address"},{"internalType":"bool","name":"fromInternalBalance","type":"bool"},{"internalType":"address payable","name":"recipient","type":"address"},{"internalType":"bool","name":"toInternalBalance","type":"bool"}],"internalType":"struct IBalancerV2Vault.FundManagement","name":"funds","type":"tuple"},{"internalType":"int256[]","name":"limits","type":"int256[]"},{"internalType":"uint256","name":"fromAmount","type":"uint256"},{"internalType":"uint256","name":"toAmount","type":"uint256"},{"internalType":"uint256","name":"expectedAmount","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"uint256","name":"feePercent","type":"uint256"},{"internalType":"address","name":"vault","type":"address"},{"internalType":"address payable","name":"partner","type":"address"},{"internalType":"bool","name":"isApproved","type":"bool"},{"internalType":"address payable","name":"beneficiary","type":"address"},{"internalType":"bytes","name":"permit","type":"bytes"},{"internalType":"bytes16","name":"uuid","type":"bytes16"}],"internalType":"struct Utils.DirectBalancerV2","name":"data","type":"tuple"}],"name":"directBalancerV2GivenOutSwap","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"fromToken","type":"address"},{"internalType":"address","name":"toToken","type":"address"},{"internalType":"address","name":"exchange","type":"address"},{"internalType":"uint256","name":"fromAmount","type":"uint256"},{"internalType":"uint256","name":"toAmount","type":"uint256"},{"internalType":"uint256","name":"expectedAmount","type":"uint256"},{"internalType":"uint256","name":"feePercent","type":"uint256"},{"internalType":"int128","name":"i","type":"int128"},{"internalType":"int128","name":"j","type":"int128"},{"internalType":"address payable","name":"partner","type":"address"},{"internalType":"bool","name":"isApproved","type":"bool"},{"internalType":"enum Utils.CurveSwapType","name":"swapType","type":"uint8"},{"internalType":"address payable","name":"beneficiary","type":"address"},{"internalType":"bool","name":"needWrapNative","type":"bool"},{"internalType":"bytes","name":"permit","type":"bytes"},{"internalType":"bytes16","name":"uuid","type":"bytes16"}],"internalType":"struct Utils.DirectCurveV1","name":"data","type":"tuple"}],"name":"directCurveV1Swap","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"fromToken","type":"address"},{"internalType":"address","name":"toToken","type":"address"},{"internalType":"address","name":"exchange","type":"address"},{"internalType":"address","name":"poolAddress","type":"address"},{"internalType":"uint256","name":"fromAmount","type":"uint256"},{"internalType":"uint256","name":"toAmount","type":"uint256"},{"internalType":"uint256","name":"expectedAmount","type":"uint256"},{"internalType":"uint256","name":"feePercent","type":"uint256"},{"internalType":"uint256","name":"i","type":"uint256"},{"internalType":"uint256","name":"j","type":"uint256"},{"internalType":"address payable","name":"partner","type":"address"},{"internalType":"bool","name":"isApproved","type":"bool"},{"internalType":"enum Utils.CurveSwapType","name":"swapType","type":"uint8"},{"internalType":"address payable","name":"beneficiary","type":"address"},{"internalType":"bool","name":"needWrapNative","type":"bool"},{"internalType":"bytes","name":"permit","type":"bytes"},{"internalType":"bytes16","name":"uuid","type":"bytes16"}],"internalType":"struct Utils.DirectCurveV2","name":"data","type":"tuple"}],"name":"directCurveV2Swap","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"fromToken","type":"address"},{"internalType":"address","name":"toToken","type":"address"},{"internalType":"address","name":"exchange","type":"address"},{"internalType":"uint256","name":"fromAmount","type":"uint256"},{"internalType":"uint256","name":"toAmount","type":"uint256"},{"internalType":"uint256","name":"expectedAmount","type":"uint256"},{"internalType":"uint256","name":"feePercent","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"address payable","name":"partner","type":"address"},{"internalType":"bool","name":"isApproved","type":"bool"},{"internalType":"address payable","name":"beneficiary","type":"address"},{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"bytes","name":"permit","type":"bytes"},{"internalType":"bytes16","name":"uuid","type":"bytes16"}],"internalType":"struct Utils.DirectUniV3","name":"data","type":"tuple"}],"name":"directUniV3Buy","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"address","name":"fromToken","type":"address"},{"internalType":"address","name":"toToken","type":"address"},{"internalType":"address","name":"exchange","type":"address"},{"internalType":"uint256","name":"fromAmount","type":"uint256"},{"internalType":"uint256","name":"toAmount","type":"uint256"},{"internalType":"uint256","name":"expectedAmount","type":"uint256"},{"internalType":"uint256","name":"feePercent","type":"uint256"},{"internalType":"uint256","name":"deadline","type":"uint256"},{"internalType":"address payable","name":"partner","type":"address"},{"internalType":"bool","name":"isApproved","type":"bool"},{"internalType":"address payable","name":"beneficiary","type":"address"},{"internalType":"bytes","name":"path","type":"bytes"},{"internalType":"bytes","name":"permit","type":"bytes"},{"internalType":"bytes16","name":"uuid","type":"bytes16"}],"internalType":"struct Utils.DirectUniV3","name":"data","type":"tuple"}],"name":"directUniV3Swap","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"feeClaimer","outputs":[{"internalType":"contract IFeeClaimer","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getKey","outputs":[{"internalType":"bytes32","name":"","type":"bytes32"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"bytes","name":"","type":"bytes"}],"name":"initialize","outputs":[],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"maxFeePercent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"paraswapReferralShare","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"paraswapSlippageShare","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"partnerSharePercent","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"weth","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"}]
1 change: 1 addition & 0 deletions pkg/parser/paraswap_taker/gen.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abigen --abi=abi.json --pkg=ps_taker --out=ps_taker.go -type ps_taker
129 changes: 129 additions & 0 deletions pkg/parser/paraswap_taker/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package pstaker

import (
"strings"

"github.com/KyberNetwork/tradelogs/pkg/parser"
"github.com/KyberNetwork/tradelogs/pkg/storage"
tradingTypes "github.com/KyberNetwork/tradelogs/pkg/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
)

const (
SwapEvent = "SwappedV3"
SwapDirect = "SwappedDirect"
)

type Parser struct {
abi *abi.ABI
ps *PsTakerFilterer
eventHash string
directEventHash string
}

func MustNewParser() *Parser {
ps, err := NewPsTakerFilterer(common.Address{}, nil)
if err != nil {
panic(err)
}
ab, err := PsTakerMetaData.GetAbi()
if err != nil {
panic(err)
}
event, ok := ab.Events[SwapEvent]
if !ok {
panic("no such event: SwappedV3")
}
directEvent, ok := ab.Events[SwapDirect]
if !ok {
panic("no such event: SwappedDirect")
}
return &Parser{
ps: ps,
abi: ab,
eventHash: event.ID.String(),
directEventHash: directEvent.ID.String(),
}
}

func (p *Parser) Topics() []string {
return []string{
p.eventHash,
p.directEventHash,
}
}

func (p *Parser) Parse(log types.Log, blockTime uint64) (storage.TradeLog, error) {
if len(log.Topics) == 0 {
return storage.TradeLog{}, parser.ErrInvalidTopic
}

if log.Topics[0].Hex() == p.eventHash {
e, err := p.ps.ParseSwappedV3(log)
if err != nil {
return storage.TradeLog{}, err
}
res := storage.TradeLog{
Taker: e.Beneficiary.String(),
MakerToken: e.DestToken.String(),
TakerToken: e.SrcToken.String(),
MakerTokenAmount: e.ReceivedAmount.String(),
TakerTokenAmount: e.SrcAmount.String(),
ContractAddress: e.Raw.Address.String(),
BlockNumber: e.Raw.BlockNumber,
TxHash: e.Raw.TxHash.String(),
LogIndex: uint64(e.Raw.Index),
Timestamp: blockTime * 1000,
EventHash: p.eventHash,
}
return res, nil
}
if log.Topics[0].Hex() == p.directEventHash {
e, err := p.ps.ParseSwappedDirect(log)
if err != nil {
return storage.TradeLog{}, err
}
res := storage.TradeLog{
Taker: e.Initiator.String(),
MakerToken: e.DestToken.String(),
TakerToken: e.SrcToken.String(),
MakerTokenAmount: e.ReceivedAmount.String(),
TakerTokenAmount: e.SrcAmount.String(),
ContractAddress: e.Raw.Address.String(),
BlockNumber: e.Raw.BlockNumber,
TxHash: e.Raw.TxHash.String(),
LogIndex: uint64(e.Raw.Index),
Timestamp: blockTime * 1000,
EventHash: p.directEventHash,
}
return res, nil
}
return storage.TradeLog{}, parser.ErrInvalidTopic
}

func (p *Parser) Exchange() string {
return parser.ExParaswapTaker
}

func (p *Parser) UseTraceCall() bool {
return false
}

func (p *Parser) ParseWithCallFrame(
callFrame tradingTypes.CallFrame,
log types.Log,
blockTime uint64,
) (storage.TradeLog, error) {
return p.Parse(log, blockTime)
}

func (p *Parser) LogFromExchange(log types.Log) bool {
return strings.EqualFold(log.Address.String(), parser.AddrParaswapTaker) &&
len(log.Topics) > 0 && (strings.EqualFold(log.Topics[0].String(), p.eventHash) || strings.EqualFold(log.Topics[0].String(), p.directEventHash))
}

func (p *Parser) Address() string {
return parser.AddrParaswapTaker
}
Loading

0 comments on commit 7b0a323

Please sign in to comment.