在 devnet 上部署和初始化锚定托管合约
Deploying and Initializing anchor-escrow contract on devnet
这与其说是一个实际问题,不如说是一个问题
我要使用合约
https://github.com/ironaddicteddog/anchor-escrow
交换代币。
我的计划如下,但是我没有成功初始化合约。
logs: [
'Program 11111111111111111111111111111111 invoke [1]',
'Create Account: account Address { address: 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ, base: None } already in use',
'Program 11111111111111111111111111111111 failed: custom program error: 0x0'
]
注:2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ
为lib.rs
中的合约地址
- 部署程序 ✅
2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ
- 创建
Token A
DhS6x9pTrfCeY8iwkRdGstxUuphbeHqddT2vZSWRw3d2
和 Token B
HpdLmjjxD8YZav2S1aastqyLsKDUb1ToLDfq4hPsLBoc
✅
- 创建代币账户代币A账户
Cj6cMp4xfAaCFegMg9G7GQaZWqYbQqgmu7Vjd4BbGYHh
、代币B账户HpdLmjjxD8YZav2S1aastqyLsKDUb1ToLDfq4hPsLBoc
✅
- 初始化合约❌。
- 兑换代币
我编写了以下脚本来初始化合约。
import * as anchor from '@project-serum/anchor';
import { PublicKey, SystemProgram, Transaction, Connection, Commitment } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID, Token } from "@solana/spl-token";
import {
escrowAccount,
initializerMainAccount,
initializerTokenAccountA,
initializerTokenAccountB,
mintAPublicKey,
mintBPublicKey } from './accounts'
import NodeWallet from '@project-serum/anchor/dist/cjs/nodewallet';
const takerAmount = 1000;
const initializerAmount = 500;
const commitment: Commitment = 'processed';
const connection = new Connection('https://api.devnet.solana.com', { commitment, wsEndpoint: 'wss://api.devnet.solana.com/' });
// const connection = new Connection('http://127.0.0.1:8899', { commitment, wsEndpoint: 'wss://127.0.0.1:8899/' });
const options = anchor.Provider.defaultOptions();
const wallet = new NodeWallet(initializerMainAccount);
const provider = new anchor.Provider(connection, wallet, options);
anchor.setProvider(provider);
// Read the generated IDL.
const idl = JSON.parse(
require("fs").readFileSync("./tests/keypair/anchor_escrow.json", "utf8")
);
// Address of the deployed program.
const programId = new anchor.web3.PublicKey("2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ");
// Generate the program client from IDL.
const program = new anchor.Program(idl, programId);
const initEscrow = async () => {
const [_vault_account_pda, _vault_account_bump] = await PublicKey.findProgramAddress(
[Buffer.from(anchor.utils.bytes.utf8.encode("token-seed"))],
program.programId,
);
const vault_account_pda = _vault_account_pda;
const vault_account_bump = _vault_account_bump;
const [_vault_authority_pda, _vault_authority_bump] = await PublicKey.findProgramAddress(
[Buffer.from(anchor.utils.bytes.utf8.encode("escrow"))],
program.programId,
);
// DEBUG BEGIN
// console.info(`initializerMainAccount: ` + JSON.stringify(initializerMainAccount, null, 2));
// console.info(`Escrow account: ` + JSON.stringify(escrowAccount));
console.info(`Mint A: ` + mintAPublicKey.toBase58());
console.info(`Mint B: ` + mintBPublicKey.toBase58());
console.info(`TOKEN_PROGRAM_ID: ` + TOKEN_PROGRAM_ID);
console.info(`SYSVAR_RENT_PUBKEY: ` + anchor.web3.SYSVAR_RENT_PUBKEY);
// DEBUG CONSOLE END
await program.rpc.initialize(
vault_account_bump,
new anchor.BN(initializerAmount),
new anchor.BN(takerAmount),
{
accounts: {
initializer: initializerMainAccount.publicKey,
mint: mintAPublicKey,
vaultAccount: vault_account_pda,
initializerDepositTokenAccount: initializerTokenAccountA,
initializerReceiveTokenAccount: initializerTokenAccountB,
escrowAccount: escrowAccount.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
tokenProgram: TOKEN_PROGRAM_ID,
},
instructions: [
await program.account.escrowAccount.createInstruction(escrowAccount),
],
signers: [escrowAccount, initializerMainAccount],
}
);
}
initEscrow();
错误输出的长版本是
➜ anchor-escrow git:(master) ✗ ts-node tests/init2.ts
Mint A: DhS6x9pTrfCeY8iwkRdGstxUuphbeHqddT2vZSWRw3d2
Mint B: HpdLmjjxD8YZav2S1aastqyLsKDUb1ToLDfq4hPsLBoc
TOKEN_PROGRAM_ID: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
SYSVAR_RENT_PUBKEY: SysvarRent111111111111111111111111111111111
Transaction simulation failed: Error processing Instruction 0: custom program error: 0x0
Program 11111111111111111111111111111111 invoke [1]
Create Account: account Address { address: 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ, base: None } already in use
Program 11111111111111111111111111111111 failed: custom program error: 0x0
/Users/tuncatunc/git/anchor-escrow/node_modules/@solana/web3.js/src/connection.ts:3961
throw new SendTransactionError(
^
SendTransactionError: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x0
at Connection.sendEncodedTransaction (/Users/tuncatunc/git/anchor-escrow/node_modules/@solana/web3.js/src/connection.ts:3961:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Connection.sendRawTransaction (/Users/tuncatunc/git/anchor-escrow/node_modules/@solana/web3.js/src/connection.ts:3918:20)
at async sendAndConfirmRawTransaction (/Users/tuncatunc/git/anchor-escrow/node_modules/@solana/web3.js/src/util/send-and-confirm-raw-transaction.ts:27:21)
at async Provider.send (/Users/tuncatunc/git/anchor-escrow/node_modules/@project-serum/anchor/src/provider.ts:118:18)
at async Object.rpc [as initialize] (/Users/tuncatunc/git/anchor-escrow/node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:25:23) {
logs: [
'Program 11111111111111111111111111111111 invoke [1]',
'Create Account: account Address { address: 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ, base: None } already in use',
'Program 11111111111111111111111111111111 failed: custom program error: 0x0'
]
}
这是一个长post
非常感谢您编写此合约以指导 solana 合约开发人员。
BR
我会在这里回答我自己的问题,
我发现托管程序已经初始化。
错误日志说,它不能使用与 re-initialize.
相同的托管帐户
这与其说是一个实际问题,不如说是一个问题
我要使用合约
https://github.com/ironaddicteddog/anchor-escrow
交换代币。
我的计划如下,但是我没有成功初始化合约。
logs: [
'Program 11111111111111111111111111111111 invoke [1]',
'Create Account: account Address { address: 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ, base: None } already in use',
'Program 11111111111111111111111111111111 failed: custom program error: 0x0'
]
注:2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ
为lib.rs
- 部署程序 ✅
2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ
- 创建
Token A
DhS6x9pTrfCeY8iwkRdGstxUuphbeHqddT2vZSWRw3d2
和Token B
HpdLmjjxD8YZav2S1aastqyLsKDUb1ToLDfq4hPsLBoc
✅ - 创建代币账户代币A账户
Cj6cMp4xfAaCFegMg9G7GQaZWqYbQqgmu7Vjd4BbGYHh
、代币B账户HpdLmjjxD8YZav2S1aastqyLsKDUb1ToLDfq4hPsLBoc
✅ - 初始化合约❌。
- 兑换代币
我编写了以下脚本来初始化合约。
import * as anchor from '@project-serum/anchor';
import { PublicKey, SystemProgram, Transaction, Connection, Commitment } from '@solana/web3.js';
import { TOKEN_PROGRAM_ID, Token } from "@solana/spl-token";
import {
escrowAccount,
initializerMainAccount,
initializerTokenAccountA,
initializerTokenAccountB,
mintAPublicKey,
mintBPublicKey } from './accounts'
import NodeWallet from '@project-serum/anchor/dist/cjs/nodewallet';
const takerAmount = 1000;
const initializerAmount = 500;
const commitment: Commitment = 'processed';
const connection = new Connection('https://api.devnet.solana.com', { commitment, wsEndpoint: 'wss://api.devnet.solana.com/' });
// const connection = new Connection('http://127.0.0.1:8899', { commitment, wsEndpoint: 'wss://127.0.0.1:8899/' });
const options = anchor.Provider.defaultOptions();
const wallet = new NodeWallet(initializerMainAccount);
const provider = new anchor.Provider(connection, wallet, options);
anchor.setProvider(provider);
// Read the generated IDL.
const idl = JSON.parse(
require("fs").readFileSync("./tests/keypair/anchor_escrow.json", "utf8")
);
// Address of the deployed program.
const programId = new anchor.web3.PublicKey("2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ");
// Generate the program client from IDL.
const program = new anchor.Program(idl, programId);
const initEscrow = async () => {
const [_vault_account_pda, _vault_account_bump] = await PublicKey.findProgramAddress(
[Buffer.from(anchor.utils.bytes.utf8.encode("token-seed"))],
program.programId,
);
const vault_account_pda = _vault_account_pda;
const vault_account_bump = _vault_account_bump;
const [_vault_authority_pda, _vault_authority_bump] = await PublicKey.findProgramAddress(
[Buffer.from(anchor.utils.bytes.utf8.encode("escrow"))],
program.programId,
);
// DEBUG BEGIN
// console.info(`initializerMainAccount: ` + JSON.stringify(initializerMainAccount, null, 2));
// console.info(`Escrow account: ` + JSON.stringify(escrowAccount));
console.info(`Mint A: ` + mintAPublicKey.toBase58());
console.info(`Mint B: ` + mintBPublicKey.toBase58());
console.info(`TOKEN_PROGRAM_ID: ` + TOKEN_PROGRAM_ID);
console.info(`SYSVAR_RENT_PUBKEY: ` + anchor.web3.SYSVAR_RENT_PUBKEY);
// DEBUG CONSOLE END
await program.rpc.initialize(
vault_account_bump,
new anchor.BN(initializerAmount),
new anchor.BN(takerAmount),
{
accounts: {
initializer: initializerMainAccount.publicKey,
mint: mintAPublicKey,
vaultAccount: vault_account_pda,
initializerDepositTokenAccount: initializerTokenAccountA,
initializerReceiveTokenAccount: initializerTokenAccountB,
escrowAccount: escrowAccount.publicKey,
systemProgram: anchor.web3.SystemProgram.programId,
rent: anchor.web3.SYSVAR_RENT_PUBKEY,
tokenProgram: TOKEN_PROGRAM_ID,
},
instructions: [
await program.account.escrowAccount.createInstruction(escrowAccount),
],
signers: [escrowAccount, initializerMainAccount],
}
);
}
initEscrow();
错误输出的长版本是
➜ anchor-escrow git:(master) ✗ ts-node tests/init2.ts
Mint A: DhS6x9pTrfCeY8iwkRdGstxUuphbeHqddT2vZSWRw3d2
Mint B: HpdLmjjxD8YZav2S1aastqyLsKDUb1ToLDfq4hPsLBoc
TOKEN_PROGRAM_ID: TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA
SYSVAR_RENT_PUBKEY: SysvarRent111111111111111111111111111111111
Transaction simulation failed: Error processing Instruction 0: custom program error: 0x0
Program 11111111111111111111111111111111 invoke [1]
Create Account: account Address { address: 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ, base: None } already in use
Program 11111111111111111111111111111111 failed: custom program error: 0x0
/Users/tuncatunc/git/anchor-escrow/node_modules/@solana/web3.js/src/connection.ts:3961
throw new SendTransactionError(
^
SendTransactionError: failed to send transaction: Transaction simulation failed: Error processing Instruction 0: custom program error: 0x0
at Connection.sendEncodedTransaction (/Users/tuncatunc/git/anchor-escrow/node_modules/@solana/web3.js/src/connection.ts:3961:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async Connection.sendRawTransaction (/Users/tuncatunc/git/anchor-escrow/node_modules/@solana/web3.js/src/connection.ts:3918:20)
at async sendAndConfirmRawTransaction (/Users/tuncatunc/git/anchor-escrow/node_modules/@solana/web3.js/src/util/send-and-confirm-raw-transaction.ts:27:21)
at async Provider.send (/Users/tuncatunc/git/anchor-escrow/node_modules/@project-serum/anchor/src/provider.ts:118:18)
at async Object.rpc [as initialize] (/Users/tuncatunc/git/anchor-escrow/node_modules/@project-serum/anchor/src/program/namespace/rpc.ts:25:23) {
logs: [
'Program 11111111111111111111111111111111 invoke [1]',
'Create Account: account Address { address: 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ, base: None } already in use',
'Program 11111111111111111111111111111111 failed: custom program error: 0x0'
]
}
这是一个长post 非常感谢您编写此合约以指导 solana 合约开发人员。
BR
我会在这里回答我自己的问题, 我发现托管程序已经初始化。 错误日志说,它不能使用与 re-initialize.
相同的托管帐户