fix unit test

This commit is contained in:
Solovyov1796 2024-05-09 19:35:16 +08:00 committed by 0g-wh
parent a67af32108
commit 35de15eef8
44 changed files with 723 additions and 526 deletions

View File

@ -8,8 +8,8 @@ import (
"net/http/httptest"
"testing"
sdkmath "cosmossdk.io/math"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
abci "github.com/cometbft/cometbft/abci/types"
tmbytes "github.com/cometbft/cometbft/libs/bytes"
@ -62,11 +62,11 @@ func (suite *SimulateRequestTestSuite) TestSimulateRequest() {
bank.MsgSend{
FromAddress: fromAddr,
ToAddress: toAddr,
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e6)),
},
},
Fee: auth.StdFee{
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(5e4))),
Amount: sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(5e4)),
Gas: 1e6,
},
Memo: "test memo",

View File

@ -68,7 +68,7 @@ func TestAppAnteHandler_AuthorizedMempool(t *testing.T) {
chainID,
app.NewFundedGenStateWithSameCoins(
tApp.AppCodec(),
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 1e9)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e9)),
testAddresses,
),
newBep3GenStateMulti(tApp.AppCodec(), deputy),
@ -116,7 +116,7 @@ func TestAppAnteHandler_AuthorizedMempool(t *testing.T) {
banktypes.NewMsgSend(
tc.address,
testAddresses[0],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 1_000_000)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1_000_000)),
),
},
sdk.NewCoins(), // no fee

View File

@ -12,6 +12,7 @@ import (
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/app/ante"
"github.com/0glabs/0g-chain/chaincfg"
)
var _ sdk.AnteHandler = (&MockAnteHandler{}).AnteHandle
@ -45,7 +46,7 @@ func TestAuthenticatedMempoolDecorator_AnteHandle_NotCheckTx(t *testing.T) {
banktypes.NewMsgSend(
testAddresses[0],
testAddresses[1],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100_000_000)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100_000_000)),
),
},
sdk.NewCoins(), // no fee
@ -80,12 +81,12 @@ func TestAuthenticatedMempoolDecorator_AnteHandle_Pass(t *testing.T) {
banktypes.NewMsgSend(
testAddresses[0],
testAddresses[1],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100)),
),
banktypes.NewMsgSend(
testAddresses[2],
testAddresses[1],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100)),
),
},
sdk.NewCoins(), // no fee
@ -121,7 +122,7 @@ func TestAuthenticatedMempoolDecorator_AnteHandle_Reject(t *testing.T) {
banktypes.NewMsgSend(
testAddresses[0],
testAddresses[1],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100)),
),
},
sdk.NewCoins(), // no fee

View File

@ -16,6 +16,7 @@ import (
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/app/ante"
"github.com/0glabs/0g-chain/chaincfg"
)
func newMsgGrant(granter sdk.AccAddress, grantee sdk.AccAddress, a authz.Authorization, expiration time.Time) *authz.MsgGrant {
@ -58,7 +59,7 @@ func TestAuthzLimiterDecorator(t *testing.T) {
banktypes.NewMsgSend(
testAddresses[0],
testAddresses[1],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100e6)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100e6)),
),
},
checkTx: false,
@ -128,7 +129,7 @@ func TestAuthzLimiterDecorator(t *testing.T) {
[]sdk.Msg{banktypes.NewMsgSend(
testAddresses[0],
testAddresses[3],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100e6)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100e6)),
)}),
},
checkTx: false,
@ -161,7 +162,7 @@ func TestAuthzLimiterDecorator(t *testing.T) {
banktypes.NewMsgSend(
testAddresses[0],
testAddresses[3],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100e6)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100e6)),
),
&evmtypes.MsgEthereumTx{},
},

View File

@ -34,6 +34,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
evmutilkeeper "github.com/0glabs/0g-chain/x/evmutil/keeper"
evmutiltestutil "github.com/0glabs/0g-chain/x/evmutil/testutil"
evmutiltypes "github.com/0glabs/0g-chain/x/evmutil/types"
@ -156,7 +157,7 @@ func (suite *EIP712TestSuite) SetupTest() {
// Genesis states
evmGs := evmtypes.NewGenesisState(
evmtypes.NewParams(
"neuron", // evmDenom
chaincfg.BaseDenom, // evmDenom
false, // allowedUnprotectedTxs
true, // enableCreate
true, // enableCall
@ -222,10 +223,10 @@ func (suite *EIP712TestSuite) SetupTest() {
pricefeedtypes.ModuleName: cdc.MustMarshalJSON(&pricefeedGenState),
}
// funds our test accounts with some ua0gi
// funds our test accounts with some auxiliary denom
coinsGenState := app.NewFundedGenStateWithSameCoins(
tApp.AppCodec(),
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 1e9)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e9)),
[]sdk.AccAddress{suite.testAddr, suite.testAddr2},
)
@ -312,17 +313,17 @@ func (suite *EIP712TestSuite) SetupTest() {
params := evmKeeper.GetParams(suite.ctx)
params.EIP712AllowedMsgs = []evmtypes.EIP712AllowedMsg{
{
MsgTypeUrl: "/0g-chain.evmutil.v1beta1.MsgConvertERC20ToCoin",
MsgTypeUrl: "/zgc.evmutil.v1beta1.MsgConvertERC20ToCoin",
MsgValueTypeName: "MsgValueEVMConvertERC20ToCoin",
ValueTypes: []evmtypes.EIP712MsgAttrType{
{Name: "initiator", Type: "string"},
{Name: "receiver", Type: "string"},
{Name: "0gchain_erc20_address", Type: "string"},
{Name: "zgchain_erc20_address", Type: "string"},
{Name: "amount", Type: "string"},
},
},
{
MsgTypeUrl: "/0g-chain.evmutil.v1beta1.MsgConvertCoinToERC20",
MsgTypeUrl: "/zgc.evmutil.v1beta1.MsgConvertCoinToERC20",
MsgValueTypeName: "MsgValueEVMConvertCoinToERC20",
ValueTypes: []evmtypes.EIP712MsgAttrType{
{Name: "initiator", Type: "string"},
@ -375,7 +376,7 @@ func (suite *EIP712TestSuite) deployUSDCERC20(app app.TestApp, ctx sdk.Context)
suite.tApp.FundModuleAccount(
suite.ctx,
evmutiltypes.ModuleName,
sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(0))),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(0)),
)
contractAddr, err := suite.evmutilKeeper.DeployTestMintableERC20Contract(suite.ctx, "USDC", "USDC", uint8(18))
@ -475,7 +476,7 @@ func (suite *EIP712TestSuite) TestEIP712Tx() {
errMsg: "insufficient funds",
updateTx: func(txBuilder client.TxBuilder, msgs []sdk.Msg) client.TxBuilder {
bk := suite.tApp.GetBankKeeper()
gasCoins := bk.GetBalance(suite.ctx, suite.testAddr, "ua0gi")
gasCoins := bk.GetBalance(suite.ctx, suite.testAddr, chaincfg.AuxiliaryDenom)
suite.tApp.GetBankKeeper().SendCoins(suite.ctx, suite.testAddr, suite.testAddr2, sdk.NewCoins(gasCoins))
return txBuilder
},
@ -487,7 +488,7 @@ func (suite *EIP712TestSuite) TestEIP712Tx() {
failCheckTx: true,
errMsg: "invalid chain-id",
updateTx: func(txBuilder client.TxBuilder, msgs []sdk.Msg) client.TxBuilder {
gasAmt := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(20)))
gasAmt := sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(20))
return suite.createTestEIP712CosmosTxBuilder(
suite.testAddr, suite.testPrivKey, "kavatest_12-1", uint64(sims.DefaultGenTxGas*10), gasAmt, msgs,
)
@ -500,7 +501,7 @@ func (suite *EIP712TestSuite) TestEIP712Tx() {
failCheckTx: true,
errMsg: "invalid pubkey",
updateTx: func(txBuilder client.TxBuilder, msgs []sdk.Msg) client.TxBuilder {
gasAmt := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(20)))
gasAmt := sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(20))
return suite.createTestEIP712CosmosTxBuilder(
suite.testAddr2, suite.testPrivKey2, ChainID, uint64(sims.DefaultGenTxGas*10), gasAmt, msgs,
)
@ -528,7 +529,7 @@ func (suite *EIP712TestSuite) TestEIP712Tx() {
msgs = tc.updateMsgs(msgs)
}
gasAmt := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(20)))
gasAmt := sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(20))
txBuilder := suite.createTestEIP712CosmosTxBuilder(
suite.testAddr, suite.testPrivKey, ChainID, uint64(sims.DefaultGenTxGas*10), gasAmt, msgs,
)
@ -602,7 +603,7 @@ func (suite *EIP712TestSuite) TestEIP712Tx_DepositAndWithdraw() {
}
// deliver deposit msg
gasAmt := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(20)))
gasAmt := sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(20))
txBuilder := suite.createTestEIP712CosmosTxBuilder(
suite.testAddr, suite.testPrivKey, ChainID, uint64(sims.DefaultGenTxGas*10), gasAmt, depositMsgs,
)

View File

@ -13,6 +13,7 @@ import (
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/app/ante"
"github.com/0glabs/0g-chain/chaincfg"
)
func mustParseDecCoins(value string) sdk.DecCoins {
@ -30,7 +31,7 @@ func TestEvmMinGasFilter(t *testing.T) {
ctx := tApp.NewContext(true, tmproto.Header{Height: 1, Time: tmtime.Now()})
tApp.GetEvmKeeper().SetParams(ctx, evmtypes.Params{
EvmDenom: "neuron",
EvmDenom: chaincfg.BaseDenom,
})
testCases := []struct {

View File

@ -14,6 +14,7 @@ import (
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/app/ante"
"github.com/0glabs/0g-chain/chaincfg"
)
func TestVestingMempoolDecorator_MsgCreateVestingAccount_Unauthorized(t *testing.T) {
@ -33,7 +34,7 @@ func TestVestingMempoolDecorator_MsgCreateVestingAccount_Unauthorized(t *testing
"MsgCreateVestingAccount",
vesting.NewMsgCreateVestingAccount(
testAddresses[0], testAddresses[1],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100)),
time.Date(1998, 1, 1, 0, 0, 0, 0, time.UTC).Unix(),
false,
),
@ -44,7 +45,7 @@ func TestVestingMempoolDecorator_MsgCreateVestingAccount_Unauthorized(t *testing
"MsgCreateVestingAccount",
vesting.NewMsgCreatePermanentLockedAccount(
testAddresses[0], testAddresses[1],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100)),
),
true,
"MsgTypeURL /cosmos.vesting.v1beta1.MsgCreatePermanentLockedAccount not supported",
@ -63,7 +64,7 @@ func TestVestingMempoolDecorator_MsgCreateVestingAccount_Unauthorized(t *testing
"other messages not affected",
banktypes.NewMsgSend(
testAddresses[0], testAddresses[1],
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 100)),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100)),
),
false,
"",

View File

@ -153,7 +153,7 @@ func GenesisStateWithSingleValidator(
balances := []banktypes.Balance{
{
Address: acc.GetAddress().String(),
Coins: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(100000000000000))),
Coins: sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(100000000000000)),
},
}
@ -216,7 +216,7 @@ func genesisStateWithValSet(
}
// set validators and delegations
currentStakingGenesis := stakingtypes.GetGenesisStateFromAppState(app.appCodec, genesisState)
currentStakingGenesis.Params.BondDenom = "ua0gi"
currentStakingGenesis.Params.BondDenom = chaincfg.AuxiliaryDenom // TODO:
stakingGenesis := stakingtypes.NewGenesisState(
currentStakingGenesis.Params,
@ -236,13 +236,13 @@ func genesisStateWithValSet(
for range delegations {
// add delegated tokens to total supply
totalSupply = totalSupply.Add(sdk.NewCoin("ua0gi", bondAmt))
totalSupply = totalSupply.Add(chaincfg.MakeCoinForAuxiliaryDenom(bondAmt))
}
// add bonded amount to bonded pool module account
balances = append(balances, banktypes.Balance{
Address: authtypes.NewModuleAddress(stakingtypes.BondedPoolName).String(),
Coins: sdk.Coins{sdk.NewCoin("ua0gi", bondAmt)},
Coins: sdk.Coins{chaincfg.MakeCoinForAuxiliaryDenom(bondAmt)},
})
bankGenesis := banktypes.NewGenesisState(

57
chaincfg/coin_helper.go Normal file
View File

@ -0,0 +1,57 @@
package chaincfg
import (
"fmt"
"math/big"
"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/shopspring/decimal"
)
func toBigInt(amount any) *big.Int {
if amount == nil {
return big.NewInt(0)
}
var val *big.Int
switch amount.(type) {
case int:
val = big.NewInt(int64(amount.(int)))
case int32:
val = big.NewInt(int64(amount.(int32)))
case int64:
val = big.NewInt(amount.(int64))
case string:
var ok bool
val, ok = new(big.Int).SetString(amount.(string), 0)
if !ok {
panic(fmt.Sprintf("invalid amount string: %s", amount.(string)))
}
case math.Int:
val = amount.(math.Int).BigInt()
case *big.Int:
val = amount.(*big.Int)
case float64:
val = decimal.NewFromFloat(amount.(float64)).BigInt()
default:
panic(fmt.Sprintf("invalid amount type: %T", amount))
}
return val
}
func MakeCoinForStandardDenom(amount any) sdk.Coin {
return makeCoin(StandardDenom, toBigInt(amount))
}
func MakeCoinForAuxiliaryDenom(amount any) sdk.Coin {
return makeCoin(AuxiliaryDenom, toBigInt(amount))
}
func MakeCoinForBaseDenom(amount any) sdk.Coin {
return makeCoin(BaseDenom, toBigInt(amount))
}
func makeCoin(denom string, amount *big.Int) sdk.Coin {
return sdk.NewCoin(denom, math.NewIntFromBigInt(amount))
}

37
chaincfg/denoms.go Normal file
View File

@ -0,0 +1,37 @@
package chaincfg
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
StandardDenom = "a0gi"
AuxiliaryDenom = "ua0gi"
BaseDenom = "neuron"
BondDenom = BaseDenom
AuxiliaryDenomUnit = 6
BaseDenomUnit = 18
AuxiliaryDenomConversionMultiplier = 1e12
BaseDenomConversionMultiplier = 1e18
)
// RegisterDenoms registers the base and auxiliary denominations to the SDK.
func RegisterDenoms() {
if err := sdk.RegisterDenom(StandardDenom, sdk.OneDec()); err != nil {
panic(err)
}
if err := sdk.RegisterDenom(AuxiliaryDenom, sdk.NewDecWithPrec(1, AuxiliaryDenomUnit)); err != nil {
panic(err)
}
if err := sdk.RegisterDenom(BaseDenom, sdk.NewDecWithPrec(1, BaseDenomUnit)); err != nil {
panic(err)
}
}

80
chaincfg/denoms_test.go Normal file
View File

@ -0,0 +1,80 @@
package chaincfg
import (
"testing"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/assert"
)
func TestRegisterDenoms(t *testing.T) {
RegisterDenoms()
tests := []struct {
name string
from sdk.Coin
targetDenom string
expCoin sdk.Coin
expErr error
}{
{
"standard to auxiliary",
MakeCoinForStandardDenom(99),
AuxiliaryDenom,
MakeCoinForAuxiliaryDenom(99 * (BaseDenomConversionMultiplier / AuxiliaryDenomConversionMultiplier)),
nil,
},
{
"auxiliary to standard",
MakeCoinForAuxiliaryDenom(5e7),
StandardDenom,
MakeCoinForStandardDenom(50),
nil,
},
{
"standard to base",
MakeCoinForStandardDenom(22),
BaseDenom,
MakeCoinForBaseDenom(22 * BaseDenomConversionMultiplier),
nil,
},
{
"base to standard",
MakeCoinForBaseDenom("97000000000000000000"),
StandardDenom,
MakeCoinForStandardDenom(97),
nil,
},
{
"auxiliary to base",
MakeCoinForAuxiliaryDenom(33),
BaseDenom,
MakeCoinForBaseDenom(33 * AuxiliaryDenomConversionMultiplier),
nil,
},
{
"base to auxiliary",
MakeCoinForBaseDenom("770000000000000"),
AuxiliaryDenom,
MakeCoinForAuxiliaryDenom(770000000000000 / AuxiliaryDenomConversionMultiplier),
nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ret, err := sdk.ConvertCoin(tt.from, tt.targetDenom)
if tt.expErr != nil {
if err == nil {
t.Errorf("expErr is not nil, but got nil")
return
}
} else {
if err != nil {
t.Errorf("expErr is nil, but got %v", err)
return
}
}
assert.Equal(t, tt.expCoin, ret)
})
}
}

View File

@ -73,7 +73,7 @@ func TestKvCLIKeysAddRecover(t *testing.T) {
f.Cleanup()
}
func TestKavaCLIKeysAddRecoverHDPath(t *testing.T) {
func TestZgChainCLIKeysAddRecoverHDPath(t *testing.T) {
t.Parallel()
f := InitFixtures(t)

View File

@ -81,7 +81,7 @@ func NewRootCmd() *cobra.Command {
return err
}
customAppTemplate, customAppConfig := servercfg.AppConfig("ua0gi")
customAppTemplate, customAppConfig := servercfg.AppConfig(chaincfg.AuxiliaryDenom)
return server.InterceptConfigsPreRunHandler(
cmd,

3
go.mod
View File

@ -164,6 +164,7 @@ require (
github.com/klauspost/compress v1.15.15 // indirect
github.com/lib/pq v1.10.7 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/lukehoban/go-outline v0.0.0-20161011150102-e78556874252 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
@ -196,6 +197,8 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sasha-s/go-deadlock v0.3.1 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/shopspring/decimal v1.4.0 // indirect
github.com/spf13/afero v1.9.3 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect

4
go.sum
View File

@ -944,6 +944,8 @@ github.com/lightstep/lightstep-tracer-go v0.18.1/go.mod h1:jlF1pusYV4pidLvZ+XD0U
github.com/linxGnu/grocksdb v1.8.6 h1:O7I6SIGPrypf3f/gmrrLUBQDKfO8uOoYdWf4gLS06tc=
github.com/linxGnu/grocksdb v1.8.6/go.mod h1:xZCIb5Muw+nhbDK4Y5UJuOrin5MceOuiXkVUR7vp4WY=
github.com/lucasjones/reggen v0.0.0-20180717132126-cdb49ff09d77/go.mod h1:5ELEyG+X8f+meRWHuqUOewBOhvHkl7M76pdGEansxW4=
github.com/lukehoban/go-outline v0.0.0-20161011150102-e78556874252 h1:D2VNityLQ1srKF+MSllSGQ4NwMci20llMkvVAmU2aCk=
github.com/lukehoban/go-outline v0.0.0-20161011150102-e78556874252/go.mod h1:O9bIJ6BRFBmP3AKTW8cqESVbauSmifSrRB/n9zq6x9Q=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
@ -1179,6 +1181,8 @@ github.com/segmentio/kafka-go v0.2.0/go.mod h1:X6itGqS9L4jDletMsxZ7Dz+JFWxM6JHfP
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible h1:Bn1aCHHRnjv4Bl16T8rcaFjYSrGrIZvpiGO6P3Q4GpU=
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=

View File

@ -5,6 +5,7 @@ import (
"time"
sdkmath "cosmossdk.io/math"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
@ -41,7 +42,7 @@ func TestResetPeriodVestingAccount_NoVestingPeriods(t *testing.T) {
}
func TestResetPeriodVestingAccount_SingleVestingPeriod_Vested(t *testing.T) {
balance := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6)))
balance := sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6)))
vestingStartTime := time.Now().Add(-30 * 24 * time.Hour) // 30 days in past
periods := vestingtypes.Periods{
@ -64,7 +65,7 @@ func TestResetPeriodVestingAccount_SingleVestingPeriod_Vested(t *testing.T) {
}
func TestResetPeriodVestingAccount_SingleVestingPeriod_Vesting(t *testing.T) {
balance := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6)))
balance := sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6)))
vestingStartTime := time.Now().Add(-30 * 24 * time.Hour) // 30 days in past
periods := vestingtypes.Periods{
@ -97,7 +98,7 @@ func TestResetPeriodVestingAccount_SingleVestingPeriod_Vesting(t *testing.T) {
}
func TestResetPeriodVestingAccount_SingleVestingPeriod_ExactStartTime(t *testing.T) {
balance := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6)))
balance := sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6)))
vestingStartTime := time.Now().Add(-30 * 24 * time.Hour) // 30 days in past
periods := vestingtypes.Periods{
@ -125,25 +126,25 @@ func TestResetPeriodVestingAccount_SingleVestingPeriod_ExactStartTime(t *testing
}
func TestResetPeriodVestingAccount_MultiplePeriods(t *testing.T) {
balance := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(4e6)))
balance := sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(4e6)))
vestingStartTime := time.Now().Add(-30 * 24 * time.Hour) // 30 days in past
periods := vestingtypes.Periods{
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // -15 days - vested
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // 0 days - exact on the start time
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // +15 days - vesting
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // +30 days - vesting
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
}
@ -159,36 +160,36 @@ func TestResetPeriodVestingAccount_MultiplePeriods(t *testing.T) {
expectedPeriods := []vestingtypes.Period{
{
Length: 15 * 24 * 60 * 60, // 15 days
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
{
Length: 15 * 24 * 60 * 60, // 15 days
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
}
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(2e6))), vacc.OriginalVesting, "expected original vesting to be updated")
assert.Equal(t, sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(2e6))), vacc.OriginalVesting, "expected original vesting to be updated")
assert.Equal(t, newVestingStartTime.Unix(), vacc.StartTime, "expected vesting start time to be updated")
assert.Equal(t, expectedEndtime, vacc.EndTime, "expected vesting end time end at last period")
assert.Equal(t, expectedPeriods, vacc.VestingPeriods, "expected vesting periods to be updated")
}
func TestResetPeriodVestingAccount_DelegatedVesting_GreaterThanVesting(t *testing.T) {
balance := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(3e6)))
balance := sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(3e6)))
vestingStartTime := time.Now().Add(-30 * 24 * time.Hour) // 30 days in past
periods := vestingtypes.Periods{
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // -15 days - vested
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // 0 days - exact on the start time
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // +15 days - vesting
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
}
@ -198,35 +199,35 @@ func TestResetPeriodVestingAccount_DelegatedVesting_GreaterThanVesting(t *testin
newVestingStartTime := vestingStartTime.Add(30 * 24 * time.Hour)
ResetPeriodicVestingAccount(vacc, newVestingStartTime)
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(2e6))), vacc.DelegatedFree, "expected delegated free to be updated")
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))), vacc.DelegatedVesting, "expected delegated vesting to be updated")
assert.Equal(t, sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(2e6))), vacc.DelegatedFree, "expected delegated free to be updated")
assert.Equal(t, sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))), vacc.DelegatedVesting, "expected delegated vesting to be updated")
}
func TestResetPeriodVestingAccount_DelegatedVesting_LessThanVested(t *testing.T) {
balance := sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(3e6)))
balance := sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(3e6)))
vestingStartTime := time.Now().Add(-30 * 24 * time.Hour) // 30 days in past
periods := vestingtypes.Periods{
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // -15 days - vested
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // 0 days - exact on the start time
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
vestingtypes.Period{
Length: 15 * 24 * 60 * 60, // +15 days - vesting
Amount: sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))),
Amount: sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))),
},
}
vacc := createVestingAccount(balance, vestingStartTime, periods)
vacc.TrackDelegation(vestingStartTime, balance, sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))))
vacc.TrackDelegation(vestingStartTime, balance, sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))))
newVestingStartTime := vestingStartTime.Add(30 * 24 * time.Hour)
ResetPeriodicVestingAccount(vacc, newVestingStartTime)
assert.Equal(t, sdk.Coins(nil), vacc.DelegatedFree, "expected delegrated free to be unmodified")
assert.Equal(t, sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(1e6))), vacc.DelegatedVesting, "expected delegated vesting to be unmodified")
assert.Equal(t, sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(1e6))), vacc.DelegatedVesting, "expected delegated vesting to be unmodified")
}

View File

@ -13,7 +13,7 @@ message ConversionPair {
option (gogoproto.goproto_getters) = false;
// ERC20 address of the token on the 0gChain EVM
bytes zgChain_erc20_address = 1 [
bytes zgchain_erc20_address = 1 [
(gogoproto.customname) = "ZgChainERC20Address",
(gogoproto.casttype) = "HexBytes"
];

View File

@ -44,7 +44,7 @@ message MsgConvertERC20ToCoin {
// 0gChain bech32 address that will receive the converted sdk.Coin.
string receiver = 2 [(cosmos_proto.scalar) = "cosmos.AddressString"];
// EVM 0x hex address of the ERC20 contract.
string zgChain_erc20_address = 3 [(gogoproto.customname) = "ZgChainERC20Address"];
string zgchain_erc20_address = 3 [(gogoproto.customname) = "ZgChainERC20Address"];
// ERC20 token amount to convert.
string amount = 4 [
(cosmos_proto.scalar) = "cosmos.Int",

View File

@ -12,6 +12,7 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/tests/e2e/testutil"
"github.com/0glabs/0g-chain/tests/util"
evmutiltypes "github.com/0glabs/0g-chain/x/evmutil/types"
@ -63,7 +64,7 @@ func (suite *IntegrationTestSuite) setupAccountWithCosmosCoinERC20Balance(
tx := util.ZgChainMsgRequest{
Msgs: []sdk.Msg{&msg},
GasLimit: 4e5,
FeeAmount: sdk.NewCoins(a0gi(big.NewInt(400))),
FeeAmount: sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(400)),
Data: "converting sdk coin to erc20",
}
res := user.SignAndBroadcastZgChainTx(tx)
@ -102,7 +103,7 @@ func (suite *IntegrationTestSuite) TestConvertCosmosCoinsToFromERC20() {
tx := util.ZgChainMsgRequest{
Msgs: []sdk.Msg{&convertToErc20Msg},
GasLimit: 2e6,
FeeAmount: sdk.NewCoins(a0gi(big.NewInt(2000))),
FeeAmount: sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(2000)),
Data: "converting sdk coin to erc20",
}
res := user.SignAndBroadcastZgChainTx(tx)
@ -144,7 +145,7 @@ func (suite *IntegrationTestSuite) TestConvertCosmosCoinsToFromERC20() {
tx = util.ZgChainMsgRequest{
Msgs: []sdk.Msg{&convertFromErc20Msg},
GasLimit: 2e5,
FeeAmount: sdk.NewCoins(a0gi(big.NewInt(200))),
FeeAmount: sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(200)),
Data: "converting erc20 to cosmos coin",
}
res = user.SignAndBroadcastZgChainTx(tx)
@ -183,7 +184,7 @@ func (suite *IntegrationTestSuite) TestEIP712ConvertCosmosCoinsToFromERC20() {
user,
suite.ZgChain,
2e6,
sdk.NewCoins(a0gi(big.NewInt(1e4))),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e4)),
[]sdk.Msg{&convertToErc20Msg},
"this is a memo",
).GetTx()
@ -237,7 +238,7 @@ func (suite *IntegrationTestSuite) TestEIP712ConvertCosmosCoinsToFromERC20() {
user,
suite.ZgChain,
2e5,
sdk.NewCoins(a0gi(big.NewInt(200))),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(200)),
[]sdk.Msg{&convertFromErc20Msg},
"",
).GetTx()
@ -331,7 +332,7 @@ func (suite *IntegrationTestSuite) TestConvertCosmosCoins_ERC20Magic() {
"cosmo-coin-converter-complex-alice", initialAliceAmount,
)
gasMoney := sdk.NewCoins(a0gi(big.NewInt(1e5)))
gasMoney := sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e5))
bob := suite.ZgChain.NewFundedAccount("cosmo-coin-converter-complex-bob", gasMoney)
amount := big.NewInt(1e3) // test assumes this is half of alice's balance.
@ -412,7 +413,7 @@ func (suite *IntegrationTestSuite) TestConvertCosmosCoins_ERC20Magic() {
convertTx := util.ZgChainMsgRequest{
Msgs: []sdk.Msg{&convertMsg},
GasLimit: 2e5,
FeeAmount: sdk.NewCoins(a0gi(big.NewInt(200))),
FeeAmount: sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(200)),
Data: "bob converts his new erc20 to an sdk.Coin",
}
convertRes := bob.SignAndBroadcastZgChainTx(convertTx)

View File

@ -11,7 +11,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/tests/e2e/contracts/greeter"
"github.com/0glabs/0g-chain/tests/util"
)
@ -20,7 +20,7 @@ func (suite *IntegrationTestSuite) TestEthCallToGreeterContract() {
// this test manipulates state of the Greeter contract which means other tests shouldn't use it.
// setup funded account to interact with contract
user := suite.ZgChain.NewFundedAccount("greeter-contract-user", sdk.NewCoins(a0gi(big.NewInt(1e6))))
user := suite.ZgChain.NewFundedAccount("greeter-contract-user", sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e6)))
greeterAddr := suite.ZgChain.ContractAddrs["greeter"]
contract, err := greeter.NewGreeter(greeterAddr, suite.ZgChain.EvmClient)
@ -63,12 +63,12 @@ func (suite *IntegrationTestSuite) TestEthCallToErc20() {
func (suite *IntegrationTestSuite) TestEip712BasicMessageAuthorization() {
// create new funded account
sender := suite.ZgChain.NewFundedAccount("eip712-msgSend", sdk.NewCoins(a0gi(big.NewInt(2e4))))
sender := suite.ZgChain.NewFundedAccount("eip712-msgSend", sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(2e4)))
receiver := app.RandomAddress()
// setup message for sending some a0gi to random receiver
// setup message for sending some auxiliary denom to random receiver
msgs := []sdk.Msg{
banktypes.NewMsgSend(sender.SdkAddress, receiver, sdk.NewCoins(a0gi(big.NewInt(1e3)))),
banktypes.NewMsgSend(sender.SdkAddress, receiver, sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e3))),
}
// create tx
@ -76,7 +76,7 @@ func (suite *IntegrationTestSuite) TestEip712BasicMessageAuthorization() {
sender,
suite.ZgChain,
1e6,
sdk.NewCoins(a0gi(big.NewInt(1e4))),
sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e4)),
msgs,
"this is a memo",
).GetTx()
@ -95,10 +95,10 @@ func (suite *IntegrationTestSuite) TestEip712BasicMessageAuthorization() {
_, err = util.WaitForSdkTxCommit(suite.ZgChain.Tx, res.TxResponse.TxHash, 6*time.Second)
suite.NoError(err)
// check that the message was processed & the a0gi is transferred.
// check that the message was processed & the auxiliary denom is transferred.
balRes, err := suite.ZgChain.Bank.Balance(context.Background(), &banktypes.QueryBalanceRequest{
Address: receiver.String(),
Denom: "ua0gi",
Denom: chaincfg.AuxiliaryDenom,
})
suite.NoError(err)
suite.Equal(sdk.NewInt(1e3), balRes.Balance.Amount)
@ -113,7 +113,7 @@ func (suite *IntegrationTestSuite) TestEip712BasicMessageAuthorization() {
// sdkDenom := suite.DeployedErc20.CosmosDenom
// // create new funded account
// depositor := suite.ZgChain.NewFundedAccount("eip712-lend-depositor", sdk.NewCoins(a0gi(big.NewInt(1e5)))
// depositor := suite.ZgChain.NewFundedAccount("eip712-lend-depositor", sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e5)))
// // give them erc20 balance to deposit
// fundRes := suite.FundZgChainErc20Balance(depositor.EvmAddress, amount.BigInt())
// suite.NoError(fundRes.Err)
@ -143,7 +143,7 @@ func (suite *IntegrationTestSuite) TestEip712BasicMessageAuthorization() {
// depositor,
// suite.ZgChain,
// 1e6,
// sdk.NewCoins(a0gi(big.NewInt(1e4)),
// sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e4)),
// msgs,
// "doing the USDT Earn workflow! erc20 -> sdk.Coin -> USDX hard deposit",
// ).GetTx()
@ -189,7 +189,7 @@ func (suite *IntegrationTestSuite) TestEip712BasicMessageAuthorization() {
// withdrawAndConvertBack := util.ZgChainMsgRequest{
// Msgs: []sdk.Msg{&withdraw, &convertBack},
// GasLimit: 1e6,
// FeeAmount: sdk.NewCoins(a0gi(big.NewInt(1000)),
// FeeAmount: sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1000)),
// Data: "withdrawing from mint & converting back to erc20",
// }
// lastRes := depositor.SignAndBroadcastZgChainTx(withdrawAndConvertBack)

View File

@ -13,6 +13,7 @@ import (
ethtypes "github.com/ethereum/go-ethereum/core/types"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/tests/util"
)
@ -23,10 +24,10 @@ func (suite *IntegrationTestSuite) TestEthGasPriceReturnsMinFee() {
minGasPrices, err := getMinFeeFromAppToml(util.ZgChainHomePath())
suite.NoError(err)
// evm uses neuron, get neuron min fee
evmMinGas := minGasPrices.AmountOf("neuron").TruncateInt().BigInt()
// evm uses base denom, get base denom min fee
evmMinGas := minGasPrices.AmountOf(chaincfg.BaseDenom).TruncateInt().BigInt()
// returns eth_gasPrice, units in a0gi
// returns eth_gasPrice, units in auxiliary denom
gasPrice, err := suite.ZgChain.EvmClient.SuggestGasPrice(context.Background())
suite.NoError(err)
@ -37,13 +38,13 @@ func (suite *IntegrationTestSuite) TestEvmRespectsMinFee() {
suite.SkipIfKvtoolDisabled()
// setup sender & receiver
sender := suite.ZgChain.NewFundedAccount("evm-min-fee-test-sender", sdk.NewCoins(a0gi(big.NewInt(1e3))))
sender := suite.ZgChain.NewFundedAccount("evm-min-fee-test-sender", sdk.NewCoins(chaincfg.MakeCoinForAuxiliaryDenom(1e3)))
randoReceiver := util.SdkToEvmAddress(app.RandomAddress())
// get min gas price for evm (from app.toml)
minFees, err := getMinFeeFromAppToml(util.ZgChainHomePath())
suite.NoError(err)
minGasPrice := minFees.AmountOf("neuron").TruncateInt()
minGasPrice := minFees.AmountOf(chaincfg.BaseDenom).TruncateInt()
// attempt tx with less than min gas price (min fee - 1)
tooLowGasPrice := minGasPrice.Sub(sdk.OneInt()).BigInt()

View File

@ -19,18 +19,15 @@ import (
emtypes "github.com/evmos/ethermint/types"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/tests/e2e/testutil"
"github.com/0glabs/0g-chain/tests/util"
)
var (
minEvmGasPrice = big.NewInt(1e10) // neuron
minEvmGasPrice = big.NewInt(1e10) // base denom
)
func a0gi(amt *big.Int) sdk.Coin {
return sdk.NewCoin("ua0gi", sdkmath.NewIntFromBigInt(amt))
}
type IntegrationTestSuite struct {
testutil.E2eTestSuite
}
@ -57,7 +54,7 @@ func (suite *IntegrationTestSuite) TestChainID() {
// example test that funds a new account & queries its balance
func (suite *IntegrationTestSuite) TestFundedAccount() {
funds := a0gi(big.NewInt(1e3))
funds := chaincfg.MakeCoinForAuxiliaryDenom(1e3)
acc := suite.ZgChain.NewFundedAccount("example-acc", sdk.NewCoins(funds))
// check that the sdk & evm signers are for the same account
@ -66,21 +63,21 @@ func (suite *IntegrationTestSuite) TestFundedAccount() {
// check balance via SDK query
res, err := suite.ZgChain.Bank.Balance(context.Background(), banktypes.NewQueryBalanceRequest(
acc.SdkAddress, "ua0gi",
acc.SdkAddress, chaincfg.AuxiliaryDenom,
))
suite.NoError(err)
suite.Equal(funds, *res.Balance)
// check balance via EVM query
neuronBal, err := suite.ZgChain.EvmClient.BalanceAt(context.Background(), acc.EvmAddress, nil)
baseDenomBal, err := suite.ZgChain.EvmClient.BalanceAt(context.Background(), acc.EvmAddress, nil)
suite.NoError(err)
suite.Equal(funds.Amount.MulRaw(1e12).BigInt(), neuronBal)
suite.Equal(funds.Amount.MulRaw(1e12).BigInt(), baseDenomBal)
}
// example test that signs & broadcasts an EVM tx
func (suite *IntegrationTestSuite) TestTransferOverEVM() {
// fund an account that can perform the transfer
initialFunds := a0gi(big.NewInt(1e6)) // 1 A0GI
initialFunds := chaincfg.MakeCoinForAuxiliaryDenom(1e6) // 1 (auxiliary denom)
acc := suite.ZgChain.NewFundedAccount("evm-test-transfer", sdk.NewCoins(initialFunds))
// get a rando account to send 0gchain to
@ -92,10 +89,10 @@ func (suite *IntegrationTestSuite) TestTransferOverEVM() {
suite.NoError(err)
suite.Equal(uint64(0), nonce) // sanity check. the account should have no prior txs
// transfer a0gi over EVM
a0giToTransfer := big.NewInt(1e17) // .1 A0GI; neuron has 18 decimals.
// transfer auxiliary denom over EVM
AuxiliaryDenomToTransfer := big.NewInt(1e17) // .1 (auxiliary denom); base denom has 18 decimals.
req := util.EvmTxRequest{
Tx: ethtypes.NewTransaction(nonce, to, a0giToTransfer, 1e5, minEvmGasPrice, nil),
Tx: ethtypes.NewTransaction(nonce, to, AuxiliaryDenomToTransfer, 1e5, minEvmGasPrice, nil),
Data: "any ol' data to track this through the system",
}
res := acc.SignAndBroadcastEvmTx(req)
@ -103,31 +100,31 @@ func (suite *IntegrationTestSuite) TestTransferOverEVM() {
suite.Equal(ethtypes.ReceiptStatusSuccessful, res.Receipt.Status)
// evm txs refund unused gas. so to know the expected balance we need to know how much gas was used.
a0giUsedForGas := sdkmath.NewIntFromBigInt(minEvmGasPrice).
AuxiliaryDenomUsedForGas := sdkmath.NewIntFromBigInt(minEvmGasPrice).
Mul(sdkmath.NewIntFromUint64(res.Receipt.GasUsed)).
QuoRaw(1e12) // convert neuron to a0gi
QuoRaw(1e12) // convert base denom to auxiliary denom
// expect (9 - gas used) A0GI remaining in account.
// expect (9 - gas used) (auxiliary denom) remaining in account.
balance := suite.ZgChain.QuerySdkForBalances(acc.SdkAddress)
suite.Equal(sdkmath.NewInt(9e5).Sub(a0giUsedForGas), balance.AmountOf("ua0gi"))
suite.Equal(sdkmath.NewInt(9e5).Sub(AuxiliaryDenomUsedForGas), balance.AmountOf(chaincfg.AuxiliaryDenom))
}
// TestIbcTransfer transfers A0GI from the primary 0g-chain (suite.ZgChain) to the ibc chain (suite.Ibc).
// TestIbcTransfer transfers (auxiliary denom) from the primary 0g-chain (suite.ZgChain) to the ibc chain (suite.Ibc).
// Note that because the IBC chain also runs 0g-chain's binary, this tests both the sending & receiving.
func (suite *IntegrationTestSuite) TestIbcTransfer() {
suite.SkipIfIbcDisabled()
// ARRANGE
// setup 0g-chain account
funds := a0gi(big.NewInt(1e5)) // .1 A0GI
funds := chaincfg.MakeCoinForAuxiliaryDenom(1e5) // .1 (auxiliary denom)
zgChainAcc := suite.ZgChain.NewFundedAccount("ibc-transfer-0g-side", sdk.NewCoins(funds))
// setup ibc account
ibcAcc := suite.Ibc.NewFundedAccount("ibc-transfer-ibc-side", sdk.NewCoins())
gasLimit := int64(2e5)
fee := a0gi(big.NewInt(200))
fee := chaincfg.MakeCoinForAuxiliaryDenom(200)
fundsToSend := a0gi(big.NewInt(5e4)) // .005 A0GI
fundsToSend := chaincfg.MakeCoinForAuxiliaryDenom(5e4) // .005 (auxiliary denom)
transferMsg := ibctypes.NewMsgTransfer(
testutil.IbcPort,
testutil.IbcChannel,
@ -157,7 +154,7 @@ func (suite *IntegrationTestSuite) TestIbcTransfer() {
// the balance should be deducted from 0g-chain account
suite.Eventually(func() bool {
balance := suite.ZgChain.QuerySdkForBalances(zgChainAcc.SdkAddress)
return balance.AmountOf("ua0gi").Equal(expectedSrcBalance.Amount)
return balance.AmountOf(chaincfg.AuxiliaryDenom).Equal(expectedSrcBalance.Amount)
}, 10*time.Second, 1*time.Second)
// expect the balance to be transferred to the ibc chain!

View File

@ -3,7 +3,6 @@ package e2e_test
import (
"context"
"fmt"
"math/big"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -12,6 +11,7 @@ import (
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/tests/e2e/testutil"
"github.com/0glabs/0g-chain/tests/util"
)
@ -101,13 +101,13 @@ func (suite *IntegrationTestSuite) TestModuleAccountGovTransfers() {
name: "transfer from community to kavadist for incentive rewards",
sender: communityAcc,
receiver: zgChainDistAcc,
amount: a0gi(big.NewInt(100)),
amount: chaincfg.MakeCoinForAuxiliaryDenom(100),
},
{
name: "transfer from kavadist to community",
sender: zgChainDistAcc,
receiver: communityAcc,
amount: a0gi(big.NewInt(50)),
amount: chaincfg.MakeCoinForAuxiliaryDenom(50),
},
}
@ -153,7 +153,7 @@ func (suite *IntegrationTestSuite) submitAndPassProposal(msgs []sdk.Msg) int64 {
suite.NoError(err)
gasLimit := 1e6
fee := sdk.NewCoin("neuron", sdk.NewInt(1e15))
fee := chaincfg.MakeCoinForBaseDenom(1e15)
req := util.ZgChainMsgRequest{
Msgs: []sdk.Msg{proposalMsg},

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/0glabs/0g-chain/chaincfg"
rpchttpclient "github.com/cometbft/cometbft/rpc/client/http"
"github.com/ethereum/go-ethereum/ethclient"
)
@ -73,7 +74,7 @@ var (
EvmRpcUrl: "http://localhost:8545",
ChainId: "0gchainlocalnet_8888-1",
StakingDenom: "ua0gi",
StakingDenom: chaincfg.AuxiliaryDenom,
}
kvtoolIbcChain = ChainDetails{
RpcUrl: "http://localhost:26658",

View File

@ -262,7 +262,7 @@ func (a *SigningAccount) BankSend(to sdk.AccAddress, amount sdk.Coins) util.ZgCh
util.ZgChainMsgRequest{
Msgs: []sdk.Msg{banktypes.NewMsgSend(a.SdkAddress, to, amount)},
GasLimit: 2e5, // 200,000 gas
FeeAmount: sdk.NewCoins(sdk.NewCoin(a.gasDenom, sdkmath.NewInt(200))), // assume min gas price of .001a0gi
FeeAmount: sdk.NewCoins(sdk.NewCoin(a.gasDenom, sdkmath.NewInt(200))), // assume min gas price of .001 auxiliary denom
Data: fmt.Sprintf("sending %s to %s", amount, to),
},
)

View File

@ -106,7 +106,7 @@ func QueryCalcSwapIDCmd(queryRoute string) *cobra.Command {
return &cobra.Command{
Use: "calc-swapid [random-number-hash] [sender] [sender-other-chain]",
Short: "calculate swap ID for the given random number hash, sender, and sender other chain",
Example: "bep3 calc-swapid 0677bd8a303dd981810f34d8e5cc6507f13b391899b84d3c1be6c6045a17d747 kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny bnb1ud3q90r98l3mhd87kswv3h8cgrymzeljct8qn7",
Example: "bep3 calc-swapid 0677bd8a303dd981810f34d8e5cc6507f13b391899b84d3c1be6c6045a17d747 0g1l0xsq2z7gqd7yly0g40y5836g0appumark77ny bnb1ud3q90r98l3mhd87kswv3h8cgrymzeljct8qn7",
Args: cobra.MinimumNArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
@ -220,7 +220,7 @@ func QueryGetAtomicSwapsCmd(queryRoute string) *cobra.Command {
Short: "query atomic swaps with optional filters",
Long: strings.TrimSpace(`Query for all paginated atomic swaps that match optional filters:
Example:
$ kvcli q bep3 swaps --involve=kava1l0xsq2z7gqd7yly0g40y5836g0appumark77ny
$ kvcli q bep3 swaps --involve=0g1l0xsq2z7gqd7yly0g40y5836g0appumark77ny
$ kvcli q bep3 swaps --expiration=280
$ kvcli q bep3 swaps --status=(Open|Completed|Expired)
$ kvcli q bep3 swaps --direction=(Incoming|Outgoing)

View File

@ -12,6 +12,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/bep3"
"github.com/0glabs/0g-chain/x/bep3/keeper"
"github.com/0glabs/0g-chain/x/bep3/types"
@ -35,7 +36,7 @@ func (suite *MsgServerTestSuite) SetupTest() {
// Set up genesis state and initialize
_, addrs := app.GeneratePrivKeyAddressPairs(3)
coins := sdk.NewCoins(c("bnb", 10000000000), c("a0gi", 10000))
coins := sdk.NewCoins(c("bnb", 10000000000), c(chaincfg.AuxiliaryDenom, 10000))
authGS := app.NewFundedGenStateWithSameCoins(tApp.AppCodec(), coins, addrs)
tApp.InitializeFromGenesisStates(authGS, NewBep3GenStateMulti(cdc, addrs[0]))

View File

@ -20,7 +20,7 @@ type GenesisTestSuite struct {
}
func (suite *GenesisTestSuite) SetupTest() {
coin := sdk.NewCoin("a0gi", sdk.OneInt())
coin := chaincfg.MakeCoinForAuxiliaryDenom(1)
suite.swaps = atomicSwaps(10)
supply := types.NewAssetSupply(coin, coin, coin, coin, time.Duration(0))

View File

@ -5,12 +5,13 @@ import (
"time"
sdkmath "cosmossdk.io/math"
"github.com/0glabs/0g-chain/chaincfg"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)
func TestAssetSupplyValidate(t *testing.T) {
coin := sdk.NewCoin("a0gi", sdk.OneInt())
coin := chaincfg.MakeCoinForAuxiliaryDenom(1)
invalidCoin := sdk.Coin{Denom: "Invalid Denom", Amount: sdkmath.NewInt(-1)}
testCases := []struct {
msg string

View File

@ -6,12 +6,12 @@ import (
"github.com/stretchr/testify/suite"
sdkmath "cosmossdk.io/math"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/committee/keeper"
"github.com/0glabs/0g-chain/x/committee/types"
)
@ -61,7 +61,7 @@ func (suite *MsgServerTestSuite) SetupTest() {
[]types.Proposal{},
[]types.Vote{},
)
suite.communityPoolAmt = sdk.NewCoins(sdk.NewCoin("neuron", sdkmath.NewInt(1000000000000000)))
suite.communityPoolAmt = sdk.NewCoins(chaincfg.MakeCoinForBaseDenom(1000000000000000))
suite.app.InitializeFromGenesisStates(
app.GenesisState{types.ModuleName: cdc.MustMarshalJSON(testGenesis)},
// TODO: not used?

View File

@ -4,6 +4,7 @@ import (
fmt "fmt"
"time"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
@ -18,7 +19,7 @@ const (
BaseCommitteeType = "0g/BaseCommittee"
MemberCommitteeType = "0g/MemberCommittee" // Committee is composed of member addresses that vote to enact proposals within their permissions
TokenCommitteeType = "0g/TokenCommittee" // Committee is composed of token holders with voting power determined by total token balance
BondDenom = "neuron"
BondDenom = chaincfg.BondDenom
)
// Marshal needed for protobuf compatibility.

View File

@ -171,10 +171,10 @@ func NewVoteCmd() *cobra.Command {
tokens = val.GetTokens()
}
}
// the denom of token is neuron, need to convert to A0GI
a0gi := tokens.Quo(sdk.NewInt(1_000_000_000_000_000_000))
// the denom of token is base denom, need to convert to A0GI
a0giTokenCnt := tokens.Quo(sdk.NewInt(1_000_000_000_000_000_000))
// 1_000 0AGI token / vote
numBallots := a0gi.Quo(sdk.NewInt(1_000)).Uint64()
numBallots := a0giTokenCnt.Quo(sdk.NewInt(1_000)).Uint64()
ballots := make([]*types.Ballot, numBallots)
for i := range ballots {
ballotID := uint64(i)

View File

@ -21,8 +21,8 @@ var (
const (
// Amino names
registerName = "evmos/council/MsgRegister"
voteName = "evmos/council/MsgVote"
registerName = "0g/council/MsgRegister"
voteName = "0g/council/MsgVote"
)
// NOTE: This is required for the GetSignBytes function

View File

@ -9,64 +9,57 @@ import (
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/evmutil/types"
)
const (
// EvmDenom is the gas denom used by the evm
EvmDenom = "neuron"
// CosmosDenom is the gas denom used by the 0g-chain app
CosmosDenom = "ua0gi"
)
// ConversionMultiplier is the conversion multiplier between neuron and ua0gi
var ConversionMultiplier = sdkmath.NewInt(1_000_000_000_000)
// ConversionMultiplier is the conversion multiplier between base denom and auxiliary denom
var ConversionMultiplier = sdkmath.NewInt(chaincfg.AuxiliaryDenomConversionMultiplier)
var _ evmtypes.BankKeeper = EvmBankKeeper{}
// EvmBankKeeper is a BankKeeper wrapper for the x/evm module to allow the use
// of the 18 decimal neuron coin on the evm.
// x/evm consumes gas and send coins by minting and burning neuron coins in its module
// of the 18 decimal base denom coin on the evm.
// x/evm consumes gas and send coins by minting and burning base denom coins in its module
// account and then sending the funds to the target account.
// This keeper uses both the a0gi coin and a separate neuron balance to manage the
// This keeper uses both the auxiliary denom coin and a separate base denom balance to manage the
// extra percision needed by the evm.
type EvmBankKeeper struct {
neuronKeeper Keeper
baseDenomKeeper Keeper
bk types.BankKeeper
ak types.AccountKeeper
}
func NewEvmBankKeeper(neuronKeeper Keeper, bk types.BankKeeper, ak types.AccountKeeper) EvmBankKeeper {
func NewEvmBankKeeper(baseKeeper Keeper, bk types.BankKeeper, ak types.AccountKeeper) EvmBankKeeper {
return EvmBankKeeper{
neuronKeeper: neuronKeeper,
baseDenomKeeper: baseKeeper,
bk: bk,
ak: ak,
}
}
// GetBalance returns the total **spendable** balance of neuron for a given account by address.
// GetBalance returns the total **spendable** balance of base denom for a given account by address.
func (k EvmBankKeeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
if denom != EvmDenom {
panic(fmt.Errorf("only evm denom %s is supported by EvmBankKeeper", EvmDenom))
if denom != chaincfg.BaseDenom {
panic(fmt.Errorf("only evm denom %s is supported by EvmBankKeeper", chaincfg.BaseDenom))
}
spendableCoins := k.bk.SpendableCoins(ctx, addr)
cosmosDenomFromBank := spendableCoins.AmountOf(CosmosDenom)
evmDenomFromBank := spendableCoins.AmountOf(EvmDenom)
evmDenomFromEvmBank := k.neuronKeeper.GetBalance(ctx, addr)
auxiliaryDenomFromBank := spendableCoins.AmountOf(chaincfg.AuxiliaryDenom)
baseDenomFromBank := spendableCoins.AmountOf(chaincfg.BaseDenom)
baseDenomFromEvmBank := k.baseDenomKeeper.GetBalance(ctx, addr)
var total sdkmath.Int
if cosmosDenomFromBank.IsPositive() {
total = cosmosDenomFromBank.Mul(ConversionMultiplier).Add(evmDenomFromBank).Add(evmDenomFromEvmBank)
if auxiliaryDenomFromBank.IsPositive() {
total = auxiliaryDenomFromBank.Mul(ConversionMultiplier).Add(baseDenomFromBank).Add(baseDenomFromEvmBank)
} else {
total = evmDenomFromBank.Add(evmDenomFromEvmBank)
total = baseDenomFromBank.Add(baseDenomFromEvmBank)
}
return sdk.NewCoin(EvmDenom, total)
return sdk.NewCoin(chaincfg.BaseDenom, total)
}
// SendCoins transfers neuron coins from a AccAddress to an AccAddress.
// SendCoins transfers base denom coins from a AccAddress to an AccAddress.
func (k EvmBankKeeper) SendCoins(ctx sdk.Context, senderAddr sdk.AccAddress, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
// SendCoins method is not used by the evm module, but is required by the
// evmtypes.BankKeeper interface. This must be updated if the evm module
@ -74,148 +67,148 @@ func (k EvmBankKeeper) SendCoins(ctx sdk.Context, senderAddr sdk.AccAddress, rec
panic("not implemented")
}
// SendCoinsFromModuleToAccount transfers neuron coins from a ModuleAccount to an AccAddress.
// SendCoinsFromModuleToAccount transfers base denom coins from a ModuleAccount to an AccAddress.
// It will panic if the module account does not exist. An error is returned if the recipient
// address is black-listed or if sending the tokens fails.
func (k EvmBankKeeper) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
ua0gi, neuron, err := SplitNeuronCoins(amt)
auxiliaryDenomCoin, baseDemonCnt, err := SplitBaseDenomCoins(amt)
if err != nil {
return err
}
if ua0gi.Amount.IsPositive() {
if err := k.bk.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, sdk.NewCoins(ua0gi)); err != nil {
if auxiliaryDenomCoin.Amount.IsPositive() {
if err := k.bk.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
return err
}
}
senderAddr := k.GetModuleAddress(senderModule)
if err := k.ConvertOneUa0giToNeuronIfNeeded(ctx, senderAddr, neuron); err != nil {
if err := k.ConvertOneAuxiliaryDenomToBaseDenomIfNeeded(ctx, senderAddr, baseDemonCnt); err != nil {
return err
}
if err := k.neuronKeeper.SendBalance(ctx, senderAddr, recipientAddr, neuron); err != nil {
if err := k.baseDenomKeeper.SendBalance(ctx, senderAddr, recipientAddr, baseDemonCnt); err != nil {
return err
}
return k.ConvertNeuronToUa0gi(ctx, recipientAddr)
return k.ConvertBaseDenomToAuxiliaryDenom(ctx, recipientAddr)
}
// SendCoinsFromAccountToModule transfers neuron coins from an AccAddress to a ModuleAccount.
// SendCoinsFromAccountToModule transfers base denom coins from an AccAddress to a ModuleAccount.
// It will panic if the module account does not exist.
func (k EvmBankKeeper) SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error {
ua0gi, neuronNeeded, err := SplitNeuronCoins(amt)
auxiliaryDenomCoin, baseDenomCnt, err := SplitBaseDenomCoins(amt)
if err != nil {
return err
}
if ua0gi.IsPositive() {
if err := k.bk.SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, sdk.NewCoins(ua0gi)); err != nil {
if auxiliaryDenomCoin.IsPositive() {
if err := k.bk.SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
return err
}
}
if err := k.ConvertOneUa0giToNeuronIfNeeded(ctx, senderAddr, neuronNeeded); err != nil {
if err := k.ConvertOneAuxiliaryDenomToBaseDenomIfNeeded(ctx, senderAddr, baseDenomCnt); err != nil {
return err
}
recipientAddr := k.GetModuleAddress(recipientModule)
if err := k.neuronKeeper.SendBalance(ctx, senderAddr, recipientAddr, neuronNeeded); err != nil {
if err := k.baseDenomKeeper.SendBalance(ctx, senderAddr, recipientAddr, baseDenomCnt); err != nil {
return err
}
return k.ConvertNeuronToUa0gi(ctx, recipientAddr)
return k.ConvertBaseDenomToAuxiliaryDenom(ctx, recipientAddr)
}
// MintCoins mints neuron coins by minting the equivalent a0gi coins and any remaining neuron coins.
// MintCoins mints base denom coins by minting the equivalent auxiliary denom coins and any remaining base denom coins.
// It will panic if the module account does not exist or is unauthorized.
func (k EvmBankKeeper) MintCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error {
ua0gi, neuron, err := SplitNeuronCoins(amt)
auxiliaryDenomCoin, baseDemonCnt, err := SplitBaseDenomCoins(amt)
if err != nil {
return err
}
if ua0gi.IsPositive() {
if err := k.bk.MintCoins(ctx, moduleName, sdk.NewCoins(ua0gi)); err != nil {
if auxiliaryDenomCoin.IsPositive() {
if err := k.bk.MintCoins(ctx, moduleName, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
return err
}
}
recipientAddr := k.GetModuleAddress(moduleName)
if err := k.neuronKeeper.AddBalance(ctx, recipientAddr, neuron); err != nil {
if err := k.baseDenomKeeper.AddBalance(ctx, recipientAddr, baseDemonCnt); err != nil {
return err
}
return k.ConvertNeuronToUa0gi(ctx, recipientAddr)
return k.ConvertBaseDenomToAuxiliaryDenom(ctx, recipientAddr)
}
// BurnCoins burns neuron coins by burning the equivalent a0gi coins and any remaining neuron coins.
// BurnCoins burns base denom coins by burning the equivalent auxiliary denom coins and any remaining base denom coins.
// It will panic if the module account does not exist or is unauthorized.
func (k EvmBankKeeper) BurnCoins(ctx sdk.Context, moduleName string, amt sdk.Coins) error {
ua0gi, neuron, err := SplitNeuronCoins(amt)
auxiliaryDenomCoin, baseDemonCnt, err := SplitBaseDenomCoins(amt)
if err != nil {
return err
}
if ua0gi.IsPositive() {
if err := k.bk.BurnCoins(ctx, moduleName, sdk.NewCoins(ua0gi)); err != nil {
if auxiliaryDenomCoin.IsPositive() {
if err := k.bk.BurnCoins(ctx, moduleName, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
return err
}
}
moduleAddr := k.GetModuleAddress(moduleName)
if err := k.ConvertOneUa0giToNeuronIfNeeded(ctx, moduleAddr, neuron); err != nil {
if err := k.ConvertOneAuxiliaryDenomToBaseDenomIfNeeded(ctx, moduleAddr, baseDemonCnt); err != nil {
return err
}
return k.neuronKeeper.RemoveBalance(ctx, moduleAddr, neuron)
return k.baseDenomKeeper.RemoveBalance(ctx, moduleAddr, baseDemonCnt)
}
// ConvertOneUa0giToNeuronIfNeeded converts 1 a0gi to neuron for an address if
// its neuron balance is smaller than the neuronNeeded amount.
func (k EvmBankKeeper) ConvertOneUa0giToNeuronIfNeeded(ctx sdk.Context, addr sdk.AccAddress, neuronNeeded sdkmath.Int) error {
neuronBal := k.neuronKeeper.GetBalance(ctx, addr)
if neuronBal.GTE(neuronNeeded) {
// ConvertOneauxiliaryDenomToBaseDenomIfNeeded converts 1 auxiliary denom to base denom for an address if
// its base denom balance is smaller than the baseDenomCnt amount.
func (k EvmBankKeeper) ConvertOneAuxiliaryDenomToBaseDenomIfNeeded(ctx sdk.Context, addr sdk.AccAddress, baseDenomCnt sdkmath.Int) error {
baseDenomBal := k.baseDenomKeeper.GetBalance(ctx, addr)
if baseDenomBal.GTE(baseDenomCnt) {
return nil
}
ua0giToStore := sdk.NewCoins(sdk.NewCoin(CosmosDenom, sdk.OneInt()))
if err := k.bk.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, ua0giToStore); err != nil {
auxiliaryDenomToStore := sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdk.OneInt()))
if err := k.bk.SendCoinsFromAccountToModule(ctx, addr, types.ModuleName, auxiliaryDenomToStore); err != nil {
return err
}
// add 1a0gi equivalent of neuron to addr
neuronToReceive := ConversionMultiplier
if err := k.neuronKeeper.AddBalance(ctx, addr, neuronToReceive); err != nil {
// add 1 auxiliary denom equivalent of base denom to addr
baseDenomToReceive := ConversionMultiplier
if err := k.baseDenomKeeper.AddBalance(ctx, addr, baseDenomToReceive); err != nil {
return err
}
return nil
}
// ConvertNeuronToA0gi converts all available neuron to a0gi for a given AccAddress.
func (k EvmBankKeeper) ConvertNeuronToUa0gi(ctx sdk.Context, addr sdk.AccAddress) error {
totalNeuron := k.neuronKeeper.GetBalance(ctx, addr)
ua0gi, _, err := SplitNeuronCoins(sdk.NewCoins(sdk.NewCoin(EvmDenom, totalNeuron)))
// ConvertBaseDenomToauxiliaryDenom converts all available base denom to auxiliary denom for a given AccAddress.
func (k EvmBankKeeper) ConvertBaseDenomToAuxiliaryDenom(ctx sdk.Context, addr sdk.AccAddress) error {
totalBaseDenom := k.baseDenomKeeper.GetBalance(ctx, addr)
auxiliaryDenomCoin, _, err := SplitBaseDenomCoins(sdk.NewCoins(sdk.NewCoin(chaincfg.BaseDenom, totalBaseDenom)))
if err != nil {
return err
}
// do nothing if account does not have enough neuron for a single a0gi
ua0giToReceive := ua0gi.Amount
if !ua0giToReceive.IsPositive() {
// do nothing if account does not have enough base denom for a single auxiliary denom
auxiliaryDenomToReceive := auxiliaryDenomCoin.Amount
if !auxiliaryDenomToReceive.IsPositive() {
return nil
}
// remove neuron used for converting to ua0gi
neuronToBurn := ua0giToReceive.Mul(ConversionMultiplier)
finalBal := totalNeuron.Sub(neuronToBurn)
if err := k.neuronKeeper.SetBalance(ctx, addr, finalBal); err != nil {
// remove base denom used for converting to auxiliary denom
baseDenomToBurn := auxiliaryDenomToReceive.Mul(ConversionMultiplier)
finalBal := totalBaseDenom.Sub(baseDenomToBurn)
if err := k.baseDenomKeeper.SetBalance(ctx, addr, finalBal); err != nil {
return err
}
fromAddr := k.GetModuleAddress(types.ModuleName)
if err := k.bk.SendCoins(ctx, fromAddr, addr, sdk.NewCoins(ua0gi)); err != nil {
if err := k.bk.SendCoins(ctx, fromAddr, addr, sdk.NewCoins(auxiliaryDenomCoin)); err != nil {
return err
}
@ -230,35 +223,35 @@ func (k EvmBankKeeper) GetModuleAddress(moduleName string) sdk.AccAddress {
return addr
}
// SplitNeuronCoins splits neuron coins to the equivalent a0gi coins and any remaining neuron balance.
// An error will be returned if the coins are not valid or if the coins are not the neuron denom.
func SplitNeuronCoins(coins sdk.Coins) (sdk.Coin, sdkmath.Int, error) {
neuron := sdk.ZeroInt()
ua0gi := sdk.NewCoin(CosmosDenom, sdk.ZeroInt())
// SplitBaseDenomCoins splits base denom coins to the equivalent auxiliary denom coins and any remaining base denom balance.
// An error will be returned if the coins are not valid or if the coins are not the base denom.
func SplitBaseDenomCoins(coins sdk.Coins) (sdk.Coin, sdkmath.Int, error) {
baseDemonCnt := sdk.ZeroInt()
auxiliaryDenomAmt := sdk.NewCoin(chaincfg.AuxiliaryDenom, sdk.ZeroInt())
if len(coins) == 0 {
return ua0gi, neuron, nil
return auxiliaryDenomAmt, baseDemonCnt, nil
}
if err := ValidateEvmCoins(coins); err != nil {
return ua0gi, neuron, err
return auxiliaryDenomAmt, baseDemonCnt, err
}
// note: we should always have len(coins) == 1 here since coins cannot have dup denoms after we validate.
coin := coins[0]
remainingBalance := coin.Amount.Mod(ConversionMultiplier)
if remainingBalance.IsPositive() {
neuron = remainingBalance
baseDemonCnt = remainingBalance
}
ua0giAmount := coin.Amount.Quo(ConversionMultiplier)
if ua0giAmount.IsPositive() {
ua0gi = sdk.NewCoin(CosmosDenom, ua0giAmount)
auxiliaryDenomAmount := coin.Amount.Quo(ConversionMultiplier)
if auxiliaryDenomAmount.IsPositive() {
auxiliaryDenomAmt = sdk.NewCoin(chaincfg.AuxiliaryDenom, auxiliaryDenomAmount)
}
return ua0gi, neuron, nil
return auxiliaryDenomAmt, baseDemonCnt, nil
}
// ValidateEvmCoins validates the coins from evm is valid and is the EvmDenom (neuron).
// ValidateEvmCoins validates the coins from evm is valid and is the base denom.
func ValidateEvmCoins(coins sdk.Coins) error {
if len(coins) == 0 {
return nil
@ -269,9 +262,9 @@ func ValidateEvmCoins(coins sdk.Coins) error {
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, coins.String())
}
// validate that coin denom is neuron
if len(coins) != 1 || coins[0].Denom != EvmDenom {
errMsg := fmt.Sprintf("invalid evm coin denom, only %s is supported", EvmDenom)
// validate that coin denom is base denom
if len(coins) != 1 || coins[0].Denom != chaincfg.BaseDenom {
errMsg := fmt.Sprintf("invalid evm coin denom, only %s is supported", chaincfg.BaseDenom)
return errorsmod.Wrap(sdkerrors.ErrInvalidCoins, errMsg)
}

View File

@ -14,6 +14,7 @@ import (
vesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
evmtypes "github.com/evmos/ethermint/x/evm/types"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/evmutil/keeper"
"github.com/0glabs/0g-chain/x/evmutil/testutil"
"github.com/0glabs/0g-chain/x/evmutil/types"
@ -34,8 +35,8 @@ func (suite *evmBankKeeperTestSuite) SetupTest() {
}
func (suite *evmBankKeeperTestSuite) TestGetBalance_ReturnsSpendable() {
startingCoins := sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 10))
startingNeuron := sdkmath.NewInt(100)
startingCoins := sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10))
startingBaseDenom := sdkmath.NewInt(100)
now := tmtime.Now()
endTime := now.Add(24 * time.Hour)
@ -45,24 +46,26 @@ func (suite *evmBankKeeperTestSuite) TestGetBalance_ReturnsSpendable() {
err := suite.App.FundAccount(suite.Ctx, suite.Addrs[0], startingCoins)
suite.Require().NoError(err)
err = suite.Keeper.SetBalance(suite.Ctx, suite.Addrs[0], startingNeuron)
err = suite.Keeper.SetBalance(suite.Ctx, suite.Addrs[0], startingBaseDenom)
suite.Require().NoError(err)
coin := suite.EvmBankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], "neuron")
suite.Require().Equal(startingNeuron, coin.Amount)
coin := suite.EvmBankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], chaincfg.BaseDenom)
suite.Require().Equal(startingBaseDenom, coin.Amount)
ctx := suite.Ctx.WithBlockTime(now.Add(12 * time.Hour))
coin = suite.EvmBankKeeper.GetBalance(ctx, suite.Addrs[0], "neuron")
coin = suite.EvmBankKeeper.GetBalance(ctx, suite.Addrs[0], chaincfg.BaseDenom)
suite.Require().Equal(sdkmath.NewIntFromUint64(5_000_000_000_100), coin.Amount)
}
func (suite *evmBankKeeperTestSuite) TestGetBalance_NotEvmDenom() {
suite.Require().Panics(func() {
suite.EvmBankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], "ua0gi")
suite.EvmBankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], chaincfg.AuxiliaryDenom)
})
suite.Require().Panics(func() {
suite.EvmBankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], "busd")
})
}
func (suite *evmBankKeeperTestSuite) TestGetBalance() {
tests := []struct {
name string
@ -70,41 +73,41 @@ func (suite *evmBankKeeperTestSuite) TestGetBalance() {
expAmount sdkmath.Int
}{
{
"ua0gi with neuron",
"auxiliary denom with base denom",
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 100),
sdk.NewInt64Coin("ua0gi", 10),
sdk.NewInt64Coin(chaincfg.BaseDenom, 100),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10),
),
sdk.NewIntFromBigInt(makeBigIntByString("10000000000100")),
sdkmath.NewInt(10_000_000_000_100),
},
{
"just neuron",
"just base denom",
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 100),
sdk.NewInt64Coin(chaincfg.BaseDenom, 100),
sdk.NewInt64Coin("busd", 100),
),
sdkmath.NewInt(100),
},
{
"just ua0gi",
"just auxiliary denom",
sdk.NewCoins(
sdk.NewInt64Coin("ua0gi", 10),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10),
sdk.NewInt64Coin("busd", 100),
),
sdk.NewIntFromBigInt(makeBigIntByString("10000000000000")),
sdkmath.NewInt(10_000_000_000_000),
},
{
"no ua0gi or neuron",
"no auxiliary denom or base denom",
sdk.NewCoins(),
sdk.ZeroInt(),
},
{
"with avaka that is more than 1 ua0gi",
"with avaka that is more than 1 auxiliary denom",
sdk.NewCoins(
sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("20000000000220"))),
sdk.NewInt64Coin("ua0gi", 11),
sdk.NewInt64Coin(chaincfg.BaseDenom, 20_000_000_000_220),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 11),
),
sdk.NewIntFromBigInt(makeBigIntByString("31000000000220")),
sdkmath.NewInt(31_000_000_000_220),
},
}
@ -113,15 +116,16 @@ func (suite *evmBankKeeperTestSuite) TestGetBalance() {
suite.SetupTest()
suite.FundAccountWithZgChain(suite.Addrs[0], tt.startingAmount)
coin := suite.EvmBankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], "neuron")
coin := suite.EvmBankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], chaincfg.BaseDenom)
suite.Require().Equal(tt.expAmount, coin.Amount)
})
}
}
func (suite *evmBankKeeperTestSuite) TestSendCoinsFromModuleToAccount() {
startingModuleCoins := sdk.NewCoins(
sdk.NewInt64Coin("neuron", 200),
sdk.NewInt64Coin("ua0gi", 100),
sdk.NewInt64Coin(chaincfg.BaseDenom, 200),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 100),
)
tests := []struct {
name string
@ -131,102 +135,102 @@ func (suite *evmBankKeeperTestSuite) TestSendCoinsFromModuleToAccount() {
hasErr bool
}{
{
"send more than 1 ua0gi",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("12000000000010")))),
"send more than 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 12_000_000_000_010)),
sdk.Coins{},
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 10),
sdk.NewInt64Coin("ua0gi", 12),
sdk.NewInt64Coin(chaincfg.BaseDenom, 10),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 12),
),
false,
},
{
"send less than 1 ua0gi",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 122)),
"send less than 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 122)),
sdk.Coins{},
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 122),
sdk.NewInt64Coin("ua0gi", 0),
sdk.NewInt64Coin(chaincfg.BaseDenom, 122),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 0),
),
false,
},
{
"send an exact amount of ua0gi",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("98000000000000")))),
"send an exact amount of auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 98_000_000_000_000)),
sdk.Coins{},
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 0),
sdk.NewInt64Coin("ua0gi", 98),
sdk.NewInt64Coin(chaincfg.BaseDenom, 0o0),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 98),
),
false,
},
{
"send no neuron",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 0)),
"send no base denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 0)),
sdk.Coins{},
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 0),
sdk.NewInt64Coin("ua0gi", 0),
sdk.NewInt64Coin(chaincfg.BaseDenom, 0),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 0),
),
false,
},
{
"errors if sending other coins",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 500), sdk.NewInt64Coin("busd", 1000)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 500), sdk.NewInt64Coin("busd", 1000)),
sdk.Coins{},
sdk.Coins{},
true,
},
{
"errors if not enough total neuron to cover",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("100000000001000")))),
"errors if not enough total base denom to cover",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100_000_000_001_000)),
sdk.Coins{},
sdk.Coins{},
true,
},
{
"errors if not enough ua0gi to cover",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("200000000000000")))),
"errors if not enough auxiliary denom to cover",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 200_000_000_000_000)),
sdk.Coins{},
sdk.Coins{},
true,
},
{
"converts receiver's neuron to ua0gi if there's enough neuron after the transfer",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("99000000000200")))),
"converts receiver's base denom to auxiliary denom if there's enough base denom after the transfer",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 99_000_000_000_200)),
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 999_999_999_900),
sdk.NewInt64Coin("ua0gi", 1),
sdk.NewInt64Coin(chaincfg.BaseDenom, 999_999_999_900),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 1),
),
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 100),
sdk.NewInt64Coin("ua0gi", 101),
sdk.NewInt64Coin(chaincfg.BaseDenom, 100),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 101),
),
false,
},
{
"converts all of receiver's neuron to ua0gi even if somehow receiver has more than 1a0gi of neuron",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("12000000000100")))),
"converts all of receiver's base denom to auxiliary denom even if somehow receiver has more than 1 auxiliary denom of base denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 12_000_000_000_100)),
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 5_999_999_999_990),
sdk.NewInt64Coin("ua0gi", 1),
sdk.NewInt64Coin(chaincfg.BaseDenom, 5_999_999_999_990),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 1),
),
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 90),
sdk.NewInt64Coin("ua0gi", 19),
sdk.NewInt64Coin(chaincfg.BaseDenom, 90),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 19),
),
false,
},
{
"swap 1 ua0gi for neuron if module account doesn't have enough neuron",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("99000000001000")))),
"swap 1 auxiliary denom for base denom if module account doesn't have enough base denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 99_000_000_001_000)),
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 200),
sdk.NewInt64Coin("ua0gi", 1),
sdk.NewInt64Coin(chaincfg.BaseDenom, 200),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 1),
),
sdk.NewCoins(
sdk.NewInt64Coin("neuron", 1200),
sdk.NewInt64Coin("ua0gi", 100),
sdk.NewInt64Coin(chaincfg.BaseDenom, 1200),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 100),
),
false,
},
@ -239,8 +243,8 @@ func (suite *evmBankKeeperTestSuite) TestSendCoinsFromModuleToAccount() {
suite.FundAccountWithZgChain(suite.Addrs[0], tt.startingAccBal)
suite.FundModuleAccountWithZgChain(evmtypes.ModuleName, startingModuleCoins)
// fund our module with some ua0gi to account for converting extra neuron back to ua0gi
suite.FundModuleAccountWithZgChain(types.ModuleName, sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 10)))
// fund our module with some auxiliary denom to account for converting extra base denom back to auxiliary denom
suite.FundModuleAccountWithZgChain(types.ModuleName, sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10)))
err := suite.EvmBankKeeper.SendCoinsFromModuleToAccount(suite.Ctx, evmtypes.ModuleName, suite.Addrs[0], tt.sendCoins)
if tt.hasErr {
@ -250,23 +254,24 @@ func (suite *evmBankKeeperTestSuite) TestSendCoinsFromModuleToAccount() {
suite.Require().NoError(err)
}
// check ua0gi
a0giSender := suite.BankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], "ua0gi")
suite.Require().Equal(tt.expAccBal.AmountOf("ua0gi").Int64(), a0giSender.Amount.Int64())
// check auxiliary denom
AuxiliaryDenomSender := suite.BankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], chaincfg.AuxiliaryDenom)
suite.Require().Equal(tt.expAccBal.AmountOf(chaincfg.AuxiliaryDenom).Int64(), AuxiliaryDenomSender.Amount.Int64())
// check neuron
actualNeuron := suite.Keeper.GetBalance(suite.Ctx, suite.Addrs[0])
suite.Require().Equal(tt.expAccBal.AmountOf("neuron").Int64(), actualNeuron.Int64())
// check base denom
actualBaseDenom := suite.Keeper.GetBalance(suite.Ctx, suite.Addrs[0])
suite.Require().Equal(tt.expAccBal.AmountOf(chaincfg.BaseDenom).Int64(), actualBaseDenom.Int64())
})
}
}
func (suite *evmBankKeeperTestSuite) TestSendCoinsFromAccountToModule() {
startingAccCoins := sdk.NewCoins(
sdk.NewInt64Coin("neuron", 200),
sdk.NewInt64Coin("ua0gi", 100),
sdk.NewInt64Coin(chaincfg.BaseDenom, 200),
sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 100),
)
startingModuleCoins := sdk.NewCoins(
sdk.NewInt64Coin("neuron", 100_000_000_000),
sdk.NewInt64Coin(chaincfg.BaseDenom, 100_000_000_000),
)
tests := []struct {
name string
@ -276,36 +281,36 @@ func (suite *evmBankKeeperTestSuite) TestSendCoinsFromAccountToModule() {
hasErr bool
}{
{
"send more than 1 ua0gi",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("12000000000010")))),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 190), sdk.NewInt64Coin("ua0gi", 88)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 100_000_000_010), sdk.NewInt64Coin("ua0gi", 12)),
"send more than 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 12_000_000_000_010)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 190), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 88)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100_000_000_010), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 12)),
false,
},
{
"send less than 1 ua0gi",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 122)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 78), sdk.NewInt64Coin("ua0gi", 100)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 100_000_000_122), sdk.NewInt64Coin("ua0gi", 0)),
"send less than 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 122)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 78), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 100)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100_000_000_122), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 0)),
false,
},
{
"send an exact amount of ua0gi",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("98000000000000")))),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 200), sdk.NewInt64Coin("ua0gi", 2)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 100_000_000_000), sdk.NewInt64Coin("ua0gi", 98)),
"send an exact amount of auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 98_000_000_000_000)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 200), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 2)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100_000_000_000), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 98)),
false,
},
{
"send no neuron",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 0)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 200), sdk.NewInt64Coin("ua0gi", 100)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 100_000_000_000), sdk.NewInt64Coin("ua0gi", 0)),
"send no base denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 0)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 200), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 100)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100_000_000_000), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 0)),
false,
},
{
"errors if sending other coins",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 500), sdk.NewInt64Coin("busd", 1000)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 500), sdk.NewInt64Coin("busd", 1000)),
sdk.Coins{},
sdk.Coins{},
true,
@ -313,39 +318,39 @@ func (suite *evmBankKeeperTestSuite) TestSendCoinsFromAccountToModule() {
{
"errors if have dup coins",
sdk.Coins{
sdk.NewInt64Coin("neuron", 12_000_000_000_000),
sdk.NewInt64Coin("neuron", 2_000_000_000_000),
sdk.NewInt64Coin(chaincfg.BaseDenom, 12_000_000_000_000),
sdk.NewInt64Coin(chaincfg.BaseDenom, 2_000_000_000_000),
},
sdk.Coins{},
sdk.Coins{},
true,
},
{
"errors if not enough total neuron to cover",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("100000000001000")))),
"errors if not enough total base denom to cover",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100_000_000_001_000)),
sdk.Coins{},
sdk.Coins{},
true,
},
{
"errors if not enough ua0gi to cover",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("200000000000000")))),
"errors if not enough auxiliary denom to cover",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 200_000_000_000_000)),
sdk.Coins{},
sdk.Coins{},
true,
},
{
"converts 1 ua0gi to neuron if not enough neuron to cover",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("99001000000000")))),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 999_000_000_200), sdk.NewInt64Coin("ua0gi", 0)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 101_000_000_000), sdk.NewInt64Coin("ua0gi", 99)),
"converts 1 auxiliary denom to base denom if not enough base denom to cover",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 99_001_000_000_000)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 999_000_000_200), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 0)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 101_000_000_000), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 99)),
false,
},
{
"converts receiver's neuron to ua0gi if there's enough neuron after the transfer",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 5_900_000_000_200)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 100_000_000_000), sdk.NewInt64Coin("ua0gi", 94)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 200), sdk.NewInt64Coin("ua0gi", 6)),
"converts receiver's base denom to auxiliary denom if there's enough base denom after the transfer",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 5_900_000_000_200)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100_000_000_000), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 94)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 200), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 6)),
false,
},
}
@ -365,66 +370,67 @@ func (suite *evmBankKeeperTestSuite) TestSendCoinsFromAccountToModule() {
}
// check sender balance
a0giSender := suite.BankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], "ua0gi")
suite.Require().Equal(tt.expSenderCoins.AmountOf("ua0gi").Int64(), a0giSender.Amount.Int64())
actualNeuron := suite.Keeper.GetBalance(suite.Ctx, suite.Addrs[0])
suite.Require().Equal(tt.expSenderCoins.AmountOf("neuron").Int64(), actualNeuron.Int64())
AuxiliaryDenomSender := suite.BankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], chaincfg.AuxiliaryDenom)
suite.Require().Equal(tt.expSenderCoins.AmountOf(chaincfg.AuxiliaryDenom).Int64(), AuxiliaryDenomSender.Amount.Int64())
actualBaseDenom := suite.Keeper.GetBalance(suite.Ctx, suite.Addrs[0])
suite.Require().Equal(tt.expSenderCoins.AmountOf(chaincfg.BaseDenom).Int64(), actualBaseDenom.Int64())
// check module balance
moduleAddr := suite.AccountKeeper.GetModuleAddress(evmtypes.ModuleName)
a0giSender = suite.BankKeeper.GetBalance(suite.Ctx, moduleAddr, "ua0gi")
suite.Require().Equal(tt.expModuleCoins.AmountOf("ua0gi").Int64(), a0giSender.Amount.Int64())
actualNeuron = suite.Keeper.GetBalance(suite.Ctx, moduleAddr)
suite.Require().Equal(tt.expModuleCoins.AmountOf("neuron").Int64(), actualNeuron.Int64())
AuxiliaryDenomSender = suite.BankKeeper.GetBalance(suite.Ctx, moduleAddr, chaincfg.AuxiliaryDenom)
suite.Require().Equal(tt.expModuleCoins.AmountOf(chaincfg.AuxiliaryDenom).Int64(), AuxiliaryDenomSender.Amount.Int64())
actualBaseDenom = suite.Keeper.GetBalance(suite.Ctx, moduleAddr)
suite.Require().Equal(tt.expModuleCoins.AmountOf(chaincfg.BaseDenom).Int64(), actualBaseDenom.Int64())
})
}
}
func (suite *evmBankKeeperTestSuite) TestBurnCoins() {
startingA0gi := sdkmath.NewInt(100)
startingAuxiliaryDenom := sdkmath.NewInt(100)
tests := []struct {
name string
burnCoins sdk.Coins
expA0gi sdkmath.Int
expNeuron sdkmath.Int
expAuxiliaryDenom sdkmath.Int
expBaseDenom sdkmath.Int
hasErr bool
neuronStart sdkmath.Int
baseDenomStart sdkmath.Int
}{
{
"burn more than 1 ua0gi",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("12021000000002")))),
"burn more than 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 12_021_000_000_002)),
sdkmath.NewInt(88),
sdkmath.NewInt(100_000_000_000),
false,
sdk.NewIntFromBigInt(makeBigIntByString("121000000002")),
sdkmath.NewInt(121_000_000_002),
},
{
"burn less than 1 ua0gi",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 122)),
"burn less than 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 122)),
sdkmath.NewInt(100),
sdkmath.NewInt(878),
false,
sdkmath.NewInt(1000),
},
{
"burn an exact amount of ua0gi",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("98000000000000")))),
"burn an exact amount of auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 98_000_000_000_000)),
sdkmath.NewInt(2),
sdkmath.NewInt(10),
false,
sdkmath.NewInt(10),
},
{
"burn no neuron",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 0)),
startingA0gi,
"burn no base denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 0)),
startingAuxiliaryDenom,
sdk.ZeroInt(),
false,
sdk.ZeroInt(),
},
{
"errors if burning other coins",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 500), sdk.NewInt64Coin("busd", 1000)),
startingA0gi,
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 500), sdk.NewInt64Coin("busd", 1000)),
startingAuxiliaryDenom,
sdkmath.NewInt(100),
true,
sdkmath.NewInt(100),
@ -432,41 +438,41 @@ func (suite *evmBankKeeperTestSuite) TestBurnCoins() {
{
"errors if have dup coins",
sdk.Coins{
sdk.NewInt64Coin("neuron", 12_000_000_000_000),
sdk.NewInt64Coin("neuron", 2_000_000_000_000),
sdk.NewInt64Coin(chaincfg.BaseDenom, 12_000_000_000_000),
sdk.NewInt64Coin(chaincfg.BaseDenom, 2_000_000_000_000),
},
startingA0gi,
startingAuxiliaryDenom,
sdk.ZeroInt(),
true,
sdk.ZeroInt(),
},
{
"errors if burn amount is negative",
sdk.Coins{sdk.Coin{Denom: "neuron", Amount: sdkmath.NewInt(-100)}},
startingA0gi,
sdk.Coins{sdk.Coin{Denom: chaincfg.BaseDenom, Amount: sdkmath.NewInt(-100)}},
startingAuxiliaryDenom,
sdkmath.NewInt(50),
true,
sdkmath.NewInt(50),
},
{
"errors if not enough neuron to cover burn",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("100999000000000")))),
"errors if not enough base denom to cover burn",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100_999_000_000_000)),
sdkmath.NewInt(0),
sdkmath.NewInt(99_000_000_000),
true,
sdkmath.NewInt(99_000_000_000),
},
{
"errors if not enough ua0gi to cover burn",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("200000000000000")))),
"errors if not enough auxiliary denom to cover burn",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 200_000_000_000_000)),
sdkmath.NewInt(100),
sdk.ZeroInt(),
true,
sdk.ZeroInt(),
},
{
"converts 1 ua0gi to neuron if not enough neuron to cover",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("12021000000002")))),
"converts 1 auxiliary denom to base denom if not enough base denom to cover",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 12_021_000_000_002)),
sdkmath.NewInt(87),
sdkmath.NewInt(980_000_000_000),
false,
@ -478,8 +484,8 @@ func (suite *evmBankKeeperTestSuite) TestBurnCoins() {
suite.Run(tt.name, func() {
suite.SetupTest()
startingCoins := sdk.NewCoins(
sdk.NewCoin("ua0gi", startingA0gi),
sdk.NewCoin("neuron", tt.neuronStart),
sdk.NewCoin(chaincfg.AuxiliaryDenom, startingAuxiliaryDenom),
sdk.NewCoin(chaincfg.BaseDenom, tt.baseDenomStart),
)
suite.FundModuleAccountWithZgChain(evmtypes.ModuleName, startingCoins)
@ -491,52 +497,53 @@ func (suite *evmBankKeeperTestSuite) TestBurnCoins() {
suite.Require().NoError(err)
}
// check ua0gi
a0giActual := suite.BankKeeper.GetBalance(suite.Ctx, suite.EvmModuleAddr, "ua0gi")
suite.Require().Equal(tt.expA0gi, a0giActual.Amount)
// check auxiliary denom
AuxiliaryDenomActual := suite.BankKeeper.GetBalance(suite.Ctx, suite.EvmModuleAddr, chaincfg.AuxiliaryDenom)
suite.Require().Equal(tt.expAuxiliaryDenom, AuxiliaryDenomActual.Amount)
// check neuron
neuronActual := suite.Keeper.GetBalance(suite.Ctx, suite.EvmModuleAddr)
suite.Require().Equal(tt.expNeuron, neuronActual)
// check base denom
baseDenomActual := suite.Keeper.GetBalance(suite.Ctx, suite.EvmModuleAddr)
suite.Require().Equal(tt.expBaseDenom, baseDenomActual)
})
}
}
func (suite *evmBankKeeperTestSuite) TestMintCoins() {
tests := []struct {
name string
mintCoins sdk.Coins
ua0gi sdkmath.Int
neuron sdkmath.Int
AuxiliaryDenomCnt sdkmath.Int
baseDenomCnt sdkmath.Int
hasErr bool
neuronStart sdkmath.Int
baseDenomStart sdkmath.Int
}{
{
"mint more than 1 ua0gi",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("12021000000002")))),
"mint more than 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 12_021_000_000_002)),
sdkmath.NewInt(12),
sdkmath.NewInt(21_000_000_002),
false,
sdk.ZeroInt(),
},
{
"mint less than 1 ua0gi",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 901_000_000_001)),
"mint less than 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 901_000_000_001)),
sdk.ZeroInt(),
sdkmath.NewInt(901_000_000_001),
false,
sdk.ZeroInt(),
},
{
"mint an exact amount of ua0gi",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("123000000000000000")))),
"mint an exact amount of auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 123_000_000_000_000_000)),
sdkmath.NewInt(123_000),
sdk.ZeroInt(),
false,
sdk.ZeroInt(),
},
{
"mint no neuron",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 0)),
"mint no base denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 0)),
sdk.ZeroInt(),
sdk.ZeroInt(),
false,
@ -544,7 +551,7 @@ func (suite *evmBankKeeperTestSuite) TestMintCoins() {
},
{
"errors if minting other coins",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 500), sdk.NewInt64Coin("busd", 1000)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 500), sdk.NewInt64Coin("busd", 1000)),
sdk.ZeroInt(),
sdkmath.NewInt(100),
true,
@ -553,8 +560,8 @@ func (suite *evmBankKeeperTestSuite) TestMintCoins() {
{
"errors if have dup coins",
sdk.Coins{
sdk.NewInt64Coin("neuron", 12_000_000_000_000),
sdk.NewInt64Coin("neuron", 2_000_000_000_000),
sdk.NewInt64Coin(chaincfg.BaseDenom, 12_000_000_000_000),
sdk.NewInt64Coin(chaincfg.BaseDenom, 2_000_000_000_000),
},
sdk.ZeroInt(),
sdk.ZeroInt(),
@ -563,35 +570,35 @@ func (suite *evmBankKeeperTestSuite) TestMintCoins() {
},
{
"errors if mint amount is negative",
sdk.Coins{sdk.Coin{Denom: "neuron", Amount: sdkmath.NewInt(-100)}},
sdk.Coins{sdk.Coin{Denom: chaincfg.BaseDenom, Amount: sdkmath.NewInt(-100)}},
sdk.ZeroInt(),
sdkmath.NewInt(50),
true,
sdkmath.NewInt(50),
},
{
"adds to existing neuron balance",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("12021000000002")))),
"adds to existing base denom balance",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 12_021_000_000_002)),
sdkmath.NewInt(12),
sdkmath.NewInt(21_000_000_102),
false,
sdkmath.NewInt(100),
},
{
"convert neuron balance to ua0gi if it exceeds 1 ua0gi",
sdk.NewCoins(sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("10999000000000")))),
"convert base denom balance to auxiliary denom if it exceeds 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 10_999_000_000_000)),
sdkmath.NewInt(12),
sdkmath.NewInt(1_200_000_001),
false,
sdkmath.NewIntFromBigInt(makeBigIntByString("1002200000001")),
sdkmath.NewInt(1_002_200_000_001),
},
}
for _, tt := range tests {
suite.Run(tt.name, func() {
suite.SetupTest()
suite.FundModuleAccountWithZgChain(types.ModuleName, sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 10)))
suite.FundModuleAccountWithZgChain(evmtypes.ModuleName, sdk.NewCoins(sdk.NewCoin("neuron", tt.neuronStart)))
suite.FundModuleAccountWithZgChain(types.ModuleName, sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10)))
suite.FundModuleAccountWithZgChain(evmtypes.ModuleName, sdk.NewCoins(sdk.NewCoin(chaincfg.BaseDenom, tt.baseDenomStart)))
err := suite.EvmBankKeeper.MintCoins(suite.Ctx, evmtypes.ModuleName, tt.mintCoins)
if tt.hasErr {
@ -601,13 +608,13 @@ func (suite *evmBankKeeperTestSuite) TestMintCoins() {
suite.Require().NoError(err)
}
// check ua0gi
a0giActual := suite.BankKeeper.GetBalance(suite.Ctx, suite.EvmModuleAddr, "ua0gi")
suite.Require().Equal(tt.ua0gi, a0giActual.Amount)
// check auxiliary denom
AuxiliaryDenomActual := suite.BankKeeper.GetBalance(suite.Ctx, suite.EvmModuleAddr, chaincfg.AuxiliaryDenom)
suite.Require().Equal(tt.AuxiliaryDenomCnt, AuxiliaryDenomActual.Amount)
// check neuron
neuronActual := suite.Keeper.GetBalance(suite.Ctx, suite.EvmModuleAddr)
suite.Require().Equal(tt.neuron, neuronActual)
// check base denom
baseDenomActual := suite.Keeper.GetBalance(suite.Ctx, suite.EvmModuleAddr)
suite.Require().Equal(tt.baseDenomCnt, baseDenomActual)
})
}
}
@ -620,22 +627,22 @@ func (suite *evmBankKeeperTestSuite) TestValidateEvmCoins() {
}{
{
"valid coins",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 500)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 500)),
false,
},
{
"dup coins",
sdk.Coins{sdk.NewInt64Coin("neuron", 500), sdk.NewInt64Coin("neuron", 500)},
sdk.Coins{sdk.NewInt64Coin(chaincfg.BaseDenom, 500), sdk.NewInt64Coin(chaincfg.BaseDenom, 500)},
true,
},
{
"not evm coins",
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 500)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 500)),
true,
},
{
"negative coins",
sdk.Coins{sdk.Coin{Denom: "neuron", Amount: sdkmath.NewInt(-500)}},
sdk.Coins{sdk.Coin{Denom: chaincfg.BaseDenom, Amount: sdkmath.NewInt(-500)}},
true,
},
}
@ -651,8 +658,8 @@ func (suite *evmBankKeeperTestSuite) TestValidateEvmCoins() {
}
}
func (suite *evmBankKeeperTestSuite) TestConvertOneA0giToNeuronIfNeeded() {
neuronNeeded := sdkmath.NewInt(200)
func (suite *evmBankKeeperTestSuite) TestConvertOneAuxiliaryDenomToBaseDenomIfNeeded() {
baseDenomNeeded := sdkmath.NewInt(200)
tests := []struct {
name string
startingCoins sdk.Coins
@ -660,21 +667,21 @@ func (suite *evmBankKeeperTestSuite) TestConvertOneA0giToNeuronIfNeeded() {
success bool
}{
{
"not enough ua0gi for conversion",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 100)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 100)),
"not enough auxiliary denom for conversion",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100)),
false,
},
{
"converts 1 ua0gi to neuron",
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 10), sdk.NewInt64Coin("neuron", 100)),
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 9), sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("1000000000100")))),
"converts 1 auxiliary denom to base denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10), sdk.NewInt64Coin(chaincfg.BaseDenom, 100)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 9), sdk.NewInt64Coin(chaincfg.BaseDenom, 1_000_000_000_100)),
true,
},
{
"conversion not needed",
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 10), sdk.NewInt64Coin("neuron", 200)),
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 10), sdk.NewInt64Coin("neuron", 200)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10), sdk.NewInt64Coin(chaincfg.BaseDenom, 200)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10), sdk.NewInt64Coin(chaincfg.BaseDenom, 200)),
true,
},
}
@ -683,11 +690,11 @@ func (suite *evmBankKeeperTestSuite) TestConvertOneA0giToNeuronIfNeeded() {
suite.SetupTest()
suite.FundAccountWithZgChain(suite.Addrs[0], tt.startingCoins)
err := suite.EvmBankKeeper.ConvertOneUa0giToNeuronIfNeeded(suite.Ctx, suite.Addrs[0], neuronNeeded)
moduleZgChain := suite.BankKeeper.GetBalance(suite.Ctx, suite.AccountKeeper.GetModuleAddress(types.ModuleName), "ua0gi")
err := suite.EvmBankKeeper.ConvertOneAuxiliaryDenomToBaseDenomIfNeeded(suite.Ctx, suite.Addrs[0], baseDenomNeeded)
moduleZgChain := suite.BankKeeper.GetBalance(suite.Ctx, suite.AccountKeeper.GetModuleAddress(types.ModuleName), chaincfg.AuxiliaryDenom)
if tt.success {
suite.Require().NoError(err)
if tt.startingCoins.AmountOf("neuron").LT(neuronNeeded) {
if tt.startingCoins.AmountOf(chaincfg.BaseDenom).LT(baseDenomNeeded) {
suite.Require().Equal(sdk.OneInt(), moduleZgChain.Amount)
}
} else {
@ -695,52 +702,54 @@ func (suite *evmBankKeeperTestSuite) TestConvertOneA0giToNeuronIfNeeded() {
suite.Require().Equal(sdk.ZeroInt(), moduleZgChain.Amount)
}
neuron := suite.Keeper.GetBalance(suite.Ctx, suite.Addrs[0])
suite.Require().Equal(tt.expectedCoins.AmountOf("neuron"), neuron)
ua0gi := suite.BankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], "ua0gi")
suite.Require().Equal(tt.expectedCoins.AmountOf("ua0gi"), ua0gi.Amount)
baseDenomCnt := suite.Keeper.GetBalance(suite.Ctx, suite.Addrs[0])
suite.Require().Equal(tt.expectedCoins.AmountOf(chaincfg.BaseDenom), baseDenomCnt)
AuxiliaryDenomCoin := suite.BankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], chaincfg.AuxiliaryDenom)
suite.Require().Equal(tt.expectedCoins.AmountOf(chaincfg.AuxiliaryDenom), AuxiliaryDenomCoin.Amount)
})
}
}
func (suite *evmBankKeeperTestSuite) TestConvertNeuronToA0gi() {
func (suite *evmBankKeeperTestSuite) TestConvertBaseDenomToAuxiliaryDenom() {
tests := []struct {
name string
startingCoins sdk.Coins
expectedCoins sdk.Coins
}{
{
"not enough ua0gi",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 100)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 100), sdk.NewInt64Coin("ua0gi", 0)),
"not enough auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 100), sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 0)),
},
{
"converts neuron for 1 ua0gi",
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 10), sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("1000000000003")))),
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 11), sdk.NewInt64Coin("neuron", 3)),
"converts base denom for 1 auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10), sdk.NewInt64Coin(chaincfg.BaseDenom, 1_000_000_000_003)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 11), sdk.NewInt64Coin(chaincfg.BaseDenom, 3)),
},
{
"converts more than 1 ua0gi of neuron",
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 10), sdk.NewCoin("neuron", sdk.NewIntFromBigInt(makeBigIntByString("8000000000123")))),
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 18), sdk.NewInt64Coin("neuron", 123)),
"converts more than 1 auxiliary denom of base denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10), sdk.NewInt64Coin(chaincfg.BaseDenom, 8_000_000_000_123)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 18), sdk.NewInt64Coin(chaincfg.BaseDenom, 123)),
},
}
for _, tt := range tests {
suite.Run(tt.name, func() {
suite.SetupTest()
err := suite.App.FundModuleAccount(suite.Ctx, types.ModuleName, sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 10)))
err := suite.App.FundModuleAccount(suite.Ctx, types.ModuleName, sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 10)))
suite.Require().NoError(err)
suite.FundAccountWithZgChain(suite.Addrs[0], tt.startingCoins)
err = suite.EvmBankKeeper.ConvertNeuronToUa0gi(suite.Ctx, suite.Addrs[0])
err = suite.EvmBankKeeper.ConvertBaseDenomToAuxiliaryDenom(suite.Ctx, suite.Addrs[0])
suite.Require().NoError(err)
neuron := suite.Keeper.GetBalance(suite.Ctx, suite.Addrs[0])
suite.Require().Equal(tt.expectedCoins.AmountOf("neuron"), neuron)
ua0gi := suite.BankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], "ua0gi")
suite.Require().Equal(tt.expectedCoins.AmountOf("ua0gi"), ua0gi.Amount)
baseDenomCnt := suite.Keeper.GetBalance(suite.Ctx, suite.Addrs[0])
suite.Require().Equal(tt.expectedCoins.AmountOf(chaincfg.BaseDenom), baseDenomCnt)
AuxiliaryDenomCoin := suite.BankKeeper.GetBalance(suite.Ctx, suite.Addrs[0], chaincfg.AuxiliaryDenom)
suite.Require().Equal(tt.expectedCoins.AmountOf(chaincfg.AuxiliaryDenom), AuxiliaryDenomCoin.Amount)
})
}
}
func (suite *evmBankKeeperTestSuite) TestSplitNeuronCoins() {
func (suite *evmBankKeeperTestSuite) TestSplitBaseDenomCoins() {
tests := []struct {
name string
coins sdk.Coins
@ -749,7 +758,7 @@ func (suite *evmBankKeeperTestSuite) TestSplitNeuronCoins() {
}{
{
"invalid coins",
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 500)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 500)),
nil,
true,
},
@ -760,33 +769,33 @@ func (suite *evmBankKeeperTestSuite) TestSplitNeuronCoins() {
false,
},
{
"ua0gi & neuron coins",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 8_000_000_000_123)),
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 8), sdk.NewInt64Coin("neuron", 123)),
"auxiliary denom & base denom coins",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 8_000_000_000_123)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 8), sdk.NewInt64Coin(chaincfg.BaseDenom, 123)),
false,
},
{
"only neuron",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 10_123)),
sdk.NewCoins(sdk.NewInt64Coin("neuron", 10_123)),
"only base denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 10_123)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 10_123)),
false,
},
{
"only ua0gi",
sdk.NewCoins(sdk.NewInt64Coin("neuron", 5_000_000_000_000)),
sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 5)),
"only auxiliary denom",
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.BaseDenom, 5_000_000_000_000)),
sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 5)),
false,
},
}
for _, tt := range tests {
suite.Run(tt.name, func() {
ua0gi, neuron, err := keeper.SplitNeuronCoins(tt.coins)
AuxiliaryDenomCoin, baseDenomCnt, err := keeper.SplitBaseDenomCoins(tt.coins)
if tt.shouldErr {
suite.Require().Error(err)
} else {
suite.Require().NoError(err)
suite.Require().Equal(tt.expectedCoins.AmountOf("ua0gi"), ua0gi.Amount)
suite.Require().Equal(tt.expectedCoins.AmountOf("neuron"), neuron)
suite.Require().Equal(tt.expectedCoins.AmountOf(chaincfg.AuxiliaryDenom), AuxiliaryDenomCoin.Amount)
suite.Require().Equal(tt.expectedCoins.AmountOf(chaincfg.BaseDenom), baseDenomCnt)
}
})
}

View File

@ -6,6 +6,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/evmutil/types"
)
@ -50,7 +51,7 @@ func FullyBackedInvariant(bankK types.BankKeeper, k Keeper) sdk.Invariant {
})
bankAddr := authtypes.NewModuleAddress(types.ModuleName)
bankBalance := bankK.GetBalance(ctx, bankAddr, CosmosDenom).Amount.Mul(ConversionMultiplier)
bankBalance := bankK.GetBalance(ctx, bankAddr, chaincfg.AuxiliaryDenom).Amount.Mul(ConversionMultiplier)
broken = totalMinorBalances.GT(bankBalance)

View File

@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/evmutil/keeper"
"github.com/0glabs/0g-chain/x/evmutil/testutil"
"github.com/0glabs/0g-chain/x/evmutil/types"
@ -49,7 +50,7 @@ func (suite *invariantTestSuite) SetupValidState() {
suite.FundModuleAccountWithZgChain(
types.ModuleName,
sdk.NewCoins(
sdk.NewCoin("ua0gi", sdkmath.NewInt(2)), // ( sum of all minor balances ) / conversion multiplier
sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(2)), // ( sum of all minor balances ) / conversion multiplier
),
)
@ -159,8 +160,8 @@ func (suite *invariantTestSuite) TestSmallBalances() {
// increase minor balance at least above conversion multiplier
suite.Keeper.AddBalance(suite.Ctx, suite.Addrs[0], keeper.ConversionMultiplier)
// add same number of a0gi to avoid breaking other invariants
amt := sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 1))
// add same number of auxiliary denom to avoid breaking other invariants
amt := sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 1))
suite.Require().NoError(
suite.App.FundModuleAccount(suite.Ctx, types.ModuleName, amt),
)

View File

@ -115,7 +115,7 @@ func (k Keeper) SetAccount(ctx sdk.Context, account types.Account) error {
return nil
}
// GetBalance returns the total balance of neuron for a given account by address.
// GetBalance returns the total balance of base denom for a given account by address.
func (k Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress) sdkmath.Int {
account := k.GetAccount(ctx, addr)
if account == nil {
@ -124,7 +124,7 @@ func (k Keeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress) sdkmath.Int {
return account.Balance
}
// SetBalance sets the total balance of neuron for a given account by address.
// SetBalance sets the total balance of base denom for a given account by address.
func (k Keeper) SetBalance(ctx sdk.Context, addr sdk.AccAddress, bal sdkmath.Int) error {
account := k.GetAccount(ctx, addr)
if account == nil {
@ -140,10 +140,10 @@ func (k Keeper) SetBalance(ctx sdk.Context, addr sdk.AccAddress, bal sdkmath.Int
return k.SetAccount(ctx, *account)
}
// SendBalance transfers the neuron balance from sender addr to recipient addr.
// SendBalance transfers the base denom balance from sender addr to recipient addr.
func (k Keeper) SendBalance(ctx sdk.Context, senderAddr sdk.AccAddress, recipientAddr sdk.AccAddress, amt sdkmath.Int) error {
if amt.IsNegative() {
return fmt.Errorf("cannot send a negative amount of neuron: %d", amt)
return fmt.Errorf("cannot send a negative amount of base denom: %d", amt)
}
if amt.IsZero() {
@ -162,13 +162,13 @@ func (k Keeper) SendBalance(ctx sdk.Context, senderAddr sdk.AccAddress, recipien
return k.SetBalance(ctx, recipientAddr, receiverBal)
}
// AddBalance increments the neuron balance of an address.
// AddBalance increments the base denom balance of an address.
func (k Keeper) AddBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdkmath.Int) error {
bal := k.GetBalance(ctx, addr)
return k.SetBalance(ctx, addr, amt.Add(bal))
}
// RemoveBalance decrements the neuron balance of an address.
// RemoveBalance decrements the base denom balance of an address.
func (k Keeper) RemoveBalance(ctx sdk.Context, addr sdk.AccAddress, amt sdkmath.Int) error {
if amt.IsNegative() {
return fmt.Errorf("cannot remove a negative amount from balance: %d", amt)

View File

@ -37,6 +37,7 @@ import (
"github.com/stretchr/testify/suite"
"github.com/0glabs/0g-chain/app"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/evmutil/keeper"
"github.com/0glabs/0g-chain/x/evmutil/types"
)
@ -81,14 +82,14 @@ func (suite *Suite) SetupTest() {
suite.Addrs = addrs
evmGenesis := evmtypes.DefaultGenesisState()
evmGenesis.Params.EvmDenom = keeper.EvmDenom
evmGenesis.Params.EvmDenom = chaincfg.BaseDenom
feemarketGenesis := feemarkettypes.DefaultGenesisState()
feemarketGenesis.Params.EnableHeight = 1
feemarketGenesis.Params.NoBaseFee = false
cdc := suite.App.AppCodec()
coins := sdk.NewCoins(sdk.NewInt64Coin("ua0gi", 1000_000_000_000))
coins := sdk.NewCoins(sdk.NewInt64Coin(chaincfg.AuxiliaryDenom, 1000_000_000_000_000_000))
authGS := app.NewFundedGenStateWithSameCoins(cdc, coins, []sdk.AccAddress{
sdk.AccAddress(suite.Key1.PubKey().Address()),
sdk.AccAddress(suite.Key2.PubKey().Address()),
@ -185,28 +186,28 @@ func (suite *Suite) ModuleBalance(denom string) sdk.Int {
}
func (suite *Suite) FundAccountWithZgChain(addr sdk.AccAddress, coins sdk.Coins) {
ua0gi := coins.AmountOf("ua0gi")
if ua0gi.IsPositive() {
err := suite.App.FundAccount(suite.Ctx, addr, sdk.NewCoins(sdk.NewCoin("ua0gi", ua0gi)))
AuxiliaryDenomAmt := coins.AmountOf(chaincfg.AuxiliaryDenom)
if AuxiliaryDenomAmt.IsPositive() {
err := suite.App.FundAccount(suite.Ctx, addr, sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, AuxiliaryDenomAmt)))
suite.Require().NoError(err)
}
neuron := coins.AmountOf("neuron")
if neuron.IsPositive() {
err := suite.Keeper.SetBalance(suite.Ctx, addr, neuron)
baseDenomAmt := coins.AmountOf(chaincfg.BaseDenom)
if baseDenomAmt.IsPositive() {
err := suite.Keeper.SetBalance(suite.Ctx, addr, baseDenomAmt)
suite.Require().NoError(err)
}
}
func (suite *Suite) FundModuleAccountWithZgChain(moduleName string, coins sdk.Coins) {
ua0gi := coins.AmountOf("ua0gi")
if ua0gi.IsPositive() {
err := suite.App.FundModuleAccount(suite.Ctx, moduleName, sdk.NewCoins(sdk.NewCoin("ua0gi", ua0gi)))
AuxiliaryDenomAmt := coins.AmountOf(chaincfg.AuxiliaryDenom)
if AuxiliaryDenomAmt.IsPositive() {
err := suite.App.FundModuleAccount(suite.Ctx, moduleName, sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, AuxiliaryDenomAmt)))
suite.Require().NoError(err)
}
neuron := coins.AmountOf("neuron")
if neuron.IsPositive() {
baseDenomAmt := coins.AmountOf(chaincfg.BaseDenom)
if baseDenomAmt.IsPositive() {
addr := suite.AccountKeeper.GetModuleAddress(moduleName)
err := suite.Keeper.SetBalance(suite.Ctx, addr, neuron)
err := suite.Keeper.SetBalance(suite.Ctx, addr, baseDenomAmt)
suite.Require().NoError(err)
}
}
@ -217,7 +218,7 @@ func (suite *Suite) DeployERC20() types.InternalEVMAddress {
suite.App.FundModuleAccount(
suite.Ctx,
types.ModuleName,
sdk.NewCoins(sdk.NewCoin("ua0gi", sdkmath.NewInt(0))),
sdk.NewCoins(sdk.NewCoin(chaincfg.AuxiliaryDenom, sdkmath.NewInt(0))),
)
contractAddr, err := suite.Keeper.DeployTestMintableERC20Contract(suite.Ctx, "USDC", "USDC", uint8(18))
@ -318,7 +319,7 @@ func (suite *Suite) SendTx(
// Mint the max gas to the FeeCollector to ensure balance in case of refund
suite.MintFeeCollector(sdk.NewCoins(
sdk.NewCoin(
"ua0gi",
chaincfg.AuxiliaryDenom,
sdkmath.NewInt(baseFee.Int64()*int64(gasRes.Gas*2)),
)))

View File

@ -28,7 +28,7 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package
// allowed to be converted between ERC20 and sdk.Coin
type ConversionPair struct {
// ERC20 address of the token on the 0gChain EVM
ZgChainERC20Address HexBytes `protobuf:"bytes,1,opt,name=zgChain_erc20_address,json=zgChainErc20Address,proto3,casttype=HexBytes" json:"zgChain_erc20_address,omitempty"`
ZgChainERC20Address HexBytes `protobuf:"bytes,1,opt,name=zgchain_erc20_address,json=zgchainErc20Address,proto3,casttype=HexBytes" json:"zgchain_erc20_address,omitempty"`
// Denom of the corresponding sdk.Coin
Denom string `protobuf:"bytes,2,opt,name=denom,proto3" json:"denom,omitempty"`
}
@ -134,20 +134,20 @@ var fileDescriptor_6bad9d4ffa6874ec = []byte{
0x62, 0xd2, 0xd5, 0xa9, 0x08, 0x67, 0x2c, 0x92, 0x9e, 0x08, 0xed, 0x09, 0xf1, 0x22, 0x6d, 0x12,
0x89, 0x58, 0xa0, 0xda, 0x92, 0x53, 0xad, 0x40, 0xb5, 0x02, 0x6d, 0xd4, 0xb9, 0xe0, 0x22, 0xdb,
0xeb, 0xe9, 0x94, 0xa3, 0xed, 0x67, 0x00, 0x0f, 0xcc, 0x9d, 0xe4, 0x8e, 0x78, 0x11, 0x7a, 0x80,
0x87, 0x4b, 0x6e, 0x8e, 0x89, 0x17, 0xda, 0x2c, 0xa2, 0x3d, 0xc3, 0x26, 0xae, 0x1b, 0x31, 0x29,
0x55, 0xd0, 0x02, 0x9d, 0xea, 0xe0, 0x34, 0x59, 0x37, 0x6b, 0xa3, 0x1c, 0x18, 0x5a, 0x66, 0xcf,
0xe8, 0xe7, 0xeb, 0x9f, 0x75, 0xb3, 0x72, 0xc3, 0xe6, 0x83, 0x45, 0xcc, 0xa4, 0x55, 0x2b, 0x0c,
0xc3, 0x54, 0x50, 0x00, 0xa8, 0x0e, 0xff, 0xb9, 0x2c, 0x14, 0x81, 0xfa, 0xa7, 0x05, 0x3a, 0xff,
0xad, 0x3c, 0x5c, 0x95, 0x5e, 0xde, 0x9b, 0x4a, 0xfb, 0x15, 0xc0, 0xe3, 0xbe, 0xef, 0x8b, 0x27,
0xe6, 0x9a, 0x42, 0x06, 0x42, 0x9a, 0xa2, 0xd0, 0xdf, 0x8b, 0x47, 0x16, 0xa2, 0x13, 0x58, 0xa5,
0x59, 0x6f, 0xe7, 0x0a, 0x90, 0x29, 0xf6, 0xf2, 0xee, 0x3a, 0xad, 0x10, 0x82, 0xa5, 0x90, 0x04,
0xac, 0xb0, 0x67, 0x33, 0x3a, 0x82, 0x65, 0xb9, 0x08, 0x1c, 0xe1, 0xab, 0x7f, 0xb3, 0xb6, 0x48,
0xa8, 0x01, 0x2b, 0x2e, 0xa3, 0x5e, 0x40, 0x7c, 0xa9, 0x96, 0x5a, 0xa0, 0xb3, 0x6f, 0xed, 0x72,
0x7e, 0xa0, 0xc1, 0xed, 0xe6, 0x1b, 0x83, 0x8f, 0x04, 0x83, 0xcf, 0x04, 0x83, 0x55, 0x82, 0xc1,
0x26, 0xc1, 0xe0, 0x6d, 0x8b, 0x95, 0xd5, 0x16, 0x2b, 0x5f, 0x5b, 0xac, 0x8c, 0xce, 0xb9, 0x17,
0x8f, 0xa7, 0x8e, 0x46, 0x45, 0xa0, 0x1b, 0xdc, 0x27, 0x8e, 0xd4, 0x0d, 0x7e, 0x41, 0xd3, 0x6b,
0xeb, 0xf3, 0xdd, 0x4f, 0xc5, 0x8b, 0x09, 0x93, 0x4e, 0x39, 0x7b, 0xed, 0xcb, 0xdf, 0x00, 0x00,
0x00, 0xff, 0xff, 0x25, 0x71, 0x3e, 0xe1, 0xc5, 0x01, 0x00, 0x00,
0x87, 0x4b, 0x4e, 0xc7, 0xc4, 0x0b, 0x6d, 0x16, 0xd1, 0x9e, 0x61, 0x13, 0xd7, 0x8d, 0x98, 0x94,
0x2a, 0x68, 0x81, 0x4e, 0x75, 0x70, 0x9a, 0xac, 0x9b, 0xb5, 0x11, 0x37, 0x53, 0x60, 0x68, 0x99,
0x3d, 0xa3, 0x9f, 0xaf, 0x7f, 0xd6, 0xcd, 0xca, 0x0d, 0x9b, 0x0f, 0x16, 0x31, 0x93, 0x56, 0xad,
0x30, 0x0c, 0x53, 0x41, 0x01, 0xa0, 0x3a, 0xfc, 0xe7, 0xb2, 0x50, 0x04, 0xea, 0x9f, 0x16, 0xe8,
0xfc, 0xb7, 0xf2, 0x70, 0x55, 0x7a, 0x79, 0x6f, 0x2a, 0xed, 0x57, 0x00, 0x8f, 0xfb, 0xbe, 0x2f,
0x9e, 0x98, 0x6b, 0x0a, 0x19, 0x08, 0x69, 0x8a, 0x42, 0x7f, 0x2f, 0x1e, 0x59, 0x88, 0x4e, 0x60,
0x95, 0x66, 0xbd, 0x9d, 0x2b, 0x40, 0xa6, 0xd8, 0xcb, 0xbb, 0xeb, 0xb4, 0x42, 0x08, 0x96, 0x42,
0x12, 0xb0, 0xc2, 0x9e, 0xcd, 0xe8, 0x08, 0x96, 0xe5, 0x22, 0x70, 0x84, 0xaf, 0xfe, 0xcd, 0xda,
0x22, 0xa1, 0x06, 0xac, 0xb8, 0x8c, 0x7a, 0x01, 0xf1, 0xa5, 0x5a, 0x6a, 0x81, 0xce, 0xbe, 0xb5,
0xcb, 0xf9, 0x81, 0x06, 0xb7, 0x9b, 0x6f, 0x0c, 0x3e, 0x12, 0x0c, 0x3e, 0x13, 0x0c, 0x56, 0x09,
0x06, 0x9b, 0x04, 0x83, 0xb7, 0x2d, 0x56, 0x56, 0x5b, 0xac, 0x7c, 0x6d, 0xb1, 0x32, 0x3a, 0xe7,
0x5e, 0x3c, 0x9e, 0x3a, 0x1a, 0x15, 0x81, 0x6e, 0x70, 0x9f, 0x38, 0x52, 0x37, 0xf8, 0x45, 0x76,
0x6d, 0x7d, 0xbe, 0xfb, 0xa9, 0x78, 0x31, 0x61, 0xd2, 0x29, 0x67, 0xaf, 0x7d, 0xf9, 0x1b, 0x00,
0x00, 0xff, 0xff, 0xfa, 0x07, 0x29, 0xab, 0xc5, 0x01, 0x00, 0x00,
}
func (this *ConversionPair) VerboseEqual(that interface{}) error {

View File

@ -3,6 +3,7 @@ package types_test
import (
"testing"
"github.com/0glabs/0g-chain/chaincfg"
"github.com/0glabs/0g-chain/x/evmutil/testutil"
"github.com/0glabs/0g-chain/x/evmutil/types"
"github.com/stretchr/testify/require"
@ -142,7 +143,7 @@ func TestConversionPairs_Validate(t *testing.T) {
),
types.NewConversionPair(
testutil.MustNewInternalEVMAddressFromString("0x000000000000000000000000000000000000000A"),
"a0gi",
chaincfg.AuxiliaryDenom,
),
types.NewConversionPair(
testutil.MustNewInternalEVMAddressFromString("0x000000000000000000000000000000000000000B"),
@ -162,7 +163,7 @@ func TestConversionPairs_Validate(t *testing.T) {
),
types.NewConversionPair(
testutil.MustNewInternalEVMAddressFromString("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"),
"a0gi",
chaincfg.AuxiliaryDenom,
),
types.NewConversionPair(
testutil.MustNewInternalEVMAddressFromString("0x000000000000000000000000000000000000000B"),
@ -183,16 +184,16 @@ func TestConversionPairs_Validate(t *testing.T) {
),
types.NewConversionPair(
testutil.MustNewInternalEVMAddressFromString("0x000000000000000000000000000000000000000A"),
"a0gi",
chaincfg.AuxiliaryDenom,
),
types.NewConversionPair(
testutil.MustNewInternalEVMAddressFromString("0x000000000000000000000000000000000000000B"),
"a0gi",
chaincfg.AuxiliaryDenom,
),
),
errArgs{
expectPass: false,
contains: "found duplicate enabled conversion pair denom a0gi",
contains: "found duplicate enabled conversion pair denom " + chaincfg.AuxiliaryDenom,
},
},
{
@ -208,7 +209,7 @@ func TestConversionPairs_Validate(t *testing.T) {
),
types.NewConversionPair(
testutil.MustNewInternalEVMAddressFromString("0x000000000000000000000000000000000000000B"),
"a0gi",
chaincfg.AuxiliaryDenom,
),
),
errArgs{
@ -240,12 +241,12 @@ func TestAllowedCosmosCoinERC20Token_Validate(t *testing.T) {
}{
{
name: "valid token",
token: types.NewAllowedCosmosCoinERC20Token("uatom", "0g-wrapped ATOM", "kATOM", 6),
token: types.NewAllowedCosmosCoinERC20Token("uatom", "0gChain-wrapped ATOM", "kATOM", 6),
expErr: "",
},
{
name: "valid - highest allowed decimals",
token: types.NewAllowedCosmosCoinERC20Token("uatom", "0g-wrapped ATOM", "kATOM", 255),
token: types.NewAllowedCosmosCoinERC20Token("uatom", "0gChain-wrapped ATOM", "kATOM", 255),
expErr: "",
},
{
@ -280,7 +281,7 @@ func TestAllowedCosmosCoinERC20Token_Validate(t *testing.T) {
},
{
name: "invalid - decimals higher than uint8",
token: types.NewAllowedCosmosCoinERC20Token("uatom", "0g-wrapped ATOM", "kATOM", 256),
token: types.NewAllowedCosmosCoinERC20Token("uatom", "0gChain-wrapped ATOM", "kATOM", 256),
expErr: "decimals must be less than 256",
},
}

View File

@ -107,11 +107,11 @@ func (suite *ParamsTestSuite) TestParams_Validate() {
invalidConversionPairs := types.NewConversionPairs(
types.NewConversionPair(
testutil.MustNewInternalEVMAddressFromString("0x000000000000000000000000000000000000000A"),
"a0gi",
chaincfg.AuxiliaryDenom,
),
types.NewConversionPair(
testutil.MustNewInternalEVMAddressFromString("0x000000000000000000000000000000000000000B"),
"a0gi", // duplicate denom!
chaincfg.AuxiliaryDenom, // duplicate denom!
),
)
validAllowedCosmosDenoms := types.NewAllowedCosmosCoinERC20Tokens(

View File

@ -139,7 +139,7 @@ type MsgConvertERC20ToCoin struct {
// 0gChain bech32 address that will receive the converted sdk.Coin.
Receiver string `protobuf:"bytes,2,opt,name=receiver,proto3" json:"receiver,omitempty"`
// EVM 0x hex address of the ERC20 contract.
ZgChainERC20Address string `protobuf:"bytes,3,opt,name=zgChain_erc20_address,json=zgChainErc20Address,proto3" json:"zgChain_erc20_address,omitempty"`
ZgChainERC20Address string `protobuf:"bytes,3,opt,name=zgchain_erc20_address,json=zgchainErc20Address,proto3" json:"zgchain_erc20_address,omitempty"`
// ERC20 token amount to convert.
Amount github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,4,opt,name=amount,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"amount"`
}
@ -452,7 +452,7 @@ func init() {
func init() { proto.RegisterFile("zgc/evmutil/v1beta1/tx.proto", fileDescriptor_b60fa1a7a6ac0cc3) }
var fileDescriptor_b60fa1a7a6ac0cc3 = []byte{
// 563 bytes of a gzipped FileDescriptorProto
// 564 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xc4, 0x55, 0xc1, 0x6e, 0xd3, 0x30,
0x18, 0xae, 0xb7, 0x69, 0xa2, 0xe6, 0x96, 0x6e, 0xa2, 0x0b, 0x23, 0x9d, 0x8a, 0x06, 0xd3, 0xa4,
0x26, 0x69, 0x40, 0x08, 0x21, 0x2e, 0xb4, 0x1a, 0xd2, 0x04, 0xbb, 0x84, 0x9e, 0x76, 0xa9, 0x92,
@ -471,24 +471,24 @@ var fileDescriptor_b60fa1a7a6ac0cc3 = []byte{
0x6c, 0xdd, 0xb5, 0xd6, 0x74, 0x99, 0x2d, 0xf2, 0x93, 0x9a, 0xd4, 0x23, 0x15, 0xb6, 0x04, 0x36,
0x1b, 0xf0, 0x41, 0xa1, 0x3e, 0x1b, 0xf1, 0x21, 0xa3, 0x1c, 0x35, 0xbf, 0x2c, 0xe4, 0x1d, 0xc4,
0xb1, 0x1e, 0x8b, 0x80, 0xca, 0xfa, 0x5f, 0x0e, 0xf2, 0x3a, 0x9f, 0x5e, 0xd5, 0x59, 0x62, 0xef,
0xd2, 0xc1, 0x1b, 0xb8, 0x3a, 0xc1, 0xdd, 0x03, 0x87, 0xd0, 0x3e, 0x0a, 0x3c, 0xcb, 0xec, 0x3b,
0x09, 0x30, 0x36, 0x54, 0xed, 0xdc, 0x0b, 0xa7, 0x8d, 0xda, 0x7e, 0x02, 0x88, 0xa5, 0xc8, 0x3c,
0x76, 0x4d, 0xb2, 0x76, 0x22, 0x92, 0xdc, 0x54, 0x7a, 0x59, 0x39, 0x96, 0x62, 0xf6, 0xcb, 0xd3,
0x69, 0xa3, 0xf2, 0x73, 0xda, 0x78, 0x84, 0x89, 0x38, 0x18, 0xb9, 0xba, 0xc7, 0x7c, 0x79, 0x87,
0xf2, 0xa7, 0xc5, 0x07, 0xef, 0x0d, 0xf1, 0x61, 0x88, 0xb8, 0xbe, 0x4b, 0xc5, 0xf9, 0x49, 0x0b,
0x4a, 0xb9, 0xbb, 0x54, 0x14, 0x57, 0x2c, 0x57, 0x8f, 0xac, 0x62, 0x9f, 0x01, 0xbc, 0x9f, 0xaf,
0x69, 0x94, 0x21, 0x7f, 0xf3, 0xe5, 0x75, 0xfb, 0xcf, 0xf7, 0xbb, 0x09, 0x1f, 0x96, 0x68, 0xc9,
0x34, 0x1f, 0x83, 0x3f, 0xfb, 0x20, 0xc5, 0xbd, 0x0e, 0x98, 0x7f, 0x0b, 0xaa, 0x1f, 0xc3, 0xcd,
0x52, 0x35, 0xa9, 0x6e, 0xeb, 0xd3, 0x12, 0x5c, 0xdc, 0xe3, 0x58, 0x11, 0x50, 0x29, 0x98, 0xb1,
0x6d, 0xbd, 0x60, 0xc8, 0xf5, 0xc2, 0x7e, 0x57, 0xad, 0xeb, 0x63, 0xd3, 0xd3, 0x73, 0xa7, 0xe6,
0xe7, 0x62, 0xde, 0xa9, 0x39, 0xec, 0xdc, 0x53, 0x0b, 0xfa, 0x4b, 0xf9, 0x08, 0x60, 0xfd, 0x9f,
0xcd, 0x65, 0xce, 0xb5, 0x71, 0x85, 0xa1, 0x3e, 0xbf, 0x29, 0x23, 0x13, 0x72, 0x0c, 0xa0, 0x5a,
0xd2, 0x31, 0xd6, 0xb5, 0x13, 0x67, 0x1c, 0xf5, 0xc5, 0xcd, 0x39, 0xa9, 0x9c, 0xce, 0xdb, 0x8b,
0x5f, 0x1a, 0xf8, 0x1e, 0x6a, 0xe0, 0x34, 0xd4, 0xc0, 0x59, 0xa8, 0x81, 0x8b, 0x50, 0x03, 0x5f,
0x67, 0x5a, 0xe5, 0x6c, 0xa6, 0x55, 0x7e, 0xcc, 0xb4, 0xca, 0xfe, 0x76, 0x6e, 0xf0, 0x4d, 0x7c,
0xe8, 0xb8, 0xdc, 0x30, 0x71, 0xcb, 0x8b, 0x1e, 0x0e, 0xe3, 0x28, 0xfb, 0x54, 0xc4, 0x0f, 0x80,
0xbb, 0x1c, 0x3f, 0xe0, 0x4f, 0x7e, 0x07, 0x00, 0x00, 0xff, 0xff, 0x6e, 0x5d, 0x0b, 0x0a, 0x46,
0x06, 0x00, 0x00,
0xd2, 0xc1, 0x1b, 0xb8, 0x3a, 0xc1, 0xde, 0x81, 0x43, 0x68, 0x1f, 0x05, 0x9e, 0x65, 0xf6, 0x9d,
0x04, 0x18, 0x1b, 0xaa, 0x76, 0xee, 0x85, 0xd3, 0x46, 0x6d, 0x1f, 0x77, 0x23, 0x40, 0x2c, 0x45,
0xe6, 0xb1, 0x6b, 0x92, 0xb5, 0x13, 0x91, 0xe4, 0xa6, 0xd2, 0xcb, 0xca, 0xb1, 0x14, 0xb3, 0x5f,
0x9e, 0x4e, 0x1b, 0x95, 0x9f, 0xd3, 0xc6, 0x23, 0x4c, 0xc4, 0xc1, 0xc8, 0xd5, 0x3d, 0xe6, 0xcb,
0x3b, 0x94, 0x3f, 0x2d, 0x3e, 0x78, 0x6f, 0x88, 0x0f, 0x43, 0xc4, 0xf5, 0x5d, 0x2a, 0xce, 0x4f,
0x5a, 0x50, 0xca, 0xdd, 0xa5, 0xa2, 0xb8, 0x62, 0xb9, 0x7a, 0x64, 0x15, 0xfb, 0x0c, 0xe0, 0xfd,
0x7c, 0x4d, 0xa3, 0x0c, 0xf9, 0x9b, 0x2f, 0xaf, 0xdb, 0x7f, 0xbe, 0xdf, 0x4d, 0xf8, 0xb0, 0x44,
0x4b, 0xa6, 0xf9, 0x18, 0xfc, 0xd9, 0x07, 0x29, 0xee, 0x75, 0xc0, 0xfc, 0x5b, 0x50, 0xfd, 0x18,
0x6e, 0x96, 0xaa, 0x49, 0x75, 0x5b, 0x9f, 0x96, 0xe0, 0xe2, 0x1e, 0xc7, 0x8a, 0x80, 0x4a, 0xc1,
0x8c, 0x6d, 0xeb, 0x05, 0x43, 0xae, 0x17, 0xf6, 0xbb, 0x6a, 0x5d, 0x1f, 0x9b, 0x9e, 0x9e, 0x3b,
0x35, 0x3f, 0x17, 0xf3, 0x4e, 0xcd, 0x61, 0xe7, 0x9e, 0x5a, 0xd0, 0x5f, 0xca, 0x47, 0x00, 0xeb,
0xff, 0x6c, 0x2e, 0x73, 0xae, 0x8d, 0x2b, 0x0c, 0xf5, 0xf9, 0x4d, 0x19, 0x99, 0x90, 0x63, 0x00,
0xd5, 0x92, 0x8e, 0xb1, 0xae, 0x9d, 0x38, 0xe3, 0xa8, 0x2f, 0x6e, 0xce, 0x49, 0xe5, 0x74, 0xde,
0x5e, 0xfc, 0xd2, 0xc0, 0xf7, 0x50, 0x03, 0xa7, 0xa1, 0x06, 0xce, 0x42, 0x0d, 0x5c, 0x84, 0x1a,
0xf8, 0x3a, 0xd3, 0x2a, 0x67, 0x33, 0xad, 0xf2, 0x63, 0xa6, 0x55, 0xf6, 0xb7, 0x73, 0x83, 0x6f,
0xe2, 0x43, 0xc7, 0xe5, 0x86, 0x89, 0x5b, 0xf1, 0xc3, 0x61, 0x1c, 0x65, 0x9f, 0x8a, 0xf8, 0x01,
0x70, 0x97, 0xe3, 0x07, 0xfc, 0xc9, 0xef, 0x00, 0x00, 0x00, 0xff, 0xff, 0x2f, 0xc2, 0xb2, 0xda,
0x46, 0x06, 0x00, 0x00,
}
func (this *MsgConvertCoinToERC20) VerboseEqual(that interface{}) error {

View File

@ -3,13 +3,14 @@ package types
import (
"testing"
"github.com/0glabs/0g-chain/chaincfg"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"
)
func TestRawPriceKey_Iteration(t *testing.T) {
// An iterator key should only match price keys with the same market
iteratorKey := RawPriceIteratorKey("a0gi:usd")
iteratorKey := RawPriceIteratorKey(chaincfg.AuxiliaryDenom + ":usd")
addr := sdk.AccAddress("test addr")
@ -20,12 +21,12 @@ func TestRawPriceKey_Iteration(t *testing.T) {
}{
{
name: "equal marketID is included in iteration",
priceKey: RawPriceKey("a0gi:usd", addr),
priceKey: RawPriceKey(chaincfg.AuxiliaryDenom+":usd", addr),
expectErr: false,
},
{
name: "prefix overlapping marketID excluded from iteration",
priceKey: RawPriceKey("a0gi:usd:30", addr),
priceKey: RawPriceKey(chaincfg.AuxiliaryDenom+":usd:30", addr),
expectErr: true,
},
}