diff --git a/mapper/amount.go b/mapper/amount.go deleted file mode 100644 index 2ecf828b..00000000 --- a/mapper/amount.go +++ /dev/null @@ -1,44 +0,0 @@ -package mapper - -import ( - "math/big" - - cconstants "github.com/ava-labs/avalanche-rosetta/constants/cchain" - pconstants "github.com/ava-labs/avalanche-rosetta/constants/pchain" - "github.com/coinbase/rosetta-sdk-go/types" - "github.com/ethereum/go-ethereum/common" -) - -func Amount(value *big.Int, currency *types.Currency) *types.Amount { - if value == nil { - return nil - } - - return &types.Amount{ - Value: value.String(), - Currency: currency, - } -} - -func AvaxAmount(value *big.Int) *types.Amount { - return Amount(value, cconstants.AvaxCurrency) -} - -// AtomicAvaxAmount creates a Rosetta Amount representing AVAX amount in nAVAXs with given quantity -func AtomicAvaxAmount(value *big.Int) *types.Amount { - return Amount(value, pconstants.AtomicAvaxCurrency) -} - -func Erc20Amount( - bytes []byte, - currency *types.Currency, - sender bool, -) *types.Amount { - value := common.BytesToHash(bytes).Big() - - if sender { - value = new(big.Int).Neg(value) - } - - return Amount(value, currency) -} diff --git a/mapper/account.go b/mapper/cchain/account.go similarity index 94% rename from mapper/account.go rename to mapper/cchain/account.go index 9e475fdd..b9521c39 100644 --- a/mapper/account.go +++ b/mapper/cchain/account.go @@ -1,4 +1,4 @@ -package mapper +package cchain import ( "github.com/coinbase/rosetta-sdk-go/types" diff --git a/mapper/cchain/amount.go b/mapper/cchain/amount.go new file mode 100644 index 00000000..ae85e4a3 --- /dev/null +++ b/mapper/cchain/amount.go @@ -0,0 +1,28 @@ +package cchain + +import ( + "math/big" + + cconstants "github.com/ava-labs/avalanche-rosetta/constants/cchain" + "github.com/ava-labs/avalanche-rosetta/mapper" + "github.com/coinbase/rosetta-sdk-go/types" + "github.com/ethereum/go-ethereum/common" +) + +func AvaxAmount(value *big.Int) *types.Amount { + return mapper.Amount(value, cconstants.AvaxCurrency) +} + +func Erc20Amount( + bytes []byte, + currency *types.Currency, + sender bool, +) *types.Amount { + value := common.BytesToHash(bytes).Big() + + if sender { + value = new(big.Int).Neg(value) + } + + return mapper.Amount(value, currency) +} diff --git a/mapper/block.go b/mapper/cchain/block.go similarity index 98% rename from mapper/block.go rename to mapper/cchain/block.go index e43dd446..d29f7937 100644 --- a/mapper/block.go +++ b/mapper/cchain/block.go @@ -1,4 +1,4 @@ -package mapper +package cchain import ( corethTypes "github.com/ava-labs/coreth/core/types" diff --git a/mapper/transaction.go b/mapper/cchain/transaction.go similarity index 97% rename from mapper/transaction.go rename to mapper/cchain/transaction.go index eb7b86e3..35847c0c 100644 --- a/mapper/transaction.go +++ b/mapper/cchain/transaction.go @@ -1,4 +1,4 @@ -package mapper +package cchain import ( "fmt" @@ -20,6 +20,7 @@ import ( "github.com/ava-labs/avalanche-rosetta/constants" cconstants "github.com/ava-labs/avalanche-rosetta/constants/cchain" pconstants "github.com/ava-labs/avalanche-rosetta/constants/pchain" + "github.com/ava-labs/avalanche-rosetta/mapper" ) const ( @@ -90,7 +91,7 @@ func Transaction( } // If in standard mode, token address must be whitelisted - if !isAnalyticsMode && !EqualFoldContains(standardModeWhiteList, log.Address.String()) { + if !isAnalyticsMode && !mapper.EqualFoldContains(standardModeWhiteList, log.Address.String()) { continue } @@ -117,7 +118,7 @@ func Transaction( continue } - erc20Ops := erc20Ops(log, ToCurrency(symbol, decimals, log.Address), int64(len(ops))) + erc20Ops := erc20Ops(log, toCurrency(symbol, decimals, log.Address), int64(len(ops))) ops = append(ops, erc20Ops...) default: } @@ -263,7 +264,7 @@ func createExportedOuts( txID ids.ID, exportedOuts []*avax.TransferableOutput, ) ([]*types.Operation, error) { - hrp, err := GetHRP(networkIdentifier) + hrp, err := mapper.GetHRP(networkIdentifier) if err != nil { return nil, err } @@ -335,7 +336,7 @@ func CrossChainTransactions( if len(exportedOuts) > 0 { transaction.Metadata = map[string]interface{}{ - MetadataExportedOutputs: exportedOuts, + mapper.MetadataExportedOutputs: exportedOuts, } } transactions = append(transactions, transaction) @@ -577,7 +578,7 @@ func erc721Ops(transferLog *ethtypes.Log, opsLen int64) []*types.Operation { toAddress := common.BytesToAddress(transferLog.Topics[2].Bytes()) metadata := map[string]interface{}{ ContractAddressMetadata: transferLog.Address.String(), - IndexTransferredMetadata: transferLog.Topics[3].String(), + indexTransferredMetadata: transferLog.Topics[3].String(), } // Mint diff --git a/mapper/transaction_test.go b/mapper/cchain/transaction_test.go similarity index 97% rename from mapper/transaction_test.go rename to mapper/cchain/transaction_test.go index 9a43e0d1..aa7b4dd2 100644 --- a/mapper/transaction_test.go +++ b/mapper/cchain/transaction_test.go @@ -1,4 +1,4 @@ -package mapper +package cchain import ( "encoding/hex" @@ -162,7 +162,7 @@ func TestERC721Ops(t *testing.T) { }, Metadata: map[string]interface{}{ ContractAddressMetadata: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7", - IndexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051", + indexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051", }, }, { @@ -181,7 +181,7 @@ func TestERC721Ops(t *testing.T) { }, Metadata: map[string]interface{}{ ContractAddressMetadata: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7", - IndexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051", + indexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051", }, }, }, erc721Ops(log, 1)) @@ -210,7 +210,7 @@ func TestERC721Ops(t *testing.T) { }, Metadata: map[string]interface{}{ ContractAddressMetadata: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7", - IndexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051", + indexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051", }, }, }, erc721Ops(log, 1)) @@ -239,7 +239,7 @@ func TestERC721Ops(t *testing.T) { }, Metadata: map[string]interface{}{ ContractAddressMetadata: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7", - IndexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051", + indexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051", }, }, }, erc721Ops(log, 1)) diff --git a/mapper/cchain/types.go b/mapper/cchain/types.go new file mode 100644 index 00000000..c22e6ad3 --- /dev/null +++ b/mapper/cchain/types.go @@ -0,0 +1,21 @@ +package cchain + +import ( + "github.com/coinbase/rosetta-sdk-go/types" + "github.com/ethereum/go-ethereum/common" +) + +const ( + ContractAddressMetadata = "contractAddress" + indexTransferredMetadata = "indexTransferred" +) + +func toCurrency(symbol string, decimals uint8, contractAddress common.Address) *types.Currency { + return &types.Currency{ + Symbol: symbol, + Decimals: int32(decimals), + Metadata: map[string]interface{}{ + ContractAddressMetadata: contractAddress.Hex(), + }, + } +} diff --git a/mapper/types_test.go b/mapper/cchain/types_test.go similarity index 87% rename from mapper/types_test.go rename to mapper/cchain/types_test.go index 09c9f566..393c74bc 100644 --- a/mapper/types_test.go +++ b/mapper/cchain/types_test.go @@ -1,4 +1,4 @@ -package mapper +package cchain import ( "testing" @@ -20,6 +20,6 @@ var USDC = &types.Currency{ func TestMixedCaseAddress(t *testing.T) { require := require.New(t) - parsedCurrency := ToCurrency("USDC", 6, common.HexToAddress("0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E")) + parsedCurrency := toCurrency("USDC", 6, common.HexToAddress("0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E")) require.True(utils.Equal(USDC, parsedCurrency)) } diff --git a/mapper/cchainatomictx/helper.go b/mapper/cchainatomictx/helper.go index ab93500d..e7427afb 100644 --- a/mapper/cchainatomictx/helper.go +++ b/mapper/cchainatomictx/helper.go @@ -1,10 +1,14 @@ package cchainatomictx import ( + "math/big" + "github.com/ava-labs/avalanchego/utils/formatting/address" "github.com/coinbase/rosetta-sdk-go/types" "github.com/ava-labs/avalanche-rosetta/constants" + pconstants "github.com/ava-labs/avalanche-rosetta/constants/pchain" + "github.com/ava-labs/avalanche-rosetta/mapper" ) // IsCChainBech32Address checks whether a given account identifier contains a C-chain Bech32 type address @@ -14,3 +18,8 @@ func IsCChainBech32Address(accountIdentifier *types.AccountIdentifier) bool { } return false } + +// AtomicAvaxAmount creates a Rosetta Amount representing AVAX amount in nAVAXs with given quantity +func AtomicAvaxAmount(value *big.Int) *types.Amount { + return mapper.Amount(value, pconstants.AtomicAvaxCurrency) +} diff --git a/mapper/cchainatomictx/tx_parser.go b/mapper/cchainatomictx/tx_parser.go index af0bc377..d73dc748 100644 --- a/mapper/cchainatomictx/tx_parser.go +++ b/mapper/cchainatomictx/tx_parser.go @@ -15,7 +15,6 @@ import ( cconstants "github.com/ava-labs/avalanche-rosetta/constants/cchain" pconstants "github.com/ava-labs/avalanche-rosetta/constants/pchain" - "github.com/ava-labs/avalanche-rosetta/mapper" ) var ( @@ -97,7 +96,7 @@ func (t *TxParser) insToOperations(startIdx int64, op cconstants.Op, ins []evm.E Type: op.String(), Account: &types.AccountIdentifier{Address: in.Address.Hex()}, // Negating input amount - Amount: mapper.AtomicAvaxAmount(new(big.Int).Neg(inputAmount)), + Amount: AtomicAvaxAmount(new(big.Int).Neg(inputAmount)), }) idx++ } @@ -123,7 +122,7 @@ func (t *TxParser) importedInToOperations(startIdx int64, opType cconstants.Op, Type: opType.String(), Account: account, // Negating input amount - Amount: mapper.AtomicAvaxAmount(new(big.Int).Neg(inputAmount)), + Amount: AtomicAvaxAmount(new(big.Int).Neg(inputAmount)), CoinChange: &types.CoinChange{ CoinIdentifier: &types.CoinIdentifier{Identifier: utxoID}, CoinAction: types.CoinSpent, diff --git a/mapper/pchain/tx_parser.go b/mapper/pchain/tx_parser.go index 804934d0..79075e97 100644 --- a/mapper/pchain/tx_parser.go +++ b/mapper/pchain/tx_parser.go @@ -20,6 +20,7 @@ import ( "github.com/ava-labs/avalanche-rosetta/constants" pconstants "github.com/ava-labs/avalanche-rosetta/constants/pchain" "github.com/ava-labs/avalanche-rosetta/mapper" + "github.com/ava-labs/avalanche-rosetta/mapper/cchainatomictx" ) var ( @@ -142,7 +143,7 @@ func (t *TxParser) Parse(signedTx *txs.Tx) (*types.Transaction, error) { if ops.ImportIns != nil { importedInputs := addOperationIdentifiers(ops.ImportIns, idx) idx += len(importedInputs) - txMetadata[mapper.MetadataImportedInputs] = importedInputs + txMetadata[metadataImportedInputs] = importedInputs } if ops.ExportOuts != nil { @@ -478,7 +479,7 @@ func (t *TxParser) insToOperations( func (t *TxParser) buildAmount(value *big.Int, assetID ids.ID) (*types.Amount, error) { if assetID == t.cfg.AvaxAssetID { - return mapper.AtomicAvaxAmount(value), nil + return cchainatomictx.AtomicAvaxAmount(value), nil } if t.cfg.IsConstruction { diff --git a/mapper/pchain/tx_parser_test.go b/mapper/pchain/tx_parser_test.go index f3cf3a0d..291dccc3 100644 --- a/mapper/pchain/tx_parser_test.go +++ b/mapper/pchain/tx_parser_test.go @@ -13,6 +13,7 @@ import ( rosConst "github.com/ava-labs/avalanche-rosetta/constants" pconstants "github.com/ava-labs/avalanche-rosetta/constants/pchain" "github.com/ava-labs/avalanche-rosetta/mapper" + "github.com/ava-labs/avalanche-rosetta/mapper/cchainatomictx" mocks "github.com/ava-labs/avalanche-rosetta/mocks/client" ) @@ -270,7 +271,7 @@ func TestMapNonConstructionImportTx(t *testing.T) { assert.Equal(t, types.CoinCreated, rosettaTransaction.Operations[0].CoinChange.CoinAction) // Verify that export output are properly generated - importInputs, ok := rosettaTransaction.Metadata[mapper.MetadataImportedInputs].([]*types.Operation) + importInputs, ok := rosettaTransaction.Metadata[metadataImportedInputs].([]*types.Operation) assert.True(t, ok) importedInput := importTx.ImportedInputs[0] @@ -279,7 +280,7 @@ func TestMapNonConstructionImportTx(t *testing.T) { Type: pconstants.ImportAvax.String(), Status: types.String(rosConst.StatusSuccess), Account: nil, - Amount: mapper.AtomicAvaxAmount(big.NewInt(-int64(importedInput.Input().Amount()))), + Amount: cchainatomictx.AtomicAvaxAmount(big.NewInt(-int64(importedInput.Input().Amount()))), CoinChange: &types.CoinChange{ CoinIdentifier: &types.CoinIdentifier{Identifier: importedInput.UTXOID.String()}, CoinAction: types.CoinSpent, diff --git a/mapper/pchain/types.go b/mapper/pchain/types.go index b9bc3956..0a51a274 100644 --- a/mapper/pchain/types.go +++ b/mapper/pchain/types.go @@ -19,6 +19,8 @@ const ( SubAccountTypeLockedStakeable = "locked_stakeable" SubAccountTypeLockedNotStakeable = "locked_not_stakeable" SubAccountTypeStaked = "staked" + + metadataImportedInputs = "imported_inputs" ) var CallMethods = []string{} diff --git a/mapper/types.go b/mapper/types.go index b836dcb1..fe5d3699 100644 --- a/mapper/types.go +++ b/mapper/types.go @@ -1,27 +1,20 @@ package mapper import ( - "github.com/ethereum/go-ethereum/common" + "math/big" "github.com/coinbase/rosetta-sdk-go/types" ) -const ( - ContractAddressMetadata = "contractAddress" - IndexTransferredMetadata = "indexTransferred" +const MetadataExportedOutputs = "exported_outputs" - MetadataImportedInputs = "imported_inputs" - MetadataExportedOutputs = "exported_outputs" - MetadataAddressFormat = "address_format" - AddressFormatBech32 = "bech32" -) +func Amount(value *big.Int, currency *types.Currency) *types.Amount { + if value == nil { + return nil + } -func ToCurrency(symbol string, decimals uint8, contractAddress common.Address) *types.Currency { - return &types.Currency{ - Symbol: symbol, - Decimals: int32(decimals), - Metadata: map[string]interface{}{ - ContractAddressMetadata: contractAddress.Hex(), - }, + return &types.Amount{ + Value: value.String(), + Currency: currency, } } diff --git a/service/backend/cchainatomictx/backend.go b/service/backend/cchainatomictx/backend.go index df46ba0f..c78fbd48 100644 --- a/service/backend/cchainatomictx/backend.go +++ b/service/backend/cchainatomictx/backend.go @@ -9,7 +9,6 @@ import ( "github.com/ava-labs/avalanche-rosetta/client" cconstants "github.com/ava-labs/avalanche-rosetta/constants/cchain" - "github.com/ava-labs/avalanche-rosetta/mapper" cmapper "github.com/ava-labs/avalanche-rosetta/mapper/cchainatomictx" "github.com/ava-labs/avalanche-rosetta/service" ) @@ -48,7 +47,7 @@ func (b *Backend) ShouldHandleRequest(req interface{}) bool { case *types.AccountCoinsRequest: return cmapper.IsCChainBech32Address(r.AccountIdentifier) case *types.ConstructionDeriveRequest: - return r.Metadata[mapper.MetadataAddressFormat] == mapper.AddressFormatBech32 + return r.Metadata[metadataAddressFormat] == addressFormatBech32 case *types.ConstructionMetadataRequest: return r.Options[cmapper.MetadataAtomicTxGas] != nil case *types.ConstructionPreprocessRequest: diff --git a/service/backend/cchainatomictx/backend_test.go b/service/backend/cchainatomictx/backend_test.go index e699fd67..c61fcca0 100644 --- a/service/backend/cchainatomictx/backend_test.go +++ b/service/backend/cchainatomictx/backend_test.go @@ -9,7 +9,6 @@ import ( "github.com/ava-labs/avalanche-rosetta/constants" cconstants "github.com/ava-labs/avalanche-rosetta/constants/cchain" - "github.com/ava-labs/avalanche-rosetta/mapper" cmapper "github.com/ava-labs/avalanche-rosetta/mapper/cchainatomictx" "github.com/ava-labs/avalanche-rosetta/service" ) @@ -52,7 +51,7 @@ func TestShouldHandleRequest(t *testing.T) { assert.True(t, backend.ShouldHandleRequest(&types.ConstructionDeriveRequest{ NetworkIdentifier: cChainNetworkIdentifier, Metadata: map[string]interface{}{ - mapper.MetadataAddressFormat: mapper.AddressFormatBech32, + metadataAddressFormat: addressFormatBech32, }, })) assert.True(t, backend.ShouldHandleRequest(&types.ConstructionMetadataRequest{ diff --git a/service/backend/cchainatomictx/construction.go b/service/backend/cchainatomictx/construction.go index a1059f64..8e63f466 100644 --- a/service/backend/cchainatomictx/construction.go +++ b/service/backend/cchainatomictx/construction.go @@ -18,7 +18,8 @@ import ( "github.com/ava-labs/avalanche-rosetta/constants" cconstants "github.com/ava-labs/avalanche-rosetta/constants/cchain" "github.com/ava-labs/avalanche-rosetta/mapper" - cmapper "github.com/ava-labs/avalanche-rosetta/mapper/cchainatomictx" + cmapper "github.com/ava-labs/avalanche-rosetta/mapper/cchain" + camapper "github.com/ava-labs/avalanche-rosetta/mapper/cchainatomictx" "github.com/ava-labs/avalanche-rosetta/service" "github.com/ava-labs/avalanche-rosetta/service/backend/common" ) @@ -55,13 +56,13 @@ func (b *Backend) ConstructionPreprocess( return nil, service.WrapError(service.ErrInvalidInput, err) } - preprocessOptions := cmapper.Options{ + preprocessOptions := camapper.Options{ AtomicTxGas: new(big.Int).SetUint64(gasUsed), } switch firstIn.Type { case cconstants.Import.String(): - v, ok := req.Metadata[cmapper.MetadataSourceChain] + v, ok := req.Metadata[camapper.MetadataSourceChain] if !ok { return nil, service.WrapError(service.ErrInvalidInput, "source_chain metadata must be provided") } @@ -80,7 +81,7 @@ func (b *Backend) ConstructionPreprocess( preprocessOptions.From = firstIn.Account.Address preprocessOptions.DestinationChain = chain - if v, ok := req.Metadata[cmapper.MetadataNonce]; ok { + if v, ok := req.Metadata[camapper.MetadataNonce]; ok { stringObj, ok := v.(string) if !ok { return nil, service.WrapError(service.ErrInvalidInput, fmt.Errorf("%s is not a valid nonce string", v)) @@ -105,10 +106,10 @@ func (b *Backend) ConstructionPreprocess( func (b *Backend) estimateGasUsed(opType string, matches []*parser.Match) (uint64, error) { // building tx with dummy data to get byte size for fee estimate - tx, _, err := cmapper.BuildTx( + tx, _, err := camapper.BuildTx( opType, matches, - cmapper.Metadata{ + camapper.Metadata{ SourceChainID: &ids.Empty, DestinationChainID: &ids.Empty, }, @@ -127,7 +128,7 @@ func (b *Backend) ConstructionMetadata( ctx context.Context, req *types.ConstructionMetadataRequest, ) (*types.ConstructionMetadataResponse, *types.Error) { - var input cmapper.Options + var input camapper.Options err := mapper.UnmarshalJSONMap(req.Options, &input) if err != nil { return nil, service.WrapError(service.ErrInvalidInput, err) @@ -143,7 +144,7 @@ func (b *Backend) ConstructionMetadata( return nil, service.WrapError(service.ErrClientError, err) } - metadata := cmapper.Metadata{ + metadata := camapper.Metadata{ NetworkID: networkID, CChainID: cChainID, } @@ -203,8 +204,8 @@ func (b *Backend) calculateSuggestedFee(ctx context.Context, gasUsed *big.Int) ( } suggestedFeeEth := new(big.Int).Mul(gasUsed, baseFee) - suggestedFee := new(big.Int).Div(suggestedFeeEth, mapper.X2crate) - return mapper.AtomicAvaxAmount(suggestedFee), nil + suggestedFee := new(big.Int).Div(suggestedFeeEth, cmapper.X2crate) + return camapper.AtomicAvaxAmount(suggestedFee), nil } // ConstructionPayloads implements /construction/payloads endpoint for C-chain atomic transactions diff --git a/service/backend/cchainatomictx/construction_test.go b/service/backend/cchainatomictx/construction_test.go index b08c2531..15baacab 100644 --- a/service/backend/cchainatomictx/construction_test.go +++ b/service/backend/cchainatomictx/construction_test.go @@ -15,7 +15,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/ava-labs/avalanche-rosetta/constants" - "github.com/ava-labs/avalanche-rosetta/mapper" + camapper "github.com/ava-labs/avalanche-rosetta/mapper/cchainatomictx" mocks "github.com/ava-labs/avalanche-rosetta/mocks/client" "github.com/ava-labs/avalanche-rosetta/service" "github.com/ava-labs/avalanche-rosetta/service/backend/common" @@ -88,7 +88,7 @@ func TestExportTxConstruction(t *testing.T) { RelatedOperations: nil, Type: opExport, Account: cAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(-10_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(-10_000_000)), }, { OperationIdentifier: &types.OperationIdentifier{Index: 1}, @@ -97,7 +97,7 @@ func TestExportTxConstruction(t *testing.T) { }, Type: opExport, Account: pAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(9_719_250)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(9_719_250)), }, } @@ -296,7 +296,7 @@ func TestImportTxConstruction(t *testing.T) { OperationIdentifier: &types.OperationIdentifier{Index: 0}, Type: opImport, Account: cAccountBech32Identifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(-15_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(-15_000_000)), CoinChange: &types.CoinChange{ CoinIdentifier: &types.CoinIdentifier{Identifier: coinID1}, CoinAction: types.CoinSpent, @@ -306,7 +306,7 @@ func TestImportTxConstruction(t *testing.T) { OperationIdentifier: &types.OperationIdentifier{Index: 1}, Type: opImport, Account: cAccountBech32Identifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(-5_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(-5_000_000)), CoinChange: &types.CoinChange{ CoinIdentifier: &types.CoinIdentifier{Identifier: coinID2}, CoinAction: types.CoinSpent, @@ -320,7 +320,7 @@ func TestImportTxConstruction(t *testing.T) { }, Type: opImport, Account: cAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(19_692_050)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(19_692_050)), }, } diff --git a/service/backend/cchainatomictx/types.go b/service/backend/cchainatomictx/types.go index 8a34ece1..8efaac1b 100644 --- a/service/backend/cchainatomictx/types.go +++ b/service/backend/cchainatomictx/types.go @@ -21,6 +21,11 @@ var ( _ common.TxParser = &cAtomicTxParser{} ) +const ( + metadataAddressFormat = "address_format" + addressFormatBech32 = "bech32" +) + type cAtomicTx struct { Tx *evm.Tx Codec codec.Manager diff --git a/service/backend/pchain/construction.go b/service/backend/pchain/construction.go index 6c4834fe..dbbfd9bb 100644 --- a/service/backend/pchain/construction.go +++ b/service/backend/pchain/construction.go @@ -15,6 +15,7 @@ import ( "github.com/ava-labs/avalanche-rosetta/constants" pconstants "github.com/ava-labs/avalanche-rosetta/constants/pchain" "github.com/ava-labs/avalanche-rosetta/mapper" + camapper "github.com/ava-labs/avalanche-rosetta/mapper/cchainatomictx" pmapper "github.com/ava-labs/avalanche-rosetta/mapper/pchain" "github.com/ava-labs/avalanche-rosetta/service" "github.com/ava-labs/avalanche-rosetta/service/backend/common" @@ -170,7 +171,7 @@ func (b *Backend) buildStakingMetadata(options map[string]interface{}) (*pmapper Shares: preprocessOptions.Shares, } - zeroAvax := mapper.AtomicAvaxAmount(big.NewInt(0)) + zeroAvax := camapper.AtomicAvaxAmount(big.NewInt(0)) return &pmapper.Metadata{StakingMetadata: stakingMetadata}, zeroAvax, nil } @@ -182,7 +183,7 @@ func (b *Backend) getBaseTxFee(ctx context.Context) (*types.Amount, error) { } feeAmount := new(big.Int).SetUint64(uint64(fees.TxFee)) - suggestedFee := mapper.AtomicAvaxAmount(feeAmount) + suggestedFee := camapper.AtomicAvaxAmount(feeAmount) return suggestedFee, nil } diff --git a/service/backend/pchain/construction_test.go b/service/backend/pchain/construction_test.go index 38efc922..2fdcef59 100644 --- a/service/backend/pchain/construction_test.go +++ b/service/backend/pchain/construction_test.go @@ -16,7 +16,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/ava-labs/avalanche-rosetta/constants" - "github.com/ava-labs/avalanche-rosetta/mapper" + camapper "github.com/ava-labs/avalanche-rosetta/mapper/cchainatomictx" mocks "github.com/ava-labs/avalanche-rosetta/mocks/client" idxmocks "github.com/ava-labs/avalanche-rosetta/mocks/service/backend/pchain/indexer" "github.com/ava-labs/avalanche-rosetta/service" @@ -111,7 +111,7 @@ func TestExportTxConstruction(t *testing.T) { RelatedOperations: nil, Type: opExportAvax, Account: pAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(-1_000_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(-1_000_000_000)), CoinChange: &types.CoinChange{ CoinIdentifier: &types.CoinIdentifier{Identifier: coinID1}, CoinAction: types.CoinSpent, @@ -126,7 +126,7 @@ func TestExportTxConstruction(t *testing.T) { OperationIdentifier: &types.OperationIdentifier{Index: 1}, Type: opExportAvax, Account: cAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(999_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(999_000_000)), Metadata: map[string]interface{}{ "type": opTypeExport, "threshold": 1.0, @@ -329,7 +329,7 @@ func TestImportTxConstruction(t *testing.T) { RelatedOperations: nil, Type: opImportAvax, Account: cAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(-1_000_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(-1_000_000_000)), CoinChange: &types.CoinChange{ CoinIdentifier: &types.CoinIdentifier{Identifier: coinID1}, CoinAction: types.CoinSpent, @@ -344,7 +344,7 @@ func TestImportTxConstruction(t *testing.T) { OperationIdentifier: &types.OperationIdentifier{Index: 1}, Type: opImportAvax, Account: pAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(999_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(999_000_000)), Metadata: map[string]interface{}{ "type": opTypeOutput, "threshold": 1.0, @@ -548,7 +548,7 @@ func TestAddValidatorTxConstruction(t *testing.T) { RelatedOperations: nil, Type: opAddValidator, Account: pAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(-2_000_000_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(-2_000_000_000_000)), CoinChange: &types.CoinChange{ CoinIdentifier: &types.CoinIdentifier{Identifier: coinID1}, CoinAction: "coin_spent", @@ -563,7 +563,7 @@ func TestAddValidatorTxConstruction(t *testing.T) { OperationIdentifier: &types.OperationIdentifier{Index: 1}, Type: opAddValidator, Account: pAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(2_000_000_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(2_000_000_000_000)), Metadata: map[string]interface{}{ "type": opTypeStake, "locktime": 0.0, @@ -784,7 +784,7 @@ func TestAddDelegatorTxConstruction(t *testing.T) { RelatedOperations: nil, Type: opAddDelegator, Account: pAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(-25_000_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(-25_000_000_000)), CoinChange: &types.CoinChange{ CoinIdentifier: &types.CoinIdentifier{Identifier: coinID1}, CoinAction: "coin_spent", @@ -799,7 +799,7 @@ func TestAddDelegatorTxConstruction(t *testing.T) { OperationIdentifier: &types.OperationIdentifier{Index: 1}, Type: opAddDelegator, Account: pAccountIdentifier, - Amount: mapper.AtomicAvaxAmount(big.NewInt(25_000_000_000)), + Amount: camapper.AtomicAvaxAmount(big.NewInt(25_000_000_000)), Metadata: map[string]interface{}{ "type": opTypeStake, "locktime": 0.0, diff --git a/service/service_account.go b/service/service_account.go index 7339186e..70568b76 100644 --- a/service/service_account.go +++ b/service/service_account.go @@ -17,6 +17,7 @@ import ( "github.com/ava-labs/avalanche-rosetta/client" cconstants "github.com/ava-labs/avalanche-rosetta/constants/cchain" "github.com/ava-labs/avalanche-rosetta/mapper" + cmapper "github.com/ava-labs/avalanche-rosetta/mapper/cchain" ) // AccountBackend represents a backend that implements /account family of apis for a subset of requests. @@ -107,14 +108,14 @@ func (s AccountService) AccountBalance( balances := []*types.Amount{} if len(req.Currencies) == 0 { - balances = append(balances, mapper.AvaxAmount(avaxBalance)) + balances = append(balances, cmapper.AvaxAmount(avaxBalance)) } for _, currency := range req.Currencies { - value, ok := currency.Metadata[mapper.ContractAddressMetadata] + value, ok := currency.Metadata[cmapper.ContractAddressMetadata] if !ok { if utils.Equal(currency, cconstants.AvaxCurrency) { - balances = append(balances, mapper.AvaxAmount(avaxBalance)) + balances = append(balances, cmapper.AvaxAmount(avaxBalance)) continue } return nil, WrapError(ErrCallInvalidParams, errors.New("non-avax currencies must specify contractAddress in metadata")) @@ -137,7 +138,7 @@ func (s AccountService) AccountBalance( return nil, WrapError(ErrInternalError, err) } - amount := mapper.Erc20Amount(response, currency, false) + amount := cmapper.Erc20Amount(response, currency, false) balances = append(balances, amount) } diff --git a/service/service_block.go b/service/service_block.go index 3092b74a..24083551 100644 --- a/service/service_block.go +++ b/service/service_block.go @@ -15,7 +15,7 @@ import ( "github.com/ava-labs/avalanche-rosetta/client" "github.com/ava-labs/avalanche-rosetta/constants" - "github.com/ava-labs/avalanche-rosetta/mapper" + cmapper "github.com/ava-labs/avalanche-rosetta/mapper/cchain" ) // BlockBackend represents a backend that implements /block family of apis for a subset of requests @@ -136,7 +136,7 @@ func (s *BlockService) Block( ParentBlockIdentifier: parentBlockIdentifier, Timestamp: int64(block.Time() * utils.MillisecondsInSecond), Transactions: append(transactions, crosstx...), - Metadata: mapper.BlockMetadata(block), + Metadata: cmapper.BlockMetadata(block), }, }, nil } @@ -227,7 +227,7 @@ func (s *BlockService) fetchTransaction( return nil, WrapError(ErrClientError, err) } - transaction, err := mapper.Transaction(header, tx, &msg, receipt, trace, flattened, s.client, s.config.IsAnalyticsMode(), s.config.TokenWhiteList, s.config.IndexUnknownTokens) + transaction, err := cmapper.Transaction(header, tx, &msg, receipt, trace, flattened, s.client, s.config.IsAnalyticsMode(), s.config.TokenWhiteList, s.config.IndexUnknownTokens) if err != nil { return nil, WrapError(ErrInternalError, err) } @@ -245,7 +245,7 @@ func (s *BlockService) parseCrossChainTransactions( chainIDToAliasMapping := map[ids.ID]constants.ChainIDAlias{ ids.Empty: constants.PChain, } - crossTxs, err := mapper.CrossChainTransactions(networkIdentifier, chainIDToAliasMapping, s.config.AvaxAssetID, block, s.config.AP5Activation) + crossTxs, err := cmapper.CrossChainTransactions(networkIdentifier, chainIDToAliasMapping, s.config.AvaxAssetID, block, s.config.AP5Activation) if err != nil { return nil, WrapError(ErrInternalError, err) } diff --git a/service/service_construction.go b/service/service_construction.go index 70f109e6..bc7bb02a 100644 --- a/service/service_construction.go +++ b/service/service_construction.go @@ -22,6 +22,7 @@ import ( "github.com/ava-labs/avalanche-rosetta/client" cconstants "github.com/ava-labs/avalanche-rosetta/constants/cchain" "github.com/ava-labs/avalanche-rosetta/mapper" + cmapper "github.com/ava-labs/avalanche-rosetta/mapper/cchain" ) const ( @@ -170,7 +171,7 @@ func (s ConstructionService) ConstructionMetadata( return &types.ConstructionMetadataResponse{ Metadata: metadataMap, SuggestedFee: []*types.Amount{ - mapper.AvaxAmount(big.NewInt(suggestedFee)), + cmapper.AvaxAmount(big.NewInt(suggestedFee)), }, }, nil } @@ -518,7 +519,7 @@ func (s ConstructionService) ConstructionPayloads( transferData = []byte{} sendToAddress = ethcommon.HexToAddress(checkTo) } else { - contract, ok := fromCurrency.Metadata[mapper.ContractAddressMetadata].(string) + contract, ok := fromCurrency.Metadata[cmapper.ContractAddressMetadata].(string) if !ok { return nil, WrapError(ErrInvalidInput, fmt.Errorf("%s currency doesn't have a contract address in metadata", fromCurrency.Symbol)) @@ -731,7 +732,7 @@ func (s ConstructionService) CreateOperationDescription( } // ERC-20s must have contract address in metadata - if _, ok := currency.Metadata[mapper.ContractAddressMetadata].(string); !ok { + if _, ok := currency.Metadata[cmapper.ContractAddressMetadata].(string); !ok { return nil, fmt.Errorf("contractAddress must be populated in currency metadata") } @@ -799,7 +800,7 @@ func (s ConstructionService) getErc20TransferGasLimit( value *big.Int, currency *types.Currency, ) (uint64, error) { - contract, ok := currency.Metadata[mapper.ContractAddressMetadata] + contract, ok := currency.Metadata[cmapper.ContractAddressMetadata] if len(to) == 0 || value == nil || !ok { return erc20TransferGasLimit, nil } diff --git a/service/service_mempool.go b/service/service_mempool.go index 3802a6f3..2afae56e 100644 --- a/service/service_mempool.go +++ b/service/service_mempool.go @@ -3,11 +3,10 @@ package service import ( "context" + "github.com/ava-labs/avalanche-rosetta/client" + cmapper "github.com/ava-labs/avalanche-rosetta/mapper/cchain" "github.com/coinbase/rosetta-sdk-go/server" "github.com/coinbase/rosetta-sdk-go/types" - - "github.com/ava-labs/avalanche-rosetta/client" - "github.com/ava-labs/avalanche-rosetta/mapper" ) // MempoolService implements the /mempool/* endpoints @@ -40,8 +39,8 @@ func (s MempoolService) Mempool( return &types.MempoolResponse{ TransactionIdentifiers: append( - mapper.MempoolTransactionsIDs(content.Pending), - mapper.MempoolTransactionsIDs(content.Queued)..., + cmapper.MempoolTransactionsIDs(content.Pending), + cmapper.MempoolTransactionsIDs(content.Queued)..., ), }, nil }