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

feat!: create account if NFT recipient account does not exist #319

Merged
merged 1 commit into from
Dec 16, 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
39 changes: 37 additions & 2 deletions x/ibc/nft-transfer/keeper/integration_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package keeper_test

import (
"crypto/rand"

channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"

sdktypes "github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"

"github.com/initia-labs/initia/x/ibc/nft-transfer/types"
ibctesting "github.com/initia-labs/initia/x/ibc/testing"
Expand Down Expand Up @@ -51,7 +53,7 @@ func (suite *KeeperTestSuite) CreateNftClass(

func (suite *KeeperTestSuite) MintNft(
endpoint *ibctesting.Endpoint,
receiver sdktypes.AccAddress,
receiver sdk.AccAddress,
classId, className, tokenId, tokenUri, tokenData string,
) {
classNameBz, err := vmtypes.SerializeString(className)
Expand Down Expand Up @@ -224,6 +226,39 @@ func (suite *KeeperTestSuite) TestSendAndReceive() {
suite.ConfirmClassId(pathA2B.EndpointA, classId, targetClassId)
}
})

// transfer from chainA to chainB (non-existing account)
suite.Run("transfer A->B (non-existing account)", func() {
{
sender := pathA2B.EndpointA.Chain.SenderAccount.GetAddress()

// random receiver account
receiver := make(sdk.AccAddress, 20)
_, err := rand.Read(receiver)
suite.NoError(err)

packet = suite.transferNft(
pathA2B.EndpointA,
pathA2B.EndpointB,
targetClassId,
nftId,
sender.String(),
receiver.String(),
)

targetClassId = suite.receiverNft(
pathA2B.EndpointA,
pathA2B.EndpointB,
packet,
)

suite.ConfirmClassId(pathA2B.EndpointB, classId, targetClassId)

// receiver account should be created
suite.True(suite.chainB.App.GetAccountKeeper().HasAccount(suite.chainB.GetContext(), receiver))
}
})

}

func (suite *KeeperTestSuite) transferNft(
Expand Down
5 changes: 5 additions & 0 deletions x/ibc/nft-transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
return err
}

// create account if receiver does not exist
if !k.authKeeper.HasAccount(ctx, receiver) {
k.authKeeper.SetAccount(ctx, k.authKeeper.NewAccountWithAddress(ctx, receiver))
}
beer-1 marked this conversation as resolved.
Show resolved Hide resolved

labels := []metrics.Label{
telemetry.NewLabel(coretypes.LabelSourcePort, packet.GetSourcePort()),
telemetry.NewLabel(coretypes.LabelSourceChannel, packet.GetSourceChannel()),
Expand Down
4 changes: 4 additions & 0 deletions x/ibc/nft-transfer/types/expected_keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type AccountKeeper interface {
AddressCodec() address.Codec
GetModuleAddress(name string) sdk.AccAddress
GetModuleAccount(ctx context.Context, name string) sdk.ModuleAccountI

SetAccount(ctx context.Context, acc sdk.AccountI)
HasAccount(ctx context.Context, addr sdk.AccAddress) bool
NewAccountWithAddress(ctx context.Context, addr sdk.AccAddress) sdk.AccountI
}

type NftKeeper interface {
Expand Down
Loading