@solana/spl-token#createTransferInstruction TypeScript Examples
The following examples show how to use
@solana/spl-token#createTransferInstruction.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: associatedToken.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
* add unWrap SOL IX
* @param {TransactionInstruction[]} instructions
* @param {Provider} provider
* @param {owner} owner
* @param {tokenAccount} tokenAccount
* @param {mint} mint
* @param {amount} amount
*/
static async withUnwrapIfNative(
instructions: TransactionInstruction[],
provider: AnchorProvider,
owner: PublicKey, //user pubkey
tokenAccount: PublicKey,
mint: PublicKey,
amount: BN
): Promise<void> {
if (mint.equals(NATIVE_MINT)) {
//create a new ata if ata doesn't not exist
const ata = await this.withCreate(instructions, provider, owner, mint)
//IX to transfer wSOL to ATA
const transferIx = createTransferInstruction(tokenAccount, ata, owner, BigInt(amount.toString()))
//add transfer IX
instructions.push(transferIx)
//add close account IX
await this.withClose(instructions, owner, mint, owner)
}
}
Example #2
Source File: solana.tx.ts From tatum-js with MIT License | 6 votes |
transferNft = async (
body: TransferSolanaNft,
web3: SolanaWeb3,
provider?: string,
feePayer?: string,
feePayerPrivateKey?: string,
) => {
const connection = web3.getClient(provider)
const from = new PublicKey(body.from as string)
const transaction = new Transaction({ feePayer: feePayer ? new PublicKey(feePayer) : from })
const walletAddress = new PublicKey(body.to)
const mint = new PublicKey(body.contractAddress)
const toTokenAccountAddress = (
await PublicKey.findProgramAddress(
[new PublicKey(body.to).toBuffer(), TOKEN_PROGRAM_ID.toBuffer(), mint.toBuffer()],
SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID,
)
)[0]
const fromTokenAddress = await getAssociatedTokenAddress(mint, from)
transaction.add(
createAssociatedTokenAccountInstruction(toTokenAccountAddress, from, walletAddress, mint),
createTransferInstruction(fromTokenAddress, toTokenAccountAddress, from, 1, [], TOKEN_PROGRAM_ID),
)
if (body.signatureId) {
transaction.recentBlockhash = '7WyEshBZcZwEbJsvSeGgCkSNMxxxFAym3x7Cuj6UjAUE'
return { txData: transaction.compileMessage().serialize().toString('hex') }
}
const signers = [web3.generateKeyPair(body.fromPrivateKey)]
if (feePayerPrivateKey) {
signers.push(web3.generateKeyPair(feePayerPrivateKey))
}
return {
txId: await connection.sendTransaction(transaction, signers),
}
}
Example #3
Source File: solana.tx.ts From tatum-js with MIT License | 5 votes |
transferSplToken = async (
body: TransferSolanaSpl,
web3: SolanaWeb3,
provider?: string,
feePayer?: string,
feePayerPrivateKey?: string,
) => {
const connection = web3.getClient(provider)
const from = new PublicKey(body.from as string)
const transaction = new Transaction({ feePayer: feePayer ? new PublicKey(feePayer) : from })
const mint = new PublicKey(body.contractAddress)
const to = new PublicKey(body.to)
const fromTokenAddress = await getAssociatedTokenAddress(mint, from)
const toTokenAccountAddress = await getAssociatedTokenAddress(mint, to)
try {
await getAccount(connection, toTokenAccountAddress)
} catch (e) {
transaction.add(createAssociatedTokenAccountInstruction(toTokenAccountAddress, from, to, mint))
}
transaction.add(
createTransferInstruction(
fromTokenAddress,
toTokenAccountAddress,
from,
new BigNumber(body.amount).multipliedBy(10 ** body.digits).toNumber(),
[],
TOKEN_PROGRAM_ID,
),
)
if (body.signatureId) {
transaction.recentBlockhash = '7WyEshBZcZwEbJsvSeGgCkSNMxxxFAym3x7Cuj6UjAUE'
return { txData: transaction.compileMessage().serialize().toString('hex') }
}
const signers = [web3.generateKeyPair(body.fromPrivateKey)]
if (feePayerPrivateKey) {
signers.push(web3.generateKeyPair(feePayerPrivateKey))
}
return {
txId: await connection.sendTransaction(transaction, signers),
}
}