Skip to content

Commit a65af36

Browse files
committed
[EVM] call SendCoinsAndWei to support 18-decimal point precision
1 parent 0580149 commit a65af36

File tree

9 files changed

+90
-39
lines changed

9 files changed

+90
-39
lines changed

.github/workflows/compatiblity_check.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ jobs:
1717

1818
- name: Check Latest Dependencies
1919
run: |
20-
git clone https://github.com/sei-protocol/sei-cosmos.git
21-
git clone https://github.com/sei-protocol/sei-tendermint.git
20+
git clone -b evm-temp https://github.com/sei-protocol/sei-cosmos.git
21+
git clone -b evm-temp https://github.com/sei-protocol/sei-tendermint.git
2222
git clone https://github.com/sei-protocol/sei-iavl.git
2323
git clone https://github.com/sei-protocol/go-ethereum.git
2424
go mod edit -replace github.com/cosmos/iavl=./sei-iavl

app/app.go

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ var (
223223
evmtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
224224
dexmoduletypes.ModuleName: nil,
225225
tokenfactorytypes.ModuleName: {authtypes.Minter, authtypes.Burner},
226+
banktypes.WeiEscrowName: nil,
226227
// this line is used by starport scaffolding # stargate/app/maccPerms
227228
}
228229

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ require (
322322
replace (
323323
github.com/CosmWasm/wasmd => github.com/sei-protocol/sei-wasmd v0.0.2
324324
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
325-
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.2.66-evm-11
325+
github.com/cosmos/cosmos-sdk => github.com/sei-protocol/sei-cosmos v0.2.66-evm-12
326326
github.com/cosmos/iavl => github.com/sei-protocol/sei-iavl v0.1.7
327327
github.com/cosmos/ibc-go/v3 => github.com/sei-protocol/sei-ibc-go/v3 v3.2.0
328328
github.com/ethereum/go-ethereum => github.com/sei-protocol/go-ethereum v1.13.5-sei

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -1325,8 +1325,8 @@ github.com/sei-protocol/go-ethereum v1.13.5-sei h1:050SdAmqc3JWULg69daOXaVRvowwp
13251325
github.com/sei-protocol/go-ethereum v1.13.5-sei/go.mod h1:kcRZmuzRn1lVejiFNTz4l4W7imnpq1bDAnuKS/RyhbQ=
13261326
github.com/sei-protocol/goutils v0.0.2 h1:Bfa7Sv+4CVLNM20QcpvGb81B8C5HkQC/kW1CQpIbXDA=
13271327
github.com/sei-protocol/goutils v0.0.2/go.mod h1:iYE2DuJfEnM+APPehr2gOUXfuLuPsVxorcDO+Tzq9q8=
1328-
github.com/sei-protocol/sei-cosmos v0.2.66-evm-11 h1:IZUtYzhdFbMaE9H4VGQBjcYEQSrARGfeizDp0wvCECw=
1329-
github.com/sei-protocol/sei-cosmos v0.2.66-evm-11/go.mod h1:FrJ75cMf1+Mx/AVWGzpcrCtvBoXd1wurJSRHCtP0cvQ=
1328+
github.com/sei-protocol/sei-cosmos v0.2.66-evm-12 h1:gBT79tL0sRKn/tIRxONNTfe2Gpqlf1L7QU1MErnY/ms=
1329+
github.com/sei-protocol/sei-cosmos v0.2.66-evm-12/go.mod h1:FrJ75cMf1+Mx/AVWGzpcrCtvBoXd1wurJSRHCtP0cvQ=
13301330
github.com/sei-protocol/sei-iavl v0.1.7 h1:cUdHDBkxs0FF/kOt1qCVLm0K+Bqaw92/dbZSgn4kxiA=
13311331
github.com/sei-protocol/sei-iavl v0.1.7/go.mod h1:7PfkEVT5dcoQE+s/9KWdoXJ8VVVP1QpYYPLdxlkSXFk=
13321332
github.com/sei-protocol/sei-ibc-go/v3 v3.2.0 h1:T8V75OEWKvYDraPZZKilprl7ZkahZYGo40crxNL4unc=

x/evm/client/cli/tx.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
ethtypes "github.com/ethereum/go-ethereum/core/types"
2828
"github.com/sei-protocol/sei-chain/evmrpc"
2929
"github.com/sei-protocol/sei-chain/x/evm/artifacts"
30-
"github.com/sei-protocol/sei-chain/x/evm/state"
3130
"github.com/sei-protocol/sei-chain/x/evm/types"
3231
"github.com/sei-protocol/sei-chain/x/evm/types/ethtx"
3332
)
@@ -138,7 +137,7 @@ func CmdAssociateAddress() *cobra.Command {
138137

139138
func CmdSend() *cobra.Command {
140139
cmd := &cobra.Command{
141-
Use: "send [to EVM address] [amount in usei] --from=<sender> --gas-fee-cap=<cap> --gas-limit=<limit> --evm-chain-id=<chain-id> --evm-rpc=<url>",
140+
Use: "send [to EVM address] [amount in wei] --from=<sender> --gas-fee-cap=<cap> --gas-limit=<limit> --evm-chain-id=<chain-id> --evm-rpc=<url>",
142141
Short: "send usei to EVM address",
143142
Long: "",
144143
Args: cobra.ExactArgs(2),
@@ -193,9 +192,9 @@ func CmdSend() *cobra.Command {
193192
}
194193

195194
to := common.HexToAddress(args[0])
196-
val, err := strconv.ParseUint(args[1], 10, 64)
197-
if err != nil {
198-
return err
195+
val, success := new(big.Int).SetString(args[1], 10)
196+
if !success {
197+
return fmt.Errorf("%s is an invalid amount to send", args[1])
199198
}
200199
gasFeeCap, err := cmd.Flags().GetUint64(FlagGasFeeCap)
201200
if err != nil {
@@ -214,7 +213,7 @@ func CmdSend() *cobra.Command {
214213
GasFeeCap: new(big.Int).SetUint64(gasFeeCap),
215214
Gas: gasLimit,
216215
To: &to,
217-
Value: new(big.Int).Mul(new(big.Int).SetUint64(val), state.UseiToSweiMultiplier),
216+
Value: val,
218217
Data: []byte(""),
219218
ChainID: new(big.Int).SetUint64(chainID),
220219
}

x/evm/module.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -159,18 +159,21 @@ func (am AppModule) BeginBlock(sdk.Context, abci.RequestBeginBlock) {
159159
// returns no validator updates.
160160
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
161161
evmTxIndices := am.keeper.GetEVMTxIndices()
162+
denom := am.keeper.GetBaseDenom(ctx)
162163
for _, idx := range evmTxIndices {
163164
middleManAddress := state.GetMiddleManAddress(sdk.Context{}.WithTxIndex(idx))
164-
balances := am.keeper.BankKeeper().GetAllBalances(ctx, middleManAddress)
165-
if !balances.IsZero() {
166-
if err := am.keeper.BankKeeper().SendCoinsFromAccountToModule(ctx, middleManAddress, types.ModuleName, balances); err != nil {
165+
balance := am.keeper.BankKeeper().GetBalance(ctx, middleManAddress, denom)
166+
weiBalance := am.keeper.BankKeeper().GetWeiBalance(ctx, middleManAddress)
167+
if !balance.Amount.IsZero() || !weiBalance.IsZero() {
168+
if err := am.keeper.BankKeeper().SendCoinsAndWei(ctx, middleManAddress, am.keeper.AccountKeeper().GetModuleAddress(types.ModuleName), nil, denom, balance.Amount, weiBalance); err != nil {
167169
panic(err)
168170
}
169171
}
170172
coinbaseAddress := state.GetCoinbaseAddress(sdk.Context{}.WithTxIndex(idx))
171-
balances = am.keeper.BankKeeper().GetAllBalances(ctx, coinbaseAddress)
172-
if !balances.IsZero() {
173-
if err := am.keeper.BankKeeper().SendCoinsFromAccountToModule(ctx, coinbaseAddress, authtypes.FeeCollectorName, balances); err != nil {
173+
balance = am.keeper.BankKeeper().GetBalance(ctx, coinbaseAddress, denom)
174+
weiBalance = am.keeper.BankKeeper().GetWeiBalance(ctx, coinbaseAddress)
175+
if !balance.Amount.IsZero() || !weiBalance.IsZero() {
176+
if err := am.keeper.BankKeeper().SendCoinsAndWei(ctx, coinbaseAddress, am.keeper.AccountKeeper().GetModuleAddress(authtypes.FeeCollectorName), nil, denom, balance.Amount, weiBalance); err != nil {
174177
panic(err)
175178
}
176179
}

x/evm/state/balance.go

+17-22
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func (s *DBImpl) SubBalance(evmAddr common.Address, amt *big.Int) {
1717
return
1818
}
1919

20-
s.err = s.k.BankKeeper().SendCoinsWithoutAccCreation(s.ctx, s.getSeiAddress(evmAddr), s.middleManAddress, s.bigIntAmtToCoins(amt))
20+
s.send(s.getSeiAddress(evmAddr), s.middleManAddress, amt)
2121
}
2222

2323
func (s *DBImpl) AddBalance(evmAddr common.Address, amt *big.Int) {
@@ -29,32 +29,34 @@ func (s *DBImpl) AddBalance(evmAddr common.Address, amt *big.Int) {
2929
return
3030
}
3131

32-
s.err = s.k.BankKeeper().SendCoinsWithoutAccCreation(s.ctx, s.middleManAddress, s.getSeiAddress(evmAddr), s.bigIntAmtToCoins(amt))
32+
s.send(s.middleManAddress, s.getSeiAddress(evmAddr), amt)
3333
}
3434

3535
func (s *DBImpl) GetBalance(evmAddr common.Address) *big.Int {
36-
return s.coinToBigIntAmt(s.k.BankKeeper().GetBalance(s.ctx, s.getSeiAddress(evmAddr), s.k.GetBaseDenom(s.ctx)))
36+
usei := s.k.BankKeeper().GetBalance(s.ctx, s.getSeiAddress(evmAddr), s.k.GetBaseDenom(s.ctx)).Amount
37+
wei := s.k.BankKeeper().GetWeiBalance(s.ctx, s.getSeiAddress(evmAddr))
38+
return usei.Mul(sdk.NewIntFromBigInt(UseiToSweiMultiplier)).Add(wei).BigInt()
3739
}
3840

3941
// should only be called during simulation
4042
func (s *DBImpl) SetBalance(evmAddr common.Address, amt *big.Int) {
4143
if !s.simulation {
4244
panic("should never call SetBalance in a non-simulation setting")
4345
}
44-
// Fields that were denominated in usei will be converted to swei (1usei = 10^12swei)
45-
// for existing Ethereum application (which assumes 18 decimal points) to display properly.
46-
amt = new(big.Int).Quo(amt, UseiToSweiMultiplier)
4746
seiAddr := s.getSeiAddress(evmAddr)
48-
balance := s.k.BankKeeper().GetBalance(s.ctx, seiAddr, s.k.GetBaseDenom(s.ctx))
49-
if err := s.k.BankKeeper().SendCoinsFromAccountToModule(s.ctx, seiAddr, types.ModuleName, sdk.NewCoins(balance)); err != nil {
50-
panic(err)
47+
moduleAddr := s.k.AccountKeeper().GetModuleAddress(types.ModuleName)
48+
s.send(seiAddr, moduleAddr, s.GetBalance(evmAddr))
49+
if s.err != nil {
50+
panic(s.err)
5151
}
52-
coinsAmt := sdk.NewCoins(sdk.NewCoin(s.k.GetBaseDenom(s.ctx), sdk.NewIntFromBigInt(amt)))
52+
usei, _ := SplitUseiWeiAmount(amt)
53+
coinsAmt := sdk.NewCoins(sdk.NewCoin(s.k.GetBaseDenom(s.ctx), sdk.NewIntFromBigInt(usei).Add(sdk.OneInt())))
5354
if err := s.k.BankKeeper().MintCoins(s.ctx, types.ModuleName, coinsAmt); err != nil {
5455
panic(err)
5556
}
56-
if err := s.k.BankKeeper().SendCoinsFromModuleToAccount(s.ctx, types.ModuleName, seiAddr, coinsAmt); err != nil {
57-
panic(err)
57+
s.send(moduleAddr, seiAddr, amt)
58+
if s.err != nil {
59+
panic(s.err)
5860
}
5961
}
6062

@@ -69,14 +71,7 @@ func (s *DBImpl) getSeiAddress(evmAddr common.Address) (seiAddr sdk.AccAddress)
6971
return
7072
}
7173

72-
func (s *DBImpl) bigIntAmtToCoins(amt *big.Int) sdk.Coins {
73-
// Fields that were denominated in usei will be converted to swei (1usei = 10^12swei)
74-
// for existing Ethereum application (which assumes 18 decimal points) to display properly.
75-
amt = new(big.Int).Quo(amt, UseiToSweiMultiplier)
76-
return sdk.NewCoins(sdk.NewCoin(s.k.GetBaseDenom(s.ctx), sdk.NewIntFromBigInt(amt)))
77-
}
78-
79-
func (s *DBImpl) coinToBigIntAmt(coin sdk.Coin) *big.Int {
80-
balanceInUsei := coin.Amount.BigInt()
81-
return new(big.Int).Mul(balanceInUsei, UseiToSweiMultiplier)
74+
func (s *DBImpl) send(from sdk.AccAddress, to sdk.AccAddress, amt *big.Int) {
75+
usei, wei := SplitUseiWeiAmount(amt)
76+
s.err = s.k.BankKeeper().SendCoinsAndWei(s.ctx, from, to, nil, s.k.GetBaseDenom(s.ctx), sdk.NewIntFromBigInt(usei), sdk.NewIntFromBigInt(wei))
8277
}

x/evm/state/utils.go

+6
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ func GetCoinbaseAddress(ctx sdk.Context) sdk.AccAddress {
2525
binary.BigEndian.PutUint64(txIndexBz, uint64(ctx.TxIndex()))
2626
return sdk.AccAddress(append(CoinbaseAddressPrefix, txIndexBz...))
2727
}
28+
29+
func SplitUseiWeiAmount(amt *big.Int) (usei *big.Int, wei *big.Int) {
30+
wei = new(big.Int).Mod(amt, UseiToSweiMultiplier)
31+
usei = new(big.Int).Quo(amt, UseiToSweiMultiplier)
32+
return
33+
}

x/evm/state/utils_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package state_test
2+
3+
import (
4+
"math/big"
5+
"testing"
6+
7+
"github.com/sei-protocol/sei-chain/x/evm/state"
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestSplitUseiWeiAmount(t *testing.T) {
12+
for _, test := range []struct {
13+
amt *big.Int
14+
expectedSei *big.Int
15+
expectedWei *big.Int
16+
}{
17+
{
18+
amt: big.NewInt(0),
19+
expectedSei: big.NewInt(0),
20+
expectedWei: big.NewInt(0),
21+
}, {
22+
amt: big.NewInt(1),
23+
expectedSei: big.NewInt(0),
24+
expectedWei: big.NewInt(1),
25+
}, {
26+
amt: big.NewInt(999_999_999_999),
27+
expectedSei: big.NewInt(0),
28+
expectedWei: big.NewInt(999_999_999_999),
29+
}, {
30+
amt: big.NewInt(1_000_000_000_000),
31+
expectedSei: big.NewInt(1),
32+
expectedWei: big.NewInt(0),
33+
}, {
34+
amt: big.NewInt(1_000_000_000_001),
35+
expectedSei: big.NewInt(1),
36+
expectedWei: big.NewInt(1),
37+
}, {
38+
amt: big.NewInt(123_456_789_123_456_789),
39+
expectedSei: big.NewInt(123456),
40+
expectedWei: big.NewInt(789_123_456_789),
41+
},
42+
} {
43+
usei, wei := state.SplitUseiWeiAmount(test.amt)
44+
require.Equal(t, test.expectedSei, usei)
45+
require.Equal(t, test.expectedWei, wei)
46+
}
47+
}

0 commit comments

Comments
 (0)