From 393d39697dd2158b4d20a63f1cd63075123cb75b Mon Sep 17 00:00:00 2001 From: Jingfu Wang Date: Tue, 14 Feb 2023 18:27:03 -0500 Subject: [PATCH] feat: handle mint op for user deposit tx (#25) * feat: handle mint op for user deposit tx Signed-off-by: Jingfu Wang * fix: mint logic for deposit Signed-off-by: Jingfu Wang * refactor: update short circuit evaluation Signed-off-by: Jingfu Wang --------- Signed-off-by: Jingfu Wang --- Makefile | 1 - pkg/client/client_ops.go | 29 ++--------------------------- pkg/handlers/fee.go | 6 ++++-- pkg/handlers/mint.go | 4 ++-- pkg/handlers/trace.go | 3 +++ pkg/handlers/utils.go | 19 +++++++++++++++++++ 6 files changed, 30 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index bbc9eab..03917ca 100644 --- a/Makefile +++ b/Makefile @@ -50,4 +50,3 @@ run: # Run tests test: go test -v ./... - diff --git a/pkg/client/client_ops.go b/pkg/client/client_ops.go index 3ca5e14..d46c191 100644 --- a/pkg/client/client_ops.go +++ b/pkg/client/client_ops.go @@ -1,10 +1,7 @@ package client import ( - "strings" - evmClient "github.com/coinbase/rosetta-geth-sdk/client" - sdkTypes "github.com/coinbase/rosetta-geth-sdk/types" RosettaTypes "github.com/coinbase/rosetta-sdk-go/types" "github.com/mdehoog/op-rosetta/pkg/handlers" ) @@ -17,36 +14,14 @@ func (c *OpClient) ParseOps( ) ([]*RosettaTypes.Operation, error) { var ops []*RosettaTypes.Operation - if tx.Receipt.Type == L1ToL2DepositType && len(tx.Trace) > 0 { - call := tx.Trace[0] - fromAddress := evmClient.MustChecksum(call.From.String()) - toAddress := evmClient.MustChecksum(call.To.String()) - - if fromAddress != toAddress { - feeOps, err := handlers.FeeOps(tx) - if err != nil { - return nil, err - } - ops = append(ops, feeOps...) - } - - opIndex := int64(len(ops) + 0) - opType := strings.ToUpper(call.Type) - opStatus := sdkTypes.SuccessStatus - toAmount := evmClient.Amount(call.Value, sdkTypes.Currency) - - toOp := handlers.GenerateOp(opIndex, nil, opType, opStatus, toAddress, toAmount, nil) - ops = append(ops, toOp) - return ops, nil - } - feeOps, err := handlers.FeeOps(tx) if err != nil { return nil, err } ops = append(ops, feeOps...) - // ops = append(ops, handlers.MintOps(tx, len(ops))...) + ops = append(ops, handlers.MintOps(tx, len(ops))...) + // TODO(Jingfu): handle burn operations // ops = append(ops, handlers.BurnOps(tx, len(ops))...) ops = append(ops, handlers.TraceOps(tx.Trace, len(ops))...) diff --git a/pkg/handlers/fee.go b/pkg/handlers/fee.go index 114fcab..adb884e 100644 --- a/pkg/handlers/fee.go +++ b/pkg/handlers/fee.go @@ -23,11 +23,13 @@ func FeeOps(tx *evmClient.LoadedTransaction) ([]*RosettaTypes.Operation, error) } sequencerFeeAmount := new(big.Int).Set(tx.FeeAmount) + L1Fee := big.NewInt(0) if tx.FeeBurned != nil { sequencerFeeAmount.Sub(sequencerFeeAmount, tx.FeeBurned) } if receipt.L1Fee != nil { - sequencerFeeAmount.Sub(sequencerFeeAmount, receipt.L1Fee) + L1Fee = receipt.L1Fee + sequencerFeeAmount.Sub(sequencerFeeAmount, L1Fee) } if sequencerFeeAmount == nil { return nil, nil @@ -62,7 +64,7 @@ func FeeOps(tx *evmClient.LoadedTransaction) ([]*RosettaTypes.Operation, error) }, } L1FeeVaultAddress := common.L1FeeVault.Hex() - L1FeeVaultAmount := evmClient.Amount(receipt.L1Fee, sdkTypes.Currency) + L1FeeVaultAmount := evmClient.Amount(L1Fee, sdkTypes.Currency) ops := []*RosettaTypes.Operation{ GenerateOp(0, nil, opType, opStatus, fromAddress, fromAmount, nil), diff --git a/pkg/handlers/mint.go b/pkg/handlers/mint.go index 0098953..d8f362d 100644 --- a/pkg/handlers/mint.go +++ b/pkg/handlers/mint.go @@ -16,10 +16,10 @@ func MintOps(tx *evmClient.LoadedTransaction, startIndex int) []*RosettaTypes.Op opIndex := int64(startIndex) opType := common.MintOpType opStatus := sdkTypes.SuccessStatus - toAddress := evmClient.MustChecksum(tx.Transaction.To().String()) + fromAddress := evmClient.MustChecksum(tx.From.String()) amount := evmClient.Amount(tx.Transaction.Mint(), sdkTypes.Currency) return []*RosettaTypes.Operation{ - GenerateOp(opIndex, nil, opType, opStatus, toAddress, amount, nil), + GenerateOp(opIndex, nil, opType, opStatus, fromAddress, amount, nil), } } diff --git a/pkg/handlers/trace.go b/pkg/handlers/trace.go index e5eb890..ad72fda 100644 --- a/pkg/handlers/trace.go +++ b/pkg/handlers/trace.go @@ -24,6 +24,9 @@ func TraceOps( opType := strings.ToUpper(call.Type) fromAddress := evmClient.MustChecksum(call.From.String()) toAddress := evmClient.MustChecksum(call.To.String()) + if strings.Contains(fromAddress, proxyContractFilter) && isIdenticalContractAddress(fromAddress, toAddress) { + toAddress = fromAddress + } value := call.Value metadata := map[string]interface{}{} diff --git a/pkg/handlers/utils.go b/pkg/handlers/utils.go index 7ffa272..c9e5f54 100644 --- a/pkg/handlers/utils.go +++ b/pkg/handlers/utils.go @@ -1,9 +1,16 @@ package handlers import ( + "strings" + "github.com/coinbase/rosetta-sdk-go/types" ) +const ( + proxyContractFilter = "0x420000000000000000000000000000000000" + implementationContractFilter = "0xc0d3c0d3c0d3c0d3c0d3c0d3c0d3c0d3c0d3" +) + func GenerateOp(opIndex int64, relatedOps []*types.OperationIdentifier, opType string, opStatus string, address string, amount *types.Amount, metadata map[string]interface{}) *types.Operation { return &types.Operation{ OperationIdentifier: &types.OperationIdentifier{ @@ -19,3 +26,15 @@ func GenerateOp(opIndex int64, relatedOps []*types.OperationIdentifier, opType s Metadata: metadata, } } + +func isIdenticalContractAddress(from string, to string) bool { + from = strings.ToLower(from) + to = strings.ToLower(to) + proxyContractIndex := from[len(proxyContractFilter):] + implementationContractIndex := to[len(implementationContractFilter):] + if strings.Contains(from, proxyContractFilter) && strings.Contains(to, implementationContractFilter) && proxyContractIndex == implementationContractIndex { + return true + } + + return false +}