bancorprotocol / sdk

Apache License 2.0
18 stars 24 forks source link

Feature request: Getting the reverse exchange rate #3

Open olekon opened 4 years ago

olekon commented 4 years ago

Let's suppose I need to get X amount of SomeToken, and I have AnotherToken

It would be nice to have a function that calculates the following: How many of AnotherToken should I spend to obtain X amount of SomeToken.

A simple example, explaining why it can be needed and getRate is not enough sometimes. Let's try to use the current getRate function to calculate how many BNT tokens one should spend to get 10 ETH:

const ethToken = {
    blockchainType: 'ethereum',
    blockchainId: '0xc0829421C1d260BD3cB3E0F06cfE2D52db2cE315'
};
const bntToken = {
    blockchainType: 'ethereum',
    blockchainId: '0x1F573D6Fb3F13d689FF844B4cE37794d79a7FF1C'
}

const initialAmount = "10";
console.log(initialAmount);

const bntTokenAmount = await bancor.getRate(ethToken, bntToken, initialAmount);
console.log(bntTokenAmount);

//now check the conversion result
const ethTokenAmount = await bancor.getRate(bntToken, ethToken, bntTokenAmount);
console.log(ethTokenAmount);

In this example, ethTokenAmount is quite close to initial (for now it is 9.944, ~0.56% deviation), but as initialAmount grows, the difference becomes greater too.

with initialAmount set to "1000" resulting ethTokenAmount is "859.966" which is 14% deviation, and that is a lot.

Though I understand that it might be hard to get an exact value, but I guess it's OK to specify a precision.

yudilevi commented 4 years ago

@olekon yeah - makes sense and we'll definitely add that one - thanks :)