Skip to main content

代码备份

  // public async sendTransaction(params: SolanaTransfer) {

// const instructions: Parameters<typeof this.program.methods.executeTransaction>[0]['instructions'] = [
// // {
// // accountSize: 0,
// // data: Buffer.from([1, 2])
// // }
// ]
// // const tx = await this.program.methods!.executeTransaction({
// // owner: owner,
// // signs: Buffer.from(signObj.signature),
// // recoveryId: signObj.recoveryId,
// // instructions: [
// // {
// // accountSize: 2,
// // data: transferInstuction.data,
// // }
// // ],
// // })

// const { sender, to, tokenAddress, amount /*decimals */, programId } = params

// const fromPubkey = new PublicKey(sender)
// const toPubkey = new PublicKey(to)
// // const tx = this.program.methods.executeTransaction({
// // owner: new Buffer([1, 2,]),
// // signs: new Buffer([1, 2,]),
// // recoveryId: 0,
// // instructions: []
// // }).accounts({

// // }).remainingAccounts([])


// // const transaction = tx.transaction()

// // let transaction: Transaction = new Transaction() // Initialize with a default Transaction

// if (!programId) {
// // sol
// if (tokenAddress === '11111111111111111111111111111111') {
// // transaction = new Transaction().add(
// // SystemProgram.transfer({
// // fromPubkey,
// // toPubkey,
// // lamports: amount,
// // }),
// // )




// } else {
// // spl
// const tokenPubkey = new PublicKey(tokenAddress)
// // Get the token account of the fromWallet Solana address, if it does not exist, create it
// const fromTokenAccount = await this.getOrCreateAssociatedTokenAccount(
// this.connection,
// this.payer, // Payer
// tokenPubkey,
// fromPubkey,
// )
// //get the token account of the toWallet Solana address, if it does not exist, create it
// const receiverTokenAccount = await this.getOrCreateAssociatedTokenAccount(
// this.connection,
// this.payer, // Payer
// tokenPubkey,
// toPubkey,
// )

// const transferInstruction = createTransferInstruction(fromTokenAccount.address, receiverTokenAccount.address, fromPubkey, amount, [])
// transaction = new Transaction().add(transferInstruction)
// }
// const txId = await this.sendBundlerTransaction(transaction)
// console.log('solana txId', txId)
// return txId
// } else {
// // programId
// console.log('solana programId', programId)

// const provider = anchor.AnchorProvider.local()
// anchor.setProvider(provider)

// const idl: Idl = {
// address: programId,
// version: '0.1.0',
// name: 'counter',
// instructions: [
// {
// name: 'createCounter',
// accounts: [
// {
// name: 'authority',
// isMut: true,
// isSigner: true,
// },
// {
// name: 'counter',
// isMut: true,
// isSigner: false,
// },
// {
// name: 'systemProgram',
// isMut: false,
// isSigner: false,
// },
// ],
// args: [],
// },
// {
// name: 'updateCounter',
// accounts: [
// {
// name: 'authority',
// isMut: false,
// isSigner: true,
// },
// {
// name: 'counter',
// isMut: true,
// isSigner: false,
// },
// ],
// args: [],
// },
// ],
// accounts: [
// {
// name: 'Counter',
// type: {
// kind: 'struct',
// fields: [
// {
// name: 'authority',
// type: 'publicKey',
// },
// {
// name: 'count',
// type: 'u64',
// },
// ],
// },
// },
// ],
// }
// // Create the program interface
// // TODO: idl dynamic fetch
// const program = new anchor.Program(idl, provider)
// try {
// // Call the set_data method
// const [counter, _counterBump] = await anchor.web3.PublicKey.findProgramAddress(
// [provider.wallet.publicKey.toBytes()],
// program.programId
// );
// console.log("Your counter address", counter.toString());
// } catch (err) {
// console.error('Error calling set_data:', err)
// }
// }
// }

// private async sendAndConfirmTransaction(transferTransaction: Transaction) {
// const preparedTransaction = await this.prepareTransaction(transferTransaction)
// const signedTransaction = await this.signTransaction(preparedTransaction)

// // // Serialize the transaction
// // const rawTransaction = signedTransaction.serialize()

// // // Send the transaction
// // const txid = await this.connection.sendRawTransaction(rawTransaction, { skipPreflight: false })

// // // Confirm the transaction
// // const confirmation = await this.connection.confirmTransaction(txid, 'confirmed')
// // if (confirmation.value.err) {
// // throw new Error('Transaction failed')
// // }
// // return txid

// // Send the transaction
// return await sendAndConfirmTransaction(this.connection, signedTransaction, [])
// }

// private async prepareTransaction(transaction: Transaction, options: SendOptions = {}): Promise<Transaction> {
// const publicKey = this.publicKey
// if (!publicKey) throw new WalletNotConnectedError()

// transaction.feePayer = transaction.feePayer || publicKey
// transaction.recentBlockhash =
// transaction.recentBlockhash ||
// (
// await this.connection.getLatestBlockhash({
// commitment: options.preflightCommitment,
// minContextSlot: options.minContextSlot,
// })
// ).blockhash

// return transaction
// }

// private async signTransaction(prepareTransaction: Transaction): Promise<Transaction> {
// // transaction.partialSign(this.keypair);
// // generate signature
// const serializedTransaction = prepareTransaction.serializeMessage();
// // TODO: signWithPasskey
// // const signature = await signWithPasskey(serializedTransaction)
// // prepareTransaction.addSignature(this.publicKey, signature)
// // return prepareTransaction
// }