Skip to content

Commit

Permalink
v3 setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Sluder committed Mar 6, 2024
1 parent d8643db commit 71e5fc6
Show file tree
Hide file tree
Showing 10 changed files with 510 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SundaeSwapV1 } from '../sundaeswap-v1';
import { RequestConfig } from '@app/types';
import { appendSlash } from '@app/utils';

export class SundaeSwapApi extends BaseApi {
export class SundaeSwapV1Api extends BaseApi {

protected readonly api: AxiosInstance;
protected readonly dex: SundaeSwapV1;
Expand Down
114 changes: 114 additions & 0 deletions src/dex/api/sundaeswap-v3-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import { BaseApi } from './base-api';
import { Asset, Token } from '../models/asset';
import { LiquidityPool } from '../models/liquidity-pool';
import axios, { AxiosInstance } from 'axios';
import { SundaeSwapV1 } from '../sundaeswap-v1';
import { RequestConfig } from '@app/types';
import { appendSlash } from '@app/utils';
import { SundaeSwapV3 } from '@dex/sundaeswap-v3';

export class SundaeSwapV3Api extends BaseApi {

protected readonly api: AxiosInstance;
protected readonly dex: SundaeSwapV3;

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

this.dex = dex;
this.api = axios.create({
timeout: requestConfig.timeout,
baseURL: `${appendSlash(requestConfig.proxyUrl)}https://stats.sundaeswap.finance/graphql`,
headers: {
'Content-Type': 'application/json',
}
});
}

liquidityPools(assetA: Token, assetB?: Token): Promise<LiquidityPool[]> {
const maxPerPage: number = 100;

const assetAId: string = (assetA === 'lovelace')
? ''
: assetA.identifier('.');
let assetBId: string = (assetB && assetB !== 'lovelace')
? assetB.identifier('.')
: '';

const getPaginatedResponse = (page: number): Promise<LiquidityPool[]> => {
return this.api.post('', {
operationName: 'getPoolsByAssetIds',
query: `
query getPoolsByAssetIds($assetIds: [String!]!, $pageSize: Int, $page: Int) {
pools(assetIds: $assetIds, pageSize: $pageSize, page: $page) {
...PoolFragment
}
}
fragment PoolFragment on Pool {
assetA {
...AssetFragment
}
assetB {
...AssetFragment
}
assetLP {
...AssetFragment
}
name
fee
quantityA
quantityB
quantityLP
ident
assetID
}
fragment AssetFragment on Asset {
assetId
decimals
}
`,
variables: {
page: page,
pageSize: maxPerPage,
assetIds: [assetBId !== '' ? assetBId : assetAId],
},
}).then((response: any) => {
const pools = response.data.data.pools;
const liquidityPools = pools.map((pool: any) => {
let liquidityPool: LiquidityPool = new LiquidityPool(
SundaeSwapV1.identifier,
pool.assetA.assetId
? Asset.fromIdentifier(pool.assetA.assetId, pool.assetA.decimals)
: 'lovelace',
pool.assetB.assetId
? Asset.fromIdentifier(pool.assetB.assetId, pool.assetB.decimals)
: 'lovelace',
BigInt(pool.quantityA),
BigInt(pool.quantityB),
this.dex.poolAddress,
this.dex.orderAddress,
this.dex.orderAddress,
);

liquidityPool.identifier = pool.ident;
liquidityPool.lpToken = Asset.fromIdentifier(pool.assetLP.assetId);
liquidityPool.poolFeePercent = Number(pool.fee);
liquidityPool.totalLpTokens = BigInt(pool.quantityLP);

return liquidityPool;
});

if (pools.length < maxPerPage) {
return liquidityPools;
}

return getPaginatedResponse(page + 1).then((nextPagePools: LiquidityPool[]) => {
return liquidityPools.concat(nextPagePools);
});
});
};

return getPaginatedResponse(0);
}

}
2 changes: 1 addition & 1 deletion src/dex/base-dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export abstract class BaseDex {
/**
* Fees associated with submitting a swap order.
*/
abstract swapOrderFees(): SwapFee[];
abstract swapOrderFees(liquidityPool?: LiquidityPool, swapInToken?: Token, swapInAmount?: bigint): SwapFee[];

/**
* Adjust the payment for the DEX order address to include the swap in amount.
Expand Down
82 changes: 82 additions & 0 deletions src/dex/definitions/sundaeswap-v3/order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { DatumParameterKey } from '@app/constants';

export default {
constructor: 0,
fields: [
{
bytes: DatumParameterKey.PoolIdentifier
},
{
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.SenderPubKeyHash
}
]
},
{
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.SenderStakingKeyHash
}
]
}
]
}
]
}
]
},
{
constructor: 1,
fields: []
}
]
},
{
constructor: 1,
fields: []
}
]
},
{
int: DatumParameterKey.ScooperFee
},
{
constructor: 0,
fields: [
{
constructor: DatumParameterKey.Action,
fields: []
},
{
int: DatumParameterKey.SwapInAmount
},
{
constructor: 0,
fields: [
{
int: DatumParameterKey.MinReceive
}
]
}
]
}
]
}
51 changes: 51 additions & 0 deletions src/dex/definitions/sundaeswap-v3/pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { DatumParameterKey } from '@app/constants';

export default {
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.PoolAssetAPolicyId
},
{
bytes: DatumParameterKey.PoolAssetAAssetName
}
]
},
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.PoolAssetBPolicyId
},
{
bytes: DatumParameterKey.PoolAssetBAssetName
}
]
}
]
},
{
bytes: DatumParameterKey.PoolIdentifier
},
{
int: DatumParameterKey.TotalLpTokens
},
{
constructor: 0,
fields: [
{
int: DatumParameterKey.LpFeeNumerator
},
{
int: DatumParameterKey.LpFeeDenominator
}
]
}
]
}
4 changes: 2 additions & 2 deletions src/dex/sundaeswap-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { AddressType, DatumParameterKey } from '@app/constants';
import pool from '@dex/definitions/sundaeswap-v1/pool';
import order from '@dex/definitions/sundaeswap-v1/order';
import { BaseApi } from '@dex/api/base-api';
import { SundaeSwapApi } from '@dex/api/sundaeswap-api';
import { SundaeSwapV1Api } from '@dex/api/sundaeswap-v1-api';
import { Script } from 'lucid-cardano';

export class SundaeSwapV1 extends BaseDex {
Expand All @@ -41,7 +41,7 @@ export class SundaeSwapV1 extends BaseDex {
constructor(requestConfig: RequestConfig = {}) {
super();

this.api = new SundaeSwapApi(this, requestConfig);
this.api = new SundaeSwapV1Api(this, requestConfig);
}

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

0 comments on commit 71e5fc6

Please sign in to comment.