Skip to content

Commit

Permalink
feat: handle mint op for user deposit tx (#25)
Browse files Browse the repository at this point in the history
* feat: handle mint op for user deposit tx

Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>

* fix: mint logic for deposit

Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>

* refactor: update short circuit evaluation

Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>

---------

Signed-off-by: Jingfu Wang <jingfu.wang@coinbase.com>
  • Loading branch information
GeekArthur authored Feb 14, 2023
1 parent fa0077f commit 393d396
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 32 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ run:
# Run tests
test:
go test -v ./...

29 changes: 2 additions & 27 deletions pkg/client/client_ops.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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))...)

Expand Down
6 changes: 4 additions & 2 deletions pkg/handlers/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions pkg/handlers/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
3 changes: 3 additions & 0 deletions pkg/handlers/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{}

Expand Down
19 changes: 19 additions & 0 deletions pkg/handlers/utils.go
Original file line number Diff line number Diff line change
@@ -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{
Expand All @@ -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
}

0 comments on commit 393d396

Please sign in to comment.