Skip to content

Commit 9ea7371

Browse files
authored
[hosted-sdk] add example to select aggregator (#18)
1 parent d802781 commit 9ea7371

14 files changed

Lines changed: 233 additions & 141 deletions

hosted-sdk-demo/src/add-liquidity-dual.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,39 @@ import { AddLiquidityDualData } from "./types";
44

55
export async function addLiquidityDualSyAndPt() {
66
// Use 1 SY and 1 PT to add liquidity to wstETH pool with 1% slippage
7-
const res = await callSDK<AddLiquidityDualData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity-dual`, {
7+
const resp = await callSDK<AddLiquidityDualData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity-dual`, {
88
receiver: RECEIVER_ADDRESS,
99
slippage: 0.01,
1010
tokenIn: SY_ADDRESS,
1111
amountTokenIn: '1000000000000000000',
1212
amountPtIn: '1000000000000000000',
1313
});
1414

15-
console.log('Amount LP Out: ', res.data.amountOut);
16-
console.log('Price impact: ', res.data.priceImpact);
15+
console.log('Amount LP Out: ', resp.data.data.amountOut);
16+
console.log('Price impact: ', resp.data.data.priceImpact);
17+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
1718

1819
// Send tx
19-
getSigner().sendTransaction(res.tx);
20+
// getSigner().sendTransaction(resp.data.tx);
2021
}
2122

2223
export async function addLiquidityDualTokenAndPt() {
2324
// USe 1 wstETH and 1 PT to add liquidity to wstETH pool with 1% slippage
24-
const res = await callSDK<AddLiquidityDualData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity-dual`, {
25+
const resp = await callSDK<AddLiquidityDualData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity-dual`, {
2526
receiver: RECEIVER_ADDRESS,
2627
slippage: 0.01,
2728
tokenIn: wstETH,
2829
amountTokenIn: '1000000000000000000',
2930
amountPtIn: '1000000000000000000',
3031
});
3132

32-
console.log('Amount LP Out: ', res.data.amountOut);
33-
console.log('Price impact: ', res.data.priceImpact);
33+
console.log('Amount LP Out: ', resp.data.data.amountOut);
34+
console.log('Price impact: ', resp.data.data.priceImpact);
35+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
3436

3537
// Send tx
36-
getSigner().sendTransaction(res.tx);
38+
// getSigner().sendTransaction(resp.data.tx);
3739
}
40+
41+
// addLiquidityDualSyAndPt();
42+
// addLiquidityDualTokenAndPt();
Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,99 @@
1-
import { CHAIN_ID, MARKET_ADDRESS, PT_ADDRESS, RECEIVER_ADDRESS, SY_ADDRESS, wstETH } from "./constants";
1+
import { CHAIN_ID, MARKET_ADDRESS, PT_ADDRESS, RECEIVER_ADDRESS, SY_ADDRESS, USDC, wstETH } from "./constants";
22
import { callSDK, getSigner } from "./helper";
33
import { AddLiquidityData } from "./types";
44

55
export async function addLiquiditySinglePt() {
66
// Use 1 PT to add liquidity to wstETH pool with 1% slippage
7-
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
7+
const resp = await callSDK<AddLiquidityData>(`/v2/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
88
receiver: RECEIVER_ADDRESS,
99
slippage: 0.01,
1010
tokenIn: PT_ADDRESS,
1111
amountIn: '1000000000000000000'
1212
});
1313

14-
console.log('Amount LP Out: ', res.data.amountLpOut);
15-
console.log('Price impact: ', res.data.priceImpact);
14+
console.log('Amount LP Out: ', resp.data.data.amountLpOut);
15+
console.log('Price impact: ', resp.data.data.priceImpact);
16+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
1617

1718
// Send tx
18-
getSigner().sendTransaction(res.tx);
19+
// getSigner().sendTransaction(resp.data.tx);
1920
}
2021

2122
export async function addLiquiditySingleSy() {
2223
// Use 1 SY to add liquidity to wstETH pool with 1% slippage
23-
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
24+
const resp = await callSDK<AddLiquidityData>(`/v2/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
2425
receiver: RECEIVER_ADDRESS,
2526
slippage: 0.01,
2627
tokenIn: SY_ADDRESS,
2728
amountIn: '1000000000000000000',
2829
});
2930

30-
console.log('Amount LP Out: ', res.data.amountLpOut);
31-
console.log('Price impact: ', res.data.priceImpact);
31+
console.log('Amount LP Out: ', resp.data.data.amountLpOut);
32+
console.log('Price impact: ', resp.data.data.priceImpact);
33+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
3234

3335
// Send tx
34-
getSigner().sendTransaction(res.tx);
36+
// getSigner().sendTransaction(resp.data.tx);
3537
}
3638

3739
export async function addLiquiditySingleSyKeepYt() {
3840
// Use 1 SY to add liquidity to wstETH pool (zero price impact mode) with 1% slippage
39-
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
41+
const resp = await callSDK<AddLiquidityData>(`/v2/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
4042
receiver: RECEIVER_ADDRESS,
4143
slippage: 0.01,
4244
tokenIn: SY_ADDRESS,
4345
amountIn: '1000000000000000000',
4446
zpi: true,
4547
});
4648

47-
console.log('Amount LP Out: ', res.data.amountLpOut);
48-
console.log('Amount YT Out: ', res.data.amountYtOut);
49-
console.log('Price impact: ', res.data.priceImpact);
49+
console.log('Amount LP Out: ', resp.data.data.amountLpOut);
50+
console.log('Amount YT Out: ', resp.data.data.amountYtOut);
51+
console.log('Price impact: ', resp.data.data.priceImpact);
52+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
5053

5154
// Send tx
52-
getSigner().sendTransaction(res.tx);
55+
// getSigner().sendTransaction(resp.data.tx);
5356
}
5457

5558
export async function addLiquiditySingleToken() {
5659
// Use 1 wstETH to add liquidity to wstETH pool with 1% slippage
57-
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
60+
const resp = await callSDK<AddLiquidityData>(`/v2/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
5861
receiver: RECEIVER_ADDRESS,
5962
slippage: 0.01,
6063
tokenIn: wstETH,
6164
amountIn: '1000000000000000000'
6265
});
6366

64-
console.log('Amount LP Out: ', res.data.amountLpOut);
65-
console.log('Price impact: ', res.data.priceImpact);
67+
console.log('Amount LP Out: ', resp.data.data.amountLpOut);
68+
console.log('Price impact: ', resp.data.data.priceImpact);
69+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
6670

6771
// Send tx
68-
getSigner().sendTransaction(res.tx);
72+
// getSigner().sendTransaction(resp.data.tx);
6973
}
7074

7175
export async function addLiquiditySingleTokenKeepYt() {
7276
// Use 1 wstETH to add liquidity to wstETH pool (zero price impact mode) with 1% slippage
73-
const res = await callSDK<AddLiquidityData>(`/v1/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
77+
const resp = await callSDK<AddLiquidityData>(`/v2/sdk/${CHAIN_ID}/markets/${MARKET_ADDRESS}/add-liquidity`, {
7478
receiver: RECEIVER_ADDRESS,
7579
slippage: 0.01,
7680
tokenIn: wstETH,
7781
amountIn: '1000000000000000000',
7882
zpi: true,
7983
});
8084

81-
console.log('Amount LP Out: ', res.data.amountLpOut);
82-
console.log('Amount YT Out: ', res.data.amountYtOut);
83-
console.log('Price impact: ', res.data.priceImpact);
85+
console.log('Amount LP Out: ', resp.data.data.amountLpOut);
86+
console.log('Amount YT Out: ', resp.data.data.amountYtOut);
87+
console.log('Price impact: ', resp.data.data.priceImpact);
88+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
8489

8590
// Send tx
86-
getSigner().sendTransaction(res.tx);
87-
}
91+
// getSigner().sendTransaction(resp.data.tx);
92+
}
93+
94+
95+
// addLiquiditySinglePt();
96+
// addLiquiditySingleSy();
97+
// addLiquiditySingleSyKeepYt();
98+
// addLiquiditySingleToken();
99+
// addLiquiditySingleTokenKeepYt();

hosted-sdk-demo/src/cancel-limit-order.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import { LimitOrderResponse } from "./types";
55

66
export async function cancelAll() {
77
// Cancel all limit orders on <CHAIN_ID> made by <USER_ADDRESS>
8-
const res = await callSDK(`v1/sdk/${CHAIN_ID}/limit-order/cancel-all`, {
8+
const resp = await callSDK(`v1/sdk/${CHAIN_ID}/limit-order/cancel-all`, {
99
userAddress: USER_ADDRESS,
1010
});
1111

1212
// Send tx
13-
getSigner().sendTransaction(res.tx);
13+
// getSigner().sendTransaction(resp.data.tx);
1414
}
1515

1616
export async function cancelSingle() {
@@ -20,7 +20,7 @@ export async function cancelSingle() {
2020
} = await axios.get<LimitOrderResponse>(LIMIT_ORDER_URL + `/v1/limit-order/${ORDER_ID}`, {});
2121

2222
// Cancel the order
23-
const res = await callSDK(`v1/sdk/${CHAIN_ID}/limit-order/cancel-single`, {
23+
const resp = await callSDK(`v1/sdk/${CHAIN_ID}/limit-order/cancel-single`, {
2424
userAddress: USER_ADDRESS,
2525
salt: orderData.salt,
2626
expiry: orderData.expiry,
@@ -37,5 +37,9 @@ export async function cancelSingle() {
3737
});
3838

3939
// Send tx
40-
getSigner().sendTransaction(res.tx);
41-
}
40+
// getSigner().sendTransaction(resp.data.tx);
41+
}
42+
43+
44+
// cancelAll();
45+
// cancelSingle();

hosted-sdk-demo/src/constants.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export const PT_ADDRESS = '0xb253eff1104802b97ac7e3ac9fdd73aece295a2c';
77
export const YT_ADDRESS = '0x04b7fa1e727d7290d6e24fa9b426d0c940283a95';
88

99
export const CHAIN_ID = 1;
10-
export const RECEIVER_ADDRESS = '<RECEVER_ADDRESS>';
10+
// replace with your own address
11+
export const RECEIVER_ADDRESS = '0x0000000000000000000000000000000000000001';
1112
export const ORDER_ID = '<ORDER_ID>'
1213
export const USER_ADDRESS = '<USER_ADDRESS>'

hosted-sdk-demo/src/helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ export async function callSDK<Data>(path: string, params: Record<string, any> =
1818
params
1919
});
2020

21-
return response.data;
21+
return response;
2222
}
2323

2424
export function getSigner() {
2525
const provider = new ethers.providers.JsonRpcProvider('https://rpc.ankr.com/eth');
2626
const signer = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
2727
return signer;
28-
}
28+
}

hosted-sdk-demo/src/mint-py.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,40 @@ import { MintPyData } from "./types";
44

55
export async function mintPyFromSy() {
66
// Use 1 SY to mint PT and YT with 1% slippage
7-
const res = await callSDK<MintPyData>(`/v1/sdk/${CHAIN_ID}/mint`, {
7+
const resp = await callSDK<MintPyData>(`/v2/sdk/${CHAIN_ID}/mint`, {
88
receiver: RECEIVER_ADDRESS,
99
yt: YT_ADDRESS,
1010
slippage: 0.01,
1111
tokenIn: SY_ADDRESS,
1212
amountIn: '1000000000000000000',
1313
});
1414

15-
console.log('Amount PT & YT Out: ', res.data.amountOut);
16-
console.log('Price impact: ', res.data.priceImpact);
15+
console.log('Amount PT & YT Out: ', resp.data.data.amountOut);
16+
console.log('Price impact: ', resp.data.data.priceImpact);
17+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
1718

1819
// Send tx
19-
getSigner().sendTransaction(res.tx);
20+
// getSigner().sendTransaction(resp.data.tx);
2021
}
2122

2223
export async function mintPyFromToken() {
2324
// Use 1 wstETH to mint PT and YT with 1% slippage
24-
const res = await callSDK<MintPyData>(`/v1/sdk/${CHAIN_ID}/mint`, {
25+
const resp = await callSDK<MintPyData>(`/v2/sdk/${CHAIN_ID}/mint`, {
2526
receiver: RECEIVER_ADDRESS,
2627
yt: YT_ADDRESS,
2728
slippage: 0.01,
2829
tokenIn: wstETH,
2930
amountIn: '1000000000000000000',
3031
});
3132

32-
console.log('Amount PT & YT Out: ', res.data.amountOut);
33-
console.log('Price impact: ', res.data.priceImpact);
33+
console.log('Amount PT & YT Out: ', resp.data.data.amountOut);
34+
console.log('Price impact: ', resp.data.data.priceImpact);
35+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
3436

3537
// Send tx
36-
getSigner().sendTransaction(res.tx);
38+
// getSigner().sendTransaction(resp.data.tx);
3739
}
40+
41+
42+
// mintPyFromSy();
43+
// mintPyFromToken();

hosted-sdk-demo/src/mint-sy.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ import { MintSyData } from "./types";
44

55
export async function mintSyFromToken() {
66
// Use 1 wstETH to mint SY with 1% slippage
7-
const res = await callSDK<MintSyData>(`/v1/sdk/${CHAIN_ID}/mint-sy`, {
7+
const resp = await callSDK<MintSyData>(`/v2/sdk/${CHAIN_ID}/mint-sy`, {
88
receiver: RECEIVER_ADDRESS,
99
sy: SY_ADDRESS,
1010
slippage: 0.01,
1111
tokenIn: wstETH,
1212
amountIn: '1000000000000000000',
1313
});
1414

15-
console.log('Amount SY Out: ', res.data.amountOut);
16-
console.log('Price impact: ', res.data.priceImpact);
15+
console.log('Amount SY Out: ', resp.data.data.amountOut);
16+
console.log('Price impact: ', resp.data.data.priceImpact);
17+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
1718

1819
// Send tx
19-
getSigner().sendTransaction(res.tx);
20-
}
20+
// getSigner().sendTransaction(resp.data.tx);
21+
}
22+
23+
mintSyFromToken();

hosted-sdk-demo/src/redeem-py.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,39 @@ import { RedeemPyData } from "./types";
44

55
export async function redeemPyToSy() {
66
// Redeem 1 PT and 1 YT to SY with 1% slippage
7-
const res = await callSDK<RedeemPyData>(`/v1/sdk/${CHAIN_ID}/redeem`, {
7+
const resp = await callSDK<RedeemPyData>(`/v2/sdk/${CHAIN_ID}/redeem`, {
88
receiver: RECEIVER_ADDRESS,
99
slippage: 0.01,
1010
yt: YT_ADDRESS,
1111
amountIn: '1000000000000000000',
1212
tokenOut: SY_ADDRESS,
1313
});
1414

15-
console.log('Amount SY Out: ', res.data.amountOut);
16-
console.log('Price impact: ', res.data.priceImpact);
15+
console.log('Amount SY Out: ', resp.data.data.amountOut);
16+
console.log('Price impact: ', resp.data.data.priceImpact);
17+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
1718

1819
// Send tx
19-
getSigner().sendTransaction(res.tx);
20+
// getSigner().sendTransaction(resp.data.tx);
2021
}
2122

2223
export async function redeemPyToToken() {
2324
// Redeem 1 PT and 1 YT to wstETH with 1% slippage
24-
const res = await callSDK<RedeemPyData>(`/v1/sdk/${CHAIN_ID}/redeem`, {
25+
const resp = await callSDK<RedeemPyData>(`/v2/sdk/${CHAIN_ID}/redeem`, {
2526
receiver: RECEIVER_ADDRESS,
2627
slippage: 0.01,
2728
yt: YT_ADDRESS,
2829
amountIn: '1000000000000000000',
2930
tokenOut: wstETH,
3031
});
3132

32-
console.log('Amount wstETH Out: ', res.data.amountOut);
33-
console.log('Price impact: ', res.data.priceImpact);
33+
console.log('Amount wstETH Out: ', resp.data.data.amountOut);
34+
console.log('Price impact: ', resp.data.data.priceImpact);
35+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
3436

3537
// Send tx
36-
getSigner().sendTransaction(res.tx);
38+
// getSigner().sendTransaction(resp.data.tx);
3739
}
40+
41+
// redeemPyToSy();
42+
// redeemPyToToken();

hosted-sdk-demo/src/redeem-sy.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ import { RedeemSyData } from "./types";
44

55
export async function redeemSyToToken() {
66
// Redeem 1 SY to wstETH with 1% slippage
7-
const res = await callSDK<RedeemSyData>(`/v1/sdk/${CHAIN_ID}/redeem-sy`, {
7+
const resp = await callSDK<RedeemSyData>(`/v2/sdk/${CHAIN_ID}/redeem-sy`, {
88
receiver: RECEIVER_ADDRESS,
99
slippage: 0.01,
1010
sy: SY_ADDRESS,
1111
amountIn: '1000000000000000000',
1212
tokenOut: wstETH,
1313
});
1414

15-
console.log('Amount wstETH Out: ', res.data.amountOut);
16-
console.log('Price impact: ', res.data.priceImpact);
15+
console.log('Amount wstETH Out: ', resp.data.data.amountOut);
16+
console.log('Price impact: ', resp.data.data.priceImpact);
17+
console.log('Computing unit: ', resp.headers['x-computing-unit']);
1718

1819
// Send tx
19-
getSigner().sendTransaction(res.tx);
20-
}
20+
// getSigner().sendTransaction(resp.data.tx);
21+
}
22+
23+
// redeemSyToToken();

0 commit comments

Comments
 (0)