Skip to content

Commit

Permalink
imp(bank): Add supplyOf query (#2041)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vvaradinov committed Nov 14, 2023
1 parent 273c00b commit 17c0a46
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
- (osmosis-outpost) [#2025](https://github.com/evmos/evmos/pull/2025) Use a struct to wrap parsed parameters from Solidity.
- (erc20) [#2037](https://github.com/evmos/evmos/pull/2037) Add IsTransactions and RequiredGas tests for the ERC20 extension.
- (bank) [#2040](https://github.com/evmos/evmos/pull/2040) Add bank extension unit tests for queries.
- (bank) [#2041](https://github.com/evmos/evmos/pull/2041) Add `supplyOf` query to bank extension.

### Bug Fixes

Expand Down
5 changes: 5 additions & 0 deletions precompiles/bank/IBank.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ interface IBank {
/// 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);
}
19 changes: 19 additions & 0 deletions precompiles/bank/abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,25 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "erc20Address",
"type": "address"
}
],
"name": "supplyOf",
"outputs": [
{
"internalType": "uint256",
"name": "totalSupply",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "totalSupply",
Expand Down
18 changes: 9 additions & 9 deletions precompiles/bank/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
package bank

import (
"bytes"
"embed"
"fmt"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/vm"
cmn "github.com/evmos/evmos/v15/precompiles/common"
Expand All @@ -26,6 +24,9 @@ const (

// GasTotalSupply defines the gas cost for a single ERC-20 totalSupply query
GasTotalSupply uint64 = 100 // TODO: get actual estimated gas cost

// GasSupplyOf defines the gas cost for a single ERC-20 supplyOf query, taken from totalSupply of ERC20
GasSupplyOf uint64 = 2_477
)

var _ vm.PrecompiledContract = &Precompile{}
Expand All @@ -48,12 +49,7 @@ func NewPrecompile(
bankKeeper bankkeeper.Keeper,
erc20Keeper erc20keeper.Keeper,
) (*Precompile, error) {
abiBz, err := f.ReadFile("abi.json")
if err != nil {
return nil, err
}

newAbi, err := abi.JSON(bytes.NewReader(abiBz))
newABI, err := cmn.LoadABI(f, "abi.json")
if err != nil {
return nil, err
}
Expand All @@ -62,7 +58,7 @@ func NewPrecompile(
// during the run execution
return &Precompile{
Precompile: cmn.Precompile{
ABI: newAbi,
ABI: newABI,
KvGasConfig: storetypes.GasConfig{},
TransientKVGasConfig: storetypes.GasConfig{},
},
Expand Down Expand Up @@ -94,6 +90,8 @@ func (p Precompile) RequiredGas(input []byte) uint64 {
return GasBalanceOf
case TotalSupplyMethod:
return GasTotalSupply
case SupplyOfMethod:
return GasSupplyOf
}

return 0
Expand All @@ -116,6 +114,8 @@ func (p Precompile) Run(evm *vm.EVM, contract *vm.Contract, readOnly bool) (bz [
bz, err = p.Balances(ctx, contract, method, args)
case TotalSupplyMethod:
bz, err = p.TotalSupply(ctx, contract, method, args)
case SupplyOfMethod:
bz, err = p.SupplyOf(ctx, contract, method, args)
default:
return nil, fmt.Errorf(cmn.ErrUnknownMethod, method.Name)
}
Expand Down
27 changes: 27 additions & 0 deletions precompiles/bank/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/core/vm"
"github.com/evmos/evmos/v15/x/erc20/types"
)

const (
Expand All @@ -16,6 +17,9 @@ const (
// TotalSupplyMethod defines the ABI method name for the bank TotalSupply
// query.
TotalSupplyMethod = "totalSupply"
// SupplyOfMethod defines the ABI method name for the bank SupplyOf
// query.
SupplyOfMethod = "supplyOf"
)

// Balances returns all the native token balances (address, amount) for a given
Expand Down Expand Up @@ -96,3 +100,26 @@ func (p Precompile) TotalSupply(

return method.Outputs.Pack(totalSupply)
}

// SupplyOf returns the total native supply of a given registered erc20 token.
func (p Precompile) SupplyOf(
ctx sdk.Context,
_ *vm.Contract,
method *abi.Method,
args []interface{},
) ([]byte, error) {
erc20ContractAddress, err := ParseSupplyOfArgs(args)
if err != nil {
return nil, err
}

tokenPairID := p.erc20Keeper.GetERC20Map(ctx, erc20ContractAddress)
tokenPair, found := p.erc20Keeper.GetTokenPair(ctx, tokenPairID)
if !found {
return nil, types.ErrTokenPairNotFound
}

supply := p.bankKeeper.GetSupply(ctx, tokenPair.Denom)

return method.Outputs.Pack(supply.Amount.BigInt())
}
14 changes: 14 additions & 0 deletions precompiles/bank/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@ func ParseBalancesArgs(args []interface{}) (sdk.AccAddress, error) {

return account.Bytes(), nil
}

// ParseSupplyOfArgs parses the call arguments for the bank SupplyOf query.
func ParseSupplyOfArgs(args []interface{}) (common.Address, error) {
if len(args) != 1 {
return common.Address{}, fmt.Errorf(cmn.ErrInvalidNumberOfArgs, 1, len(args))
}

erc20Address, ok := args[0].(common.Address)
if !ok {
return common.Address{}, fmt.Errorf(cmn.ErrInvalidType, "erc20Address", common.Address{}, args[0])
}

return erc20Address, nil
}

0 comments on commit 17c0a46

Please sign in to comment.