Skip to content

Commit

Permalink
rewrite ibc test to use reliable amounts (not > or < quota based on r…
Browse files Browse the repository at this point in the history
…ounding)
  • Loading branch information
toteki committed Oct 12, 2023
1 parent fe8a3ad commit 3f3d6b7
Showing 1 changed file with 65 additions and 75 deletions.
140 changes: 65 additions & 75 deletions tests/e2e/e2e_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const (

var powerReduction = sdk.MustNewDecFromStr("10").Power(6)

// mulCoin multiplies the amount of a coin by a dec (given as string)
func mulCoin(c sdk.Coin, d string) sdk.Coin {
newAmount := sdk.MustNewDecFromStr(d).MulInt(c.Amount).RoundInt()
return sdk.NewCoin(c.Denom, newAmount)
}

func (s *E2ETest) checkOutflowByPercentage(endpoint, excDenom string, outflow, amount, perDiff sdk.Dec) {
// get historic average price for denom (SYMBOL_DENOM)
histoAvgPrice, err := s.QueryHistAvgPrice(endpoint, excDenom)
Expand Down Expand Up @@ -93,84 +99,69 @@ func (s *E2ETest) TestIBCTokenTransfer() {
totalQuota := math.NewInt(120)
tokenQuota := math.NewInt(100)

// send uatom from gaia to umee
// Note : gaia -> umee (ibc_quota will not check token limit)
// compute the amount of ATOM sent from umee to gaia which would meet atom's token quota
atomPrice, err := s.QueryHistAvgPrice(umeeAPIEndpoint, atomSymbol)
s.Require().NoError(err)
s.Require().True(atomPrice.GT(sdk.OneDec()),
"atom price should be non zero, and expecting higher than 1, got: %s", atomPrice)
emOfAtom := sdk.NewDecFromInt(totalQuota).Quo(atomPrice)
c := sdk.NewInt64Coin("uatom", emOfAtom.Mul(powerReduction).RoundInt64())
s.Require().True(c.Amount.GT(sdk.NewInt(2_000_000)),
"amount should be non zero, and expecting much higher than 2 atom = 2e6 uatom, got: %s", c.Amount)
s.SendIBC(setup.GaiaChainID, s.Chain.ID, "", c, false, "uatom from gaia to umee")
s.checkSupply(umeeAPIEndpoint, uatomIBCHash, c.Amount)

// sending more tokens than token_quota limit of umee (token_quota is 100$)
histoAvgPriceOfUmee, err := s.QueryHistAvgPrice(umeeAPIEndpoint, umeeSymbol)
atomQuota := sdk.NewCoin("uatom",
sdk.NewDecFromInt(tokenQuota).Quo(atomPrice).Mul(powerReduction).RoundInt(),
)

// send $500 ATOM from gaia to umee. (ibc_quota will not check token limit)
atomFromGaia := mulCoin(atomQuota, "5.0")
s.SendIBC(setup.GaiaChainID, s.Chain.ID, "", atomFromGaia, false, "")
s.checkSupply(umeeAPIEndpoint, uatomIBCHash, atomFromGaia.Amount)

// compute the amout of UMEE sent to gaia which would meet umee's token quota
umeePrice, err := s.QueryHistAvgPrice(umeeAPIEndpoint, umeeSymbol)
s.Require().NoError(err)
s.Require().True(histoAvgPriceOfUmee.GT(sdk.MustNewDecFromStr("0.001")),
"umee price should be non zero, and expecting higher than 0.001, got: %s", histoAvgPriceOfUmee)
exceedAmountOfUmee := sdk.NewDecFromInt(totalQuota).Quo(histoAvgPriceOfUmee).Mul(
sdk.MustNewDecFromStr("1.1"), // send 110% the quota amount
s.Require().True(umeePrice.GT(sdk.MustNewDecFromStr("0.001")),
"umee price should be non zero, and expecting higher than 0.001, got: %s", umeePrice)
umeeQuota := sdk.NewCoin(appparams.BondDenom,
sdk.NewDecFromInt(tokenQuota).Quo(atomPrice).Mul(powerReduction).RoundInt(),
)
exceedAmountCoin := sdk.NewInt64Coin(appparams.BondDenom, exceedAmountOfUmee.Mul(powerReduction).RoundInt64())
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", exceedAmountCoin, true, fmt.Sprintf(
"sending %s amount %s more than %s", umeeSymbol, exceedAmountOfUmee.String(), totalQuota.String(),
))
// check the ibc (umee) quota after ibc txs

// send $110 UMEE from umee to gaia (token_quota is 100$)
exceedUmee := mulCoin(umeeQuota, "1.1")
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", exceedUmee, true, "")
// check the ibc (umee) quota after ibc txs - this one should have failed
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, math.ZeroInt())

// send $90 UMEE from umee to gaia
// Note: receiver is null means hermes will default send to key_name (from config) of target chain (gaia)
// umee -> gaia (ibc_quota will check)
umeeInitialQuota := math.NewInt(90)
belowTokenQuota := sdk.NewDecFromInt(umeeInitialQuota).Quo(histoAvgPriceOfUmee)
token := sdk.NewInt64Coin(appparams.BondDenom, belowTokenQuota.Mul(powerReduction).RoundInt64())
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", token, false, fmt.Sprintf(
"sending %s amount %s less than token quota %s", "UMEE", belowTokenQuota.String(), tokenQuota.String()))
s.checkOutflows(umeeAPIEndpoint, appparams.BondDenom, true, sdk.NewDecFromInt(token.Amount), appparams.Name)
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, token.Amount)

// send uatom (ibc/27394FB092D2ECCD56123C74F36E4C1F926001CEADA9CA97EA622B25F41E5EB2) from umee to gaia
uatomIBCToken := sdk.NewInt64Coin(uatomIBCHash, c.Amount.Int64())
// supply will be not be decreased because sending uatomIBCToken amount is more than token quota so it will fail
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", uatomIBCToken, true, "uatom from umee to gaia")
s.checkSupply(umeeAPIEndpoint, uatomIBCHash, uatomIBCToken.Amount)

// send uatom below the token quota
/*
umee -> gaia
umee token_quot = 90$
total_quota = 120$
*/
belowTokenQuotabutNotBelowTotalQuota := sdk.NewDecFromInt(math.NewInt(90)).Quo(atomPrice)
uatomIBCToken.Amount = math.NewInt(belowTokenQuotabutNotBelowTotalQuota.Mul(powerReduction).RoundInt64())
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", uatomIBCToken, true, "below token quota but not total quota")
// send $90 UMEE from umee to gaia (ibc_quota will check)
// Note: receiver is null so hermes will default send to key_name (from config) of target chain (gaia)
sendUmee := mulCoin(umeeQuota, "0.9")
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", sendUmee, false, fmt.Sprintf(
"sending %s (less than token quota) ", sendUmee.String()))
s.checkOutflows(umeeAPIEndpoint, appparams.BondDenom, true, sdk.NewDecFromInt(sendUmee.Amount), appparams.Name)
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, sendUmee.Amount)

// send $110 ATOM from umee to gaia
exceedAtom := mulCoin(atomQuota, "1.1")
// supply will be not be decreased because sending amount is more than token quota so it will fail
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", exceedAtom, true, "uatom from umee to gaia")
s.checkSupply(umeeAPIEndpoint, uatomIBCHash, atomFromGaia.Amount)

// send $40 ATOM from umee to gaia
atom40 := mulCoin(atomQuota, "0.4")
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", atom40, true, "below token quota but not total quota")
// supply will be not be decreased because sending more than total quota from umee to gaia
s.checkSupply(umeeAPIEndpoint, uatomIBCHash, c.Amount)
// making sure below the total quota
belowTokenQuotaInUSD := totalQuota.Sub(umeeInitialQuota).Sub(math.NewInt(2))
belowTokenQuotaforAtom := sdk.NewDecFromInt(belowTokenQuotaInUSD).Quo(atomPrice)
uatomIBCToken.Amount = math.NewInt(belowTokenQuotaforAtom.Mul(powerReduction).RoundInt64())
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", uatomIBCToken, false, "below both quotas")
// remaing supply still exists for uatom in umee
s.checkSupply(umeeAPIEndpoint, uatomIBCHash, c.Amount.Sub(uatomIBCToken.Amount))
s.checkOutflows(umeeAPIEndpoint, uatomIBCHash, true, sdk.NewDecFromInt(uatomIBCToken.Amount), atomSymbol)

// sending more tokens then token_quota limit of umee
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", exceedAmountCoin, true, "above umee quota")
// check the ibc (umee) supply after ibc txs, it will same as previous because it will fail because to quota limit exceed
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, token.Amount)

/* sending back some amount from receiver to sender (ibc/XXX)
gaia -> umee
*/
s.SendIBC(setup.GaiaChainID, s.Chain.ID, "", sdk.NewInt64Coin(umeeIBCHash, 1000), false, "send back some")
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, token.Amount.Sub(math.NewInt(1000)))
// sending back remaining ibc amount from receiver to sender (ibc/XXX)
s.SendIBC(setup.GaiaChainID, s.Chain.ID, "",
sdk.NewInt64Coin(umeeIBCHash, token.Amount.Sub(math.NewInt(1000)).Int64()), false, "send back remaining")
s.checkSupply(umeeAPIEndpoint, uatomIBCHash, atomFromGaia.Amount)

// send $15 ATOM from umee to gaia
sendAtom := mulCoin(atomQuota, "0.15")
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", sendAtom, false, "below both quotas")
// remaing supply decreased uatom on umee
s.checkSupply(umeeAPIEndpoint, uatomIBCHash, atomFromGaia.Amount.Sub(sendAtom.Amount))
s.checkOutflows(umeeAPIEndpoint, uatomIBCHash, true, sdk.NewDecFromInt(sendAtom.Amount), atomSymbol)

// send $45 UMEE from gaia to umee
returnUmee := mulCoin(sendUmee, "0.5")
s.SendIBC(setup.GaiaChainID, s.Chain.ID, "", returnUmee, false, "send back some umee")
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, returnUmee.Amount) // half was returned, so half remains

// sending back remaining amount
s.SendIBC(setup.GaiaChainID, s.Chain.ID, "", returnUmee, false, "send back remaining umee")
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, math.ZeroInt())

// reset the outflows
Expand Down Expand Up @@ -206,12 +197,11 @@ func (s *E2ETest) TestIBCTokenTransfer() {
uibcParams, err := s.AccountClient(0).QueryUIBCParams()
s.Require().NoError(err)
s.Require().Equal(uibcParams.IbcStatus, uibc.IBCTransferStatus_IBC_TRANSFER_STATUS_QUOTA_DISABLED)
token = sdk.NewInt64Coin("uumee", 100000000) // 100 Umee

// sending the umee tokens
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", token, false, "sending umee")
// sending the umee tokens - they would have exceeded quota before
s.SendIBC(s.Chain.ID, setup.GaiaChainID, "", exceedUmee, false, "sending umee")
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, exceedUmee.Amount)
// Check the outflows
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, token.Amount)
s.Require().Eventually(
func() bool {
a, err := s.QueryOutflows(umeeAPIEndpoint, appparams.BondDenom)
Expand All @@ -223,8 +213,8 @@ func (s *E2ETest) TestIBCTokenTransfer() {
30*time.Second,
1*time.Second,
)
// resend the umee token from gaia to umee
s.SendIBC(setup.GaiaChainID, s.Chain.ID, "", sdk.NewInt64Coin(umeeIBCHash, token.Amount.Int64()), false, "resend")
// return the tokens to umee
s.SendIBC(setup.GaiaChainID, s.Chain.ID, "", exceedUmee, false, "return umee")
s.checkSupply(gaiaAPIEndpoint, umeeIBCHash, sdk.ZeroInt())
})
}

0 comments on commit 3f3d6b7

Please sign in to comment.