Error with token amount calculation
So I was trying to buy tokens worth 0.001 SOL. But instead the calculation you provided bought tokens 0.000000034 SOL.
https://solscan.io/tx/2w62deyJ3uGhZerxSr1XrGJPFWjvLpkzW9cYHuBFPbmB6Dnp7yG14xMtf1vAX1F3sUs1TnhsBE5Rh1ZQ2ZBoGyYb
This is the transaction. I have no idea what is wrong with my calculations.
const structure = struct([
u64('discriminator'),
u64('virtualTokenReserves'),
u64('virtualSolReserves'),
u64('realTokenReserves'),
u64('realSolReserves'),
u64('tokenTotalSupply'),
]);
const value = structure.decode(ataInfo.data);
const amount = BigInt(maxLamports / LAMPORTS_PER_SOL);
const virtualSolReserves = BigInt(value.virtualSolReserves);
const virtualTokenReserves = BigInt(value.virtualTokenReserves);
const realTokenReserves = BigInt(value.realTokenReserves);
const n = virtualSolReserves * virtualTokenReserves;
const i = virtualSolReserves + amount;
const r = n / i + 1n;
const s = virtualTokenReserves - r;
const tokens = s < realTokenReserves ? s : realTokenReserves;
return Number(tokens);
这个 tokens 指的是 代币的数量,而不是 sol 的数量,因为有滑点的原因,所以,你购买的时候需要传入你想要支付的最大 sol 和 你想要得到的 代币
@YaminZheng I am not looking to buy a pre determined number of tokens. I am looking to buy whatever the amount of tokens is possible for a pre determined number of SOL (say 0.1 SOL).
对,你可以看库里的。getBuyInstructions 方法,他的第四个参数就是 token 数量,第五个参数就是你愿意花费的 sol 的数量,如果你想要花费 0.1 sol ,那 第四个参数就是 0.1sol 通过 getBuyPrice 计算出来的 token 数量
@YaminZheng I did do the same for the transaction below. But the buy amount was way less.
https://solscan.io/tx/2w62deyJ3uGhZerxSr1XrGJPFWjvLpkzW9cYHuBFPbmB6Dnp7yG14xMtf1vAX1F3sUs1TnhsBE5Rh1ZQ2ZBoGyYb
您好 @YaminZheng,如果您能告诉我,将非常有帮助。有什么方法可以计算 SOL 可以以给定数量购买的代币的估计数量?
tokenAmount = getBuyPrice(solAmount)
When using jup to purchase pump, you can use sol as the standard. Previously, pump seemed to be standard on tokens. Now there is a tab on the website that allows you to use sol or token as the standard.