-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
510 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.