Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: all tx ops #22

Merged
merged 1 commit into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
4 changes: 2 additions & 2 deletions pkg/client/client_gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
)

// GetNativeTransferGasLimit is Ethereum's custom implementation of estimating gas.
func (c *OpClient) GetNativeTransferGasLimit(ctx context.Context, toAddress string,
fromAddress string, value *big.Int) (uint64, error) {
func (c *OpClient) GetNativeTransferGasLimit(ctx context.Context, toAddress string, fromAddress string, value *big.Int) (uint64, error) {
if len(toAddress) == 0 || value == nil {
// We guard against malformed inputs that may have been generated using
// a previous version of asset's rosetta
return 21000, nil
}

to := common.HexToAddress(toAddress)
return c.EstimateGas(ctx, ethereum.CallMsg{
From: common.HexToAddress(fromAddress),
Expand Down
34 changes: 11 additions & 23 deletions pkg/client/client_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,24 @@ func (c *OpClient) ParseOps(
var ops []*RosettaTypes.Operation

if tx.Receipt.Type == L1ToL2DepositType && len(tx.Trace) > 0 && tx.Transaction.IsSystemTx() {
trace := tx.Trace[0]
traceType := strings.ToUpper(trace.Type)
opStatus := sdkTypes.SuccessStatus
from := evmClient.MustChecksum(trace.From.String())
to := evmClient.MustChecksum(trace.To.String())
metadata := map[string]interface{}{}
call := tx.Trace[0]
fromAddress := evmClient.MustChecksum(call.From.String())
toAddress := evmClient.MustChecksum(call.To.String())

if from != to {
if fromAddress != toAddress {
feeOps, err := handlers.FeeOps(tx)
if err != nil {
return nil, err
}
ops = append(ops, feeOps...)
}

toOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(len(ops) + 0),
},
Type: traceType,
Status: RosettaTypes.String(opStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: to,
},
Amount: &RosettaTypes.Amount{
Value: trace.Value.String(),
Currency: sdkTypes.Currency,
},
Metadata: metadata,
}
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
}
Expand All @@ -59,7 +47,7 @@ func (c *OpClient) ParseOps(
ops = append(ops, feeOps...)

ops = append(ops, handlers.MintOps(tx, len(ops))...)
// ops = append(ops, handlers.BurnOps(tx, len(ops))...)
ops = append(ops, handlers.BurnOps(tx, len(ops))...)
ops = append(ops, handlers.TraceOps(tx.Trace, len(ops))...)

return ops, nil
Expand Down
File renamed without changes.
19 changes: 8 additions & 11 deletions pkg/handlers/burn.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ func BurnOps(tx *evmClient.LoadedTransaction, startIndex int) []*RosettaTypes.Op
if *tx.Transaction.To() != common.L2ToL1MessagePasser {
return nil
}

opIndex := int64(startIndex)
opType := common.BurnOpType
opStatus := sdkTypes.SuccessStatus
fromAddress := evmClient.MustChecksum(tx.From.String())
amount := evmClient.Amount(tx.Transaction.Value(), sdkTypes.Currency)

return []*RosettaTypes.Operation{
{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(startIndex),
},
Type: common.BurnOpType,
Status: RosettaTypes.String(sdkTypes.SuccessStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: tx.From.String(),
},
Amount: evmClient.Amount(tx.Transaction.Value(), sdkTypes.Currency),
},
GenerateOp(opIndex, nil, opType, opStatus, fromAddress, amount, nil),
}
}
18 changes: 7 additions & 11 deletions pkg/handlers/erc20_bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,13 @@ func NativeInitializationOps(tx *evmClient.LoadedTransaction, startIndex int) []
}

// Return the associated operation
opIndex := int64(startIndex)
opType := sdkTypes.OpErc20Transfer
opStatus := sdkTypes.SuccessStatus
fromAddress := evmClient.MustChecksum(tx.From.String())
amount := evmClient.Erc20Amount(transferLog.Data, transferLog.Address, sdkTypes.Currency.Symbol, sdkTypes.Currency.Decimals, false)

return []*RosettaTypes.Operation{
{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(startIndex),
},
Type: sdkTypes.OpErc20Transfer,
Status: RosettaTypes.String(sdkTypes.SuccessStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: tx.From.String(),
},
Amount: evmClient.Erc20Amount(transferLog.Data, transferLog.Address, sdkTypes.Currency.Symbol, sdkTypes.Currency.Decimals, false),
},
GenerateOp(opIndex, nil, opType, opStatus, fromAddress, amount, nil),
}
}
82 changes: 24 additions & 58 deletions pkg/handlers/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func FeeOps(tx *evmClient.LoadedTransaction) ([]*RosettaTypes.Operation, error)
if receipt.L1Fee != nil {
sequencerFeeAmount.Sub(sequencerFeeAmount, receipt.L1Fee)
}

if sequencerFeeAmount == nil {
return nil, nil
}
Expand All @@ -39,70 +38,37 @@ func FeeOps(tx *evmClient.LoadedTransaction) ([]*RosettaTypes.Operation, error)
feeRewarder = tx.Author
}

ops := []*RosettaTypes.Operation{
opType := sdkTypes.FeeOpType
opStatus := sdkTypes.SuccessStatus
fromAddress := evmClient.MustChecksum(tx.From.String())
fromAmount := evmClient.Amount(new(big.Int).Neg(tx.Receipt.TransactionFee), sdkTypes.Currency)
sequencerRelatedOps := []*RosettaTypes.OperationIdentifier{
{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: 0,
},
Type: sdkTypes.FeeOpType,
Status: RosettaTypes.String(sdkTypes.SuccessStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: evmClient.MustChecksum(tx.From.String()),
},
Amount: evmClient.Amount(new(big.Int).Neg(tx.Receipt.TransactionFee), sdkTypes.Currency),
Index: 0,
},

}
sequencerAddress := evmClient.MustChecksum(feeRewarder)
sequencerAmount := evmClient.Amount(sequencerFeeAmount, sdkTypes.Currency)
baseFeeVaultRelatedOps := []*RosettaTypes.OperationIdentifier{
{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: 1,
},
RelatedOperations: []*RosettaTypes.OperationIdentifier{
{
Index: 0,
},
},
Type: sdkTypes.FeeOpType,
Status: RosettaTypes.String(sdkTypes.SuccessStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: evmClient.MustChecksum(feeRewarder),
},
Amount: evmClient.Amount(sequencerFeeAmount, sdkTypes.Currency),
Index: 0,
},

}
baseFeeVaultAddress := common.BaseFeeVault.Hex()
baseFeeVaultAmount := evmClient.Amount(tx.FeeBurned, sdkTypes.Currency)
L1FeeVaultRelatedOps := []*RosettaTypes.OperationIdentifier{
{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: 2,
},
RelatedOperations: []*RosettaTypes.OperationIdentifier{
{
Index: 0,
},
},
Type: sdkTypes.FeeOpType,
Status: RosettaTypes.String(sdkTypes.SuccessStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: common.BaseFeeVault.Hex(),
},
// Note: The basefee is not actually burned on L2
Amount: evmClient.Amount(tx.FeeBurned, sdkTypes.Currency),
Index: 0,
},
}
L1FeeVaultAddress := common.L1FeeVault.Hex()
L1FeeVaultAmount := evmClient.Amount(receipt.L1Fee, sdkTypes.Currency)

{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: 3,
},
RelatedOperations: []*RosettaTypes.OperationIdentifier{
{
Index: 0,
},
},
Type: sdkTypes.FeeOpType,
Status: RosettaTypes.String(sdkTypes.SuccessStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: common.L1FeeVault.Hex(),
},
Amount: evmClient.Amount(receipt.L1Fee, sdkTypes.Currency),
},
ops := []*RosettaTypes.Operation{
GenerateOp(0, nil, opType, opStatus, fromAddress, fromAmount, nil),
GenerateOp(1, sequencerRelatedOps, opType, opStatus, sequencerAddress, sequencerAmount, nil),
GenerateOp(2, baseFeeVaultRelatedOps, opType, opStatus, baseFeeVaultAddress, baseFeeVaultAmount, nil),
GenerateOp(3, L1FeeVaultRelatedOps, opType, opStatus, L1FeeVaultAddress, L1FeeVaultAmount, nil),
}

return ops, nil
Expand Down
12 changes: 1 addition & 11 deletions pkg/handlers/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ func MintOps(tx *evmClient.LoadedTransaction, startIndex int) []*RosettaTypes.Op
amount := evmClient.Amount(tx.Transaction.Mint(), sdkTypes.Currency)

return []*RosettaTypes.Operation{
{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: opIndex,
},
Type: opType,
Status: RosettaTypes.String(opStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: toAddress,
},
Amount: amount,
},
GenerateOp(opIndex, nil, opType, opStatus, toAddress, amount, nil),
}
}
75 changes: 28 additions & 47 deletions pkg/handlers/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func TraceOps(
destroyedAccountBalance := make(map[string]*big.Int)
for _, call := range calls {
opType := strings.ToUpper(call.Type)
from := evmClient.MustChecksum(call.From.String())
to := evmClient.MustChecksum(call.To.String())
fromAddress := evmClient.MustChecksum(call.From.String())
toAddress := evmClient.MustChecksum(call.To.String())
value := call.Value
metadata := map[string]interface{}{}

Expand All @@ -34,59 +34,43 @@ func TraceOps(
metadata["error"] = call.ErrorMessage
}

fromOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: int64(len(ops) + startIndex),
},
Type: opType,
Status: RosettaTypes.String(opStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: from,
},
Amount: evmClient.Amount(new(big.Int).Neg(value), sdkTypes.Currency),
Metadata: metadata,
}
if _, ok := destroyedAccountBalance[from]; ok && opStatus == sdkTypes.SuccessStatus {
destroyedAccountBalance[from] = new(big.Int).Sub(destroyedAccountBalance[from], value)
// Generate "from" operation
fromOpIndex := int64(len(ops) + startIndex)
fromAmount := evmClient.Amount(new(big.Int).Neg(value), sdkTypes.Currency)
fromOp := GenerateOp(fromOpIndex, nil, opType, opStatus, fromAddress, fromAmount, metadata)
if _, ok := destroyedAccountBalance[fromAddress]; ok && opStatus == sdkTypes.SuccessStatus {
destroyedAccountBalance[fromAddress] = new(big.Int).Sub(destroyedAccountBalance[fromAddress], value)
}
ops = append(ops, fromOp)

// Add to the destroyed account balance if SELFDESTRUCT, and overwrite existing balance.
if opType == sdkTypes.SelfDestructOpType {
destroyedAccountBalance[from] = new(big.Int)
destroyedAccountBalance[fromAddress] = new(big.Int)

// If destination of of SELFDESTRUCT is self, we should skip.
// In the EVM, the balance is reset after the balance is increased on the destination, so this is a no-op.
if from == to {
if fromAddress == toAddress {
continue
}
}

// If the account is resurrected, we remove it from the destroyed account balance map.
if sdkTypes.CreateType(opType) {
delete(destroyedAccountBalance, to)
delete(destroyedAccountBalance, toAddress)
}

// Generate "to" operation
lastOpIndex := ops[len(ops)-1].OperationIdentifier.Index
toOp := &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: lastOpIndex + 1,
},
RelatedOperations: []*RosettaTypes.OperationIdentifier{
{
Index: lastOpIndex,
},
toOpIndex := lastOpIndex + 1
toRelatedOps := []*RosettaTypes.OperationIdentifier{
{
Index: lastOpIndex,
},
Type: opType,
Status: RosettaTypes.String(opStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: to,
},
Amount: evmClient.Amount(new(big.Int).Abs(value), sdkTypes.Currency),
Metadata: metadata,
}
if _, ok := destroyedAccountBalance[to]; ok && opStatus == sdkTypes.SuccessStatus {
destroyedAccountBalance[to] = new(big.Int).Add(destroyedAccountBalance[to], value)
toAmount := evmClient.Amount(new(big.Int).Abs(value), sdkTypes.Currency)
toOp := GenerateOp(toOpIndex, toRelatedOps, opType, opStatus, toAddress, toAmount, metadata)
if _, ok := destroyedAccountBalance[toAddress]; ok && opStatus == sdkTypes.SuccessStatus {
destroyedAccountBalance[toAddress] = new(big.Int).Add(destroyedAccountBalance[toAddress], value)
}
ops = append(ops, toOp)
}
Expand All @@ -101,17 +85,14 @@ func TraceOps(
log.Fatalf("negative balance for suicided account %s: %s\n", acct, balance.String())
}

ops = append(ops, &RosettaTypes.Operation{
OperationIdentifier: &RosettaTypes.OperationIdentifier{
Index: ops[len(ops)-1].OperationIdentifier.Index + 1,
},
Type: sdkTypes.DestructOpType,
Status: RosettaTypes.String(sdkTypes.SuccessStatus),
Account: &RosettaTypes.AccountIdentifier{
Address: acct,
},
Amount: evmClient.Amount(new(big.Int).Neg(balance), sdkTypes.Currency),
})
// Generate "destruct" operation
destructOpIndex := ops[len(ops)-1].OperationIdentifier.Index + 1
destructOpType := sdkTypes.DestructOpType
destructOpStatus := RosettaTypes.String(sdkTypes.SuccessStatus)
address := acct
amount := evmClient.Amount(new(big.Int).Neg(balance), sdkTypes.Currency)
destructOp := GenerateOp(destructOpIndex, nil, destructOpType, *destructOpStatus, address, amount, nil)
ops = append(ops, destructOp)
}

return ops
Expand Down
21 changes: 21 additions & 0 deletions pkg/handlers/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package handlers

import (
"github.com/coinbase/rosetta-sdk-go/types"
)

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{
Index: opIndex,
},
RelatedOperations: relatedOps,
Type: opType,
Status: types.String(opStatus),
Account: &types.AccountIdentifier{
Address: address,
},
Amount: amount,
Metadata: metadata,
}
}