0

So I literally copied from (https://docs.solana.com/developing/clients/javascript-api)

I need to send a transaction from one wallet to another, easy right? But this doest work here

error

1

Specifically, the transfer method cannot create an instruction. I've tried everything I can, but it hasn't worked. Please help dear programmers

MY CODE:

async function transfer() {
  const provider = await IsConnected();
  console.log(provider.publicKey.toString());
  const connection = new solanaWeb3.Connection(
    "https://api.devnet.solana.com",
    "confirmed"
  );

  var recieverWallet = new solanaWeb3.PublicKey(
    "3uePV7kJcT3w5qMLPTHVAdLc72SF2iDRwNcxDE713EMf"
  );

  // var airdropSignature = await connection.requestAirdrop(
  //   provider.publicKey,
  //   solanaWeb3.LAMPORTS_PER_SOL * 0.4
  // );

  // await connection
  //   .confirmTransaction(airdropSignature)
  //   .then(console.log("Airdropped"));
  var transaction = new solanaWeb3.Transaction();

  transaction.feePayer = await provider.publicKey;
  let blockhashObj = await connection.getRecentBlockhash();
  transaction.recentBlockhash = await blockhashObj.blockhash;
  transaction.add(
    await solanaWeb3.SystemProgram.transfer({
      fromPubkey: provider.publicKey,
      toPubkey: recieverWallet,
      lamports: 10000,
    })
  );
  if (transaction) {
    console.log("Txn created successfully");
  }

  let signed = await provider.signTransaction(transaction).serialize();

  let signature = await connection.sendRawTransaction(signed);

  console.log(signature);

  let bConfirmed = await connection.confirmTransaction(signature);

 
  console.log(bConfirmed);
}

CODE WHERE PROBLEM:

static transfer(
    params: TransferParams | TransferWithSeedParams,
  ): TransactionInstruction {
    let data;
    let keys;
    if ('basePubkey' in params) {
      const type = SYSTEM_INSTRUCTION_LAYOUTS.TransferWithSeed;
      data = encodeData(type, {
        lamports: BigInt(params.lamports),
        seed: params.seed,
        programId: toBuffer(params.programId.toBuffer()),
      });
      keys = [
        {pubkey: params.fromPubkey, isSigner: false, isWritable: true},
        {pubkey: params.basePubkey, isSigner: true, isWritable: false},
        {pubkey: params.toPubkey, isSigner: false, isWritable: true},
      ];
    } else {
      const type = SYSTEM_INSTRUCTION_LAYOUTS.Transfer;
     ***THERE ->*** data = encodeData(type, {lamports: BigInt(params.lamports)});
      keys = [
        {pubkey: params.fromPubkey, isSigner: true, isWritable: true},
        {pubkey: params.toPubkey, isSigner: false, isWritable: true},
      ];
    }

    return new TransactionInstruction({
      keys,
      programId: this.programId,
      data,
    });
  }


export function encodeData<TInputData extends IInstructionInputData>(
  type: InstructionType<TInputData>,
  fields?: any,
): Buffer {
  const allocLength =
    type.layout.span >= 0 ? type.layout.span : Layout.getAlloc(type, fields);
  const data = Buffer.alloc(allocLength);
  const layoutFields = Object.assign({instruction: type.index}, fields);
  type.layout.encode(layoutFields, data);
  return data;
}



    encode(src, b, offset = 0) {
        const firstOffset = offset;
        let lastOffset = 0;
        let lastWrote = 0;
        for (const fd of this.fields) {
            let span = fd.span;
            lastWrote = (0 < span) ? span : 0;
            if (undefined !== fd.property) {
                const fv = src[fd.property];
                if (undefined !== fv) {
                    lastWrote = fd.encode(fv, b, offset);
                    if (0 > span) {
                        /* Read the as-encoded span, which is not necessarily the
                         * same as what we wrote. */
                        span = fd.getSpan(b, offset);
                    }
                }
            }
            lastOffset = offset;
            offset += span;
        }


const bigInt =
  (length: number) =>
  (property?: string): Layout<bigint> => {
    const layout = blob(length, property);
    const {encode, decode} = encodeDecode(layout);

    const bigIntLayout = layout as Layout<unknown> as Layout<bigint>;

    bigIntLayout.decode = (buffer: Buffer, offset: number) => {
      const src = decode(buffer, offset);
      return toBigIntLE(Buffer.from(src));
    };

    bigIntLayout.encode = (bigInt: bigint, buffer: Buffer, offset: number) => {
      const src = toBufferLE(bigInt, length);
      return encode(src, buffer, offset);
    };

    return bigIntLayout;
  };



function toBufferLE(num, width) {
    {
        const hex = num.toString(16);
        const buffer = Buffer.from(hex.padStart(width * 2, '0').slice(0, width * 2), 'hex');
        buffer.reverse();
        return buffer;
    }
    // Allocation is done here, since it is slower using napi in C
    return converter.fromBigInt(num, Buffer.allocUnsafe(width), false);
}
exports.toBufferLE = toBufferLE;

  • 1
    Make sure you included the right script from their page, they show an unpkg URL that has all dependencies (`https://unpkg.com/@solana/web3.js@latest/lib/index.iife.js`). If you manually include the module with npm, you need to remember to also add a polyfill for `Buffer` such as [this one](https://github.com/feross/buffer) because it only exists on node.js otherwise. – CherryDT Sep 02 '22 at 11:02
  • @CherryDT Yeap, i included right ** ** – pidor ads243 Sep 02 '22 at 11:08

0 Answers0