feat: use separate file to avoid cycle reference

This commit is contained in:
Kyle Fang
2023-03-15 14:04:29 +08:00
parent a391c0a7db
commit dfa343268c
12 changed files with 76 additions and 66 deletions

48
src/alexSDK.ts Normal file
View File

@@ -0,0 +1,48 @@
import { Currency } from './currency';
import { getLiquidityProviderFee } from './helpers/FeeHelper';
import { AMMSwapPool } from './utils/ammPool';
import { getRoute } from './helpers/RouteHelper';
import { getYAmountFromXAmount } from './helpers/RateHelper';
import { runSpot, TxToBroadCast } from './helpers/SwapHelper';
import { findCurrencyByNativeAddress } from './utils/currencyUtils';
export class AlexSDK {
getFee(from: Currency, to: Currency): Promise<bigint> {
return getLiquidityProviderFee(from, to, AMMSwapPool.ammTokens);
}
getRouter(from: Currency, to: Currency): Promise<Currency[]> {
return getRoute(from, to, AMMSwapPool.ammTokens);
}
getAmountTo(
from: Currency,
fromAmount: bigint,
to: Currency,
): Promise<bigint> {
return getYAmountFromXAmount(from, to, fromAmount, AMMSwapPool.ammTokens);
}
runSwap(
stxAddress: string,
currencyX: Currency,
currencyY: Currency,
fromAmount: bigint,
minDy: bigint,
router: Currency[],
): TxToBroadCast {
return runSpot(
stxAddress,
currencyX,
currencyY,
fromAmount,
minDy,
router,
AMMSwapPool.ammTokens,
);
}
getCurrencyFrom(address: string): Currency | undefined {
return findCurrencyByNativeAddress(address);
}
}

13
src/currency.ts Normal file
View File

@@ -0,0 +1,13 @@
export enum Currency {
ALEX = 'age000-governance-token',
USDA = 'token-wusda',
STX = 'token-wstx',
BANANA = 'token-wban',
XBTC = 'token-wbtc',
DIKO = 'token-wdiko',
SLIME = 'token-wslm',
XUSD = 'token-wxusd',
MIA = 'token-wmia',
NYCC = 'token-wnycc',
CORGI = 'token-wcorgi',
}

View File

@@ -1,7 +1,8 @@
import { AlexSDK, Currency } from '../index';
import { AMMSwapPool } from '../utils/ammPool';
import { unwrapResponse } from 'clarity-codegen';
import { readonlyCall } from '../utils/readonlyCallExecutor';
import { Currency } from '../currency';
import { AlexSDK } from '../alexSDK';
export async function getLiquidityProviderFee(
tokenX: Currency,

View File

@@ -1,7 +1,8 @@
import { AlexSDK, Currency } from '../index';
import { AMMSwapPool } from '../utils/ammPool';
import { unwrapResponse } from 'clarity-codegen';
import { readonlyCall } from '../utils/readonlyCallExecutor';
import { Currency } from '../currency';
import { AlexSDK } from '../alexSDK';
export const getYAmountFromXAmount = async (
tokenX: Currency,

View File

@@ -1,7 +1,8 @@
import { AlexSDK, Currency } from '../index';
import { AMMSwapPool } from '../utils/ammPool';
import { unwrapResponse } from 'clarity-codegen';
import { readonlyCall } from '../utils/readonlyCallExecutor';
import { Currency } from '../currency';
import { AlexSDK } from '../alexSDK';
export async function getRoute(
from: Currency,

View File

@@ -14,7 +14,7 @@ import {
} from 'clarity-codegen';
import { AlexContracts } from '../generated/smartContract/contracts_Alex';
import { CONTRACT_DEPLOYER } from '../config';
import { Currency } from '../index';
import { Currency } from '../currency';
export type TxToBroadCast = {
contractAddress: string;

View File

@@ -1,57 +1,2 @@
import { getLiquidityProviderFee } from './helpers/FeeHelper';
import { AMMSwapPool } from './utils/ammPool';
import { getRoute } from './helpers/RouteHelper';
import { getYAmountFromXAmount } from './helpers/RateHelper';
import { runSpot, TxToBroadCast } from './helpers/SwapHelper';
import { findCurrencyByNativeAddress } from './utils/currencyUtils';
export enum Currency {
ALEX = 'age000-governance-token',
USDA = 'token-wusda',
STX = 'token-wstx',
BANANA = 'token-wban',
XBTC = 'token-wbtc',
DIKO = 'token-wdiko',
SLIME = 'token-wslm',
XUSD = 'token-wxusd',
MIA = 'token-wmia',
NYCC = 'token-wnycc',
CORGI = 'token-wcorgi',
}
export class AlexSDK {
getFee(from: Currency, to: Currency): Promise<bigint> {
return getLiquidityProviderFee(from, to, AMMSwapPool.ammTokens);
}
getRouter(from: Currency, to: Currency): Promise<Currency[]> {
return getRoute(from, to, AMMSwapPool.ammTokens);
}
getAmountTo(
from: Currency,
fromAmount: bigint,
to: Currency
): Promise<bigint> {
return getYAmountFromXAmount(from, to, fromAmount, AMMSwapPool.ammTokens);
}
runSwap(
stxAddress: string,
currencyX: Currency,
currencyY: Currency,
fromAmount: bigint,
minDy: bigint,
router: Currency[]
): TxToBroadCast {
return runSpot(
stxAddress,
currencyX,
currencyY,
fromAmount,
minDy,
router,
AMMSwapPool.ammTokens
);
}
getCurrencyFrom(address: string): Currency | undefined {
return findCurrencyByNativeAddress(address);
}
}
export * from './currency';
export * from './alexSDK';

View File

@@ -1,7 +1,7 @@
import { AMMSwapRoute, resolveAmmRoute } from './ammRouteResolver';
import { bridgeHelperResolver } from './bridgeHelperResolver';
import { assertNever } from './utils';
import { Currency } from '../index';
import { Currency } from '../currency';
export namespace AMMSwapPool {
export enum Pool {

View File

@@ -1,6 +1,6 @@
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
import { AMMSwapPool } from './ammPool';
import { Currency } from '../index';
import { Currency } from '../currency';
export function bridgeHelperResolver(
from: Currency,

View File

@@ -1,4 +1,4 @@
import { Currency } from '../index';
import { Currency } from '../currency';
const tokenNativeAddressDefinition = {
'age000-governance-token':

View File

@@ -7,7 +7,7 @@ import {
} from '@stacks/transactions';
import { CONTRACT_DEPLOYER } from '../config';
import { getCurrencyNativeAddress, getCurrencyNativeScale } from './currencyUtils';
import { Currency } from '../index';
import { Currency } from '../currency';
export const AlexVault = `${CONTRACT_DEPLOYER}.alex-vault`;

View File

@@ -1,5 +1,6 @@
import { AlexSDK, Currency } from '../src';
import { CONTRACT_DEPLOYER } from '../src/config';
import { Currency } from '../src/currency';
import { AlexSDK } from '../src/alexSDK';
describe.skip('AlexSDK', () => {
it('Get fee', async () => {