Skip to content

Instantly share code, notes, and snippets.

@sevazhidkov
Created July 8, 2022 11:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sevazhidkov/1728a07cf0cd14f392424ae325c00818 to your computer and use it in GitHub Desktop.
Save sevazhidkov/1728a07cf0cd14f392424ae325c00818 to your computer and use it in GitHub Desktop.
const axios = require('axios').default;
const { Keypair, PublicKey, Connection, Transaction, sendAndConfirmRawTransaction, } = require('@solana/web3.js');
const {
createAssociatedTokenAccountInstruction,
createTransferInstruction,
getAssociatedTokenAddress,
} = require('@solana/spl-token');
const base58 = require('bs58');
const baseURL = 'https://octane-mainnet-beta.breakroom.show/api';
async function main() {
const connection = new Connection('https://api.mainnet-beta.solana.com', 'confirmed');
// (1) Preparation
// Let's now connect to Octane instance and get some needed params:
// 1) how much we should pay for transaction in tokens
// 2) which account will pay for transaction in SOL (feePayer)
const response = (await axios.get(baseURL, {
headers: {'Accept': 'application/json'}
})).data;
const feePayer = new PublicKey(response.feePayer);
const mint = new PublicKey(response.endpoints.createAccount.tokens[0].mint);
const feeAccount = new PublicKey(response.endpoints.createAccount.tokens[0].account);
const feeToCreateAccount = response.endpoints.createAccount.tokens[0].fee;
// (2) Prepare the hypothetical end user account
const endUserKeypair = Keypair.fromSecretKey(base58.decode(process.env.SECRET_KEY));
const endUserTokenAccount = await getAssociatedTokenAddress(mint, endUserKeypair.publicKey);
// (3) Create the transaction payload
const targetOwner = Keypair.generate();
const targetAccount = await getAssociatedTokenAddress(mint, targetOwner.publicKey);
// The transaction would consist of two instructions:
// 1) token transfer to octane's token account as payment for running transaction gasless
// 2) the payload, actual instruction we want to execute. in this case, it's also a token transfer to targetKeypair, but it could be anything.
const transaction = new Transaction();
transaction.add(createTransferInstruction(endUserTokenAccount, feeAccount, endUserKeypair.publicKey, feeToCreateAccount));
transaction.add(createAssociatedTokenAccountInstruction(
feePayer,
targetAccount,
targetOwner.publicKey,
mint
));
transaction.feePayer = feePayer;
transaction.recentBlockhash = (await connection.getLatestBlockhash()).blockhash;
transaction.partialSign(endUserKeypair); // in client code, you should use wallet adapter instead
// (4) Send the transaction to octane to sign and attach signature to transaction
const octaneResponse = (await axios.post(baseURL + '/createAccount', {
transaction: base58.encode(transaction.serialize({requireAllSignatures: false})),
})).data;
transaction.addSignature(feePayer, base58.decode(octaneResponse.signature));
// (5) Send transaction to network
await sendAndConfirmRawTransaction(connection, transaction.serialize(), { commitment: 'confirmed' });
console.log('Find on mainnet explorer ///', octaneResponse.signature);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment