Skip to content

Commit

Permalink
write test and fix return type of getStablePoolByLpAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n999999 committed Sep 18, 2024
1 parent 4fce42b commit 86a125d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 24 deletions.
10 changes: 10 additions & 0 deletions examples/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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].
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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].
Expand Down Expand Up @@ -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;
Expand Down
34 changes: 10 additions & 24 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export class BlockfrostAdapter {

public async getStablePoolByLpAsset(
lpAsset: Asset
): Promise<StablePool.State> {
): Promise<StablePool.State | null> {
const config = StableswapConstant.CONFIG[this.networkId].find(
(cfg) => cfg.lpAsset === Asset.toString(lpAsset)
);
Expand All @@ -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(
Expand All @@ -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;
}

Expand Down
23 changes: 23 additions & 0 deletions test/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 86a125d

Please sign in to comment.