orca-so / typescript-sdk

The Orca SDK contains a set of simple to use APIs to allow developers to integrate with the Orca platform.
155 stars 49 forks source link

No api is equivalent to the getAmountIns() function #106

Open laptrinhbockchain opened 2 years ago

laptrinhbockchain commented 2 years ago

Currently Orca SDK only provides getQuote() function which is equivalent to getAmountOuts() function like on other AMM. I have not found a function on the Orca SDK that is equivalent to the getAmountIns() function. Please support. Thank you!

staccDOTsol commented 1 year ago

The Orca SDK currently doesn't have a function equivalent to the getAmountIns() function. However, you can calculate the input amount using the getQuote() function and some simple math. Here's an example:

Let's say you want to swap 100 USDC for ORCA. You can use the getQuote() function to get the expected output amount:

const orca = orcaFactory.getPool("ORCA/USDC");
const outputAmount = await orca.getQuote(new TokenAmount(orca.getOutputToken(), "100"));
console.log(outputAmount.toExact()); // expected output amount

Now, to calculate the input amount, you can use the formula:

inputAmount = outputAmount / (1 - fee)

where fee is the fee percentage of the pool. For example, if the fee is 0.3%, fee would be 0.003.

So, in our example, if the expected output amount is 50 ORCA and the fee is 0.3%, the input amount would be:

inputAmount = 50 / (1 - 0.003) = 50.15 USDC

I hope this helps! Let me know if you have any further questions.

staccDOTsol commented 1 year ago

The Orca SDK does not currently have a function equivalent to the getAmountIns() function. However, you can calculate the input amount required for a given output amount using the getQuote() function and some basic math. Here's an example:

import { OrcaU64 } from '@orca-so/sdk';

const outputAmount = new OrcaU64(1000); // The desired output amount
const pool = ... // The Orca pool you're trading on
const { baseAmount, quoteAmount } = pool.getReserves(); // The current reserves of the pool

const numerator = quoteAmount.multiply(outputAmount);
const denominator = baseAmount.add(outputAmount);
const inputAmount = numerator.divide(denominator);

console.log(`Input amount required: ${inputAmount.toString()}`);

This code calculates the input amount required to get the desired output amount using the current reserves of the pool. You can adjust the outputAmount variable to whatever value you need.