Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spectrum integration #70

Merged
merged 6 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to Dexter will be documented in this file.

## [UNRELEASED]
- TeddySwap integration
- Spectrum integration

## [v4.2.0]
- Fix WR price impact formula for 0 decimals
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<img src="https://raw.githubusercontent.com/IndigoProtocol/dexter/master/src/dex/logo/wingriders.png" width="30" />
<img src="https://raw.githubusercontent.com/IndigoProtocol/dexter/master/src/dex/logo/vyfinance.png" width="30" />
<img src="https://raw.githubusercontent.com/IndigoProtocol/dexter/master/src/dex/logo/teddyswap.png" width="30" />
<img src="https://raw.githubusercontent.com/IndigoProtocol/dexter/master/src/dex/logo/spectrum.png" width="30" />
</div>

### What You Can Do
Expand Down
64 changes: 64 additions & 0 deletions src/dex/api/spectrum-api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
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';
import { Spectrum } from '@dex/spectrum';

export class SpectrumApi 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-balanced.spectrum.fi/cardano`,
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(
Spectrum.identifier,
tokenA,
tokenB,
BigInt(poolResponse.lockedX.amount),
BigInt(poolResponse.lockedY.amount),
'', // Not supplied
this.dex.orderAddress,
this.dex.orderAddress,
);

const [poolNftPolicyId, poolNftName] = poolResponse.id.split('.');
liquidityPool.poolNft = new Asset(poolNftPolicyId, Buffer.from(poolNftName, 'utf8').toString('hex'));
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 = liquidityPool.lpToken.identifier();

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

}
85 changes: 85 additions & 0 deletions src/dex/definitions/spectrum/order.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { DatumParameterKey } from '@app/constants';
import { DatumParameters, DefinitionField } from '@app/types';

export default {
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.SwapInTokenPolicyId
},
{
bytes: DatumParameterKey.SwapInTokenAssetName
}
],
},
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.SwapOutTokenPolicyId
},
{
bytes: DatumParameterKey.SwapOutTokenAssetName
}
],
},
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.TokenPolicyId // Pool NFT
},
{
bytes: DatumParameterKey.TokenAssetName
}
],
},
{
int: DatumParameterKey.LpFee
},
{
int: DatumParameterKey.LpFeeNumerator // Execution fee numerator
},
{
int: DatumParameterKey.LpFeeDenominator // Execution fee denominator
},
{
bytes: DatumParameterKey.SenderPubKeyHash
},
(field: DefinitionField, parameters: DatumParameters, shouldExtract: boolean = true) => {
if (! shouldExtract) {
const stakeKeyHash: string = parameters[DatumParameterKey.SenderStakingKeyHash] as string ?? null;

if (! stakeKeyHash) return;

return {
constructor: 0,
fields: [
{
bytes: stakeKeyHash,
}
],
};
}

if ('fields' in field) {
if (field.constructor === 1) return;

if (field.fields.length > 0 && 'bytes' in field.fields[0]) {
parameters[DatumParameterKey.SenderStakingKeyHash] = field.fields[0].bytes;
}
}

return;
},
{
int: DatumParameterKey.SwapInAmount
},
{
int: DatumParameterKey.MinReceive
}
],
}
62 changes: 62 additions & 0 deletions src/dex/definitions/spectrum/pool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { DatumParameterKey } from '@app/constants';

export default {
constructor: 0,
fields: [
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.TokenPolicyId // Pool NFT
},
{
bytes: DatumParameterKey.TokenAssetName
}
]
},
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.PoolAssetAPolicyId
},
{
bytes: DatumParameterKey.PoolAssetAAssetName
}
]
},
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.PoolAssetBPolicyId
},
{
bytes: DatumParameterKey.PoolAssetBAssetName
}
]
},
{
constructor: 0,
fields: [
{
bytes: DatumParameterKey.LpTokenPolicyId
},
{
bytes: DatumParameterKey.LpTokenAssetName
}
]
},
{
int: DatumParameterKey.LpFee
},
[
{
bytes: DatumParameterKey.StakeAdminPolicy
}
],
{
int: DatumParameterKey.LqBound
}
]
}
Binary file added src/dex/logo/spectrum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading