5

I am writing a program in which the accepted payment MUST be from a token account. Anchor context snippet here:

pub struct Payment<'info> { 
    #[account(mut)]
    pub buyer_token_account: Account<'info, TokenAccount>
    ...
    ...

}

In order for the user to pay via SOL, i am currently doing 3 separate transactions:

  1. Create WSOL account
  2. Fund WSOL account with SOL
  3. Call Payment with WSOL account

Although, this results in the user having to accept more than one transaction.

So I wanted to know if there are ways to do this at once, such that the user only sees SOL being deducted in the Phantom Wallet summary. I.e. - 1 SOL, and not -1 SOL, + 1 WSOL.

I assume that I could create a transaction consisting of all 3 instructions. Would this work? If yes, are there other ways to do this as well?

1
  • 2
    what is "pure" SOL? Native SOL is the protocol base economic unit. wrapped SOL is the SPL Token wrapped equivalent. can you please update your question to clarify these terms?
    – trent.sol
    Commented Jul 28, 2022 at 0:01

1 Answer 1

6

Each transaction will require the wallet to sign the transaction.

Rather than three separate transactions, create one transaction with the three instructions. This is the only way to have the user accept only one transaction.

Something along the lines of:

const transaction = new web3.Transaction();

const createWsolIx = new web3.TransactionInstruction({
// some instruction
});

const fundWsolIx = new web3.TransactionInstruction({
// some instruction
});

const transferWsol = new web3.TransactionInstruction({
// some instruction
});

transaction.add(createWsolIx, fundWsolIx, transferWsol);

const tx = await web3.sendAndConfirmTransaction(connection, transaction, []);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.