Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: import/export tokenfactory hooks #590

Merged
merged 2 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
pr0n00gler marked this conversation as resolved.
Show resolved Hide resolved
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
Loading