Skip to content

Commit

Permalink
Merge pull request #590 from neutron-org/fix/import-export-tokenfacto…
Browse files Browse the repository at this point in the history
…ry-hooks

Fix: import/export tokenfactory hooks
  • Loading branch information
pr0n00gler authored Jun 20, 2024
2 parents 28b1d2c + 033aefc commit a73edfc
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 39 deletions.
2 changes: 2 additions & 0 deletions proto/osmosis/tokenfactory/v1beta1/genesis.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ message GenesisDenom {
(gogoproto.moretags) = "yaml:\"authority_metadata\"",
(gogoproto.nullable) = false
];

string hook_contract_address = 3 [(gogoproto.nullable) = true];
}
19 changes: 17 additions & 2 deletions x/tokenfactory/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,26 @@ func (k Keeper) InitGenesis(ctx sdk.Context, genState types.GenesisState) {
if err != nil {
panic(err)
}

err = k.createDenomAfterValidation(ctx, creator, genDenom.GetDenom())
if err != nil {
panic(err)
}

err = k.setAuthorityMetadata(ctx, genDenom.GetDenom(), genDenom.GetAuthorityMetadata())
if err != nil {
panic(err)
}

if _, err := sdk.AccAddressFromBech32(genDenom.HookContractAddress); genDenom.HookContractAddress != "" && err != nil {
panic(err)
}

if genDenom.HookContractAddress != "" {
if err := k.setBeforeSendHook(ctx, genDenom.Denom, genDenom.HookContractAddress); err != nil {
panic(err)
}
}
}
}

Expand All @@ -43,14 +55,17 @@ func (k Keeper) ExportGenesis(ctx sdk.Context) *types.GenesisState {
for ; iterator.Valid(); iterator.Next() {
denom := string(iterator.Value())

contractHook := k.GetBeforeSendHook(ctx, denom)

authorityMetadata, err := k.GetAuthorityMetadata(ctx, denom)
if err != nil {
panic(err)
}

genDenoms = append(genDenoms, types.GenesisDenom{
Denom: denom,
AuthorityMetadata: authorityMetadata,
Denom: denom,
AuthorityMetadata: authorityMetadata,
HookContractAddress: contractHook,
})
}

Expand Down
2 changes: 2 additions & 0 deletions x/tokenfactory/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func (suite *KeeperTestSuite) TestGenesis() {
AuthorityMetadata: types.DenomAuthorityMetadata{
Admin: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2",
},
HookContractAddress: "",
},
{
Denom: "factory/neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2/diff-admin",
Expand All @@ -26,6 +27,7 @@ func (suite *KeeperTestSuite) TestGenesis() {
AuthorityMetadata: types.DenomAuthorityMetadata{
Admin: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2",
},
HookContractAddress: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2",
},
},
}
Expand Down
23 changes: 12 additions & 11 deletions x/tokenfactory/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import (

// x/tokenfactory module sentinel errors
var (
ErrDenomExists = errorsmod.Register(ModuleName, 2, "attempting to create a denom that already exists (has bank metadata)")
ErrUnauthorized = errorsmod.Register(ModuleName, 3, "unauthorized account")
ErrInvalidDenom = errorsmod.Register(ModuleName, 4, "invalid denom")
ErrInvalidCreator = errorsmod.Register(ModuleName, 5, "invalid creator")
ErrInvalidAuthorityMetadata = errorsmod.Register(ModuleName, 6, "invalid authority metadata")
ErrInvalidGenesis = errorsmod.Register(ModuleName, 7, "invalid genesis")
ErrSubdenomTooLong = errorsmod.Register(ModuleName, 8, fmt.Sprintf("subdenom too long, max length is %d bytes", MaxSubdenomLength))
ErrCreatorTooLong = errorsmod.Register(ModuleName, 9, fmt.Sprintf("creator too long, max length is %d bytes", MaxCreatorLength))
ErrDenomDoesNotExist = errorsmod.Register(ModuleName, 10, "denom does not exist")
ErrBurnFromModuleAccount = errorsmod.Register(ModuleName, 11, "burning from Module Account is not allowed")
ErrTrackBeforeSendOutOfGas = errorsmod.Register(ModuleName, 12, "gas meter hit maximum limit")
ErrDenomExists = errorsmod.Register(ModuleName, 2, "attempting to create a denom that already exists (has bank metadata)")
ErrUnauthorized = errorsmod.Register(ModuleName, 3, "unauthorized account")
ErrInvalidDenom = errorsmod.Register(ModuleName, 4, "invalid denom")
ErrInvalidCreator = errorsmod.Register(ModuleName, 5, "invalid creator")
ErrInvalidAuthorityMetadata = errorsmod.Register(ModuleName, 6, "invalid authority metadata")
ErrInvalidGenesis = errorsmod.Register(ModuleName, 7, "invalid genesis")
ErrSubdenomTooLong = errorsmod.Register(ModuleName, 8, fmt.Sprintf("subdenom too long, max length is %d bytes", MaxSubdenomLength))
ErrCreatorTooLong = errorsmod.Register(ModuleName, 9, fmt.Sprintf("creator too long, max length is %d bytes", MaxCreatorLength))
ErrDenomDoesNotExist = errorsmod.Register(ModuleName, 10, "denom does not exist")
ErrBurnFromModuleAccount = errorsmod.Register(ModuleName, 11, "burning from Module Account is not allowed")
ErrTrackBeforeSendOutOfGas = errorsmod.Register(ModuleName, 12, "gas meter hit maximum limit")
ErrInvalidHookContractAddress = errorsmod.Register(ModuleName, 13, "invalid hook contract address")
)
4 changes: 4 additions & 0 deletions x/tokenfactory/types/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ func (gs GenesisState) Validate() error {
return errorsmod.Wrapf(ErrInvalidAuthorityMetadata, "Invalid admin address (%s)", err)
}
}

if _, err := sdk.AccAddressFromBech32(denom.HookContractAddress); denom.HookContractAddress != "" && err != nil {
return errorsmod.Wrapf(ErrInvalidHookContractAddress, "Invalid hook contract address (%s)", err)
}
}

return nil
Expand Down
109 changes: 83 additions & 26 deletions x/tokenfactory/types/genesis.pb.go

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

30 changes: 30 additions & 0 deletions x/tokenfactory/types/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,36 @@ func TestGenesisState_Validate(t *testing.T) {
},
valid: false,
},
{
desc: "empty hook address",
genState: &types.GenesisState{
FactoryDenoms: []types.GenesisDenom{
{
Denom: "factory/neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2/bitcoin",
AuthorityMetadata: types.DenomAuthorityMetadata{
Admin: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2",
},
HookContractAddress: "",
},
},
},
valid: true,
},
{
desc: "invalid hook address",
genState: &types.GenesisState{
FactoryDenoms: []types.GenesisDenom{
{
Denom: "factory/neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2/bitcoin",
AuthorityMetadata: types.DenomAuthorityMetadata{
Admin: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2",
},
HookContractAddress: "sfsdfsdfsdfs",
},
},
},
valid: false,
},
} {
t.Run(tc.desc, func(t *testing.T) {
err := tc.genState.Validate()
Expand Down

0 comments on commit a73edfc

Please sign in to comment.