Skip to content

Commit

Permalink
Merge pull request #722 from UnUniFi/irs_module_merge_dev
Browse files Browse the repository at this point in the history
Merge Develop changes into irs_module
  • Loading branch information
Senna46 authored Feb 7, 2024
2 parents c40010c + cfbe418 commit 45799cb
Show file tree
Hide file tree
Showing 12 changed files with 896 additions and 98 deletions.
3 changes: 2 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import (
v3_2_1 "github.com/UnUniFi/chain/app/upgrades/v3.2.1"
v3_2_2 "github.com/UnUniFi/chain/app/upgrades/v3.2.2"
v4 "github.com/UnUniFi/chain/app/upgrades/v4"
v4_0_1 "github.com/UnUniFi/chain/app/upgrades/v4.0.1"
)

const Name = "ununifi"
Expand Down Expand Up @@ -107,7 +108,7 @@ var (
stakeibctypes.ModuleName: true,
}

Upgrades = []upgrades.Upgrade{v3.Upgrade, v3_1.Upgrade, v3_2.Upgrade, v3_2_1.Upgrade, v3_2_2.Upgrade, v4.Upgrade}
Upgrades = []upgrades.Upgrade{v3.Upgrade, v3_1.Upgrade, v3_2.Upgrade, v3_2_1.Upgrade, v3_2_2.Upgrade, v4.Upgrade, v4_0_1.Upgrade}
)

var (
Expand Down
146 changes: 146 additions & 0 deletions app/upgrades/v4.0.1/bank_send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package v4_0_1

import (
"fmt"
"strconv"

sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authvesting "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
)

func upgradeBankSend(
ctx sdk.Context,
authkeeper authkeeper.AccountKeeper,
bankkeeper bankkeeper.Keeper,
bank_send_list ResultList) error {
ctx.Logger().Info(fmt.Sprintf("upgrade :%s", UpgradeName))

total_allocate_coin := sdk.NewCoin(Denom, sdk.NewInt(0))
assumed_coin := sdk.NewCoin(Denom, sdk.NewInt(0))

// before get total supply
before_total_supply := bankkeeper.GetSupply(ctx, Denom)
ctx.Logger().Info(fmt.Sprintf("bank send : total supply[%d]", before_total_supply.Amount))

fromAddr, err := sdk.AccAddressFromBech32(FromAddress)
if err != nil {
panic(err)
}
// Campaign
for index, value := range bank_send_list.Campaign {
ctx.Logger().Info(fmt.Sprintf("bank send validator :%s[%s]", strconv.Itoa(index), value.ToAddress))
coin := tokenAllocation(ctx, authkeeper, bankkeeper, index, value, fromAddr)
total_allocate_coin.Add(coin)
}

// Check the amount of tokens sent
assumed_coin.Add(sdk.NewCoin(Denom, sdk.NewInt(TotalAmountCampaign)))
if !total_allocate_coin.IsEqual(assumed_coin) {
panic(fmt.Sprintf("error: assumed amount sent to the campaign rewards does not match.: Actual[%d] Assumed[%d]",
total_allocate_coin.Amount,
assumed_coin.Amount))
}

// after get total supply
after_total_supply := bankkeeper.GetSupply(ctx, Denom)
ctx.Logger().Info(fmt.Sprintf("bank send : total supply[%d]", after_total_supply.Amount))

return nil
}

func tokenAllocation(
ctx sdk.Context,
authkeeper authkeeper.AccountKeeper,
bankkeeper bankkeeper.Keeper,
index int,
value BankSendTarget,
fromAddr sdk.AccAddress) sdk.Coin {
not_exist_vesting_account := true
add_coin := sdk.NewCoin(value.Denom, sdk.NewInt(value.Amount))

// check exits VestingAccount
toAddr, err := sdk.AccAddressFromBech32(value.ToAddress)
if err != nil {
panic(err)
}

// if the account is not existent, this method creates account internally
if err := bankkeeper.SendCoins(ctx, fromAddr, toAddr, sdk.NewCoins(add_coin)); err != nil {
panic(err)
}
accI := authkeeper.GetAccount(ctx, toAddr)

cont_acc, ok := accI.(*authvesting.ContinuousVestingAccount)

if ok {
ctx.Logger().Info(fmt.Sprintf("bank send[%s] : ContinuousVestingAccount is exits [%s]", strconv.Itoa(index), cont_acc.String()))
not_exist_vesting_account = false

// add coins
newAmount := cont_acc.OriginalVesting.Add(add_coin)
cont_acc.OriginalVesting = newAmount

// start time sets a more past date.
if cont_acc.GetStartTime() > value.VestingStarts {
cont_acc.StartTime = value.VestingStarts
}
// end time sets a more future date.
if cont_acc.GetEndTime() < value.VestingEnds {
cont_acc.EndTime = value.VestingEnds
}

if err := cont_acc.Validate(); err != nil {
panic(fmt.Errorf("failed to validate ContinuousVestingAccount: %w", err))
}

authkeeper.SetAccount(ctx, cont_acc)

ctx.Logger().Info(fmt.Sprintf("bank send[%s] : ContinuousVestingAccount [%s]", strconv.Itoa(index), cont_acc.String()))
}

delayed_acc, ok := accI.(*authvesting.DelayedVestingAccount)
if ok {
ctx.Logger().Info(fmt.Sprintf("bank send[%s] : DelayedVestingAccount is exits [%s]", strconv.Itoa(index), delayed_acc.String()))
not_exist_vesting_account = false

// add coins
newAmount := delayed_acc.DelegatedVesting.Add(add_coin)
delayed_acc.DelegatedVesting = newAmount

// end time sets a more future date.
if delayed_acc.GetEndTime() < value.VestingEnds {
delayed_acc.EndTime = value.VestingEnds
}
if err := delayed_acc.Validate(); err != nil {
panic(fmt.Errorf("failed to validate DelayedVestingAccount: %w", err))
}

authkeeper.SetAccount(ctx, delayed_acc)

ctx.Logger().Info(fmt.Sprintf("bank send[%s] : DelayedVestingAccount [%s]", strconv.Itoa(index), delayed_acc.String()))
}

if not_exist_vesting_account {
ctx.Logger().Info(fmt.Sprintf("bank send[%s] : ContinuousVestingAccount / DelayedVestingAccount not exits", strconv.Itoa(index)))
// not exist
// create vesting account
cont_vesting_acc := authvesting.NewContinuousVestingAccount(
accI.(*authtypes.BaseAccount),
sdk.NewCoins(add_coin),
value.VestingStarts,
value.VestingEnds)

if err := cont_vesting_acc.Validate(); err != nil {
panic(fmt.Errorf("failed to validate new ContinuousVestingAccount: %w", err))
}

authkeeper.SetAccount(ctx, cont_vesting_acc)

ctx.Logger().Info(fmt.Sprintf("bank send[%s] : NewContinuousVestingAccount [%s]", strconv.Itoa(index), cont_vesting_acc.String()))
}

return add_coin
}
Loading

0 comments on commit 45799cb

Please sign in to comment.