@kasarlabs/ekubo-mcp 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/index.js +12 -2
- package/build/index.js.map +1 -1
- package/build/lib/constants/addresses.d.ts +0 -3
- package/build/lib/constants/addresses.js +0 -33
- package/build/lib/constants/addresses.js.map +1 -1
- package/build/lib/constants/index.d.ts +1 -0
- package/build/lib/constants/index.js +2 -0
- package/build/lib/constants/index.js.map +1 -0
- package/build/lib/utils/contracts.d.ts +3 -3
- package/build/lib/utils/contracts.js +8 -4
- package/build/lib/utils/contracts.js.map +1 -1
- package/build/lib/utils/math.d.ts +4 -0
- package/build/lib/utils/math.js +34 -1
- package/build/lib/utils/math.js.map +1 -1
- package/build/lib/utils/pools.js +4 -6
- package/build/lib/utils/pools.js.map +1 -1
- package/build/lib/utils/position.d.ts +10 -0
- package/build/lib/utils/position.js +95 -0
- package/build/lib/utils/position.js.map +1 -0
- package/build/lib/utils/quote.d.ts +2 -0
- package/build/lib/utils/quote.js +20 -0
- package/build/lib/utils/quote.js.map +1 -1
- package/build/lib/utils/swap.d.ts +1 -1
- package/build/lib/utils/swap.js +2 -2
- package/build/lib/utils/swap.js.map +1 -1
- package/build/lib/utils/token.d.ts +1 -15
- package/build/lib/utils/token.js +13 -19
- package/build/lib/utils/token.js.map +1 -1
- package/build/schemas/index.d.ts +222 -266
- package/build/schemas/index.js +129 -174
- package/build/schemas/index.js.map +1 -1
- package/build/tools/read/getPoolFeesPerLiquidity.d.ts +2 -13
- package/build/tools/read/getPoolFeesPerLiquidity.js +8 -3
- package/build/tools/read/getPoolFeesPerLiquidity.js.map +1 -1
- package/build/tools/read/getPoolInfo.d.ts +2 -21
- package/build/tools/read/getPoolInfo.js +7 -4
- package/build/tools/read/getPoolInfo.js.map +1 -1
- package/build/tools/read/getPoolLiquidity.d.ts +2 -12
- package/build/tools/read/getPoolLiquidity.js +8 -3
- package/build/tools/read/getPoolLiquidity.js.map +1 -1
- package/build/tools/read/getPosition.d.ts +25 -0
- package/build/tools/read/getPosition.js +64 -0
- package/build/tools/read/getPosition.js.map +1 -0
- package/build/tools/read/getTokenPrice.d.ts +2 -15
- package/build/tools/read/getTokenPrice.js +12 -6
- package/build/tools/read/getTokenPrice.js.map +1 -1
- package/build/tools/write/addLiquidity.d.ts +2 -20
- package/build/tools/write/addLiquidity.js +53 -25
- package/build/tools/write/addLiquidity.js.map +1 -1
- package/build/tools/write/createPosition.d.ts +2 -20
- package/build/tools/write/createPosition.js +50 -20
- package/build/tools/write/createPosition.js.map +1 -1
- package/build/tools/write/swap.d.ts +2 -18
- package/build/tools/write/swap.js +32 -20
- package/build/tools/write/swap.js.map +1 -1
- package/build/tools/write/transferPosition.d.ts +2 -15
- package/build/tools/write/transferPosition.js +5 -3
- package/build/tools/write/transferPosition.js.map +1 -1
- package/build/tools/write/withdrawLiquidity.d.ts +2 -18
- package/build/tools/write/withdrawLiquidity.js +29 -9
- package/build/tools/write/withdrawLiquidity.js.map +1 -1
- package/package.json +5 -5
- package/build/interfaces/index.d.ts +0 -21
- package/build/interfaces/index.js +0 -2
- package/build/interfaces/index.js.map +0 -1
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { getContract } from '../../lib/utils/contracts.js';
|
|
2
|
+
import { preparePoolKeyFromParams } from '../../lib/utils/pools.js';
|
|
3
|
+
import { buildBounds } from '../../lib/utils/liquidity.js';
|
|
4
|
+
import { convertTickSpacingExponentToPercent, convertFeeU128ToPercent, } from '../../lib/utils/math.js';
|
|
5
|
+
import { fetchPositionData, fetchPositionOwner, } from '../../lib/utils/position.js';
|
|
6
|
+
export const getPosition = async (env, params) => {
|
|
7
|
+
const provider = env.provider;
|
|
8
|
+
try {
|
|
9
|
+
const positionsContract = await getContract(provider, 'positions');
|
|
10
|
+
const positionData = await fetchPositionData(params.position_id);
|
|
11
|
+
const ownerAddress = await fetchPositionOwner(params.position_id);
|
|
12
|
+
const token0_address = positionData.token0;
|
|
13
|
+
const token1_address = positionData.token1;
|
|
14
|
+
const fee = convertFeeU128ToPercent(positionData.fee);
|
|
15
|
+
const tick_spacing = convertTickSpacingExponentToPercent(Number(positionData.tick_spacing));
|
|
16
|
+
const extension = positionData.extension || '0x0';
|
|
17
|
+
const { poolKey, token0, token1 } = await preparePoolKeyFromParams(provider, {
|
|
18
|
+
token0_address,
|
|
19
|
+
token1_address,
|
|
20
|
+
fee,
|
|
21
|
+
tick_spacing,
|
|
22
|
+
extension,
|
|
23
|
+
});
|
|
24
|
+
const lowerTick = Number(positionData.tick_lower);
|
|
25
|
+
const upperTick = Number(positionData.tick_upper);
|
|
26
|
+
if (isNaN(lowerTick) || isNaN(upperTick)) {
|
|
27
|
+
throw new Error(`Invalid tick values: lower=${positionData.tick_lower}, upper=${positionData.tick_upper}`);
|
|
28
|
+
}
|
|
29
|
+
if (lowerTick >= upperTick) {
|
|
30
|
+
throw new Error(`Lower tick (${lowerTick}) must be less than upper tick (${upperTick})`);
|
|
31
|
+
}
|
|
32
|
+
const bounds = buildBounds(lowerTick, upperTick);
|
|
33
|
+
const positionResult = await positionsContract.get_token_info(params.position_id, poolKey, bounds);
|
|
34
|
+
return {
|
|
35
|
+
status: 'success',
|
|
36
|
+
data: {
|
|
37
|
+
position_id: params.position_id,
|
|
38
|
+
owner_address: ownerAddress,
|
|
39
|
+
liquidity: positionResult.liquidity.toString(),
|
|
40
|
+
amount0: positionResult.amount0.toString(),
|
|
41
|
+
amount1: positionResult.amount1.toString(),
|
|
42
|
+
fees0: positionResult.fees0.toString(),
|
|
43
|
+
fees1: positionResult.fees1.toString(),
|
|
44
|
+
token0: token0.symbol || token0.address,
|
|
45
|
+
token1: token1.symbol || token1.address,
|
|
46
|
+
lower_tick: lowerTick,
|
|
47
|
+
upper_tick: upperTick,
|
|
48
|
+
pool_fee: fee,
|
|
49
|
+
tick_spacing: tick_spacing,
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
console.error('Error getting position:', error);
|
|
55
|
+
const errorMessage = error instanceof Error
|
|
56
|
+
? error.message
|
|
57
|
+
: 'Unknown error while getting position';
|
|
58
|
+
return {
|
|
59
|
+
status: 'failure',
|
|
60
|
+
error: errorMessage,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=getPosition.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getPosition.js","sourceRoot":"","sources":["../../../src/tools/read/getPosition.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EACL,mCAAmC,EACnC,uBAAuB,GACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,6BAA6B,CAAC;AAErC,MAAM,CAAC,MAAM,WAAW,GAAG,KAAK,EAC9B,GAAgB,EAChB,MAAyB,EACzB,EAAE;IACF,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;IAC9B,IAAI,CAAC;QACH,MAAM,iBAAiB,GAAG,MAAM,WAAW,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;QAGnE,MAAM,YAAY,GAAG,MAAM,iBAAiB,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;QAGjE,MAAM,YAAY,GAAG,MAAM,kBAAkB,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;QAGlE,MAAM,cAAc,GAAG,YAAY,CAAC,MAAM,CAAC;QAC3C,MAAM,cAAc,GAAG,YAAY,CAAC,MAAM,CAAC;QAC3C,MAAM,GAAG,GAAG,uBAAuB,CAAC,YAAY,CAAC,GAAG,CAAC,CAAC;QACtD,MAAM,YAAY,GAAG,mCAAmC,CACtD,M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AQ,EAAE;gBACtC,KAAK,EAAE,cAAc,CAAC,KAAK,CAAC,QAAQ,EAAE;gBACtC,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,OAAO;gBACvC,MAAM,EAAE,MAAM,CAAC,MAAM,IAAI,MAAM,CAAC,OAAO;gBACvC,UAAU,EAAE,SAAS;gBACrB,UAAU,EAAE,SAAS;gBACrB,QAAQ,EAAE,GAAG;gBACb,YAAY,EAAE,YAAY;aAC3B;SACF,CAAC;IACJ,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,OAAO,CAAC,KAAK,CAAC,yBAAyB,EAAE,KAAK,CAAC,CAAC;QAChD,MAAM,YAAY,GAChB,KAAK,YAAY,KAAK;YACpB,CAAC,CAAC,KAAK,CAAC,OAAO;YACf,CAAC,CAAC,sCAAsC,CAAC;QAC7C,OAAO;YACL,MAAM,EAAE,SAAS;YACjB,KAAK,EAAE,YAAY;SACpB,CAAC;IACJ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,16 +1,3 @@
|
|
|
1
|
-
import { onchainRead } from '@kasarlabs/ask-starknet-core';
|
|
1
|
+
import { onchainRead, toolResult } from '@kasarlabs/ask-starknet-core';
|
|
2
2
|
import { GetTokenPriceSchema } from '../../schemas/index.js';
|
|
3
|
-
export declare const getTokenPrice: (env: onchainRead, params: GetTokenPriceSchema) => Promise<
|
|
4
|
-
status: string;
|
|
5
|
-
data: {
|
|
6
|
-
base_token: string;
|
|
7
|
-
quote_token: string;
|
|
8
|
-
price: number;
|
|
9
|
-
sqrt_price: any;
|
|
10
|
-
};
|
|
11
|
-
error?: undefined;
|
|
12
|
-
} | {
|
|
13
|
-
status: string;
|
|
14
|
-
error: any;
|
|
15
|
-
data?: undefined;
|
|
16
|
-
}>;
|
|
3
|
+
export declare const getTokenPrice: (env: onchainRead, params: GetTokenPriceSchema) => Promise<toolResult>;
|
|
@@ -6,21 +6,25 @@ export const getTokenPrice = async (env, params) => {
|
|
|
6
6
|
try {
|
|
7
7
|
const contract = await getContract(provider, 'core');
|
|
8
8
|
const { poolKey, token0, token1, isTokenALower } = await preparePoolKeyFromParams(env.provider, {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
token0_symbol: params.token_symbol,
|
|
10
|
+
token0_address: params.token_address,
|
|
11
|
+
token1_symbol: params.quote_currency_symbol,
|
|
12
|
+
token1_address: params.quote_currency_address,
|
|
11
13
|
fee: params.fee,
|
|
12
14
|
tick_spacing: params.tick_spacing,
|
|
13
15
|
extension: params.extension,
|
|
14
16
|
});
|
|
15
17
|
const priceResult = await contract.get_pool_price(poolKey);
|
|
16
18
|
const sqrtPrice = priceResult.sqrt_ratio;
|
|
17
|
-
const price = calculateActualPrice(sqrtPrice, token0.decimals, token1.decimals);
|
|
19
|
+
const price = calculateActualPrice(sqrtPrice, isTokenALower ? token0.decimals : token1.decimals, isTokenALower ? token1.decimals : token0.decimals);
|
|
18
20
|
const finalPrice = isTokenALower ? price : 1 / price;
|
|
19
21
|
return {
|
|
20
22
|
status: 'success',
|
|
21
23
|
data: {
|
|
22
|
-
base_token: params.
|
|
23
|
-
quote_token: params.
|
|
24
|
+
base_token: params.token_symbol || params.token_address || 'unknown',
|
|
25
|
+
quote_token: params.quote_currency_symbol ||
|
|
26
|
+
params.quote_currency_address ||
|
|
27
|
+
'unknown',
|
|
24
28
|
price: finalPrice,
|
|
25
29
|
sqrt_price: sqrtPrice.toString(),
|
|
26
30
|
},
|
|
@@ -28,7 +32,9 @@ export const getTokenPrice = async (env, params) => {
|
|
|
28
32
|
}
|
|
29
33
|
catch (error) {
|
|
30
34
|
console.error('Error getting token price:', error);
|
|
31
|
-
const errorMessage = error
|
|
35
|
+
const errorMessage = error instanceof Error
|
|
36
|
+
? error.message
|
|
37
|
+
: 'Unknown error while getting token price';
|
|
32
38
|
const suggestion = errorMessage.includes('Pool not found') ||
|
|
33
39
|
errorMessage.includes('does not exist')
|
|
34
40
|
? `${errorMessage}. Try specifying different fee/tick_spacing values. Common pairs: fee=0.05 tick_spacing=0.1, fee=0.3 tick_spacing=0.6, or fee=1 tick_spacing=2.`
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getTokenPrice.js","sourceRoot":"","sources":["../../../src/tools/read/getTokenPrice.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAG/D,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AAEpE,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAChC,GAAgB,EAChB,MAA2B,
|
|
1
|
+
{"version":3,"file":"getTokenPrice.js","sourceRoot":"","sources":["../../../src/tools/read/getTokenPrice.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAG/D,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AAEpE,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,EAChC,GAAgB,EAChB,MAA2B,EACN,EAAE;IACvB,MAAM,QAAQ,GAAG,GAAG,CAAC,QAAQ,CAAC;IAC9B,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,QAAQ,EAAE,MAAM,CAAC,CAAC;QAErD,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,GAC9C,MAAM,wBAAwB,CAAC,GAAG,CAAC,QAAQ,EAAE;YAC3C,aAAa,EAAE,MAAM,CAAC,YAAY;YAClC,cAAc,EAAE,MAAM,CAAC,aAAa;YACpC,aAAa,EAAE,MAAM,CAAC,qBAAqB;YAC3C,cAAc,EAAE,MAAM,CAAC,sBAAsB;YAC7C,GAAG,EAAE,MAAM,CAAC,GAAG;YACf,YAAY,EAAE,MAAM,CAAC,YAAY;YACj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K,EAAE,UAAU;SAClB,CAAC;IACJ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,21 +1,3 @@
|
|
|
1
1
|
import { AddLiquiditySchema } from '../../schemas/index.js';
|
|
2
|
-
import { onchainWrite } from '@kasarlabs/ask-starknet-core';
|
|
3
|
-
export declare const addLiquidity: (env: onchainWrite, params: AddLiquiditySchema) => Promise<
|
|
4
|
-
status: string;
|
|
5
|
-
data: {
|
|
6
|
-
transaction_hash: string;
|
|
7
|
-
position_id: number;
|
|
8
|
-
token0: string;
|
|
9
|
-
token1: string;
|
|
10
|
-
amount0: string;
|
|
11
|
-
amount1: string;
|
|
12
|
-
lower_tick: number;
|
|
13
|
-
upper_tick: number;
|
|
14
|
-
pool_fee: number;
|
|
15
|
-
};
|
|
16
|
-
error?: undefined;
|
|
17
|
-
} | {
|
|
18
|
-
status: string;
|
|
19
|
-
error: any;
|
|
20
|
-
data?: undefined;
|
|
21
|
-
}>;
|
|
2
|
+
import { onchainWrite, toolResult } from '@kasarlabs/ask-starknet-core';
|
|
3
|
+
export declare const addLiquidity: (env: onchainWrite, params: AddLiquiditySchema) => Promise<toolResult>;
|
|
@@ -1,37 +1,62 @@
|
|
|
1
|
-
import { getERC20Contract } from '../../lib/utils/contracts.js';
|
|
2
|
-
import { getContract } from '../../lib/utils/contracts.js';
|
|
1
|
+
import { getERC20Contract, getContract } from '../../lib/utils/contracts.js';
|
|
3
2
|
import { preparePoolKeyFromParams } from '../../lib/utils/pools.js';
|
|
4
3
|
import { buildBounds } from '../../lib/utils/liquidity.js';
|
|
4
|
+
import { convertTickSpacingExponentToPercent, convertFeeU128ToPercent, } from '../../lib/utils/math.js';
|
|
5
|
+
import { fetchPositionData } from '../../lib/utils/position.js';
|
|
6
|
+
import { formatTokenAmount } from '../../lib/utils/token.js';
|
|
5
7
|
export const addLiquidity = async (env, params) => {
|
|
6
|
-
return {
|
|
7
|
-
status: 'failure',
|
|
8
|
-
error: 'This tool is currently under maintenance. ',
|
|
9
|
-
};
|
|
10
8
|
try {
|
|
11
9
|
const account = env.account;
|
|
12
10
|
const positionsContract = await getContract(env.provider, 'positions');
|
|
11
|
+
const positionData = await fetchPositionData(params.position_id);
|
|
12
|
+
const token0_address = positionData.token0;
|
|
13
|
+
const token1_address = positionData.token1;
|
|
14
|
+
const fee = convertFeeU128ToPercent(positionData.fee);
|
|
15
|
+
const tick_spacing = convertTickSpacingExponentToPercent(Number(positionData.tick_spacing));
|
|
16
|
+
const extension = positionData.extension || '0x0';
|
|
13
17
|
const { poolKey, token0, token1, isTokenALower } = await preparePoolKeyFromParams(env.provider, {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
fee
|
|
17
|
-
tick_spacing
|
|
18
|
-
extension
|
|
18
|
+
token0_address,
|
|
19
|
+
token1_address,
|
|
20
|
+
fee,
|
|
21
|
+
tick_spacing,
|
|
22
|
+
extension,
|
|
19
23
|
});
|
|
20
|
-
const
|
|
24
|
+
const lowerTick = Number(positionData.tick_lower);
|
|
25
|
+
const upperTick = Number(positionData.tick_upper);
|
|
26
|
+
if (isNaN(lowerTick) || isNaN(upperTick)) {
|
|
27
|
+
throw new Error(`Invalid tick values: lower=${positionData.tick_lower}, upper=${positionData.tick_upper}`);
|
|
28
|
+
}
|
|
29
|
+
if (lowerTick >= upperTick) {
|
|
30
|
+
throw new Error(`Lower tick (${lowerTick}) must be less than upper tick (${upperTick})`);
|
|
31
|
+
}
|
|
32
|
+
const formatAmount0 = formatTokenAmount(params.amount0, token0.decimals);
|
|
33
|
+
const formatAmount1 = formatTokenAmount(params.amount1, token1.decimals);
|
|
34
|
+
const config = isTokenALower
|
|
35
|
+
? {
|
|
36
|
+
amount0: formatAmount0,
|
|
37
|
+
amount1: formatAmount1,
|
|
38
|
+
transferToken0: token0,
|
|
39
|
+
transferToken1: token1,
|
|
40
|
+
}
|
|
41
|
+
: {
|
|
42
|
+
amount0: formatAmount1,
|
|
43
|
+
amount1: formatAmount0,
|
|
44
|
+
transferToken0: token1,
|
|
45
|
+
transferToken1: token0,
|
|
46
|
+
};
|
|
47
|
+
const { amount0, amount1, transferToken0, transferToken1 } = config;
|
|
48
|
+
const bounds = buildBounds(lowerTick, upperTick);
|
|
21
49
|
const minLiquidity = 0;
|
|
22
|
-
const token0Contract = getERC20Contract(
|
|
23
|
-
token0Contract.connect(account);
|
|
50
|
+
const token0Contract = getERC20Contract(env.account, transferToken0.address);
|
|
24
51
|
const transfer0Calldata = token0Contract.populate('transfer', [
|
|
25
52
|
positionsContract.address,
|
|
26
|
-
|
|
53
|
+
amount0,
|
|
27
54
|
]);
|
|
28
|
-
const token1Contract = getERC20Contract(
|
|
29
|
-
token1Contract.connect(account);
|
|
55
|
+
const token1Contract = getERC20Contract(env.account, transferToken1.address);
|
|
30
56
|
const transfer1Calldata = token1Contract.populate('transfer', [
|
|
31
57
|
positionsContract.address,
|
|
32
|
-
|
|
58
|
+
amount1,
|
|
33
59
|
]);
|
|
34
|
-
positionsContract.connect(account);
|
|
35
60
|
const depositCalldata = positionsContract.populate('deposit', [
|
|
36
61
|
params.position_id,
|
|
37
62
|
poolKey,
|
|
@@ -39,10 +64,10 @@ export const addLiquidity = async (env, params) => {
|
|
|
39
64
|
minLiquidity,
|
|
40
65
|
]);
|
|
41
66
|
const clearToken0Calldata = positionsContract.populate('clear', [
|
|
42
|
-
{ contract_address:
|
|
67
|
+
{ contract_address: transferToken0.address },
|
|
43
68
|
]);
|
|
44
69
|
const clearToken1Calldata = positionsContract.populate('clear', [
|
|
45
|
-
{ contract_address:
|
|
70
|
+
{ contract_address: transferToken1.address },
|
|
46
71
|
]);
|
|
47
72
|
const { transaction_hash } = await account.execute([
|
|
48
73
|
transfer0Calldata,
|
|
@@ -64,16 +89,19 @@ export const addLiquidity = async (env, params) => {
|
|
|
64
89
|
token1: token1.symbol,
|
|
65
90
|
amount0: params.amount0,
|
|
66
91
|
amount1: params.amount1,
|
|
67
|
-
lower_tick:
|
|
68
|
-
upper_tick:
|
|
69
|
-
pool_fee:
|
|
92
|
+
lower_tick: lowerTick,
|
|
93
|
+
upper_tick: upperTick,
|
|
94
|
+
pool_fee: fee,
|
|
70
95
|
},
|
|
71
96
|
};
|
|
72
97
|
}
|
|
73
98
|
catch (error) {
|
|
99
|
+
const errorMessage = error instanceof Error
|
|
100
|
+
? error.message
|
|
101
|
+
: 'Unknown error while adding liquidity';
|
|
74
102
|
return {
|
|
75
103
|
status: 'failure',
|
|
76
|
-
error:
|
|
104
|
+
error: errorMessage,
|
|
77
105
|
};
|
|
78
106
|
}
|
|
79
107
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"addLiquidity.js","sourceRoot":"","sources":["../../../src/tools/write/addLiquidity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"addLiquidity.js","sourceRoot":"","sources":["../../../src/tools/write/addLiquidity.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAE7E,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAC3D,OAAO,EACL,mCAAmC,EACnC,uBAAuB,GACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,6BAA6B,CAAC;AAChE,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAG7D,MAAM,CAAC,MAAM,YAAY,GAAG,KAAK,EAC/B,GAAiB,EACjB,MAA0B,EACL,EAAE;IACvB,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC;QAC5B,MAAM,iBAAiB,GAAG,MAAM,WAAW,CAAC,GAAG,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;QAGvE,MAAM,YAAY,GAAG,MAAM,iBAAiB,CAAC,MAAM,CAAC,WAAW,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;QAEH,MAAM,EAAE,gBAAgB,EAAE,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC;YACjD,iBAAiB;YACjB,iBAAiB;YACjB,eAAe;YACf,mBAAmB;YACnB,mBAAmB;SACpB,CAAC,CAAC;QAEH,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC;QACnE,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;QACtD,CAAC;QAED,OAAO;YACL,MAAM,EAAE,SAAS;YACjB,IAAI,EAAE;gBACJ,gBAAgB;gBAChB,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,UAAU,EAAE,SAAS;gBACrB,UAAU,EAAE,SAAS;gBACrB,QAAQ,EAAE,GAAG;aACd;SACF,CAAC;IACJ,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,MAAM,YAAY,GAChB,KAAK,YAAY,KAAK;YACpB,CAAC,CAAC,KAAK,CAAC,OAAO;YACf,CAAC,CAAC,sCAAsC,CAAC;QAC7C,OAAO;YACL,MAAM,EAAE,SAAS;YACjB,KAAK,EAAE,YAAY;SACpB,CAAC;IACJ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,21 +1,3 @@
|
|
|
1
1
|
import { CreatePositionSchema } from '../../schemas/index.js';
|
|
2
|
-
import { onchainWrite } from '@kasarlabs/ask-starknet-core';
|
|
3
|
-
export declare const createPosition: (env: onchainWrite, params: CreatePositionSchema) => Promise<
|
|
4
|
-
status: string;
|
|
5
|
-
data: {
|
|
6
|
-
transaction_hash: string;
|
|
7
|
-
position_id: string | undefined;
|
|
8
|
-
token0: string;
|
|
9
|
-
token1: string;
|
|
10
|
-
amount0: string;
|
|
11
|
-
amount1: string;
|
|
12
|
-
lower_tick: number;
|
|
13
|
-
upper_tick: number;
|
|
14
|
-
pool_fee: number;
|
|
15
|
-
};
|
|
16
|
-
error?: undefined;
|
|
17
|
-
} | {
|
|
18
|
-
status: string;
|
|
19
|
-
error: any;
|
|
20
|
-
data?: undefined;
|
|
21
|
-
}>;
|
|
2
|
+
import { onchainWrite, toolResult } from '@kasarlabs/ask-starknet-core';
|
|
3
|
+
export declare const createPosition: (env: onchainWrite, params: CreatePositionSchema) => Promise<toolResult>;
|
|
@@ -1,38 +1,63 @@
|
|
|
1
|
-
import { getChain, getEkuboAddress, getERC20Contract, } from '../../lib/utils/contracts.js';
|
|
2
|
-
import { getContract } from '../../lib/utils/contracts.js';
|
|
1
|
+
import { getChain, getEkuboAddress, getERC20Contract, getContract, } from '../../lib/utils/contracts.js';
|
|
3
2
|
import { preparePoolKeyFromParams } from '../../lib/utils/pools.js';
|
|
4
3
|
import { buildBounds } from '../../lib/utils/liquidity.js';
|
|
4
|
+
import { calculateTickFromPrice, roundTickToSpacing, } from '../../lib/utils/math.js';
|
|
5
5
|
import { extractPositionIdFromReceipt } from '../../lib/utils/events.js';
|
|
6
|
+
import { formatTokenAmount } from '../../lib/utils/token.js';
|
|
6
7
|
export const createPosition = async (env, params) => {
|
|
7
|
-
return {
|
|
8
|
-
status: 'failure',
|
|
9
|
-
error: 'This tool is currently under maintenance.',
|
|
10
|
-
};
|
|
11
8
|
try {
|
|
12
9
|
const account = env.account;
|
|
13
|
-
const positionsContract = await getContract(env.provider, 'positions');
|
|
10
|
+
const positionsContract = await getContract(env.provider, 'positions', env.account);
|
|
14
11
|
const { poolKey, token0, token1, isTokenALower } = await preparePoolKeyFromParams(env.provider, {
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
token0_symbol: params.token0_symbol,
|
|
13
|
+
token0_address: params.token0_address,
|
|
14
|
+
token1_symbol: params.token1_symbol,
|
|
15
|
+
token1_address: params.token1_address,
|
|
17
16
|
fee: params.fee,
|
|
18
17
|
tick_spacing: params.tick_spacing,
|
|
19
18
|
extension: params.extension,
|
|
20
19
|
});
|
|
21
|
-
const
|
|
20
|
+
const formatAmount0 = formatTokenAmount(params.amount0, token0.decimals);
|
|
21
|
+
const formatAmount1 = formatTokenAmount(params.amount1, token1.decimals);
|
|
22
|
+
const config = isTokenALower
|
|
23
|
+
? {
|
|
24
|
+
lowerPrice: params.lower_price,
|
|
25
|
+
upperPrice: params.upper_price,
|
|
26
|
+
decimals0: token0.decimals,
|
|
27
|
+
decimals1: token1.decimals,
|
|
28
|
+
amount0: formatAmount0,
|
|
29
|
+
amount1: formatAmount1,
|
|
30
|
+
transferToken0: token0,
|
|
31
|
+
transferToken1: token1,
|
|
32
|
+
}
|
|
33
|
+
: {
|
|
34
|
+
lowerPrice: 1 / params.upper_price,
|
|
35
|
+
upperPrice: 1 / params.lower_price,
|
|
36
|
+
decimals0: token1.decimals,
|
|
37
|
+
decimals1: token0.decimals,
|
|
38
|
+
amount0: formatAmount1,
|
|
39
|
+
amount1: formatAmount0,
|
|
40
|
+
transferToken0: token1,
|
|
41
|
+
transferToken1: token0,
|
|
42
|
+
};
|
|
43
|
+
const { lowerPrice, upperPrice, decimals0, decimals1, amount0, amount1, transferToken0, transferToken1, } = config;
|
|
44
|
+
const rawLowerTick = calculateTickFromPrice(lowerPrice, decimals0, decimals1);
|
|
45
|
+
const rawUpperTick = calculateTickFromPrice(upperPrice, decimals0, decimals1);
|
|
46
|
+
const tickSpacingExponent = poolKey.tick_spacing;
|
|
47
|
+
const lowerTick = roundTickToSpacing(rawLowerTick, tickSpacingExponent, true);
|
|
48
|
+
const upperTick = roundTickToSpacing(rawUpperTick, tickSpacingExponent, false);
|
|
49
|
+
const bounds = buildBounds(lowerTick, upperTick);
|
|
22
50
|
const minLiquidity = 0;
|
|
23
|
-
const token0Contract = getERC20Contract(
|
|
24
|
-
token0Contract.connect(account);
|
|
51
|
+
const token0Contract = getERC20Contract(env.account, transferToken0.address);
|
|
25
52
|
const transfer0Calldata = token0Contract.populate('transfer', [
|
|
26
53
|
positionsContract.address,
|
|
27
|
-
|
|
54
|
+
amount0,
|
|
28
55
|
]);
|
|
29
|
-
const token1Contract = getERC20Contract(
|
|
30
|
-
token1Contract.connect(account);
|
|
56
|
+
const token1Contract = getERC20Contract(env.account, transferToken1.address);
|
|
31
57
|
const transfer1Calldata = token1Contract.populate('transfer', [
|
|
32
58
|
positionsContract.address,
|
|
33
|
-
|
|
59
|
+
amount1,
|
|
34
60
|
]);
|
|
35
|
-
positionsContract.connect(account);
|
|
36
61
|
const mintCalldata = positionsContract.populate('mint_and_deposit_and_clear_both', [poolKey, bounds, minLiquidity]);
|
|
37
62
|
const executeResult = await account.execute([
|
|
38
63
|
transfer0Calldata,
|
|
@@ -54,16 +79,21 @@ export const createPosition = async (env, params) => {
|
|
|
54
79
|
token1: token1.symbol,
|
|
55
80
|
amount0: params.amount0,
|
|
56
81
|
amount1: params.amount1,
|
|
57
|
-
|
|
58
|
-
|
|
82
|
+
lower_price: params.lower_price,
|
|
83
|
+
upper_price: params.upper_price,
|
|
84
|
+
lower_tick: lowerTick,
|
|
85
|
+
upper_tick: upperTick,
|
|
59
86
|
pool_fee: params.fee,
|
|
60
87
|
},
|
|
61
88
|
};
|
|
62
89
|
}
|
|
63
90
|
catch (error) {
|
|
91
|
+
const errorMessage = error instanceof Error
|
|
92
|
+
? error.message
|
|
93
|
+
: 'Unknown error while adding liquidity';
|
|
64
94
|
return {
|
|
65
95
|
status: 'failure',
|
|
66
|
-
error:
|
|
96
|
+
error: errorMessage,
|
|
67
97
|
};
|
|
68
98
|
}
|
|
69
99
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createPosition.js","sourceRoot":"","sources":["../../../src/tools/write/createPosition.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,QAAQ,EACR,eAAe,EACf,gBAAgB,
|
|
1
|
+
{"version":3,"file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C,MAAM;gBACrB,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,OAAO,EAAE,MAAM,CAAC,OAAO;gBACvB,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,WAAW,EAAE,MAAM,CAAC,WAAW;gBAC/B,UAAU,EAAE,SAAS;gBACrB,UAAU,EAAE,SAAS;gBACrB,QAAQ,EAAE,MAAM,CAAC,GAAG;aACrB;SACF,CAAC;IACJ,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,MAAM,YAAY,GAChB,KAAK,YAAY,KAAK;YACpB,CAAC,CAAC,KAAK,CAAC,OAAO;YACf,CAAC,CAAC,sCAAsC,CAAC;QAC7C,OAAO;YACL,MAAM,EAAE,SAAS;YACjB,KAAK,EAAE,YAAY;SACpB,CAAC;IACJ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,19 +1,3 @@
|
|
|
1
1
|
import { SwapTokensSchema } from '../../schemas/index.js';
|
|
2
|
-
import { onchainWrite } from '@kasarlabs/ask-starknet-core';
|
|
3
|
-
export declare const swap: (env: onchainWrite, params: SwapTokensSchema) => Promise<
|
|
4
|
-
status: string;
|
|
5
|
-
data: {
|
|
6
|
-
transaction_hash: string;
|
|
7
|
-
token_in: string;
|
|
8
|
-
token_out: string;
|
|
9
|
-
amount: string;
|
|
10
|
-
is_amount_in: boolean;
|
|
11
|
-
pool_fee: number;
|
|
12
|
-
slippage_tolerance: number;
|
|
13
|
-
};
|
|
14
|
-
error?: undefined;
|
|
15
|
-
} | {
|
|
16
|
-
status: string;
|
|
17
|
-
error: any;
|
|
18
|
-
data?: undefined;
|
|
19
|
-
}>;
|
|
2
|
+
import { onchainWrite, toolResult } from '@kasarlabs/ask-starknet-core';
|
|
3
|
+
export declare const swap: (env: onchainWrite, params: SwapTokensSchema) => Promise<toolResult>;
|
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import { getERC20Contract } from '../../lib/utils/contracts.js';
|
|
2
|
-
import { getContract } from '../../lib/utils/contracts.js';
|
|
1
|
+
import { getERC20Contract, getContract } from '../../lib/utils/contracts.js';
|
|
3
2
|
import { preparePoolKeyFromParams } from '../../lib/utils/pools.js';
|
|
4
3
|
import { buildRouteNode, buildTokenAmount, calculateSqrtRatioLimit, } from '../../lib/utils/swap.js';
|
|
5
|
-
import { getSwapQuote, extractExpectedOutput, calculateMinimumOutputU256, } from '../../lib/utils/quote.js';
|
|
4
|
+
import { getSwapQuote, extractExpectedOutput, calculateMinimumOutputU256, extractRequiredInput, createExactOutputMinimum, } from '../../lib/utils/quote.js';
|
|
5
|
+
import { formatTokenAmount } from '../../lib/utils/token.js';
|
|
6
6
|
export const swap = async (env, params) => {
|
|
7
|
-
return {
|
|
8
|
-
status: 'failure',
|
|
9
|
-
error: 'This tool is currently under maintenance. ',
|
|
10
|
-
};
|
|
11
7
|
try {
|
|
12
8
|
const account = env.account;
|
|
13
|
-
const routerContract = await getContract(env.provider, 'routerV3');
|
|
14
|
-
const coreContract = await getContract(env.provider, 'core');
|
|
9
|
+
const routerContract = await getContract(env.provider, 'routerV3', env.account);
|
|
10
|
+
const coreContract = await getContract(env.provider, 'core', env.account);
|
|
15
11
|
const { poolKey, token0, token1, isTokenALower } = await preparePoolKeyFromParams(env.provider, {
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
token0_symbol: params.token_in_symbol,
|
|
13
|
+
token0_address: params.token_in_address,
|
|
14
|
+
token1_symbol: params.token_out_symbol,
|
|
15
|
+
token1_address: params.token_out_address,
|
|
18
16
|
fee: params.fee,
|
|
19
17
|
tick_spacing: params.tick_spacing,
|
|
20
18
|
extension: params.extension,
|
|
@@ -23,19 +21,32 @@ export const swap = async (env, params) => {
|
|
|
23
21
|
const tokenOut = token1;
|
|
24
22
|
const priceResult = await coreContract.get_pool_price(poolKey);
|
|
25
23
|
const currentSqrtPrice = BigInt(priceResult.sqrt_ratio);
|
|
26
|
-
const
|
|
24
|
+
const isTokenInToken0 = tokenIn.address === poolKey.token0;
|
|
25
|
+
const isSellingToken0 = isTokenInToken0;
|
|
26
|
+
const sqrtRatioLimit = calculateSqrtRatioLimit(currentSqrtPrice, params.slippage_tolerance, isSellingToken0);
|
|
27
|
+
const tokenForAmount = params.is_amount_in ? tokenIn : tokenOut;
|
|
28
|
+
const formatAmountIn = formatTokenAmount(params.amount, tokenForAmount.decimals);
|
|
27
29
|
const routeNode = buildRouteNode(poolKey, sqrtRatioLimit);
|
|
28
|
-
const tokenAmount = buildTokenAmount(
|
|
30
|
+
const tokenAmount = buildTokenAmount(tokenForAmount.address, formatAmountIn, params.is_amount_in);
|
|
29
31
|
const quote = await getSwapQuote(routerContract, routeNode, tokenAmount);
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
let transferAmount;
|
|
33
|
+
let minimumOutput;
|
|
34
|
+
if (params.is_amount_in) {
|
|
35
|
+
const expectedOutput = extractExpectedOutput(quote, isTokenALower);
|
|
36
|
+
transferAmount = formatAmountIn;
|
|
37
|
+
minimumOutput = calculateMinimumOutputU256(expectedOutput, params.slippage_tolerance);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const requiredInput = extractRequiredInput(quote, isTokenInToken0);
|
|
41
|
+
transferAmount = requiredInput.toString();
|
|
42
|
+
const desiredOutput = BigInt(formatAmountIn);
|
|
43
|
+
minimumOutput = createExactOutputMinimum(desiredOutput);
|
|
44
|
+
}
|
|
45
|
+
const tokenInContract = getERC20Contract(env.account, tokenIn.address);
|
|
34
46
|
const transferCalldata = tokenInContract.populate('transfer', [
|
|
35
47
|
routerContract.address,
|
|
36
|
-
|
|
48
|
+
transferAmount,
|
|
37
49
|
]);
|
|
38
|
-
routerContract.connect(account);
|
|
39
50
|
const swapCalldata = routerContract.populate('swap', [
|
|
40
51
|
routeNode,
|
|
41
52
|
tokenAmount,
|
|
@@ -71,9 +82,10 @@ export const swap = async (env, params) => {
|
|
|
71
82
|
};
|
|
72
83
|
}
|
|
73
84
|
catch (error) {
|
|
85
|
+
const errorMessage = error instanceof Error ? error.message : 'Unknown error during swap';
|
|
74
86
|
return {
|
|
75
87
|
status: 'failure',
|
|
76
|
-
error:
|
|
88
|
+
error: errorMessage,
|
|
77
89
|
};
|
|
78
90
|
}
|
|
79
91
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"swap.js","sourceRoot":"","sources":["../../../src/tools/write/swap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,
|
|
1
|
+
{"version":3,"file":"swap.js","sourceRoot":"","sources":["../../../src/tools/write/swap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAE7E,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AACpE,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,uBAAuB,GACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,YAAY,EACZ,qBAAqB,EACrB,0BAA0B,EAC1B,oBAAoB,EACpB,wBAAwB,GACzB,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAO7D,MAAM,CAAC,MAAM,IAAI,GAAG,KAAK,EACvB,GAAiB,EACjB,MAAwB,EACH,EAAE;IACvB,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,GAAG,CAAC,OAAO,CAAC;QAC5B,MAAM,cAAc,GAAG,MAAM,WAAW,CACtC,GAAG,CAAC,QAAQ,EACZ,UAAU,EACV,GAAG,CAAC,OAAO,CACZ,CAAC;QACF,MAAM,YAAY,GAAG,MAAM,WAAW,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;QAE1E,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,EAAE,GAC9C,MAAM,wBAAwB,CAAC,GAAG,CAAC,QAAQ,EAAE;YAC3C,aAAa,EAAE,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AAC;QACnE,IAAI,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,kCAAkC,CAAC,CAAC;QACtD,CAAC;QAED,OAAO;YACL,MAAM,EAAE,SAAS;YACjB,IAAI,EAAE;gBACJ,gBAAgB,EAAE,gBAAgB;gBAClC,QAAQ,EAAE,OAAO,CAAC,MAAM;gBACxB,SAAS,EAAE,QAAQ,CAAC,MAAM;gBAC1B,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,YAAY,EAAE,MAAM,CAAC,YAAY;gBACjC,QAAQ,EAAE,MAAM,CAAC,GAAG;gBACpB,kBAAkB,EAAE,MAAM,CAAC,kBAAkB;aAC9C;SACF,CAAC;IACJ,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,MAAM,YAAY,GAChB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,2BAA2B,CAAC;QACvE,OAAO;YACL,MAAM,EAAE,SAAS;YACjB,KAAK,EAAE,YAAY;SACpB,CAAC;IACJ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -1,16 +1,3 @@
|
|
|
1
1
|
import { TransferPositionSchema } from '../../schemas/index.js';
|
|
2
|
-
import { onchainWrite } from '@kasarlabs/ask-starknet-core';
|
|
3
|
-
export declare const transferPosition: (env: onchainWrite, params: TransferPositionSchema) => Promise<
|
|
4
|
-
status: string;
|
|
5
|
-
data: {
|
|
6
|
-
transaction_hash: string;
|
|
7
|
-
position_id: number;
|
|
8
|
-
from: string;
|
|
9
|
-
to: string;
|
|
10
|
-
};
|
|
11
|
-
error?: undefined;
|
|
12
|
-
} | {
|
|
13
|
-
status: string;
|
|
14
|
-
error: any;
|
|
15
|
-
data?: undefined;
|
|
16
|
-
}>;
|
|
2
|
+
import { onchainWrite, toolResult } from '@kasarlabs/ask-starknet-core';
|
|
3
|
+
export declare const transferPosition: (env: onchainWrite, params: TransferPositionSchema) => Promise<toolResult>;
|
|
@@ -3,8 +3,7 @@ import { getContract } from '../../lib/utils/contracts.js';
|
|
|
3
3
|
export const transferPosition = async (env, params) => {
|
|
4
4
|
try {
|
|
5
5
|
const account = env.account;
|
|
6
|
-
const NFTContract = await getContract(env.provider, 'positionsNFT');
|
|
7
|
-
NFTContract.connect(account);
|
|
6
|
+
const NFTContract = await getContract(env.provider, 'positionsNFT', account);
|
|
8
7
|
const transferCalldata = NFTContract.populate('transfer_from', [
|
|
9
8
|
account.address,
|
|
10
9
|
params.to_address,
|
|
@@ -26,9 +25,12 @@ export const transferPosition = async (env, params) => {
|
|
|
26
25
|
};
|
|
27
26
|
}
|
|
28
27
|
catch (error) {
|
|
28
|
+
const errorMessage = error instanceof Error
|
|
29
|
+
? error.message
|
|
30
|
+
: 'Unknown error while transferring position';
|
|
29
31
|
return {
|
|
30
32
|
status: 'failure',
|
|
31
|
-
error:
|
|
33
|
+
error: errorMessage,
|
|
32
34
|
};
|
|
33
35
|
}
|
|
34
36
|
};
|