How do I transfer APT to an account that doesn't exist on-chain yet?

1000 Views Asked by At

As I understand it, if you transfer APT to an account that doesn't exist on-chain yet, doing so will create the account on-chain. I've been trying to use 0x1::coin::transfer, but that doesn't seem to work.

Move abort in 0x1::coin: ECOIN_STORE_NOT_PUBLISHED(0x60005): Account hasn't registered `CoinStore` for `CoinType`

What should I do instead?

2

There are 2 best solutions below

3
On BEST ANSWER

This is a bit confusing but there are two different functions.

  • This one only transfers coins if the account already exists on-chain: 0x1::coin::transfer. Code.
  • This one will create the account first if it doesn't exist yet: 0x1::aptos_account::transfer. Code.

You can see in the second code link that it first checks if the account exists and creates it if it doesn't, and then calls 0x1::coin::transfer.

0
On

In addition to the previous answer, see this snippet demonstrating how to transfer coins with the TS SDK:

import { AptosAccount, HexString } from "aptos";

const privateKeyHex =
  "0xdcaf65ead38f7cf0cb1f81961f8fc7f9b7f1e2f45e2d4a6da0dbef85f46f6054"; // fake private key of course
const privateKeyBytes = HexString.ensure(privateKeyHex).toUint8Array();
const myAccount = new AptosAccount(privateKeyBytes);

const txnHash = await coinClient.transfer(
  myAccount,
  "0x232098630cfad4734812fa37dc18d9b8d59242feabe49259e26318d468a99584",
  717,
  { createReceiverIfMissing: true }
);

await client.waitForTransaction(txnHash, { checkSuccess: true });
  • If createReceiverIfMissing is false, it calls 0x1::coin::transfer.
  • If createReceiverIfMissing is true, it calls 0x1::aptos_account::transfer.