minswap / sdk Goto Github PK
View Code? Open in Web Editor NEWQuery Minswap data and build transactions
License: MIT License
Query Minswap data and build transactions
License: MIT License
I use calculateSwapExactIn function to calculate amount out but it is returning different data than the interface.
Tried with the ADA/tMELD pair and it is displaying amount out correctly with the interface. But it doesn't need divide by 10^6
Where is it going wrong?
async function getPool(
assetA: string,
assetB: string
): Promise<PoolState | undefined> {
for (let i = 1; ; i++) {
const pools = await blockfrostAdapter.getPools({
page: i,
});
if (pools.length === 0) {
// last page
break;
}
const pool = pools.find((p) => p.assetA === assetA && p.assetB === assetB);
if (pool) {
return pool;
}
}
return;
}
async function main(): Promise<void> {
{
// tMELD/tMIN
const pool = await getPool(
"88dc7cd1c28d3a0c7ef4df99036c7c9688d309d91a1bb6fe4b08fee9744d454c44",
"f6f49b186751e61f1fb8c64e7504e771f968cea9f4d11f5222b169e3744d494e"
);
if (pool) {
const amountIn = 10_000_000n;
const data = calculateSwapExactIn({
amountIn,
reserveIn: pool.reserveA,
reserveOut: pool.reserveB,
});
const amountOut = new Big(data.amountOut.toString()).div(
new Big(10).pow(6)
);
console.log("amountOutFull", data.amountOut.toString());
console.log("amountOut", amountOut.toString());
console.log("priceImpact", data.priceImpact.toString());
}
}
}
Output:
amountOutFull 907854
amountOut 0.907854
priceImpact 90.60331783793637772309
Use Blockfrost secret API to get pool by script hash
When I want to trade 350 ADA for C4 on miniswap it shows me that price impact is 18.72%.
But when I try to use the getAmountOut method it returns strange numbers
const pool = await this.api.getPoolById({ id: "64c84332705ee884c881219d380349b038e85afe77578ade923107ee1e36da2a"});
const { priceImpact, amountOut } = pool.getAmountOut( pool.assetA, BigInt(350), );
console.log(priceImpact.toString(), amountOut.toString());
Output for this will be "0.00002304559479445564 0"
So the question is how to calculate price impact by myself or how to use the returned numbers?
Using the for loop in the example provided, it seems we need about 30 requests to find the min/ada pool for instance. It seems this would very quickly use all the request quota of the blockfrost account.
Is there any way to optimize this?
Thanks!
I've noticed when using getPools
the pools for certain tokens are now not returned, these returned fine a few days ago, tokens affected that I am currently using in my implementation are AADA, AGIX SNEK, WMT.
When investigating further I can see that the pools are queried using the blockfrost method addressesUtxos
which takes in the MAINNET Pool address in this case addr1z8snz7c4974vzdpxu65ruphl3zjdvtxw8strf2c2tmqnxz2j2c79gy9l76sdg0xwhd7r0c0kna0tycz4y5s6mlenh8pq0xmsha
Which I believe is the wrong pool address now for AGIX etc, resulting in these being filtered out of the response.
When doing a plain addressesUtxos
directly using the blockfrost API, with the same parameters as this adapter and then filtering on 'lovelace' and assetB of f43a62fdc3965df486de8a0d32fe800963589c41b38946602a0dc53541474958
(AGIX's Asset Subject) I can see the pool address was different on the outputs for the AGIX token. I hope this helps identify pinpoint issue.
I've been testing this adapter, it works really well so far, great work guys, it's extremely valuable.
I've been tested this in conjunction with Wingriders Adapter, this actually returns the correct amount and this is mimicked in the UI.
But with this implementation, the getAmountOut
function, is returning a quantity much higher than what I am getting in the UI. I assume this amount is excluding any fees, whereas the UI indicates the quantity after fees are taken.
This may be by design so may need some guidance here, @longngn, but without knowing the correct quantity or fee deduction it is difficult for me to estimate a valid swaps quantity output, because of the large difference here, I could potentially offset this with a function to minus of the fees, but I think it would be extremely valuable to indicate its intention either way in documentation or if this function is returning the incorrect value and it should be the amount after fees or something else even better, as this is preventing me from calculating amounts and submitting transactions. Or if I am missing something completely, which could also be the case.
Details.
500 ADA Swap amount, to SUNDAE returns 10613 SUNDAE from this function, however the UI returns 10329 SUNDAE.
const amountOfTokenReceived = pool?.getAmountOut(adaAssetId, BigInt(amountToSwap));
Happy to provide further details if it helps at all! :)
No typing for this module. There is no possibility to import this package
Firstly, I'm really excited about this integration!
When I installed this package into a blank typescript project, I am receiving the following, from using the listed example in the ReadMe.
Uncaught TypeError TypeError: utxos.filter is not a function
at getPools (/Users/username/source/personal/node-typescript-template/node_modules/@minswap/blockfrost-adapter/src/adapter.ts:34:18)
at processTicksAndRejections (internal/process/task_queues:96:5)
Edit - Just done some digging and this only occurs when the network is set to testnet, mainnet works fine and pools are returned, I hope this helps.
Dear,
Months ago I've used below code to get pool ID, but it seems not work recently
`async function runExample() {
while(true){
try {
for (let i = 1; ; i++) {
const pools = await api.getPools({ page: i });
if (pools.length === 0) {
// last page
break;
}
const ABPool = (pools.find(
(p) =>
p.assetA === "lovelace" &&
p.assetB ===
"279c909f348e533da5808898f87f9a14bb2c3dfbbacccd631d927a3f534e454b") //SNEK
);
console.log(ABPool);
break;
}
await delay(10000);
} catch (err) {
console.log('error', err);
}
};
};
runExample();
`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.