I have this code that transfers a token using spl-token 0.2.x.
How do I have the same code work in 0.1.8? From my understanding of the docs, there were no breaking changes between the two, but the older version uses a Token class, but I'm not sure how to call it for the getOrCreateAssociatedTokenAccount
and transfer
functions.
async function transferToken(endpoint: string, fromWallet: Keypair, address_to: string, token_id: string)
{
const connection = new Connection(endpoint);
const toWalletPublicKey = new PublicKey(address_to);
const mint_key = new PublicKey(token_id);
// From
const from = [connection, fromWallet, mint_key, fromWallet.publicKey];
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(...from);
// To
const to = [connection, fromWallet, mint_key, toWalletPublicKey];
const toTokenAccount = await getOrCreateAssociatedTokenAccount(...to);
// Transfer
const transferParams = [connection, fromWallet, fromTokenAccount.address, toTokenAccount.address, fromWallet.publicKey, 1, []];
return await transfer(...transferParams);
}
This is how I pass the fromWallet
KeyPair
loaded from hex string.
const fromWallet = Keypair.fromSecretKey(Uint8Array.from(Buffer.from(private_key, 'hex')));