Skip to content

Commit

Permalink
fix: update convention, check input param lenght
Browse files Browse the repository at this point in the history
  • Loading branch information
vaigay committed Jul 25, 2024
1 parent fa81d4a commit 54ce8b4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
2 changes: 1 addition & 1 deletion pkg/decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func decode(method *abi.Method, data []byte) (*tradingTypes.ContractCall, error)
return contractCall, nil
}

func DecodeCustomAbi(ABI *abi.ABI, methodId Bytes4, rawData []byte) (*tradingTypes.ContractCall, error) {
func DecodeCustomABI(ABI *abi.ABI, methodId Bytes4, rawData []byte) (*tradingTypes.ContractCall, error) {
if ABI == nil {
return nil, fmt.Errorf("missing abi")
}
Expand Down
85 changes: 41 additions & 44 deletions pkg/parser/zxrfq_v3/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,25 @@ import (
)

var (
balanceArgument abi.Arguments
)

func init() {
// uint256 balance
balanceArgument = abi.Arguments{
{Name: "balance", Type: abitypes.Uint256},
}
}
)

type Parser struct {
abi *abi.ABI
customAbi *abi.ABI
traceCalls *tracecall.Cache
contractAddress common.Address
selectorAction map[decoder.Bytes4]string
selectorAction map[decoder.Bytes4]FunctionName
}

func MustNewParser(cache *tracecall.Cache, contractAddress common.Address) *Parser {
ab, err := ZxrfqV3MetaData.GetAbi()
if err != nil {
log.Fatalf("failed to get abi: %s", err)
}
customAb, err := zxrfq_v3_helper.ZxrfqV3HelperMetaData.GetAbi()
customABI, err := zxrfq_v3_helper.ZxrfqV3HelperMetaData.GetAbi()
if err != nil {
log.Fatalf("failed to get custom abi: %s", err)
}
Expand All @@ -52,7 +47,7 @@ func MustNewParser(cache *tracecall.Cache, contractAddress common.Address) *Pars
}
return &Parser{
abi: ab,
customAbi: customAb,
customAbi: customABI,
traceCalls: cache,
contractAddress: contractAddress,
selectorAction: getSettlerAction(),
Expand Down Expand Up @@ -90,7 +85,7 @@ func (p *Parser) Parse(log ethereumTypes.Log, blockTime uint64) (storage.TradeLo

callFrame, err := p.traceCalls.GetTraceCall(log.TxHash.Hex())
if err != nil {
return storage.TradeLog{}, err
return tradeLog, err
}
return p.recursiveDetectRFQTrades(tradeLog, callFrame, log)
}
Expand Down Expand Up @@ -166,43 +161,45 @@ func (p *Parser) ParseFromInternalCall(tradeLog storage.TradeLog, callFrame type
switch functionName {
// dev SC
case settler_otc_self_funded_name:
if currentActionIndex == actionIndex {
input, err := zxrfq_v3_helper.GetInputParamsOfFillRfqOrderSelfFunded(p.customAbi, methodIdDecodeParamOfFillOrderSelfFunded, data)
if err != nil {
return tradeLog, fmt.Errorf("get input param of fill rfq order self funded failed: %w", err)
}
tradeLog.Maker = input.Maker.String()
tradeLog.Taker = callFrame.From
tradeLog.MakerToken = input.Permit.Permitted.Token.String()
tradeLog.TakerToken = input.TakerToken.String()
tradeLog.Expiry = input.Permit.Deadline.Uint64()
var takerToken *big.Int
for _, subFrame := range callFrame.Calls {
x, _ := decoder.Decode(p.abi, subFrame.Input)
// find first method get balance of current contract for taker token
if x != nil && x.Name == balanceOf &&
strings.EqualFold(subFrame.To, tradeLog.TakerToken) &&
strings.EqualFold(subFrame.From, p.contractAddress.Hex()) {
//max current balance of zeroxv3 SC
takerToken, err = decodeOutputBalanceOf(subFrame.Output)
if err != nil {
return tradeLog, err
}
break
if currentActionIndex != actionIndex {
continue
}
currentActionIndex++

Check failure on line 167 in pkg/parser/zxrfq_v3/parser.go

View workflow job for this annotation

GitHub Actions / Run golangci-lint

ineffectual assignment to currentActionIndex (ineffassign)
input, err := zxrfq_v3_helper.GetInputParamsOfFillRfqOrderSelfFunded(p.customAbi, methodIdDecodeParamOfFillOrderSelfFunded, data)
if err != nil {
return tradeLog, fmt.Errorf("get input param of fill rfq order self funded failed: %w", err)
}
tradeLog.Maker = input.Maker.String()
tradeLog.Taker = callFrame.From
tradeLog.MakerToken = input.Permit.Permitted.Token.String()
tradeLog.TakerToken = input.TakerToken.String()
tradeLog.Expiry = input.Permit.Deadline.Uint64()
var takerToken *big.Int
for _, subFrame := range callFrame.Calls {
x, _ := decoder.Decode(p.abi, subFrame.Input)
// find first method get balance of current contract for taker token
if x != nil && x.Name == balanceOf &&
strings.EqualFold(subFrame.To, tradeLog.TakerToken) &&
strings.EqualFold(subFrame.From, p.contractAddress.Hex()) {
//max current balance of zeroxv3 SC
takerToken, err = decodeOutputBalanceOf(subFrame.Output)
if err != nil {
return tradeLog, err
}
break
}
if takerToken == nil {
return tradeLog, fmt.Errorf("can not find current balance of contract")
}
if takerToken.Cmp(input.MaxTakerAmount) > 0 {
takerToken = new(big.Int).Set(input.MaxTakerAmount)
}
newMakerAmount := calculateTokenAmount(input.Permit.Permitted.Amount, takerToken, input.MaxTakerAmount)
tradeLog.TakerTokenAmount = takerToken.String()
tradeLog.MakerTokenAmount = newMakerAmount.String()
return tradeLog, nil
}
case metatxn_settler_otc_permit2_function:
if takerToken == nil {
return tradeLog, fmt.Errorf("can not find current balance of contract")
}
if takerToken.Cmp(input.MaxTakerAmount) > 0 {
takerToken = new(big.Int).Set(input.MaxTakerAmount)
}
newMakerAmount := calculateTokenAmount(input.Permit.Permitted.Amount, takerToken, input.MaxTakerAmount)
tradeLog.TakerTokenAmount = takerToken.String()
tradeLog.MakerTokenAmount = newMakerAmount.String()
return tradeLog, nil
case metatxn_settler_otc_permit2_name:
//TODO
break
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/parser/zxrfq_v3/zxrfq_v3_helper/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ type InputParamOfFillRfqOrderSelfFunded struct {
MaxTakerAmount *big.Int `json:"maxTakerAmount"`
}

func GetInputParamsOfFillRfqOrderSelfFunded(customAbi *abi.ABI, actionName decoder.Bytes4, data []byte) (InputParamOfFillRfqOrderSelfFunded, error) {
contractCall, err := decoder.DecodeCustomAbi(customAbi, actionName, data)
func GetInputParamsOfFillRfqOrderSelfFunded(customABI *abi.ABI, actionName decoder.Bytes4, data []byte) (InputParamOfFillRfqOrderSelfFunded, error) {
contractCall, err := decoder.DecodeCustomABI(customABI, actionName, data)
if err != nil {
return InputParamOfFillRfqOrderSelfFunded{}, err
}
var input InputParamOfFillRfqOrderSelfFunded
var ok bool
inputParam := contractCall.Params
if len(inputParam) != 6 {
return InputParamOfFillRfqOrderSelfFunded{}, fmt.Errorf("invalid number of input params, expect 6 but got %d", len(inputParam))
}
input.Recipient, ok = inputParam[0].Value.(common.Address)
if !ok {
return InputParamOfFillRfqOrderSelfFunded{}, fmt.Errorf("failed to convert reciptent to common.Address")
Expand Down

0 comments on commit 54ce8b4

Please sign in to comment.