Skip to content

Commit

Permalink
feat: update the codebase to latest stacks.js SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
zhigang1992 committed Nov 28, 2024
1 parent a865bb3 commit cb0d068
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 239 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@
"clarity-codegen": "^0.5.2"
},
"peerDependencies": {
"@stacks/network": "^6.3.0",
"@stacks/transactions": "^6.2.0"
"@stacks/network": "^7.0.2",
"@stacks/transactions": "^7.0.2"
},
"devDependencies": {
"@size-limit/preset-small-lib": "^8.2.4",
"@stacks/network": "^6.3.0",
"@stacks/network": "^7.0.2",
"@stacks/stacks-blockchain-api-types": "^7.11.0",
"@stacks/transactions": "^6.2.0",
"@stacks/transactions": "^7.0.2",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.2",
"ajv": "^8.16.0",
Expand Down
53 changes: 19 additions & 34 deletions pnpm-lock.yaml

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

57 changes: 31 additions & 26 deletions src/helpers/FeeHelper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { unwrapResponse } from 'clarity-codegen';
import { readonlyCall } from '../utils/readonlyCallExecutor';
import { Currency } from '../currency';
import { PoolData } from '../types';
import { AMMRouteSegment, resolveAmmRoute } from '../utils/ammRouteResolver';
import type { Currency } from '../currency';
import type { PoolData } from '../types';
import { type AMMRouteSegment, resolveAmmRoute } from '../utils/ammRouteResolver';

Check failure on line 5 in src/helpers/FeeHelper.ts

View workflow job for this annotation

GitHub Actions / build

Replace `·type·AMMRouteSegment,·resolveAmmRoute·` with `⏎··type·AMMRouteSegment,⏎··resolveAmmRoute,⏎`
import { hasLength } from '../utils/arrayHelper';

export async function getLiquidityProviderFee(
tokenX: Currency,
Expand All @@ -15,44 +16,48 @@ export async function getLiquidityProviderFee(
if (ammRoute.length === 0) {
throw new Error('No AMM pools in route');
}
if (ammRoute.length === 1) {
if (hasLength(ammRoute, 1)) {
const [segment] = ammRoute;
return await readonlyCall('amm-pool-v2-01', 'fee-helper', {
'token-x': getContractId(tokenX),
'token-y': getContractId(tokenY),
factor: ammRoute[0]!.pool.factor,
factor: segment.pool.factor,
}).then(unwrapResponse);
}
if (ammRoute.length === 2) {
if (hasLength(ammRoute, 2)) {
const [segment1, segment2] = ammRoute;
return await readonlyCall('amm-pool-v2-01', 'fee-helper-a', {
'token-x': getContractId(tokenX),
'token-y': getContractId(ammRoute[0]!.neighbour),
'token-z': getContractId(ammRoute[1]!.neighbour),
'factor-x': ammRoute[0]!.pool.factor,
'factor-y': ammRoute[1]!.pool.factor,
'token-y': getContractId(segment1.neighbour),
'token-z': getContractId(segment2.neighbour),
'factor-x': segment1.pool.factor,
'factor-y': segment2.pool.factor,
}).then(unwrapResponse);
}
if (ammRoute.length === 3) {
if (hasLength(ammRoute, 3)) {
const [segment1, segment2, segment3] = ammRoute;
return await readonlyCall('amm-pool-v2-01', 'fee-helper-b', {
'token-x': getContractId(tokenX),
'token-y': getContractId(ammRoute[0]!.neighbour),
'token-z': getContractId(ammRoute[1]!.neighbour),
'token-w': getContractId(ammRoute[2]!.neighbour),
'factor-x': ammRoute[0]!.pool.factor,
'factor-y': ammRoute[1]!.pool.factor,
'factor-z': ammRoute[2]!.pool.factor,
'token-y': getContractId(segment1.neighbour),
'token-z': getContractId(segment2.neighbour),
'token-w': getContractId(segment3.neighbour),
'factor-x': segment1.pool.factor,
'factor-y': segment2.pool.factor,
'factor-z': segment3.pool.factor,
}).then(unwrapResponse);
}
if (ammRoute.length === 4) {
if (hasLength(ammRoute, 4)) {
const [segment1, segment2, segment3, segment4] = ammRoute;
return await readonlyCall('amm-pool-v2-01', 'fee-helper-c', {
'token-x': getContractId(tokenX),
'token-y': getContractId(ammRoute[0]!.neighbour),
'token-z': getContractId(ammRoute[1]!.neighbour),
'token-w': getContractId(ammRoute[2]!.neighbour),
'token-v': getContractId(ammRoute[3]!.neighbour),
'factor-x': ammRoute[0]!.pool.factor,
'factor-y': ammRoute[1]!.pool.factor,
'factor-z': ammRoute[2]!.pool.factor,
'factor-w': ammRoute[3]!.pool.factor,
'token-y': getContractId(segment1.neighbour),
'token-z': getContractId(segment2.neighbour),
'token-w': getContractId(segment3.neighbour),
'token-v': getContractId(segment4.neighbour),
'factor-x': segment1.pool.factor,
'factor-y': segment2.pool.factor,
'factor-z': segment3.pool.factor,
'factor-w': segment4.pool.factor,
}).then(unwrapResponse);
}
throw new Error('Too many AMM pools in route');
Expand Down
59 changes: 32 additions & 27 deletions src/helpers/RateHelper.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { unwrapResponse } from 'clarity-codegen';
import { readonlyCall } from '../utils/readonlyCallExecutor';
import { Currency } from '../currency';
import { PoolData } from '../types';
import { AMMRouteSegment, resolveAmmRoute } from '../utils/ammRouteResolver';
import type { Currency } from '../currency';
import type { PoolData } from '../types';
import { type AMMRouteSegment, resolveAmmRoute } from '../utils/ammRouteResolver';

Check failure on line 5 in src/helpers/RateHelper.ts

View workflow job for this annotation

GitHub Actions / build

Replace `·type·AMMRouteSegment,·resolveAmmRoute·` with `⏎··type·AMMRouteSegment,⏎··resolveAmmRoute,⏎`
import { hasLength } from '../utils/arrayHelper';

export const getYAmountFromXAmount = async (
tokenX: Currency,
Expand All @@ -16,47 +17,51 @@ export const getYAmountFromXAmount = async (
if (ammRoute.length === 0) {
throw new Error('No AMM pool found for the given route');
}
if (ammRoute.length === 1) {
if (hasLength(ammRoute, 1)) {
const [segment] = ammRoute;
return await readonlyCall('amm-pool-v2-01', 'get-helper', {
'token-x': getContractId(tokenX),
'token-y': getContractId(ammRoute[0]!.neighbour),
'token-y': getContractId(segment.neighbour),
dx: fromAmount,
factor: ammRoute[0]!.pool.factor,
factor: segment.pool.factor,
}).then(unwrapResponse);
}
if (ammRoute.length === 2) {
if (hasLength(ammRoute, 2)) {
const [segment1, segment2] = ammRoute;
return await readonlyCall('amm-pool-v2-01', 'get-helper-a', {
'token-x': getContractId(tokenX),
'token-y': getContractId(ammRoute[0]!.neighbour),
'token-z': getContractId(ammRoute[1]!.neighbour),
'factor-x': ammRoute[0]!.pool.factor,
'factor-y': ammRoute[1]!.pool.factor,
'token-y': getContractId(segment1.neighbour),
'token-z': getContractId(segment2.neighbour),
'factor-x': segment1.pool.factor,
'factor-y': segment2.pool.factor,
dx: fromAmount,
}).then(unwrapResponse);
}
if (ammRoute.length === 3) {
if (hasLength(ammRoute, 3)) {
const [segment1, segment2, segment3] = ammRoute;
return await readonlyCall('amm-pool-v2-01', 'get-helper-b', {
'token-x': getContractId(tokenX),
'token-y': getContractId(ammRoute[0]!.neighbour),
'token-z': getContractId(ammRoute[1]!.neighbour),
'token-w': getContractId(ammRoute[2]!.neighbour),
'factor-x': ammRoute[0]!.pool.factor,
'factor-y': ammRoute[1]!.pool.factor,
'factor-z': ammRoute[2]!.pool.factor,
'token-y': getContractId(segment1.neighbour),
'token-z': getContractId(segment2.neighbour),
'token-w': getContractId(segment3.neighbour),
'factor-x': segment1.pool.factor,
'factor-y': segment2.pool.factor,
'factor-z': segment3.pool.factor,
dx: fromAmount,
}).then(unwrapResponse);
}
if (ammRoute.length === 4) {
if (hasLength(ammRoute, 4)) {
const [segment1, segment2, segment3, segment4] = ammRoute;
return await readonlyCall('amm-pool-v2-01', 'get-helper-c', {
'token-x': getContractId(tokenX),
'token-y': getContractId(ammRoute[0]!.neighbour),
'token-z': getContractId(ammRoute[1]!.neighbour),
'token-w': getContractId(ammRoute[2]!.neighbour),
'token-v': getContractId(ammRoute[3]!.neighbour),
'factor-x': ammRoute[0]!.pool.factor,
'factor-y': ammRoute[1]!.pool.factor,
'factor-z': ammRoute[2]!.pool.factor,
'factor-w': ammRoute[3]!.pool.factor,
'token-y': getContractId(segment1.neighbour),
'token-z': getContractId(segment2.neighbour),
'token-w': getContractId(segment3.neighbour),
'token-v': getContractId(segment4.neighbour),
'factor-x': segment1.pool.factor,
'factor-y': segment2.pool.factor,
'factor-z': segment3.pool.factor,
'factor-w': segment4.pool.factor,
dx: fromAmount,
}).then(unwrapResponse);
}
Expand Down
6 changes: 3 additions & 3 deletions src/helpers/RouteHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Currency } from '../currency';
import { PoolData } from '../types';
import { AMMRouteSegment, resolveAmmRoute } from '../utils/ammRouteResolver';
import type { Currency } from '../currency';
import type { PoolData } from '../types';
import { type AMMRouteSegment, resolveAmmRoute } from '../utils/ammRouteResolver';

Check failure on line 3 in src/helpers/RouteHelper.ts

View workflow job for this annotation

GitHub Actions / build

Replace `·type·AMMRouteSegment,·resolveAmmRoute·` with `⏎··type·AMMRouteSegment,⏎··resolveAmmRoute,⏎`

export async function getAllPossibleRoute(
from: Currency,
Expand Down
Loading

0 comments on commit cb0d068

Please sign in to comment.