使用 python 进行 Solana SPL 代币传输

发布于 2025-01-11 03:54:52 字数 2451 浏览 0 评论 0原文

因此,在阅读了几篇文章后,我尚未了解如何创建交易并通过 Solana 区块链发送自定义 SPL 代币。 我在下面附上了我的代码。
我真的不明白交易的每一部分应该是什么。
所以我认为所有者是发送和支付交易的帐户/钱包。我假设 dest 是我希望将令牌发送到的位置。
这是我希望发送的令牌(在 devnet 上),但我不发送似乎能够。

from spl.token.constants import TOKEN_PROGRAM_ID
from spl.token.instructions import transfer_checked, TransferCheckedParams

from solana.rpc.commitment import Confirmed
from solana.rpc.api import Client
from solana.rpc.types import TxOpts
from solana.keypair import Keypair
from solana.publickey import PublicKey
from solana.transaction import Transaction

import os
from dotenv import load_dotenv

class TransferService:
    def __init__(self, client: Client, service: SolanaService, token) -> None:
        self.client = client
        self.service = service
        self.keypair = self.service.get_keypair(token)

    def make_transaction(self, source, mint, dest, owner, amount=1, decimals=0) -> Transaction:
        transaction = Transaction()
        transaction.add(transfer_checked(
            TransferCheckedParams(
                program_id=TOKEN_PROGRAM_ID,
                mint=PublicKey(mint),
                source=PublicKey(source),
                dest=PublicKey(dest),
                owner=owner,
                amount=amount,
                decimals=decimals,
                signers=[]
        )))
        return transaction

    def send_transaction(self, transaction) -> None:
        self.client.send_transaction(
            transaction,
            self.keypair,
            opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed)
        )

load_dotenv()

if __name__ == "__main__":
    token = os.getenv('TOKEN')
    client = Client('https://api.devnet.solana.com')
    service = SolanaService(client)
    token = os.getenv('KEYPAIR')
    transfer = TransferService(client, service, token)
    a = client.get_account_info(transfer.keypair.public_key)
    transaction = transfer.make_transaction(
        source='CtURxXpzn9aredXse2KNtyDMeVW627tL3p7DCucdv8bc',
        mint='DCzbhHu3YGnc8Vhez4YEMznQ38ad6WYGVYqeB4Wn3mie',
        dest='sPkypr2LBtF5Go87zYSn5fBtWxCDEcobWeQQxXHpxJR',
        owner=transfer.keypair.public_key,
        amount=1,
        decimals=9

    )
    transfer.send_transaction(transaction)

So after having read a couple of articles I'm yet to understand how to create a Transaction and send custom SPL tokens across the Solana blockchain.
I've attached my code below.
I truly don't understand what each part of the transaction is supposed to be.
So I figured that owner is the account/wallet that is sending and paying for the transaction. And I'm assuming that dest is where I wish to send the tokens to.
This is the token (on devnet) that I wish to send, But I don't seem to be able.

from spl.token.constants import TOKEN_PROGRAM_ID
from spl.token.instructions import transfer_checked, TransferCheckedParams

from solana.rpc.commitment import Confirmed
from solana.rpc.api import Client
from solana.rpc.types import TxOpts
from solana.keypair import Keypair
from solana.publickey import PublicKey
from solana.transaction import Transaction

import os
from dotenv import load_dotenv

class TransferService:
    def __init__(self, client: Client, service: SolanaService, token) -> None:
        self.client = client
        self.service = service
        self.keypair = self.service.get_keypair(token)

    def make_transaction(self, source, mint, dest, owner, amount=1, decimals=0) -> Transaction:
        transaction = Transaction()
        transaction.add(transfer_checked(
            TransferCheckedParams(
                program_id=TOKEN_PROGRAM_ID,
                mint=PublicKey(mint),
                source=PublicKey(source),
                dest=PublicKey(dest),
                owner=owner,
                amount=amount,
                decimals=decimals,
                signers=[]
        )))
        return transaction

    def send_transaction(self, transaction) -> None:
        self.client.send_transaction(
            transaction,
            self.keypair,
            opts=TxOpts(skip_confirmation=False, preflight_commitment=Confirmed)
        )

load_dotenv()

if __name__ == "__main__":
    token = os.getenv('TOKEN')
    client = Client('https://api.devnet.solana.com')
    service = SolanaService(client)
    token = os.getenv('KEYPAIR')
    transfer = TransferService(client, service, token)
    a = client.get_account_info(transfer.keypair.public_key)
    transaction = transfer.make_transaction(
        source='CtURxXpzn9aredXse2KNtyDMeVW627tL3p7DCucdv8bc',
        mint='DCzbhHu3YGnc8Vhez4YEMznQ38ad6WYGVYqeB4Wn3mie',
        dest='sPkypr2LBtF5Go87zYSn5fBtWxCDEcobWeQQxXHpxJR',
        owner=transfer.keypair.public_key,
        amount=1,
        decimals=9

    )
    transfer.send_transaction(transaction)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红衣飘飘貌似仙 2025-01-18 03:54:52

目标 sPkypr2LBtF5Go87zYSn5fBtWxCDEcobWeQQxXHpxJR 不正确。

当您发送SPL代币时,sourcedest必须是代币账户的地址,在这种情况下,sPkypr2LBtF5Go87zYSn5fBtWxCDEcobWeQQxXHpxJR是一个钱包,所以您需要为此钱包创建一个接收者帐户。

首选标准是使用关联的令牌帐户,该帐户是使用 solana-py 客户端的 SPL 部分中的 create_linked_token_account 之类的内容创建的:https://github.com/michaelhly/solana-py/blob/2c45353cb510bfeb7259fa19dacbaefe6b9ae3d1/src/spl/token/client.py#L173

作为参考,最重要的部分是创建指令来创建关联令牌帐户:

def create_associated_token_account(payer: PublicKey, owner: PublicKey, mint: PublicKey) -> TransactionInstruction:
    associated_token_address = PublicKey.find_program_address(
        seeds=[bytes(owner), bytes(TOKEN_PROGRAM_ID), bytes(mint)], program_id=ASSOCIATED_TOKEN_PROGRAM_ID
    )
    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=payer, is_signer=True, is_writable=True),
            AccountMeta(pubkey=associated_token_address, is_signer=False, is_writable=True),
            AccountMeta(pubkey=owner, is_signer=False, is_writable=False),
            AccountMeta(pubkey=mint, is_signer=False, is_writable=False),
            AccountMeta(pubkey=SYS_PROGRAM_ID, is_signer=False, is_writable=False),
            AccountMeta(pubkey=TOKEN_PROGRAM_ID, is_signer=False, is_writable=False),
            AccountMeta(pubkey=SYSVAR_RENT_PUBKEY, is_signer=False, is_writable=False),
        ],
        program_id=ASSOCIATED_TOKEN_PROGRAM_ID,
    )

有关关联令牌帐户的更多背景信息,请访问 https://spl.solana.com/linked-token-account

The destination sPkypr2LBtF5Go87zYSn5fBtWxCDEcobWeQQxXHpxJR is incorrect.

When you send SPL tokens, the source and dest must be addresses of token accounts, and in this case, sPkypr2LBtF5Go87zYSn5fBtWxCDEcobWeQQxXHpxJR is a wallet, so you'll need to create a recipient account for this wallet.

The preferred standard is to use an associated token account, created using something like create_associated_token_account from the SPL part of solana-py client: https://github.com/michaelhly/solana-py/blob/2c45353cb510bfeb7259fa19dacbaefe6b9ae3d1/src/spl/token/client.py#L173

For reference, the most important part is creating the instruction to create the associated token account:

def create_associated_token_account(payer: PublicKey, owner: PublicKey, mint: PublicKey) -> TransactionInstruction:
    associated_token_address = PublicKey.find_program_address(
        seeds=[bytes(owner), bytes(TOKEN_PROGRAM_ID), bytes(mint)], program_id=ASSOCIATED_TOKEN_PROGRAM_ID
    )
    return TransactionInstruction(
        keys=[
            AccountMeta(pubkey=payer, is_signer=True, is_writable=True),
            AccountMeta(pubkey=associated_token_address, is_signer=False, is_writable=True),
            AccountMeta(pubkey=owner, is_signer=False, is_writable=False),
            AccountMeta(pubkey=mint, is_signer=False, is_writable=False),
            AccountMeta(pubkey=SYS_PROGRAM_ID, is_signer=False, is_writable=False),
            AccountMeta(pubkey=TOKEN_PROGRAM_ID, is_signer=False, is_writable=False),
            AccountMeta(pubkey=SYSVAR_RENT_PUBKEY, is_signer=False, is_writable=False),
        ],
        program_id=ASSOCIATED_TOKEN_PROGRAM_ID,
    )

More background information about associated token accounts at https://spl.solana.com/associated-token-account

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文