Skip to content

Commit

Permalink
Merge pull request #1100 from mmsqe/release/v4_fix_liquid
Browse files Browse the repository at this point in the history
Problem: negative coin amount error when query supply liquid of non BaseCoinUnit(backport: #1099)
  • Loading branch information
mmsqe authored Dec 31, 2024
2 parents 58038fc + 0702b81 commit ebfb62d
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## UNRELEASED

### Bug Fixes

- [#1099](https://github.com/crypto-org-chain/chain-main/pull/1099) Avoid negative coin amount error when query supply liquid of non BaseCoinUnit.

*Dec 18, 2024*

## v4.2.10
Expand Down
45 changes: 38 additions & 7 deletions integration_tests/test_ibc_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ def cluster(worker_index, pytestconfig, tmp_path_factory):
)


def fund_community_pool(self, amt, **kwargs):
return json.loads(
self.raw(
"tx",
"distribution",
"fund-community-pool",
amt,
"-y",
home=self.data_dir,
keyring_backend="test",
chain_id=self.chain_id,
node=self.node_rpc,
**kwargs,
)
)


# 3 accounts ibc test
# ibc-0 (A,B) ibc-1 (C)
# EscrowAddress: sha256-hash of ibc-version,port-id,channel-id
Expand All @@ -34,6 +51,8 @@ def cluster(worker_index, pytestconfig, tmp_path_factory):

def test_ibc_extended(cluster):
src_channel, dst_channel = start_and_wait_relayer(cluster)
denom = "basecro"
amt = 10000
raw = cluster["ibc-0"].cosmos_cli().raw

addr_1 = cluster["ibc-1"].address("relayer")
Expand All @@ -55,17 +74,17 @@ def test_ibc_extended(cluster):
)
)
denom_hash = hashlib.sha256(denom_string.encode()).hexdigest().upper()
ibc_denom = f"ibc/{denom_hash}"
assert rsp["code"] == 0, rsp["raw_log"]
assert res["balances"] == [
{"denom": "basecro", "amount": "10000000000"},
{
"denom": f"ibc/{denom_hash}",
"amount": "10000",
},
{"denom": denom, "amount": "10000000000"},
{"denom": ibc_denom, "amount": f"{amt}"},
]

amt2 = 55
# send B <- C
rsp = cluster["ibc-1"].ibc_transfer(
"relayer", addr_0_signer, f"55ibc/{denom_hash}", dst_channel, 0
"relayer", addr_0_signer, f"{amt2}{ibc_denom}", dst_channel, 0
)
assert rsp["code"] == 0, rsp["raw_log"]
time.sleep(10)
Expand All @@ -79,4 +98,16 @@ def test_ibc_extended(cluster):
node=cluster["ibc-0"].node_rpc(0),
)
)
assert res["balances"] == [{"denom": "basecro", "amount": "20000000055"}]
assert res["balances"] == [{"denom": denom, "amount": "20000000055"}]

amt3 = 1
fund_community_pool(
cluster["ibc-1"].cosmos_cli(), f"{amt3}{ibc_denom}", from_=addr_1
)
time.sleep(10)
assert cluster["ibc-1"].supply("liquid") == {
"supply": [
{"denom": denom, "amount": "30000000000"},
{"denom": ibc_denom, "amount": f"{amt - amt2 - amt3}"},
]
}
8 changes: 6 additions & 2 deletions x/supply/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/crypto-org-chain/chain-main/v4/config"
"github.com/crypto-org-chain/chain-main/v4/x/supply/types"

authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
Expand Down Expand Up @@ -94,7 +93,12 @@ func (k Keeper) GetVestingAccounts(ctx sdk.Context) types.VestingAccounts {

// GetTotalSupply returns the current total supply in the system
func (k Keeper) GetTotalSupply(ctx sdk.Context) sdk.Coins {
return sdk.NewCoins(k.bankKeeper.GetSupply(ctx, config.BaseCoinUnit))
var totalSupply sdk.Coins
k.bankKeeper.IterateTotalSupply(ctx, func(coin sdk.Coin) bool {
totalSupply = totalSupply.Add(coin)
return false
})
return totalSupply
}

// GetUnvestedSupply returns total unvested supply
Expand Down
2 changes: 1 addition & 1 deletion x/supply/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
// creating a x/supply keeper.
type BankKeeper interface {
GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
GetSupply(ctx sdk.Context, denom string) sdk.Coin
IterateTotalSupply(ctx sdk.Context, cb func(sdk.Coin) bool)
LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins
}

Expand Down

0 comments on commit ebfb62d

Please sign in to comment.