Skip to content

Commit

Permalink
TeddySwap API
Browse files Browse the repository at this point in the history
  • Loading branch information
Sluder committed Dec 13, 2023
1 parent 9c096fe commit 7930518
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
61 changes: 61 additions & 0 deletions src/dex/api/teddyswap-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { BaseApi } from './base-api';
import { Asset, Token } from '../models/asset';
import { LiquidityPool } from '../models/liquidity-pool';
import axios, { AxiosInstance } from 'axios';
import { RequestConfig } from '@app/types';
import { appendSlash, tokensMatch } from '@app/utils';
import { TeddySwap } from '@dex/teddyswap';

export class TeddyswapApi extends BaseApi {

protected readonly api: AxiosInstance;
protected readonly dex: TeddySwap;

constructor(dex: TeddySwap, requestConfig: RequestConfig) {
super();

this.dex = dex;
this.api = axios.create({
timeout: requestConfig.timeout,
baseURL: `${appendSlash(requestConfig.proxyUrl)}https://analytics.teddyswap.org/v1`,
headers: {
'Content-Type': 'application/json',
}
});
}

liquidityPools(assetA: Token, assetB?: Token): Promise<LiquidityPool[]> {
return this.api.get('/front/pools', ).then((response: any) => {
return response.data.map((poolResponse: any) => {
const tokenA: Token = poolResponse.lockedX.asset.currencySymbol !== ''
? new Asset(poolResponse.lockedX.asset.currencySymbol, Buffer.from(poolResponse.lockedX.asset.tokenName, 'utf8').toString('hex'))
: 'lovelace';
const tokenB: Token = poolResponse.lockedY.asset.currencySymbol !== ''
? new Asset(poolResponse.lockedY.asset.currencySymbol, Buffer.from(poolResponse.lockedY.asset.tokenName, 'utf8').toString('hex'))
: 'lovelace';

if (! tokensMatch(tokenA, assetA) || (assetB && ! tokensMatch(tokenB, assetB))) {
return undefined;
}

let liquidityPool: LiquidityPool = new LiquidityPool(
TeddySwap.identifier,
tokenA,
tokenB,
BigInt(poolResponse.lockedX.amount),
BigInt(poolResponse.lockedY.amount),
'', // Not supplied
this.dex.orderAddress,
this.dex.orderAddress,
);

liquidityPool.lpToken = new Asset(poolResponse.lockedLQ.asset.currencySymbol, Buffer.from(poolResponse.lockedLQ.asset.tokenName, 'utf8').toString('hex'));
liquidityPool.poolFeePercent = (1 - (poolResponse.poolFeeNum / poolResponse.poolFeeDenum)) * 100;
liquidityPool.identifier = poolResponse.id;

return liquidityPool;
}).filter((pool: LiquidityPool | undefined) => pool !== undefined) as LiquidityPool[];
});
}

}
6 changes: 4 additions & 2 deletions src/dex/teddyswap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { AddressType, DatumParameterKey } from '@app/constants';
import { BaseApi } from '@dex/api/base-api';
import pool from './definitions/teddyswap/pool';
import order from './definitions/teddyswap/order';
import { correspondingReserves, tokensMatch } from '..';
import { correspondingReserves, tokensMatch } from '@app/utils';
import { TeddyswapApi } from '@dex/api/teddyswap-api';

export class TeddySwap extends BaseDex {

Expand All @@ -21,9 +22,10 @@ export class TeddySwap extends BaseDex {
public readonly orderAddress: string = 'addr1z99tz7hungv6furtdl3zn72sree86wtghlcr4jc637r2eadkp2avt5gp297dnxhxcmy6kkptepsr5pa409qa7gf8stzs0706a3';
public readonly poolAddress: string = 'addr1zy5th50h46anh3v7zdvh7ve6amac7k4h3mdfvt0p6czm8z9kp2avt5gp297dnxhxcmy6kkptepsr5pa409qa7gf8stzsxg8sx3';


constructor(requestConfig: RequestConfig = {}) {
super();

this.api = new TeddyswapApi(this, requestConfig);
}

public async liquidityPoolAddresses(provider: BaseDataProvider): Promise<string[]> {
Expand Down

0 comments on commit 7930518

Please sign in to comment.