Created
August 6, 2021 00:07
-
-
Save ezy/e03de099ca77faf3983abb5d867ece6b to your computer and use it in GitHub Desktop.
Solana token transaction dummy run template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export async function createInteractionInstruction( | |
connection: Connection | |
): Promise<string> { | |
console.log('Creating swap token a account'); | |
const owner = await newAccountWithLamports(connection, 1000000000); | |
const payer = await newAccountWithLamports(connection, 1000000000); | |
const SWAP_AMOUNT_IN = 100000; | |
const POOL_TOKEN_AMOUNT = 10000000; | |
let currentSwapTokenA = 1000000; | |
let currentSwapTokenB = 1000000; | |
const tokenSwapAccount = new Keypair(); | |
const [authority, nonce] = await PublicKey.findProgramAddress( | |
[tokenSwapAccount.publicKey.toBuffer()], | |
TOKEN_PROGRAM_ID, | |
); | |
const supply = (await connection.getTokenSupply(SOSOL_TOKEN_ID)).value.uiAmount || 0; | |
console.log('creating token A'); | |
const mintA = await Token.createMint( | |
connection, | |
payer, | |
owner.publicKey, | |
null, | |
2, | |
TOKEN_PROGRAM_ID, | |
); | |
console.log('creating token A account'); | |
const tokenAccountA = await mintA.createAccount(authority); | |
console.log('minting token A to swap'); | |
await mintA.mintTo(tokenAccountA, owner, [], currentSwapTokenA); | |
console.log('creating token B'); | |
const mintB = await Token.createMint( | |
connection, | |
payer, | |
owner.publicKey, | |
null, | |
2, | |
TOKEN_PROGRAM_ID, | |
); | |
console.log('creating token B account'); | |
const tokenAccountB = await mintB.createAccount(authority); | |
console.log('minting token B to swap'); | |
await mintB.mintTo(tokenAccountB, owner, [], currentSwapTokenB); | |
const balanceNeeded = await Token.getMinBalanceRentForExemptAccount( | |
connection, | |
); | |
const swapTokenA = await mintA.getAccountInfo(tokenAccountA); | |
const tokenA = Math.floor( | |
(swapTokenA.amount.toNumber() * POOL_TOKEN_AMOUNT) / supply, | |
); | |
const swapTokenB = await mintB.getAccountInfo(tokenAccountB); | |
const tokenB = Math.floor( | |
(swapTokenB.amount.toNumber() * POOL_TOKEN_AMOUNT) / supply, | |
); | |
const userTransferAuthority = new Keypair(); | |
console.log('Creating depositor token a account'); | |
const userAccountA = await mintA.createAccount(owner.publicKey); | |
await mintA.mintTo(userAccountA, owner, [], tokenA); | |
await mintA.approve( | |
userAccountA, | |
userTransferAuthority.publicKey, | |
owner, | |
[], | |
tokenA, | |
); | |
console.log('Creating depositor token b account'); | |
const userAccountB = await mintB.createAccount(owner.publicKey); | |
await mintB.mintTo(userAccountB, owner, [], tokenB); | |
await mintB.approve( | |
userAccountB, | |
userTransferAuthority.publicKey, | |
owner, | |
[], | |
tokenB, | |
); | |
const newAccount = new Keypair(); | |
const transaction = new Transaction(); | |
transaction.add( | |
SystemProgram.createAccount({ | |
fromPubkey: owner.publicKey, | |
newAccountPubkey: newAccount.publicKey, | |
lamports: balanceNeeded, | |
space: AccountLayout.span, | |
programId: mintB.programId, | |
}), | |
); | |
transaction.add( | |
Token.createInitAccountInstruction( | |
mintB.programId, | |
mintB.publicKey, | |
newAccount.publicKey, | |
owner.publicKey, | |
), | |
); | |
transaction.add( | |
Token.createApproveInstruction( | |
mintA.programId, | |
userAccountA, | |
userTransferAuthority.publicKey, | |
owner.publicKey, | |
[owner], | |
SWAP_AMOUNT_IN, | |
), | |
); | |
// Send the instructions | |
console.log('sending big instruction'); | |
return await sendAndConfirmTransaction( | |
'create account, approve transfer, swap', | |
connection, | |
transaction, | |
owner, | |
newAccount, | |
userTransferAuthority, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment