0g-chain/x/issuance/keeper/supply_test.go
2024-09-25 15:00:59 +00:00

85 lines
2.3 KiB
Go

package keeper_test
import (
"strings"
"time"
sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/0glabs/0g-chain/x/issuance/types"
)
func (suite *KeeperTestSuite) TestIncrementCurrentAssetSupply() {
type args struct {
assets []types.Asset
supplies []types.AssetSupply
coin sdk.Coin
}
type errArgs struct {
expectPass bool
contains string
}
testCases := []struct {
name string
args args
errArgs errArgs
}{
{
"valid supply increase",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(true, sdkmath.NewInt(10000000000), time.Hour*24)),
},
supplies: []types.AssetSupply{
types.NewAssetSupply(sdk.NewCoin("usdtoken", sdk.ZeroInt()), time.Hour),
},
coin: sdk.NewCoin("usdtoken", sdkmath.NewInt(100000)),
},
errArgs{
expectPass: true,
contains: "",
},
},
{
"over limit increase",
args{
assets: []types.Asset{
types.NewAsset(suite.addrs[0], "usdtoken", []string{suite.addrs[1]}, false, true, types.NewRateLimit(true, sdkmath.NewInt(10000000000), time.Hour*24)),
},
supplies: []types.AssetSupply{
types.NewAssetSupply(sdk.NewCoin("usdtoken", sdk.ZeroInt()), time.Hour),
},
coin: sdk.NewCoin("usdtoken", sdkmath.NewInt(10000000001)),
},
errArgs{
expectPass: false,
contains: "asset supply over limit",
},
},
}
for _, tc := range testCases {
suite.Run(tc.name, func() {
suite.SetupTest()
params := types.NewParams(tc.args.assets)
suite.keeper.SetParams(suite.ctx, params)
for _, supply := range tc.args.supplies {
suite.keeper.SetAssetSupply(suite.ctx, supply, supply.GetDenom())
}
err := suite.keeper.IncrementCurrentAssetSupply(suite.ctx, tc.args.coin)
if tc.errArgs.expectPass {
suite.Require().NoError(err, tc.name)
for _, expectedSupply := range tc.args.supplies {
expectedSupply.CurrentSupply = expectedSupply.CurrentSupply.Add(tc.args.coin)
actualSupply, found := suite.keeper.GetAssetSupply(suite.ctx, expectedSupply.GetDenom())
suite.Require().True(found)
suite.Require().Equal(expectedSupply, actualSupply, tc.name)
}
} else {
suite.Require().Error(err, tc.name)
suite.Require().True(strings.Contains(err.Error(), tc.errArgs.contains))
}
})
}
}