I create a signature with the sendtransaction method. I see a difference between the signature creation time on my local and the time on the network. Usually, it appears 5-10 seconds later on the network (Signature creation time)
I haven't found the reason yet. Should we not add Blockhash or something else?
const fs = __importStar(require("fs"));
const raydium_1 = require("./raydium");
const web3_js_1 = require("@solana/web3.js");
const spl_token_1 = require("@solana/spl-token");
const types_1 = require("./types");
const mints_1 = require("./mints");
const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
const connection_1 = __importDefault(require("./utils/connection"));
if (process.argv.length < 6) {
console.log(`Usage: node ${process.argv[1]} privateKeyFile COIN buySell sellAmt`);
process.exit();
}
const [, , fileStr, coin, buySell, sellAmt, buyAmt] = process.argv;
async function getAssociatedTokAcc(tokenId, owner) {
return await spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, mints_1.MINTS[tokenId], owner);
}
async function checkOrCreateAssociatedTokAcc(tokenId, owner) {
const mint = mints_1.MINTS[tokenId];
const acc = await spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, mint, owner);
const accInfo = await connection_1.default.getAccountInfo(acc, 'singleGossip');
if (accInfo) {
// g
}
else {
// create it and init
const createIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, mint, acc, owner, owner);
return [createIx];
}
return [];
}
async function doSwap() {
const keyStr = fs.readFileSync(fileStr, 'utf8');
const privateKey = JSON.parse(keyStr);
const keypair = web3_js_1.Keypair.fromSecretKey(new Uint8Array(privateKey));
const aptTokenAccount = await getAssociatedTokAcc(types_1.TokenID.APT, keypair.publicKey);
const usdcTokenAccount = await getAssociatedTokAcc(types_1.TokenID.USDC, keypair.publicKey);
const conn = connection_1.default;
const isBuy = buySell === 'buy';
const mainTokenType = {
APT: types_1.TokenID.APT,
}[coin];
(0, tiny_invariant_1.default)(mainTokenType);
const tokenAccounts = {
APT: aptTokenAccount,
USDC: usdcTokenAccount,
};
const mainTokenAcc = tokenAccounts[mainTokenType];
(0, tiny_invariant_1.default)(mainTokenAcc);
const getSwapper = {
APT: () => raydium_1.RAYDIUM_APT_USDC_MARKET,
}[coin];
(0, tiny_invariant_1.default)(getSwapper);
const swapper = getSwapper();
const tokenBAcc = tokenAccounts[swapper.tokenIdB];
(0, tiny_invariant_1.default)(tokenBAcc);
const buyTokenID = isBuy ? mainTokenType : swapper.tokenIdB;
const buyTokenAcc = isBuy ? mainTokenAcc : tokenBAcc;
const sellTokenID = isBuy ? swapper.tokenIdB : mainTokenType;
const sellTokenAcc = isBuy ? tokenBAcc : mainTokenAcc;
const swapperType = {
APT: types_1.SwapperType.Single,
}[coin];
(0, tiny_invariant_1.default)(swapperType);
const ixs = await checkOrCreateAssociatedTokAcc(mainTokenType, keypair.publicKey);
const tradeIxs = await swapper.createSwapInstructions(sellTokenID, parseFloat(sellAmt) * mints_1.DECIMALS[sellTokenID], sellTokenAcc, buyTokenID, parseFloat(buyAmt) * mints_1.DECIMALS[buyTokenID], buyTokenAcc, keypair.publicKey);
const tradeTx = new web3_js_1.Transaction();
const maxUnits = 1_000_000;
const totalPriorityFee = 0.00003; // TODO change price as desired
const totalInLamports = totalPriorityFee*web3_js_1.LAMPORTS_PER_SOL;
const microLamportsPrice = totalInLamports*1_000_000/maxUnits;
const cbpLimitIx = web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
units: maxUnits,
});
const cbpPriceIx = web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
microLamports: microLamportsPrice,
});
tradeTx.add(cbpLimitIx, cbpPriceIx);
// adding instructions
tradeTx.add(...ixs, ...tradeIxs);
const sig = await conn.sendTransaction(tradeTx, [keypair], {
skipPreflight: true,
preflightCommitment: 'singleGossip',
});
console.log(sig);
process.exit();
}
doSwap();