- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 solana-wallet 适配器传输自定义 SPL token 。但是,我无法获取钱包的 key /签署交易。
我已经查看了这些用于编写传输代码的答案,但我需要获取签名者并且我无法弄清楚如何使用 solana-wallet 适配器:
How can you transfer SOL using the web3.js sdk for Solana?
How to transfer custom token by '@solana/web3.js'
这些示例对 key 进行硬编码,因为我使用的是钱包扩展,所以这是不可能的。
根据 webadapter repo https://github.com/solana-labs/wallet-adapter/issues/120 上的这个问题你需要:
const SendTransaction: React.FC<Props> = ({ children }) => {
const { connection } = useConnection()
const { publicKey, sendTransaction } = useWallet()
const onSendSPLTransaction = useCallback(
async (toPubkey: string, amount: number) => {
if (!toPubkey || !amount) return
const toastId = toast.loading('Processing transaction...')
try {
if (!publicKey) throw new WalletNotConnectedError()
const toPublicKey = new PublicKey(toPubkey)
const mint = new PublicKey('Mint address')
const payer = '????' // how to get this Signer
const token = new Token(connection, mint, TOKEN_PROGRAM_ID, payer)
const fromTokenAccount = await token.getOrCreateAssociatedAccountInfo(publicKey)
const toTokenAccount = await token.getOrCreateAssociatedAccountInfo(toPublicKey)
const transaction = new Transaction().add(
Token.createTransferInstruction(
TOKEN_PROGRAM_ID,
fromTokenAccount.address,
toTokenAccount.address,
publicKey,
[],
0
)
)
const signature = await sendTransaction(transaction, connection)
const response = await connection.confirmTransaction(signature, 'processed')
console.log('response', response)
toast.success('Transaction sent', {
id: toastId,
})
} catch (error) {
toast.error(`Transaction failed: ${error.message}`, {
id: toastId,
})
}
},
[publicKey, sendTransaction, connection]
)
return <>{children(onSendSPLTransaction)}</>
}
最佳答案
所以我找到了一种方法来做到这一点,它需要一些清理和错误处理,但允许通过 @solana/wallet-adapter
进行自定义 token 事务.
// sendTransaction.tsx
import { WalletNotConnectedError } from '@solana/wallet-adapter-base'
import { useConnection, useWallet } from '@solana/wallet-adapter-react'
import { Transaction, PublicKey, LAMPORTS_PER_SOL } from '@solana/web3.js'
import React, { useCallback } from 'react'
import { toast } from 'react-hot-toast'
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { getOrCreateAssociatedTokenAccount } from './getOrCreateAssociatedTokenAccount'
import { createTransferInstruction } from './createTransferInstructions'
interface Props {
children: (sendTransaction: OnSendTransaction) => React.ReactNode
}
type OnSendTransaction = (toPublicKey: string, amount: number) => void
// Docs: https://github.com/solana-labs/solana-program-library/pull/2539/files
// https://github.com/solana-labs/wallet-adapter/issues/189
// repo: https://github.com/solana-labs/example-token/blob/v1.1/src/client/token.js
// creating a token for testing: https://learn.figment.io/tutorials/sol-mint-token
const SendTransaction: React.FC<Props> = ({ children }) => {
const { connection } = useConnection()
const { publicKey, signTransaction, sendTransaction } = useWallet()
const onSendSPLTransaction = useCallback(
async (toPubkey: string, amount: number) => {
if (!toPubkey || !amount) return
const toastId = toast.loading('Processing transaction...')
try {
if (!publicKey || !signTransaction) throw new WalletNotConnectedError()
const toPublicKey = new PublicKey(toPubkey)
const mint = new PublicKey('MINT ADDRESS')
const fromTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
publicKey,
mint,
publicKey,
signTransaction
)
const toTokenAccount = await getOrCreateAssociatedTokenAccount(
connection,
publicKey,
mint,
toPublicKey,
signTransaction
)
const transaction = new Transaction().add(
createTransferInstruction(
fromTokenAccount.address, // source
toTokenAccount.address, // dest
publicKey,
amount * LAMPORTS_PER_SOL,
[],
TOKEN_PROGRAM_ID
)
)
const blockHash = await connection.getRecentBlockhash()
transaction.feePayer = await publicKey
transaction.recentBlockhash = await blockHash.blockhash
const signed = await signTransaction(transaction)
await connection.sendRawTransaction(signed.serialize())
toast.success('Transaction sent', {
id: toastId,
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
toast.error(`Transaction failed: ${error.message}`, {
id: toastId,
})
}
},
[publicKey, sendTransaction, connection]
)
return <>{children(onSendSPLTransaction)}</>
}
export default SendTransaction
// getOrCreateAssociatedTokenAccount.ts
import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { SignerWalletAdapterProps } from '@solana/wallet-adapter-base'
import { Connection, PublicKey, Commitment, Transaction } from '@solana/web3.js'
import { createAssociatedTokenAccountInstruction } from './createAssociatedTokenAccountInstruction'
import { getAccountInfo } from './getAccountInfo'
import { getAssociatedTokenAddress } from './getAssociatedTokerAddress'
export async function getOrCreateAssociatedTokenAccount(
connection: Connection,
payer: PublicKey,
mint: PublicKey,
owner: PublicKey,
signTransaction: SignerWalletAdapterProps['signTransaction'],
allowOwnerOffCurve = false,
commitment?: Commitment,
programId = TOKEN_PROGRAM_ID,
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID
) {
const associatedToken = await getAssociatedTokenAddress(
mint,
owner,
allowOwnerOffCurve,
programId,
associatedTokenProgramId
)
// This is the optimal logic, considering TX fee, client-side computation, RPC roundtrips and guaranteed idempotent.
// Sadly we can't do this atomically.
let account
try {
account = await getAccountInfo(connection, associatedToken, commitment, programId)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
// TokenAccountNotFoundError can be possible if the associated address has already received some lamports,
// becoming a system account. Assuming program derived addressing is safe, this is the only case for the
// TokenInvalidAccountOwnerError in this code path.
if (error.message === 'TokenAccountNotFoundError' || error.message === 'TokenInvalidAccountOwnerError') {
// As this isn't atomic, it's possible others can create associated accounts meanwhile.
try {
const transaction = new Transaction().add(
createAssociatedTokenAccountInstruction(
payer,
associatedToken,
owner,
mint,
programId,
associatedTokenProgramId
)
)
const blockHash = await connection.getRecentBlockhash()
transaction.feePayer = await payer
transaction.recentBlockhash = await blockHash.blockhash
const signed = await signTransaction(transaction)
const signature = await connection.sendRawTransaction(signed.serialize())
await connection.confirmTransaction(signature)
} catch (error: unknown) {
// Ignore all errors; for now there is no API-compatible way to selectively ignore the expected
// instruction error if the associated account exists already.
}
// Now this should always succeed
account = await getAccountInfo(connection, associatedToken, commitment, programId)
} else {
throw error
}
}
if (!account.mint.equals(mint.toBuffer())) throw Error('TokenInvalidMintError')
if (!account.owner.equals(owner.toBuffer())) throw new Error('TokenInvalidOwnerError')
return account
}
// createAssociatedTokenAccountInstruction.ts
import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { PublicKey, TransactionInstruction, SystemProgram, SYSVAR_RENT_PUBKEY } from '@solana/web3.js'
export function createAssociatedTokenAccountInstruction(
payer: PublicKey,
associatedToken: PublicKey,
owner: PublicKey,
mint: PublicKey,
programId = TOKEN_PROGRAM_ID,
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID
): TransactionInstruction {
const keys = [
{ pubkey: payer, isSigner: true, isWritable: true },
{ pubkey: associatedToken, isSigner: false, isWritable: true },
{ pubkey: owner, isSigner: false, isWritable: false },
{ pubkey: mint, isSigner: false, isWritable: false },
{ pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
{ pubkey: programId, isSigner: false, isWritable: false },
{ pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false },
]
return new TransactionInstruction({
keys,
programId: associatedTokenProgramId,
data: Buffer.alloc(0),
})
}
// createTransferInstructions.ts
import { TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { AccountMeta, PublicKey, Signer, TransactionInstruction } from '@solana/web3.js'
import BufferLayout from 'buffer-layout'
import BN from 'bn.js'
export enum TokenInstruction {
InitializeMint = 0,
InitializeAccount = 1,
InitializeMultisig = 2,
Transfer = 3,
Approve = 4,
Revoke = 5,
SetAuthority = 6,
MintTo = 7,
Burn = 8,
CloseAccount = 9,
FreezeAccount = 10,
ThawAccount = 11,
TransferChecked = 12,
ApproveChecked = 13,
MintToChecked = 14,
BurnChecked = 15,
InitializeAccount2 = 16,
SyncNative = 17,
InitializeAccount3 = 18,
InitializeMultisig2 = 19,
InitializeMint2 = 20,
}
/**
* Construct a Transfer instruction
*
* @param source Source account
* @param destination Destination account
* @param owner Owner of the source account
* @param amount Number of tokens to transfer
* @param multiSigners Signing accounts if `owner` is a multisig
* @param programId SPL Token program account
*
* @return Instruction to add to a transaction
*/
export function createTransferInstruction(
source: PublicKey,
destination: PublicKey,
owner: PublicKey,
amount: number,
multiSigners: Signer[] = [],
programId = TOKEN_PROGRAM_ID
): TransactionInstruction {
const dataLayout = BufferLayout.struct([BufferLayout.u8('instruction'), BufferLayout.blob(8, 'amount')])
const keys = addSigners(
[
{ pubkey: source, isSigner: false, isWritable: true },
{ pubkey: destination, isSigner: false, isWritable: true },
],
owner,
multiSigners
)
const data = Buffer.alloc(dataLayout.span)
dataLayout.encode(
{
instruction: TokenInstruction.Transfer,
amount: new TokenAmount(amount).toBuffer(),
},
data
)
return new TransactionInstruction({ keys, programId, data })
}
export function addSigners(keys: AccountMeta[], ownerOrAuthority: PublicKey, multiSigners: Signer[]): AccountMeta[] {
if (multiSigners.length) {
keys.push({ pubkey: ownerOrAuthority, isSigner: false, isWritable: false })
for (const signer of multiSigners) {
keys.push({ pubkey: signer.publicKey, isSigner: true, isWritable: false })
}
} else {
keys.push({ pubkey: ownerOrAuthority, isSigner: true, isWritable: false })
}
return keys
}
class TokenAmount extends BN {
/**
* Convert to Buffer representation
*/
toBuffer(): Buffer {
const a = super.toArray().reverse()
const b = Buffer.from(a)
if (b.length === 8) {
return b
}
if (b.length >= 8) {
throw new Error('TokenAmount too large')
}
const zeroPad = Buffer.alloc(8)
b.copy(zeroPad)
return zeroPad
}
/**
* Construct a TokenAmount from Buffer representation
*/
static fromBuffer(buffer: Buffer): TokenAmount {
if (buffer.length !== 8) {
throw new Error(`Invalid buffer length: ${buffer.length}`)
}
return new BN(
[...buffer]
.reverse()
.map((i) => `00${i.toString(16)}`.slice(-2))
.join(''),
16
)
}
}
// getAccountInfo.ts
import { TOKEN_PROGRAM_ID, AccountLayout } from '@solana/spl-token'
import { Connection, PublicKey, Commitment } from '@solana/web3.js'
export enum AccountState {
Uninitialized = 0,
Initialized = 1,
Frozen = 2,
}
export async function getAccountInfo(
connection: Connection,
address: PublicKey,
commitment?: Commitment,
programId = TOKEN_PROGRAM_ID
) {
const info = await connection.getAccountInfo(address, commitment)
if (!info) throw new Error('TokenAccountNotFoundError')
if (!info.owner.equals(programId)) throw new Error('TokenInvalidAccountOwnerError')
if (info.data.length != AccountLayout.span) throw new Error('TokenInvalidAccountSizeError')
const rawAccount = AccountLayout.decode(Buffer.from(info.data))
return {
address,
mint: rawAccount.mint,
owner: rawAccount.owner,
amount: rawAccount.amount,
delegate: rawAccount.delegateOption ? rawAccount.delegate : null,
delegatedAmount: rawAccount.delegatedAmount,
isInitialized: rawAccount.state !== AccountState.Uninitialized,
isFrozen: rawAccount.state === AccountState.Frozen,
isNative: !!rawAccount.isNativeOption,
rentExemptReserve: rawAccount.isNativeOption ? rawAccount.isNative : null,
closeAuthority: rawAccount.closeAuthorityOption ? rawAccount.closeAuthority : null,
}
}
// getAssociatedTokerAddress.ts
import { TOKEN_PROGRAM_ID, ASSOCIATED_TOKEN_PROGRAM_ID } from '@solana/spl-token'
import { PublicKey } from '@solana/web3.js'
export async function getAssociatedTokenAddress(
mint: PublicKey,
owner: PublicKey,
allowOwnerOffCurve = false,
programId = TOKEN_PROGRAM_ID,
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID
): Promise<PublicKey> {
if (!allowOwnerOffCurve && !PublicKey.isOnCurve(owner.toBuffer())) throw new Error('TokenOwnerOffCurveError')
const [address] = await PublicKey.findProgramAddress(
[owner.toBuffer(), programId.toBuffer(), mint.toBuffer()],
associatedTokenProgramId
)
return address
}
希望这对其他人有帮助。如果有人有任何意见指针,请发表评论。
关于javascript - 如何通过 '@solana/web3.js' 和 '@solana/sol-wallet-adapter' 传输自定义 SPL token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70224185/
普通数组、SPL 数组和 SPL 数据存储之间的区别、*用法*场景是什么?如果有人能给出一些使用 SPLarray 和 SPL objectsrorage 的实际例子,那就太好了。 最佳答案 SplF
在 java 和 C++ 中,当我们不知道大小时 - 不像在 PHP 中那样使用数组,而是使用 linkedList 等。 在 PHP 中存在 SPL,但大多数时候程序员使用数组,为什么(因为人们不了
数据库语言的目标 要说清这个目标,先要理解数据库是做什么的。 数据库这个软件,名字中有个“库”字,会让人觉得它主要是为了存储的。其实不然,数据库实现的重要功能有两条:计算、事务!也就是我们常说的 OL
数据库语言的目标 要说清这个目标,先要理解数据库是做什么的。 数据库这个软件,名字中有个“库”字,会让人觉得它主要是为了存储的。其实不然,数据库实现的重要功能有两条:计算、事务!也就是我们常说的OLA
需要的MAVEN依赖 org.springframework.boot spring-boot-starter-parent 2.3.2.RELEAS
下载和安装 http://www.raqsoft.com.cn/download/download-jsq 拿到安装包后执行安装程序,按提示逐步进行: 运行安装程序 按照提示,依次点击下一步,接受许可
SPL官网 http://www.scudata.com.cn/ 介绍 业务逻辑经常包含较复杂的流程和计算,同时涉及数据库的读写。由于授权麻烦、影响数据库安全、无法迁移、技术要求高、编写困难等原因,很
下载和安装 http://www.raqsoft.com.cn/download/download-jsq 拿到安装包后执行安装程序,按提示逐步进行: 运行安装程序 按照提示,依次点击下一步,接受许可
SPL官网 http://www.scudata.com.cn/ 介绍 业务逻辑经常包含较复杂的流程和计算,同时涉及数据库的读写。由于授权麻烦、影响数据库安全、无法迁移、技术要求高、编写困难等原因,很
我知道要计算声压级(db SPL)是 20 * log10 (amplitude) 但是我在这里不明白的是值的范围。如果是16位带符号PCM音频,则最小值为-90,最大值为0: [20 * log10
在服务器端的 include_path 中,我有一个 pear 目录的引用,位于“/usr/share/pear/”中。在我的应用程序中,我包含来自公共(public)库的文件,这些文件位于“/usr
通过网络浏览后,我设法构建了一个有效的 SPL_Autoload 并在其中使用了命名空间。它确实有效,但与我正在查看的所有示例和文档相比,它对我来说也有点奇怪。我是否使用了所有内容并正确理解了它? $
我正在研究标准 PHP 库 (SPL)。以前只用过数组,现在才发现PHP有这么多标准类。但是说明书上并没有说推荐使用与否。例如,他们明确建议使用 foreach 构造来迭代数组,因为它更快。那么这个图
本文实例讲述了PHP标准库 (SPL)——Countable用法。分享给大家供大家参考,具体如下: 类实现 Countable 可被用于 count() 函数. 接口摘要
我对这三个问题的概念很迷茫 为什么我们需要辅助程序加载器? 它在哪个内存中加载和重定位? 系统内存和RAM有什么区别? 据我通过阅读链接了解到的是.. 当系统内部存储器不能完全容纳 uboot 时需要
有没有办法在 PHP 中从 SPL 自动加载器抛出异常以防失败?它似乎不能在 PHP 5.2.11 下工作。 class SPLAutoLoader{ public static functi
我正在为如何使用 SPL 迭代器删除 PHP 中的空目录树而苦恼。考虑以下目录结构,其中所有目录都是空的: /topdir level1 level2 我试过以下方法: $it =
如何使用 SPL(可能使用 RecursiveDirectoryIterator 和 RecursiveIteratorIterator)检索完整的目录树? 最佳答案 默认情况下,RecursiveI
我可以在 http://www.php.net/~helly/php/ext/spl/... 找到定义文件,但我想扩展 DirectoryIterator和 SplFileInfo在存储在数据库上的虚
我正在尝试使用 spl_autoload_register 并且我想创建一个 if() 语句来检查类方法是否已经注册。 例如: if (spl_autoload_function(array($thi
我是一名优秀的程序员,十分优秀!