Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error with token amount calculation #45

Open
tushar-sahoo opened this issue Aug 30, 2024 · 7 comments
Open

Error with token amount calculation #45

tushar-sahoo opened this issue Aug 30, 2024 · 7 comments

Comments

@tushar-sahoo
Copy link

tushar-sahoo commented Aug 30, 2024

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);
@YaminZheng
Copy link

这个 tokens 指的是 代币的数量,而不是 sol 的数量,因为有滑点的原因,所以,你购买的时候需要传入你想要支付的最大 sol 和 你想要得到的 代币

@tushar-sahoo
Copy link
Author

tushar-sahoo commented Sep 2, 2024

@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).

@YaminZheng
Copy link

对,你可以看库里的。getBuyInstructions 方法,他的第四个参数就是 token 数量,第五个参数就是你愿意花费的 sol 的数量,如果你想要花费 0.1 sol ,那 第四个参数就是 0.1sol 通过 getBuyPrice 计算出来的 token 数量

@tushar-sahoo
Copy link
Author

@YaminZheng I did do the same for the transaction below. But the buy amount was way less.

https://solscan.io/tx/2w62deyJ3uGhZerxSr1XrGJPFWjvLpkzW9cYHuBFPbmB6Dnp7yG14xMtf1vAX1F3sUs1TnhsBE5Rh1ZQ2ZBoGyYb

@yudizaxay
Copy link

您好 @YaminZheng,如果您能告诉我,将非常有帮助。有什么方法可以计算 SOL 可以以给定数量购买的代币的估计数量?

@zhengyimeng
Copy link

tokenAmount = getBuyPrice(solAmount)

@icaca
Copy link

icaca commented Sep 6, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants