1

I managed to send some USDC-dev token on the devnet with the following code. Is there a way to send more than 1 transaction with something like Transaction.add()

Here is the working code for 1 transaction

from solana.rpc.api import Client
from spl.token.client import Token
from solders.pubkey import Pubkey
from solders.keypair import Keypair
from solders.system_program import TransferParams, transfer
from solana.transaction import Transaction

mint = Pubkey.from_string(
    "Gh9ZwEmdLJ8DscKNTkTqPbNwLNNBjuSzaG9Vp2KGtKJr"
)  # eg: https://solscan.io/token/FpekncBMe3Vsi1LMkh6zbNq8pdM6xEbNiFsJBRcPbMDQ**
program_id = Pubkey.from_string(
    "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
)  # eg: https://solscan.io/account/**TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA**

privkey = "6614rQjZYrcUL8YtNQ8vcf.......VM8hJwgMPnSPdBRGHSxxroJLn"
key_pair = Keypair.from_base58_string(privkey)

solana_client = Client("https://api.devnet.solana.com")
spl_client = Token(
    conn=solana_client, pubkey=mint, program_id=program_id, payer=key_pair
)

source = Pubkey.from_string("BAPkhTqGe4Squ8hH71QzixhTcfhJv7Lb8SvV7To5gzPg")
dest = Pubkey.from_string("3ApghjTLzyAfBvq9z1kQkfTp5puoZHsKZZ9ziwBvR8T1")

try:
    source_token_account = (
        spl_client.get_accounts_by_owner(
            owner=source, commitment=None, encoding="base64"
        )
        .value[0]
        .pubkey
    )
    
except:
    source_token_account = spl_client.create_associated_token_account(
        owner=source, skip_confirmation=False, recent_blockhash=None
    )
    
try:
    dest_token_account = (
        spl_client.get_accounts_by_owner(owner=dest, commitment=None, encoding="base64")
        .value[0]
        .pubkey
    )
    
except:
    dest_token_account = spl_client.create_associated_token_account(
        owner=dest, skip_confirmation=False, recent_blockhash=None
    )

amount = 10

txn = Transaction()

transaction = spl_client.transfer(
    source=source_token_account,
    dest=dest_token_account,
    owner=key_pair,
    amount=int(float(amount) * 100000),
    multi_signers=None,
    opts=None,
    recent_blockhash=None,
)

print(transaction)

1 Answer 1

1

Certainly! The txn in your example is actually a SendTransactionResp after you call transfer on the spl_client, and it's doing the work for you of constructing and sending the transaction. You can see its definition at https://github.com/michaelhly/solana-py/blob/15a87982d91f037b53197e4d6cb73bde397eee10/src/spl/token/client.py#L354

In your case, you'll want to construct a transaction and add multiple instructions to it, ie:

txn = Transaction(fee_payer=key_pair.pubkey()).add(
            spl_token.transfer(
                spl_token.TransferParams(
                    program_id=program_id,
                    source=source_token_account,
                    dest=dest_token_account,
                    owner=key_pair.pubkey(),
                    amount=amount,
                    signers=[],
                )
            )
        ).add(
            spl_token.transfer(
                spl_token.TransferParams(
                    program_id=program_id,
                    source=source_token_account,
                    dest=dest_token_account,
                    owner=key_pair.pubkey(),
                    amount=amount,
                    signers=[],
                )
            )
        )
solana_client.send_transaction(txn, key_pair, opts=opts, recent_blockhash=recent_blockhash)

This was lifted from the implementation for transfer at https://github.com/michaelhly/solana-py/blob/15a87982d91f037b53197e4d6cb73bde397eee10/src/spl/token/core.py#L281, and has not been compiled or tested, so there may be issues.

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.