Skip to content

Commit

Permalink
Merge branch 'fetchai:master' into bls
Browse files Browse the repository at this point in the history
  • Loading branch information
kitounliu authored Jul 15, 2021
2 parents 082e071 + 9b3f86e commit a5dd8ea
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 2 deletions.
84 changes: 83 additions & 1 deletion x/airdrop/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ var (
feeCollectorAddress = authtypes.NewModuleAddress(authtypes.FeeCollectorName)
addr1 = sdk.AccAddress([]byte("addr1_______________"))
addr2 = sdk.AccAddress([]byte("addr2_______________"))
addr3 = sdk.AccAddress([]byte("addr3_______________"))
addr4 = sdk.AccAddress([]byte("addr4_______________"))
)

type KeeperTestSuite struct {
Expand All @@ -34,9 +36,10 @@ func (s *KeeperTestSuite) SetupTest() {
Height: 10,
})

s.app.AirdropKeeper.SetParams(s.ctx, types.NewParams(addr1.String(), addr2.String()))
s.app.AirdropKeeper.SetParams(s.ctx, types.NewParams(addr1.String(), addr2.String(), addr3.String(), addr4.String()))
}


func (s *KeeperTestSuite) TestAddNewFund() {
amount := sdk.NewInt64Coin(sdk.DefaultBondDenom, 4000)
s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr1, amount)) // ensure the account is funded
Expand Down Expand Up @@ -164,6 +167,85 @@ func (s *KeeperTestSuite) TestFeeDrip() {
s.Require().Equal(feeCollectorBalance, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(4040)))
}

func (s *KeeperTestSuite) TestAddNewFundWithDiffDenom() {
amount := sdk.NewInt64Coin("denom", 4000)
s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr1, amount)) // ensure the account is funded

addrBalance := s.app.BankKeeper.GetBalance(s.ctx, addr1, "denom")
moduleBalance := s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, "denom")

// sanity check
s.Require().Equal(addrBalance, sdk.NewCoin("denom", sdk.NewInt(4000)))
s.Require().Equal(moduleBalance, sdk.NewCoin("denom", sdk.NewInt(0)))

fund := types.Fund{
Amount: &amount,
DripAmount: sdk.NewInt(40),
}
s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr1, fund))

addrBalance = s.app.BankKeeper.GetBalance(s.ctx, addr1, "denom")
moduleBalance = s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, "denom")

s.Require().Equal(addrBalance, sdk.NewCoin("denom", sdk.NewInt(0)))
s.Require().Equal(moduleBalance, sdk.NewCoin("denom", sdk.NewInt(4000)))
}

func (s *KeeperTestSuite) TestMultiDenomFeeDrip() {
amountStake := sdk.NewInt64Coin(sdk.DefaultBondDenom, 1000)
amountDenom := sdk.NewInt64Coin("denom", 1000)
fund1 := types.Fund{
Amount: &amountStake,
DripAmount: sdk.NewInt(500)}

fund2 := types.Fund{
Amount: &amountStake,
DripAmount: sdk.NewInt(800),
}

fund3 := types.Fund{
Amount: &amountDenom,
DripAmount: sdk.NewInt(100)}

fund4 := types.Fund{
Amount: &amountDenom,
DripAmount: sdk.NewInt(300),
}
s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr1, amountStake))
s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr2, amountStake))
s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr3, amountDenom))
s.Require().NoError(s.app.BankKeeper.SetBalance(s.ctx, addr4, amountDenom))

s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr1, fund1))
s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr2, fund2))
s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr3, fund3))
s.Require().NoError(s.app.AirdropKeeper.AddFund(s.ctx, addr4, fund4)) // check the balances

moduleBalanceStake := s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, sdk.DefaultBondDenom)
moduleBalanceDenom := s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, "denom")
feeCollectorBalanceStake := s.app.BankKeeper.GetBalance(s.ctx, feeCollectorAddress, sdk.DefaultBondDenom)
feeCollectorBalanceDenom := s.app.BankKeeper.GetBalance(s.ctx, feeCollectorAddress, "denom")

s.Require().Equal(moduleBalanceStake, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(2000)))
s.Require().Equal(moduleBalanceDenom, sdk.NewCoin("denom", sdk.NewInt(2000)))
s.Require().Equal(feeCollectorBalanceStake, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(0)))
s.Require().Equal(feeCollectorBalanceDenom, sdk.NewCoin("denom", sdk.NewInt(0)))

_, err := s.app.AirdropKeeper.DripAllFunds(s.ctx) // test case - drip the funds
s.Require().NoError(err) // check that the fees have been transferred

moduleBalanceStake = s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, sdk.DefaultBondDenom)
moduleBalanceDenom = s.app.BankKeeper.GetBalance(s.ctx, moduleAddress, "denom")
feeCollectorBalanceStake = s.app.BankKeeper.GetBalance(s.ctx, feeCollectorAddress, sdk.DefaultBondDenom)
feeCollectorBalanceDenom = s.app.BankKeeper.GetBalance(s.ctx, feeCollectorAddress, "denom")

s.Require().Equal(feeCollectorBalanceStake, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(1300))) // 800 drip fund 1, 500 drip fund 2
s.Require().Equal(feeCollectorBalanceDenom, sdk.NewCoin("denom", sdk.NewInt(400))) // 100 drip fund 3, 300 drip fund 4
s.Require().Equal(moduleBalanceStake, sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(700))) // remainder 500 from fund 1, remainder 200 from fund 2
s.Require().Equal(moduleBalanceDenom, sdk.NewCoin("denom", sdk.NewInt(1600))) // remainder 900 from fund 3, remainder 700 from fund 4

}

func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}
2 changes: 1 addition & 1 deletion x/airdrop/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

func (k Keeper) GetWhiteListClients(ctx sdk.Context) []string {
var res []string
k.paramSpace.Get(ctx, types.KeyWhiteList, &res)
k.paramSpace.GetIfExists(ctx, types.KeyWhiteList, &res)
return res
}

Expand Down
63 changes: 63 additions & 0 deletions x/airdrop/types/genesis_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package types_test

import (
"testing"

"github.com/cosmos/cosmos-sdk/x/airdrop/types"
"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
)

var (
addr = sdk.AccAddress([]byte("addr________________"))
invalidAddr = sdk.AccAddress([]byte("\n\n\n\n\taddr________________\t\n\n\n\n\n"))
)

func TestNewGenesisState(t *testing.T) {
p := types.NewParams()
expectedFunds := []types.ActiveFund{
{
Sender: addr.String(),
Fund: &types.Fund{
Amount: &sdk.Coin{Denom: "test", Amount: sdk.NewInt(10)},
DripAmount: sdk.NewInt(1),
},
},
}
expectedState := &types.GenesisState{
Params: p,
Funds: expectedFunds,
}
require.Equal(t, expectedState.GetFunds(), expectedFunds)
require.Equal(t, expectedState.Params, p)
}

func TestValidateGenesisState(t *testing.T) {
p1 := types.Params{
AllowList: []string{
addr.String(), // valid address
},
}
p2 := types.Params{
AllowList: []string{
invalidAddr.String(), // invalid address
},
}
funds := []types.ActiveFund{
{
Sender: addr.String(),
Fund: &types.Fund{
Amount: &sdk.Coin{
Denom: "test",
Amount: sdk.NewInt(10),
},
DripAmount: sdk.NewInt(1),
},
},
}
gen1 := types.NewGenesisState(p1, funds)
gen2 := types.NewGenesisState(p2, funds)
require.NoError(t, gen1.Validate())
require.Error(t, gen2.Validate())
}

0 comments on commit a5dd8ea

Please sign in to comment.