From 348817aa0d8a28a3c0fab34177a5594dc7f035a9 Mon Sep 17 00:00:00 2001 From: Lutty Date: Sat, 19 Feb 2022 17:42:17 +0800 Subject: [PATCH] support rosetta tracer --- core/vm/evm.go | 2 +- rosetta/services/account.go | 38 ++++++++++++++++++++++++++++++++ rosetta/services/tx_operation.go | 4 ++-- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/core/vm/evm.go b/core/vm/evm.go index dfec0020c..c6a812f98 100644 --- a/core/vm/evm.go +++ b/core/vm/evm.go @@ -33,7 +33,7 @@ import ( var emptyCodeHash = crypto.Keccak256Hash(nil) type RosettaLogAddressItem struct { - Account, SubAccount common.Address + Account, SubAccount *common.Address Metadata map[string]interface{} } diff --git a/rosetta/services/account.go b/rosetta/services/account.go index 7866b5150..c726f922d 100644 --- a/rosetta/services/account.go +++ b/rosetta/services/account.go @@ -3,6 +3,7 @@ package services import ( "context" "fmt" + "github.com/harmony-one/harmony/core/vm" "math/big" "github.com/coinbase/rosetta-sdk-go/server" @@ -161,6 +162,43 @@ func newAccountIdentifier( }, nil } +// newAccountIdentifier .. +func newRosettaAccountIdentifier(address *vm.RosettaLogAddressItem) (*types.AccountIdentifier, *types.Error) { + b32Address, err := internalCommon.AddressToBech32(*address.Account) + if err != nil { + return nil, common.NewError(common.SanityCheckError, map[string]interface{}{ + "message": err.Error(), + }) + } + metadata, err := types.MarshalMap(AccountMetadata{Address: address.Account.String()}) + if err != nil { + return nil, common.NewError(common.CatchAllError, map[string]interface{}{ + "message": err.Error(), + }) + } + + ai := &types.AccountIdentifier{ + Address: b32Address, + Metadata: metadata, + } + + if address.SubAccount != nil { + b32Address, err := internalCommon.AddressToBech32(*address.SubAccount) + if err != nil { + return nil, common.NewError(common.SanityCheckError, map[string]interface{}{ + "message": err.Error(), + }) + } + + ai.SubAccount = &types.SubAccountIdentifier{ + Address: b32Address, + Metadata: address.Metadata, + } + } + + return ai, nil +} + func newSubAccountIdentifier( address ethCommon.Address, metadata map[string]interface{}, ) (*types.SubAccountIdentifier, *types.Error) { diff --git a/rosetta/services/tx_operation.go b/rosetta/services/tx_operation.go index 635f4c0d9..5e30bca57 100644 --- a/rosetta/services/tx_operation.go +++ b/rosetta/services/tx_operation.go @@ -478,12 +478,12 @@ func getContractInternalTransferNativeOperations( for _, log := range executionResult { // skip meaningless information if log.Value.Cmp(big.NewInt(0)) != 0 { - fromAccID, rosettaError := newAccountIdentifier(log.From.Account) + fromAccID, rosettaError := newRosettaAccountIdentifier(log.From) if rosettaError != nil { return nil, rosettaError } - toAccID, rosettaError := newAccountIdentifier(log.To.Account) + toAccID, rosettaError := newRosettaAccountIdentifier(log.To) if rosettaError != nil { return nil, rosettaError }