8000 chore(bank-prec): Update bank precompile doc to specify decimals used by 0xstepit · Pull Request #2924 · evmos/evmos · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

chore(bank-prec): Update bank precompile doc to specify decimals used #2924

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

Merged
merged 3 commits into from
Oct 8, 2024
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
39 changes: 21 additions & 18 deletions precompiles/bank/IBank.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
pragma solidity >=0.8.18;

/// @dev The IBank contract's address.
address constant IBANK_PRECOMPILE_ADDRESS = 0x0000000000000000000000000000000000000804;
address constant IBANK_PRECOMPILE_ADDRESS = 0x0000000000000000000000000000000000000804;

/// @dev The IBank contract's instance.
IBank constant IBANK_CONTRACT = IBank(IBANK_PRECOMPILE_ADDRESS);

/// @dev Balance specifies the ERC20 contract address and the amount of tokens.
struct Balance {
/// contractAddress defines the ERC20 contract address.
address contractAddress;
/// amount of tokens
uint256 amount;
/// contractAddress defines the ERC20 contract address.
address contractAddress;
/// amount of tokens
uint256 amount;
}

/**
Expand All @@ -21,19 +21,22 @@ struct Balance {
* @dev Interface for querying balances and supply from the Bank module.
*/
interface IBank {
/// @dev Balances defines a method for retrieving all the native token balances
/// for a given account.
/// @param account the address of the account to query balances for
/// @return balances the array of native token balances
function balances(address account) external view returns (Balance[] memory balances);
/// @dev balances defines a method for retrieving all the native token balances
/// for a given account.
/// @param account the address of the account to query balances for.
/// @return balances the array of native token balances.
function balances(
address account
) external view returns (Balance[] memory balances);

/// @dev TotalSupply defines a method for retrieving the total supply of all
/// native tokens.
/// @return totalSupply the supply as an array of native token balances
function totalSupply() external view returns (Balance[] memory totalSupply);
/// @dev totalSupply defines a method for retrieving the total supply of all
/// native tokens.
/// @return totalSupply the supply as an array of native token balances
function totalSupply() external view returns (Balance[] memory totalSupply);


/// @dev supplyOf defines a method for retrieving the total supply of a particular native coin.
/// @return totalSupply the supply as a uint256
function supplyOf(address erc20Address) external view returns (uint256 totalSupply);
/// @dev supplyOf defines a method for retrieving the total supply of a particular native coin.
/// @return totalSupply the supply as a uint256
function supplyOf(
address erc20Address
) external view returns (uint256 totalSupply);
}
14 changes: 8 additions & 6 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line chang 10000 e
@@ -1,5 +1,9 @@
// Copyright Tharsis Labs Ltd.(Evmos)
// SPDX-License-Identifier:ENCL-1.0(https://github.com/evmos/evmos/blob/main/LICENSE)
//
// The bank package contains the implementation of the x/bank module precompile.
// The precompiles returns all bank's information in the original decimals
// representation stored in the module.

package bank

Expand All @@ -17,8 +21,8 @@ import (
)

const (
// GasBalanceOf defines the gas cost for a single ERC-20 balanceOf query
GasBalanceOf = 2_851
// GasBalances defines the gas cost for a single ERC-20 balanceOf query
GasBalances = 2_851

// GasTotalSupply defines the gas cost for a single ERC-20 totalSupply query
GasTotalSupply = 2_477
Expand All @@ -41,7 +45,7 @@ type Precompile struct {
erc20Keeper erc20keeper.Keeper
}

// NewPrecompile creates a new bank Precompile instance as a
// NewPrecompile creates a new bank Precompile instance implementing the
// PrecompiledContract interface.
func NewPrecompile(
bankKeeper bankkeeper.Keeper,
Expand Down Expand Up @@ -85,11 +89,9 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
return 0
}

// NOTE: Charge the amount of gas required for a single ERC-20
// balanceOf or totalSupply query
switch method.Name {
case BalancesMethod:
return GasBalanceOf
return GasBalances
case TotalSupplyMethod:
return GasTotalSupply
case SupplyOfMethod:
Expand Down
4 changes: 2 additions & 2 deletions precompiles/bank/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ var _ = Describe("Bank Extension -", func() {
err = is.precompile.UnpackIntoInterface(&balances, bank.BalancesMethod, ethRes.Ret)
Expect(err).ToNot(HaveOccurred(), "failed to unpack balances")

gasUsed := Max(bank.GasBalanceOf, len(balances)*bank.GasBalanceOf)
gasUsed := Max(bank.GasBalances, len(balances)*bank.GasBalances)
// Here increasing the GasBalanceOf will increase the use of gas so they will never be equal
Expect(gasUsed).To(BeNumerically("<=", ethRes.GasUsed))
})
Expand Down Expand Up @@ -362,7 +362,7 @@ var _ = Describe("Bank Extension -", func() {
err = is.precompile.UnpackIntoInterface(&balances, bank.BalancesMethod, ethRes.Ret)
Expect(err).ToNot(HaveOccurred(), "failed to unpack balances")

gasUsed := Max(bank.GasBalanceOf, len(balances)*bank.GasBalanceOf)
gasUsed := Max(bank.GasBalances, len(balances)*bank.GasBalances)
// Here increasing the GasBalanceOf will increase the use of gas so they will never be equal
Expect(gasUsed).To(BeNumerically("<=", ethRes.GasUsed))
})
Expand Down
23 changes: 16 additions & 7 deletions precompiles/bank/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package bank

import (
"fmt"
"math/big"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -23,8 +24,10 @@ const (
SupplyOfMethod = "supplyOf"
)

// Balances returns all the native token balances (address, amount) for a given
// account. This method charges the account the corresponding value of an ERC-20
// Balances returns given account's balances of all tokens registered in the x/bank module
// and the corresponding ERC20 address (address, amount). The amount returned for each token
// has the original decimals precision stored in the x/bank.
// This method charges the account the corresponding value of an ERC-20
// balanceOf call for each token returned.
func (p Precompile) Balances(
ctx sdk.Context,
Expand All @@ -34,7 +37,7 @@ func (p Precompile) Balances(
) ([]byte, error) {
account, err := ParseBalancesArgs(args)
if err != nil {
return nil, err
return nil, fmt.Errorf("error calling account balances in bank precompile: %s", err)
}

i := 0
Expand All @@ -46,7 +49,7 @@ func (p Precompile) Balances(
// NOTE: we already charged for a single balanceOf request so we don't
// need to charge on the first iteration
if i > 0 {
ctx.GasMeter().ConsumeGas(GasBalanceOf, "ERC-20 extension balances method")
ctx.GasMeter().ConsumeGas(GasBalances, "ERC-20 extension balances method")
}

contractAddress, err := p.erc20Keeper.GetCoinAddress(ctx, coin.Denom)
Expand All @@ -65,7 +68,9 @@ func (p Precompile) Balances(
return method.Outputs.Pack(balances)
}

// TotalSupply returns the total supply of all the native tokens.
// TotalSupply returns the total supply of all tokens registered in the x/bank
// module. The amount returned for each token has the original
// decimals precision stored in the x/bank.
// This method charges the account the corresponding value of a ERC-20 totalSupply
// call for each token returned.
func (p Precompile) TotalSupply(
Expand Down Expand Up @@ -102,7 +107,11 @@ func (p Precompile) TotalSupply(
return method.Outputs.Pack(totalSupply)
}

// SupplyOf returns the total native supply of a given registered erc20 token.
// SupplyOf returns the total supply of a given registered erc20 token
// from the x/bank module. If the ERC20 token doesn't have a registered
// TokenPair, the method returns a supply of zero.
// The amount returned with this query has the original decimals precision
// stored in the x/bank.
func (p Precompile) SupplyOf(
ctx sdk.Context,
_ *vm.Contract,
Expand All @@ -111,7 +120,7 @@ func (p Precompile) SupplyOf(
) ([]byte, error) {
erc20ContractAddress, err := ParseSupplyOfArgs(args)
if err != nil {
return nil, err
return nil, fmt.Errorf("error getting the supply in bank precompile: %s", err)
}

tokenPairID := p.erc20Keeper.GetERC20Map(ctx, erc20ContractAddress)
Expand Down
2 changes: 1 addition & 1 deletion precompiles/bank/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
cmn "github.com/evmos/evmos/v20/precompiles/common"
)

// Balance contains the amount for a corresponding ERC-20 contract address
// Balance contains the amount for a corresponding ERC-20 contract address.
type Balance struct {
ContractAddress common.Address
Amount *big.Int
Expand Down
Loading
0