feat: add static function to configure AlexSDK

This commit is contained in:
Kyle Fang
2023-04-24 14:47:11 +08:00
parent 115791b937
commit 864f3a9fa3
7 changed files with 43 additions and 22 deletions

View File

@@ -6,8 +6,13 @@ import { getYAmountFromXAmount } from './helpers/RateHelper';
import { runSpot, TxToBroadCast } from './helpers/SwapHelper';
import { findCurrencyByNativeAddress } from './utils/currencyUtils';
import { fetchLatestPrices } from './utils/currencyPrice';
import { AlexConfig, assignConfig } from './config';
export class AlexSDK {
static configure(config: Partial<AlexConfig>) {
assignConfig(config);
}
getFeeRate(from: Currency, to: Currency): Promise<bigint> {
return getLiquidityProviderFee(from, to, AMMSwapPool.ammTokens);
}

View File

@@ -1,2 +1,15 @@
export const CONTRACT_DEPLOYER = 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9';
export const API_HOST = "https://stacks-node-api.alexlab.co"
const CONTRACT_DEPLOYER = 'SP3K8BC0PPEVCV7NZ6QSRWPQ2JE9E5B6N3PA0KBR9';
const API_HOST = 'https://stacks-node-api.alexlab.co';
const IS_MAINNET = true;
export const configs = {
IS_MAINNET,
CONTRACT_DEPLOYER,
API_HOST,
};
export type AlexConfig = typeof configs;
export function assignConfig(newConfigs: Partial<AlexConfig>) {
Object.assign(configs, newConfigs);
}

View File

@@ -6,7 +6,7 @@ import {
} from '@stacks/transactions';
import { addressResult, contractResult, transcoders } from 'clarity-codegen';
import type { Decoder } from 'clarity-codegen/lib/runtime/types';
import { CONTRACT_DEPLOYER } from '../../config';
import { configs } from '../../config';
export * from 'clarity-codegen';
@@ -18,7 +18,7 @@ export function principalCV(principal: string): PrincipalCV {
if (principal.startsWith('SP') || principal.startsWith('ST')) {
return standardPrincipalCV(principal);
}
return contractPrincipalCV(CONTRACT_DEPLOYER, principal);
return contractPrincipalCV(configs.CONTRACT_DEPLOYER, principal);
}
export const principleResult: Decoder<string> = (result) => {

View File

@@ -1,5 +1,5 @@
import { AMMSwapPool } from '../utils/ammPool';
import { AlexVault, transfer } from '../utils/postConditions';
import { transfer } from '../utils/postConditions';
import {
ClarityValue,
FungibleConditionCode,
@@ -9,11 +9,9 @@ import {
import {
OpenCallFunctionDescriptor,
ParameterObjOfDescriptor,
ReadonlyFunctionDescriptor,
ReturnTypeOfDescriptor,
} from 'clarity-codegen';
import { AlexContracts } from '../generated/smartContract/contracts_Alex';
import { CONTRACT_DEPLOYER } from '../config';
import { configs } from '../config';
import { Currency } from '../currency';
export type TxToBroadCast = {
@@ -48,7 +46,7 @@ const composeTx: <
contractName,
functionName: String(functionName),
functionArgs: clarityArgs,
contractAddress: CONTRACT_DEPLOYER,
contractAddress: configs.CONTRACT_DEPLOYER,
postConditions,
};
};
@@ -62,6 +60,7 @@ export function runSpot(
router: Currency[],
ammPools: AMMSwapPool.PoolTokens[]
): TxToBroadCast {
const AlexVault = `${configs.CONTRACT_DEPLOYER}.alex-vault`;
const ammRoute = AMMSwapPool.getRoute(currencyX, currencyY, ammPools);
const middleSteps = router.slice(1, -1);
if (ammRoute.length === 0) {

View File

@@ -5,12 +5,12 @@ import {
FungiblePostCondition,
STXPostCondition,
} from '@stacks/transactions';
import { CONTRACT_DEPLOYER } from '../config';
import { getCurrencyNativeAddress, getCurrencyNativeScale } from './currencyUtils';
import {
getCurrencyNativeAddress,
getCurrencyNativeScale,
} from './currencyUtils';
import { Currency } from '../currency';
export const AlexVault = `${CONTRACT_DEPLOYER}.alex-vault`;
export function transfer(
senderAddress: string,
currency: Currency,

View File

@@ -1,6 +1,6 @@
import { callReadOnlyFunction, ClarityValue } from '@stacks/transactions';
import { API_HOST, CONTRACT_DEPLOYER } from '../config';
import { StacksMainnet } from '@stacks/network';
import { configs } from '../config';
import { StacksMainnet, StacksTestnet } from '@stacks/network';
import {
ParameterObjOfDescriptor,
ReadonlyFunctionDescriptor,
@@ -21,10 +21,14 @@ type ReadonlyCallExecutor = (
const defaultReadonlyCallExecutor: ReadonlyCallExecutor = async (options) => {
return callReadOnlyFunction({
...options,
senderAddress: CONTRACT_DEPLOYER,
network: new StacksMainnet({
url: API_HOST,
}),
senderAddress: configs.CONTRACT_DEPLOYER,
network: configs.IS_MAINNET
? new StacksMainnet({
url: configs.API_HOST,
})
: new StacksTestnet({
url: configs.API_HOST,
}),
});
};
@@ -53,7 +57,7 @@ export async function readonlyCall<
contractName,
functionName: String(functionName),
functionArgs: clarityArgs,
contractAddress: CONTRACT_DEPLOYER,
contractAddress: configs.CONTRACT_DEPLOYER,
});
return functionDescriptor.output.decode(result);
}

View File

@@ -1,4 +1,4 @@
import { CONTRACT_DEPLOYER } from '../src/config';
import { configs } from '../src/config';
import { Currency } from '../src/currency';
import { AlexSDK } from '../src/alexSDK';
@@ -26,7 +26,7 @@ describe.skip('AlexSDK', () => {
const sdk = new AlexSDK();
const router = await sdk.getRouter(Currency.STX, Currency.DIKO);
const result = await sdk.runSwap(
CONTRACT_DEPLOYER,
configs.CONTRACT_DEPLOYER,
Currency.STX,
Currency.DIKO,
BigInt(2) * BigInt(1e8),