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

[Codebase cleanup] Cchain mapper repack #175

Open
wants to merge 1 commit into
base: operations_cleanup
Choose a base branch
from
Open
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
44 changes: 0 additions & 44 deletions mapper/amount.go

This file was deleted.

2 changes: 1 addition & 1 deletion mapper/account.go → mapper/cchain/account.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mapper
package cchain

import (
"github.com/coinbase/rosetta-sdk-go/types"
Expand Down
28 changes: 28 additions & 0 deletions mapper/cchain/amount.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion mapper/block.go → mapper/cchain/block.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mapper
package cchain

import (
corethTypes "github.com/ava-labs/coreth/core/types"
Expand Down
13 changes: 7 additions & 6 deletions mapper/transaction.go → mapper/cchain/transaction.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mapper
package cchain

import (
"fmt"
Expand All @@ -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 (
Expand Down Expand Up @@ -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
}

Expand All @@ -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:
}
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -335,7 +336,7 @@ func CrossChainTransactions(

if len(exportedOuts) > 0 {
transaction.Metadata = map[string]interface{}{
MetadataExportedOutputs: exportedOuts,
mapper.MetadataExportedOutputs: exportedOuts,
}
}
transactions = append(transactions, transaction)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions mapper/transaction_test.go → mapper/cchain/transaction_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mapper
package cchain

import (
"encoding/hex"
Expand Down Expand Up @@ -162,7 +162,7 @@ func TestERC721Ops(t *testing.T) {
},
Metadata: map[string]interface{}{
ContractAddressMetadata: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
IndexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051",
indexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051",
},
},
{
Expand All @@ -181,7 +181,7 @@ func TestERC721Ops(t *testing.T) {
},
Metadata: map[string]interface{}{
ContractAddressMetadata: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
IndexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051",
indexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051",
},
},
}, erc721Ops(log, 1))
Expand Down Expand Up @@ -210,7 +210,7 @@ func TestERC721Ops(t *testing.T) {
},
Metadata: map[string]interface{}{
ContractAddressMetadata: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
IndexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051",
indexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051",
},
},
}, erc721Ops(log, 1))
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestERC721Ops(t *testing.T) {
},
Metadata: map[string]interface{}{
ContractAddressMetadata: "0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7",
IndexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051",
indexTransferredMetadata: "0x0000000000000000000000000000000000000000000000000000000000000051",
},
},
}, erc721Ops(log, 1))
Expand Down
21 changes: 21 additions & 0 deletions mapper/cchain/types.go
Original file line number Diff line number Diff line change
@@ -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(),
},
}
}
4 changes: 2 additions & 2 deletions mapper/types_test.go → mapper/cchain/types_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package mapper
package cchain

import (
"testing"
Expand All @@ -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))
}
9 changes: 9 additions & 0 deletions mapper/cchainatomictx/helper.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
}
5 changes: 2 additions & 3 deletions mapper/cchainatomictx/tx_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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++
}
Expand All @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions mapper/pchain/tx_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 3 additions & 2 deletions mapper/pchain/tx_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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]
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions mapper/pchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const (
SubAccountTypeLockedStakeable = "locked_stakeable"
SubAccountTypeLockedNotStakeable = "locked_not_stakeable"
SubAccountTypeStaked = "staked"

metadataImportedInputs = "imported_inputs"
)

var CallMethods = []string{}
Expand Down
25 changes: 9 additions & 16 deletions mapper/types.go
Original file line number Diff line number Diff line change
@@ -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,
}
}
3 changes: 1 addition & 2 deletions service/backend/cchainatomictx/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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:
Expand Down
Loading