简体   繁体   中英

Why Solana RPC write Transaction failed to sanitize accounts offsets correctly?

I tried to copy my token swap transaction. And I get an error:

solana.rpc.core.RPCException: {'code': -32602, 'message': 'invalid transaction: Transaction failed to sanitize accounts offsets correctly'}

Successful Transaction - https://solscan.io/tx/5yL5MnDyLmDGu3xeR4gz8y3pJWGg5qXTzxMgd9qUPgYsPQ5ZUPkJt1DVHkmRAoqZUenHtDqgb4vsfkC1P2vjmKW9

Code:

from solana.transaction import AccountMeta, Transaction, TransactionInstruction
from solana.rpc.types import TxOpts
from solana.account import Account
from solana.rpc.api import Client
from solana.publickey import PublicKey
from solana.rpc.commitment import Recent, Root
from solana.keypair import Keypair
import base58
from spl.token.instructions import transfer, TransferParams

url = 'https://api.mainnet-beta.solana.com'
client = Client(url)

txn = Transaction(recent_blockhash=client.get_recent_blockhash()['result']['value']['blockhash'], fee_payer='8noB4Wv7rpUsz17eRD833G7VHnnmfp1JdstyXzuDvfoY')


byte_array = base58.b58decode('myprivatekey')
keypair = list(map(lambda b: int(str(b)), byte_array))[:]

account = Keypair(keypair[0:32])

txn.add(
    TransactionInstruction(
        keys=[
            AccountMeta(pubkey=PublicKey('6vdR9SgRuBtdiR6rgpXLcdjQgV7Ja7GtjqjL5EP3uQwK'), is_signer=False, is_writable=True),
            AccountMeta(pubkey=PublicKey('8noB4Wv7rpUsz17eRD833G7VHnnmfp1JdstyXzuDvfoY'), is_signer=True, is_writable=True),
            AccountMeta(pubkey=PublicKey('Dv3UEMWoX4zwviYDG1ZpdBNiNjmUbkoAmohZLPar3oyA'), is_signer=False, is_writable=True),
            AccountMeta(pubkey=PublicKey('2vfgEPJStq761qrkyh8xedrj9zpew1GQ8CobjtQ4wtyM'), is_signer=False, is_writable=False),
            AccountMeta(pubkey=PublicKey('2x3yujqB7LCMdCxV7fiZxPZStNy7RTYqWLSvnqtqjHR6'), is_signer=False, is_writable=True),
            AccountMeta(pubkey=PublicKey('2x3yujqB7LCMdCxV7fiZxPZStNy7RTYqWLSvnqtqjHR6'), is_signer=False, is_writable=True),
            AccountMeta(pubkey=PublicKey('6UzTmTpFt8pEbdAnstJrusWU1eWggPrNRsyjr9XaPVTX'), is_signer=False, is_writable=True),
            AccountMeta(pubkey=PublicKey('JD8dd73w3JigdsS7rKCqpVzFhzRXZcK2evZzxFURd21x'), is_signer=False, is_writable=True),
            AccountMeta(pubkey=PublicKey('FoXyMu5xwXre7zEoSvzViRk3nGawHUp9kUh97y2NDhcq'), is_signer=False, is_writable=True),
            AccountMeta(pubkey=PublicKey('2GUvz8bAtoeartcAPYifiBWjSVobjEF7jp7uC2cELMCx'), is_signer=False, is_writable=True),
            AccountMeta(pubkey=PublicKey('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL'), is_signer=False, is_writable=False),
            AccountMeta(pubkey=PublicKey('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'), is_signer=False, is_writable=False),
            AccountMeta(pubkey=PublicKey('11111111111111111111111111111111'), is_signer=False, is_writable=False),
            AccountMeta(pubkey=PublicKey('SysvarRent111111111111111111111111111111111'), is_signer=False, is_writable=False),
            
        ],
        program_id=PublicKey('8BYmYs3zsBhftNELJdiKsCN2WyCBbrTwXd6WG4AFPr6n'),
        data=bytes.fromhex('5052c1c9d81b46b801000000000000001027000000000000')
    )
)


inner_instruction_transfer = txn.add(transfer(TransferParams(
    amount=1,
    dest='JD8dd73w3JigdsS7rKCqpVzFhzRXZcK2evZzxFURd21x',
    owner='6vdR9SgRuBtdiR6rgpXLcdjQgV7Ja7GtjqjL5EP3uQwK',
    program_id='TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
    source='6UzTmTpFt8pEbdAnstJrusWU1eWggPrNRsyjr9XaPVTX',
)))


from solana.system_program import TransferParams, transfer
inner_instruction_sol_transfer = txn.add(transfer(TransferParams(from_pubkey='8noB4Wv7rpUsz17eRD833G7VHnnmfp1JdstyXzuDvfoY', to_pubkey='Dv3UEMWoX4zwviYDG1ZpdBNiNjmUbkoAmohZLPar3oyA', lamports=9800)))

inner_instruction_sol_transfer2 = txn.add(transfer(TransferParams(from_pubkey='8noB4Wv7rpUsz17eRD833G7VHnnmfp1JdstyXzuDvfoY', to_pubkey='2x3yujqB7LCMdCxV7fiZxPZStNy7RTYqWLSvnqtqjHR6', lamports=200)))



txn.sign(account)
rpc_response = client.send_transaction(
    txn,
    account,
    opts=TxOpts(skip_preflight=True, skip_confirmation=False)
)

print(rpc_response)

The issue is going to be from the second instruction that you're adding:

inner_instruction_transfer = txn.add(transfer(TransferParams(
    amount=1,
    dest='JD8dd73w3JigdsS7rKCqpVzFhzRXZcK2evZzxFURd21x',
    owner='6vdR9SgRuBtdiR6rgpXLcdjQgV7Ja7GtjqjL5EP3uQwK',
    program_id='TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
    source='6UzTmTpFt8pEbdAnstJrusWU1eWggPrNRsyjr9XaPVTX',
)))

The owner of the account in the second instruction, 6vdR9SgRuBtdiR6rgpXLcdjQgV7Ja7GtjqjL5EP3uQwK must sign this transaction for it to be valid. Currently, only account is signing, which I'm guessing is 8noB4Wv7rpUsz17eRD833G7VHnnmfp1JdstyXzuDvfoY .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM