Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deploying and Initializing anchor-escrow contract on devnet #9

Closed
tuncatunc opened this issue Mar 6, 2022 · 2 comments
Closed

Deploying and Initializing anchor-escrow contract on devnet #9

tuncatunc opened this issue Mar 6, 2022 · 2 comments

Comments

@tuncatunc
Copy link

hi @ironaddicteddog,
this is more of a question than an actual issue
I want to use the contract to exchange tokens.
My plan is as follows, however I'm unsuccessful initializing the contract.

logs: [
    'Program 11111111111111111111111111111111 invoke [1]',
    'Create Account: account Address { address: 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ, base: None } already in use',
    'Program 11111111111111111111111111111111 failed: custom program error: 0x0'
  ]

Note: 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ is the contract address in the lib.rs

  1. Deploy program ✅ 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ
  2. Create Token A DhS6x9pTrfCeY8iwkRdGstxUuphbeHqddT2vZSWRw3d2 and Token B HpdLmjjxD8YZav2S1aastqyLsKDUb1ToLDfq4hPsLBoc
  3. Create token accounts Token A account Cj6cMp4xfAaCFegMg9G7GQaZWqYbQqgmu7Vjd4BbGYHh, Token B account HpdLmjjxD8YZav2S1aastqyLsKDUb1ToLDfq4hPsLBoc
  4. Initialize contract ❌.
  5. Exchange tokens

I've written following script to initialize the contract.

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();

Long version of the error output is

➜  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'
  ]
}

This is a long post
Thx a lot in advance for writing this contract to guide solana contract devs.

BR

@tuncatunc
Copy link
Author

The reason for the error message is because, the contract is already initialized.
One of my friend told that, and than I checked the 2yTRYBq58ZMgudQcEp18UnsCBPTUx9a12ZnzZ7N7v9hQ escrow account address and yes there it's initialized.
https://explorer.solana.com/tx/3LtryBUaVkRmStskDWRprxC7wvJ3DNf7mV1MkKNsmBWLm8w57wYRxJp8SuocnGkztKWBNFZDwh8St3YrDV5nZCYZ?cluster=devnet
The escrow program

@ironaddicteddog
Copy link
Owner

ironaddicteddog commented Mar 8, 2022

Great, thx for your update 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants