diff --git a/proto/osmosis/tokenfactory/v1beta1/genesis.proto b/proto/osmosis/tokenfactory/v1beta1/genesis.proto index a7d695544..5a1726129 100644 --- a/proto/osmosis/tokenfactory/v1beta1/genesis.proto +++ b/proto/osmosis/tokenfactory/v1beta1/genesis.proto @@ -29,4 +29,6 @@ message GenesisDenom { (gogoproto.moretags) = "yaml:\"authority_metadata\"", (gogoproto.nullable) = false ]; + + string hook_contract_address = 3 [(gogoproto.nullable) = true]; } diff --git a/x/tokenfactory/keeper/genesis.go b/x/tokenfactory/keeper/genesis.go index 5b58b09a8..f013f6ac0 100644 --- a/x/tokenfactory/keeper/genesis.go +++ b/x/tokenfactory/keeper/genesis.go @@ -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) + } + } } } @@ -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, }) } diff --git a/x/tokenfactory/keeper/genesis_test.go b/x/tokenfactory/keeper/genesis_test.go index 83ad23e4f..e059b848f 100644 --- a/x/tokenfactory/keeper/genesis_test.go +++ b/x/tokenfactory/keeper/genesis_test.go @@ -14,6 +14,7 @@ func (suite *KeeperTestSuite) TestGenesis() { AuthorityMetadata: types.DenomAuthorityMetadata{ Admin: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2", }, + HookContractAddress: "", }, { Denom: "factory/neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2/diff-admin", @@ -26,6 +27,7 @@ func (suite *KeeperTestSuite) TestGenesis() { AuthorityMetadata: types.DenomAuthorityMetadata{ Admin: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2", }, + HookContractAddress: "neutron1m9l358xunhhwds0568za49mzhvuxx9ux8xafx2", }, }, } diff --git a/x/tokenfactory/types/errors.go b/x/tokenfactory/types/errors.go index c20e41980..2ade10b9d 100644 --- a/x/tokenfactory/types/errors.go +++ b/x/tokenfactory/types/errors.go @@ -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") ) diff --git a/x/tokenfactory/types/genesis.go b/x/tokenfactory/types/genesis.go index fc265c124..683f49df7 100644 --- a/x/tokenfactory/types/genesis.go +++ b/x/tokenfactory/types/genesis.go @@ -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 diff --git a/x/tokenfactory/types/genesis.pb.go b/x/tokenfactory/types/genesis.pb.go index 29435e226..50dac9062 100644 --- a/x/tokenfactory/types/genesis.pb.go +++ b/x/tokenfactory/types/genesis.pb.go @@ -82,8 +82,9 @@ func (m *GenesisState) GetFactoryDenoms() []GenesisDenom { // state. The structure contains DenomAuthorityMetadata which defines the // denom's admin. type GenesisDenom struct { - Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty" yaml:"denom"` - AuthorityMetadata DenomAuthorityMetadata `protobuf:"bytes,2,opt,name=authority_metadata,json=authorityMetadata,proto3" json:"authority_metadata" yaml:"authority_metadata"` + Denom string `protobuf:"bytes,1,opt,name=denom,proto3" json:"denom,omitempty" yaml:"denom"` + AuthorityMetadata DenomAuthorityMetadata `protobuf:"bytes,2,opt,name=authority_metadata,json=authorityMetadata,proto3" json:"authority_metadata" yaml:"authority_metadata"` + HookContractAddress string `protobuf:"bytes,3,opt,name=hook_contract_address,json=hookContractAddress,proto3" json:"hook_contract_address,omitempty"` } func (m *GenesisDenom) Reset() { *m = GenesisDenom{} } @@ -133,6 +134,13 @@ func (m *GenesisDenom) GetAuthorityMetadata() DenomAuthorityMetadata { return DenomAuthorityMetadata{} } +func (m *GenesisDenom) GetHookContractAddress() string { + if m != nil { + return m.HookContractAddress + } + return "" +} + func init() { proto.RegisterType((*GenesisState)(nil), "osmosis.tokenfactory.v1beta1.GenesisState") proto.RegisterType((*GenesisDenom)(nil), "osmosis.tokenfactory.v1beta1.GenesisDenom") @@ -143,30 +151,33 @@ func init() { } var fileDescriptor_5749c3f71850298b = []byte{ - // 368 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xd2, 0xca, 0x2f, 0xce, 0xcd, - 0x2f, 0xce, 0x2c, 0xd6, 0x2f, 0xc9, 0xcf, 0x4e, 0xcd, 0x4b, 0x4b, 0x4c, 0x2e, 0xc9, 0x2f, 0xaa, - 0xd4, 0x2f, 0x33, 0x4c, 0x4a, 0x2d, 0x49, 0x34, 0xd4, 0x4f, 0x4f, 0xcd, 0x4b, 0x2d, 0xce, 0x2c, - 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x92, 0x81, 0xaa, 0xd5, 0x43, 0x56, 0xab, 0x07, 0x55, - 0x2b, 0x25, 0x92, 0x9e, 0x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xbb, 0x09, 0x7e, 0xf7, - 0x82, 0x6d, 0x72, 0x44, 0x8f, 0x03, 0x27, 0x45, 0xa8, 0xcb, 0x25, 0x21, 0xf6, 0x61, 0x9a, 0xae, - 0x14, 0x24, 0x88, 0x11, 0x73, 0x56, 0x2c, 0x2f, 0x16, 0xc8, 0x33, 0x3a, 0x05, 0x9c, 0x78, 0x24, - 0xc7, 0x78, 0xe1, 0x91, 0x1c, 0xe3, 0x83, 0x47, 0x72, 0x8c, 0x13, 0x1e, 0xcb, 0x31, 0x5c, 0x78, - 0x2c, 0xc7, 0x70, 0xe3, 0xb1, 0x1c, 0x43, 0x94, 0x59, 0x7a, 0x66, 0x49, 0x46, 0x69, 0x92, 0x5e, - 0x72, 0x7e, 0xae, 0x7e, 0x5e, 0x6a, 0x69, 0x49, 0x51, 0x7e, 0x9e, 0x6e, 0x7e, 0x51, 0x3a, 0x8c, - 0xad, 0x5f, 0x66, 0xa2, 0x5f, 0x81, 0x1a, 0xdf, 0x25, 0x95, 0x05, 0xa9, 0xc5, 0x49, 0x6c, 0xe0, - 0x78, 0x36, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0xd1, 0xcf, 0x93, 0x08, 0xaa, 0x02, 0x00, 0x00, + // 407 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0xcf, 0xaa, 0xd3, 0x40, + 0x14, 0xc6, 0x33, 0xf7, 0x5e, 0x0b, 0xa6, 0x55, 0x34, 0x5a, 0x88, 0x45, 0x93, 0x1a, 0x44, 0x6a, + 0xc1, 0x84, 0xd6, 0x22, 0xd2, 0x5d, 0x63, 0xc1, 0x95, 0x50, 0xe2, 0xce, 0x4d, 0x98, 0x26, 0x63, + 0x1a, 0x6a, 0x72, 0xc2, 0xcc, 0xb4, 0x98, 0x17, 0x70, 0xed, 0x23, 0xf8, 0x30, 0x2e, 0xba, 0xec, + 0xd2, 0x55, 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0xba, 0xb8, 0xe9, 0xdf, 0x39, 0xee, 0xec, + 0x96, 0x74, 0x12, 0x65, 0x27, 0x90, 0xbf, 0x8d, 0xcf, 0x48, 0x37, 0xfe, 0x8e, 0x31, 0xcc, 0xd4, + 0x1c, 0xcd, 0x0b, 0xd1, 0xfb, 0xa8, 0x3e, 0xaf, 0xb8, 0x69, 0x72, 0xfd, 0x0d, 0xfc, 0xc7, 0x2a, + 0xf9, 0x03, 0x79, 0xdf, 0xb9, 0xbb, 0x13, 0xdc, 0x3d, 0x7b, 0x39, 0xe3, 0x95, 0xde, 0x5e, 0x00, + 0x2c, 0xc3, 0x08, 0x72, 0x4e, 0x71, 0xc4, 0x43, 0x1c, 0xc7, 0x94, 0x30, 0x66, 0x5e, 0x8a, 0x06, + 0xaa, 0x01, 0xa3, 0xe0, 0x5e, 0x85, 0xbc, 0x56, 0xc4, 0x44, 0x02, 0xe3, 0xab, 0x9f, 0x5f, 0x6d, + 0xe4, 0xcf, 0x36, 0x7b, 0x0b, 0x6d, 0xf7, 0x16, 0xfa, 0xb1, 0xb7, 0xd0, 0x97, 0x83, 0xa5, 0x6d, + 0x0f, 0x96, 0xf6, 0xfd, 0x60, 0x69, 0xef, 0x5f, 0x26, 0x29, 0x5f, 0xac, 0xe6, 0x6e, 0x04, 0x99, + 0x97, 0x93, 0x15, 0xa7, 0x90, 0x3f, 0x07, 0x9a, 0xfc, 0x39, 0x7b, 0xeb, 0x91, 0xf7, 0xe9, 0x74, + 0x53, 0x78, 0x59, 0x10, 0x36, 0x6f, 0x88, 0x0d, 0x79, 0xf1, 0x3b, 0x00, 0x00, 0xff, 0xff, 0xcf, + 0xa7, 0x03, 0x49, 0xe4, 0x02, 0x00, 0x00, } func (this *GenesisDenom) Equal(that interface{}) bool { @@ -194,6 +205,9 @@ func (this *GenesisDenom) Equal(that interface{}) bool { if !this.AuthorityMetadata.Equal(&that1.AuthorityMetadata) { return false } + if this.HookContractAddress != that1.HookContractAddress { + return false + } return true } func (m *GenesisState) Marshal() (dAtA []byte, err error) { @@ -263,6 +277,13 @@ func (m *GenesisDenom) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if len(m.HookContractAddress) > 0 { + i -= len(m.HookContractAddress) + copy(dAtA[i:], m.HookContractAddress) + i = encodeVarintGenesis(dAtA, i, uint64(len(m.HookContractAddress))) + i-- + dAtA[i] = 0x1a + } { size, err := m.AuthorityMetadata.MarshalToSizedBuffer(dAtA[:i]) if err != nil { @@ -323,6 +344,10 @@ func (m *GenesisDenom) Size() (n int) { } l = m.AuthorityMetadata.Size() n += 1 + l + sovGenesis(uint64(l)) + l = len(m.HookContractAddress) + if l > 0 { + n += 1 + l + sovGenesis(uint64(l)) + } return n } @@ -543,6 +568,38 @@ func (m *GenesisDenom) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field HookContractAddress", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowGenesis + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthGenesis + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthGenesis + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.HookContractAddress = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex default: iNdEx = preIndex skippy, err := skipGenesis(dAtA[iNdEx:]) diff --git a/x/tokenfactory/types/genesis_test.go b/x/tokenfactory/types/genesis_test.go index 7e19f69b7..e3039050b 100644 --- a/x/tokenfactory/types/genesis_test.go +++ b/x/tokenfactory/types/genesis_test.go @@ -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()