import {
Connection,
Keypair,
PublicKey,
SystemProgram,
Transaction,
LAMPORTS_PER_SOL,
ComputeBudgetProgram,
} from '@solana/web3.js';
import * as bs58 from 'bs58';
async sendTransTest() {
try {
const result = await fetch(
`http://fr.gateway.astralane.io/iris?api-key=<api-key>`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'getNonce',
params: [
'api-key',
],
}),
},
);
const res = await result.json();
const data = res as any;
const nonceAccount = new PublicKey(data.result.nonceAccount);
const nonceAuthority = new PublicKey(data.result.nonceAuthority);
const nonce = data.result.nonce;
const advanceNonce = SystemProgram.nonceAdvance({
noncePubkey: nonceAccount,
authorizedPubkey: nonceAuthority,
});
const secretKey = Keypair.fromSecretKey(
bs58.default.decode(
'wallet-private-key',
),
);
const wallet = secretKey;
//Tip account
const toPublicKey = new PublicKey(
'astra4uejePWneqNaJKuFFA8oonqCE1sqF6b45kDMZm',
);
const MIN_TIP_AMOUNT = 100000;
const lowTipHighFeeIx = [
advanceNonce,
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 1 * 100000,
}),
SystemProgram.transfer({
fromPubkey: wallet.publicKey,
toPubkey: toPublicKey,
lamports: MIN_TIP_AMOUNT,
}),
];
const HighTipLowFeeIx = [
advanceNonce,
ComputeBudgetProgram.setComputeUnitPrice({
microLamports: 1 * 100,
}),
SystemProgram.transfer({
fromPubkey: wallet.publicKey,
toPubkey: toPublicKey,
lamports: 100 * MIN_TIP_AMOUNT,
}),
];
const htlfTransaction = new Transaction().add(...HighTipLowFeeIx);
htlfTransaction.recentBlockhash = nonce;
htlfTransaction.feePayer = wallet.publicKey;
htlfTransaction.partialSign(wallet);
const highTipLowFeeEncoded = Buffer.from(
htlfTransaction.serialize({
requireAllSignatures: false,
verifySignatures: false,
}),
).toString('base64');
const lthfTransaction = new Transaction().add(...lowTipHighFeeIx);
lthfTransaction.recentBlockhash = nonce;
lthfTransaction.feePayer = wallet.publicKey;
lthfTransaction.partialSign(wallet);
const lowTipHighFeeEncoded = Buffer.from(
lthfTransaction.serialize({
requireAllSignatures: false,
verifySignatures: false,
}),
).toString('base64');
const final = await fetch(
`http://fr.gateway.astralane.io/iris?api-key=<api-key>`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'sendIdeal',
params: [[highTipLowFeeEncoded, lowTipHighFeeEncoded]],
}),
},
);
const finalResult = await final.json();
console.log(finalResult);
return lowTipHighFeeEncoded;
} catch (error) {
console.log(error);
}
}