我目前正在努力学习如何为Solana编写程序并与这些程序进行交互。
我正在使用例子-helloworld代码。为了与helloworld程序进行交互,nodejs代码创建了一个带有seed的帐户:
const transaction = new Transaction().add(
SystemProgram.createAccountWithSeed({
fromPubkey: payer.publicKey,
basePubkey: payer.publicKey,
seed: GREETING_SEED,
newAccountPubkey: greetedPubkey,
lamports,
space: GREETING_SIZE,
programId,
})
)
tx = client.send_transaction(transaction, payer)
我的理解是,它创建了一个数据帐户,由programId
程序拥有。还不确定为什么有种子。
我试图用以下代码替换这段代码:
const transaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: greetedPubkey,
lamports,
space: GREETING_SIZE,
programId,
})
)
tx = client.send_transaction(transaction, payer)
但这是行不通的。一旦事务发送,我将得到以下错误:
{'code': -32602, 'message': 'invalid transaction: Transaction failed to sanitize accounts offsets correctly'}
有人能解释我做错了什么吗??
发布于 2022-03-28 18:12:56
使用createAccount
创建帐户时,必须从该新帐户提供签名。所以在您的例子中,greetedPubkey
必须是一个Keypair
,就像payer
一样。你可以:
const greeted = Keypair.generate();
const transaction = new Transaction().add(
SystemProgram.createAccount({
fromPubkey: payer.publicKey,
newAccountPubkey: greeted.publicKey,
lamports,
space: GREETING_SIZE,
programId,
})
);
tx = client.send_transaction(transaction, payer, greeted);
createAccountWithSeed
是特殊的,因为它从基派生出一个新地址,这意味着您不需要用新地址签名,只需要用基地址签名。
https://stackoverflow.com/questions/71615654
复制相似问题