diff --git a/examples/example.ts b/examples/example.ts index c793ade..fe80136 100644 --- a/examples/example.ts +++ b/examples/example.ts @@ -864,6 +864,8 @@ async function _swapStableExample( const pool = await blockfrostAdapter.getStablePoolByLpAsset(lpAsset); + invariant(pool, `Can not find pool by lp asset ${Asset.toString(lpAsset)}`); + const swapAmount = 1_000n; // This pool has 2 assets in its config. They are [tDJED, tiUSD]. @@ -913,6 +915,8 @@ async function _depositStableExample( const pool = await blockfrostAdapter.getStablePoolByLpAsset(lpAsset); + invariant(pool, `Can not find pool by lp asset ${Asset.toString(lpAsset)}`); + // This pool has 2 assets in its config. They are [tDJED, tiUSD]. // This order deposits 100_000n tDJED and 1_000n tiUSD into the pool. const amountIns = [100_000n, 1_000n]; @@ -962,6 +966,8 @@ async function _withdrawStableExample( const pool = await blockfrostAdapter.getStablePoolByLpAsset(lpAsset); + invariant(pool, `Can not find pool by lp asset ${Asset.toString(lpAsset)}`); + const lpAmount = 10_000n; const amountOuts = StableswapCalculation.calculateWithdraw({ @@ -1001,6 +1007,8 @@ async function _withdrawImbalanceStableExample( const pool = await blockfrostAdapter.getStablePoolByLpAsset(lpAsset); + invariant(pool, `Can not find pool by lp asset ${Asset.toString(lpAsset)}`); + const withdrawAmounts = [1234n, 5678n]; // This pool has 2 assets in its config. They are [tDJED, tiUSD]. @@ -1046,6 +1054,8 @@ async function _zapOutStableExample( const pool = await blockfrostAdapter.getStablePoolByLpAsset(lpAsset); + invariant(pool, `Can not find pool by lp asset ${Asset.toString(lpAsset)}`); + // This pool has 2 assets in its config. They are [tDJED, tiUSD]. // This order withdraws xxx tiUSD by 12345 Lp Assets from the pool. const lpAmount = 12345n; diff --git a/src/adapter.ts b/src/adapter.ts index 1ca427d..3b97280 100644 --- a/src/adapter.ts +++ b/src/adapter.ts @@ -412,7 +412,7 @@ export class BlockfrostAdapter { public async getStablePoolByLpAsset( lpAsset: Asset - ): Promise { + ): Promise { const config = StableswapConstant.CONFIG[this.networkId].find( (cfg) => cfg.lpAsset === Asset.toString(lpAsset) ); @@ -426,10 +426,11 @@ export class BlockfrostAdapter { config.poolAddress, config.nftAsset ); - invariant(poolUtxos.length === 1, `Can not find pool utxo in blockchain`); - const poolUtxo = poolUtxos[0]; - const pool = await this.parseStablePoolState(poolUtxo); - return pool; + if (poolUtxos.length === 1) { + const poolUtxo = poolUtxos[0]; + return await this.parseStablePoolState(poolUtxo); + } + return null; } public async getStablePoolByNFT( @@ -443,29 +444,14 @@ export class BlockfrostAdapter { `Cannot find Stable Pool having NFT ${Asset.toString(nft)}` ); } - const utxos = await this.api.addressesUtxosAssetAll( + const poolUtxos = await this.api.addressesUtxosAssetAll( poolAddress, Asset.toString(nft) ); - for (const utxo of utxos) { - let datum: string; - if (utxo.inline_datum) { - datum = utxo.inline_datum; - } else if (utxo.data_hash) { - datum = await this.getDatumByDatumHash(utxo.data_hash); - } else { - throw new Error("Cannot find datum of Stable Pool"); - } - const pool = new StablePool.State( - this.networkId, - utxo.address, - { txHash: utxo.tx_hash, index: utxo.output_index }, - utxo.amount, - datum - ); - return pool; + if (poolUtxos.length === 1) { + const poolUtxo = poolUtxos[0]; + return await this.parseStablePoolState(poolUtxo); } - return null; } diff --git a/test/adapter.test.ts b/test/adapter.test.ts index fc0a220..87f2b95 100644 --- a/test/adapter.test.ts +++ b/test/adapter.test.ts @@ -142,6 +142,29 @@ test("getAllStablePools", async () => { expect(mainnetPools.length === numberOfStablePoolsMainnet); }); +test("getStablePoolByLPAsset", async () => { + const testnetCfgs = StableswapConstant.CONFIG[NetworkId.TESTNET]; + const mainnetCfgs = StableswapConstant.CONFIG[NetworkId.MAINNET]; + + for (const cfg of testnetCfgs) { + const pool = await adapterTestnet.getStablePoolByLpAsset( + Asset.fromString(cfg.lpAsset) + ); + expect(pool).not.toBeNull(); + expect(pool?.nft).toEqual(cfg.nftAsset); + expect(pool?.assets).toEqual(cfg.assets); + } + + for (const cfg of mainnetCfgs) { + const pool = await adapterMainnet.getStablePoolByLpAsset( + Asset.fromString(cfg.lpAsset) + ); + expect(pool).not.toBeNull(); + expect(pool?.nft).toEqual(cfg.nftAsset); + expect(pool?.assets).toEqual(cfg.assets); + } +}); + test("getStablePoolByNFT", async () => { const testnetCfgs = StableswapConstant.CONFIG[NetworkId.TESTNET]; const mainnetCfgs = StableswapConstant.CONFIG[NetworkId.MAINNET];