Skip to content

Commit

Permalink
Merge pull request #120 from Victor118/main
Browse files Browse the repository at this point in the history
Patch : v7.0.1
  • Loading branch information
woof-chihuahua authored Jun 10, 2024
2 parents 218d90e + f524e5d commit ed5ad95
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 60 deletions.
29 changes: 27 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ import (
const (
Bech32Prefix = "chihuahua"
Name = "chihuahua"
UpgradeName = "v7"
UpgradeName = "v7.0.1"
NodeDir = ".chihuahuad"
)

Expand Down Expand Up @@ -880,7 +880,7 @@ func New(
//alliancemoduletypes.StoreKey,
//ibchookstypes.StoreKey,
//tokenfactorytypes.ModuleName,
liquiditytypes.ModuleName,
//liquiditytypes.ModuleName,
},
}

Expand Down Expand Up @@ -1170,6 +1170,31 @@ func (app *App) RegisterUpgradeHandlers(cfg module.Configurator) {
}
return vm, err
})

app.UpgradeKeeper.SetUpgradeHandler("v7.0.1", func(ctx sdk.Context, plan upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {

tokenFactoryParams := app.TokenFactoryKeeper.GetParams(ctx)
tokenFactoryParams.BuildersAddresses = []tokenfactorytypes.WeightedAddress{
{
Address: "chihuahua1yjak0p2f6yjwhvf00r0wd4kqhdpvn57qc887m3",
Weight: sdk.NewDecWithPrec(10, 2), //will receive 10% of commission from minting tokens
},
{
Address: "chihuahua1jpfqqpna4nasv53gkn08ta9ygfryq38l8af602",
Weight: sdk.NewDecWithPrec(90, 2), //will receive 90% of commission from minting tokens
},
}
tokenFactoryParams.DenomCreationFee = nil
tokenFactoryParams.DenomCreationGasConsume = 50_000
tokenFactoryParams.BuildersCommission = sdk.NewDecWithPrec(1, 2) //1% of minted token goes to builders

tokenFactoryParams.FreeMintWhitelistAddresses = []string{
"chihuahua1e85rxa4r8utk6ee0j93serytcchgeayqteeystkshqsvryk55egs6l9jhu",
"chihuahua1hplyuj2hzxd75q8686g9vm3uzrrny9ggvt8aza2csupgdp98vg2sp0e3h0",
}
errParams := app.TokenFactoryKeeper.SetParams(ctx, tokenFactoryParams)
return vm, errParams
})
}

// SimulationManager implements the SimulationApp interface
Expand Down
2 changes: 2 additions & 0 deletions proto/osmosis/tokenfactory/v1beta1/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,6 @@ message Params {
(gogoproto.moretags) = "yaml:\"builders_addresses\"",
(gogoproto.nullable) = false
];

repeated string free_mint_whitelist_addresses = 5;
}
41 changes: 27 additions & 14 deletions x/tokenfactory/keeper/bankactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ChihuahuaChain/chihuahua/x/tokenfactory/types"
)

func (k Keeper) mintTo(ctx sdk.Context, amount sdk.Coin, mintTo string) error {
func (k Keeper) mintTo(ctx sdk.Context, amount sdk.Coin, mintTo string, sender string) error {
// verify that denom is an x/tokenfactory denom
_, _, err := types.DeconstructDenom(amount.Denom)
if err != nil {
Expand All @@ -33,28 +33,41 @@ func (k Keeper) mintTo(ctx sdk.Context, amount sdk.Coin, mintTo string) error {
params := k.GetParams(ctx)
comm := params.BuildersCommission
buildersAddresses := params.BuildersAddresses
if len(buildersAddresses) > 0 {
decAmount := sdk.NewDecCoinFromCoin(amount)
commAmount := decAmount.Amount.Mul(comm)
totalCommSent := math.NewInt(0)
for _, builderAddr := range buildersAddresses {
accBuilder := sdk.MustAccAddressFromBech32(builderAddr.Address)
builderAmount := commAmount.Mul(builderAddr.Weight).TruncateInt()
builderCoin := sdk.NewCoin(amount.Denom, builderAmount)
error := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, accBuilder, sdk.NewCoins(builderCoin))
if error == nil {
totalCommSent = totalCommSent.Add(builderAmount)
}
if !isWhitelistedAddress(sender, params) {
if len(buildersAddresses) > 0 {
decAmount := sdk.NewDecCoinFromCoin(amount)
commAmount := decAmount.Amount.Mul(comm)
totalCommSent := math.NewInt(0)
for _, builderAddr := range buildersAddresses {
accBuilder := sdk.MustAccAddressFromBech32(builderAddr.Address)
builderAmount := commAmount.Mul(builderAddr.Weight).TruncateInt()
builderCoin := sdk.NewCoin(amount.Denom, builderAmount)
error := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, accBuilder, sdk.NewCoins(builderCoin))
if error == nil {
totalCommSent = totalCommSent.Add(builderAmount)
}

}
amount = amount.SubAmount(totalCommSent)
}
amount = amount.SubAmount(totalCommSent)
}

return k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName,
addr,
sdk.NewCoins(amount))
}

func isWhitelistedAddress(sender string, params types.Params) bool {
if len(params.FreeMintWhitelistAddresses) > 0 {
for _, addr := range params.FreeMintWhitelistAddresses {
if addr == sender {
return true
}
}
}
return false
}

func (k Keeper) burnFrom(ctx sdk.Context, amount sdk.Coin, burnFrom string) error {
// verify that denom is an x/tokenfactory denom
_, _, err := types.DeconstructDenom(amount.Denom)
Expand Down
2 changes: 1 addition & 1 deletion x/tokenfactory/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (server msgServer) Mint(goCtx context.Context, msg *types.MsgMint) (*types.
msg.MintToAddress = msg.Sender
}

err = server.Keeper.mintTo(ctx, msg.Amount, msg.MintToAddress)
err = server.Keeper.mintTo(ctx, msg.Amount, msg.MintToAddress, msg.Sender)
if err != nil {
return nil, err
}
Expand Down
35 changes: 29 additions & 6 deletions x/tokenfactory/types/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ func NewParams(denomCreationFee sdk.Coins) Params {
// default tokenfactory module parameters.
func DefaultParams() Params {
return Params{
DenomCreationFee: sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 10_000_000)),
DenomCreationGasConsume: 2_000_000,
BuildersCommission: sdk.NewDecWithPrec(5, 3), // "0.005" if there is builders addresses, this commission rate from minted amount is redirected to builders
BuildersAddresses: []WeightedAddress(nil),
DenomCreationFee: sdk.NewCoins(sdk.NewInt64Coin(sdk.DefaultBondDenom, 10_000_000)),
DenomCreationGasConsume: 2_000_000,
BuildersCommission: sdk.NewDecWithPrec(5, 3), // "0.005" if there is builders addresses, this commission rate from minted amount is redirected to builders
BuildersAddresses: []WeightedAddress(nil),
FreeMintWhitelistAddresses: []string(nil),
}
}

Expand All @@ -41,10 +42,34 @@ func (p Params) Validate() error {
if err != nil {
return err
}
err = validateFreeMintWhitelistAddresses(p.FreeMintWhitelistAddresses)
if err != nil {
return err
}
return nil

}

func validateFreeMintWhitelistAddresses(i interface{}) error {
v, ok := i.([]string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}

// whitelist can be empty
if len(v) == 0 {
return nil
}
for i, addr := range v {
_, err := sdk.AccAddressFromBech32(addr)
if err != nil {
return fmt.Errorf("invalid address at %dth", i)
}

}
return nil
}

func validateBuildersAddresses(i interface{}) error {
v, ok := i.([]WeightedAddress)
if !ok {
Expand All @@ -58,8 +83,6 @@ func validateBuildersAddresses(i interface{}) error {

weightSum := sdk.ZeroDec()
for i, w := range v {
// we allow address to be "" to go to community pool

_, err := sdk.AccAddressFromBech32(w.Address)
if err != nil {
return fmt.Errorf("invalid address at %dth", i)
Expand Down
131 changes: 94 additions & 37 deletions x/tokenfactory/types/params.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit ed5ad95

Please sign in to comment.