@reyaxyz/sdk 0.131.4 → 0.131.5

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/README.md CHANGED
@@ -6,5 +6,5 @@
6
6
 
7
7
  | Statements | Branches | Functions | Lines |
8
8
  | --------------------------- | ----------------------- | ------------------------- | ----------------- |
9
- | ![Statements](https://img.shields.io/badge/statements-7.96%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-3.09%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-3.65%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-7.64%25-red.svg?style=flat) |
9
+ | ![Statements](https://img.shields.io/badge/statements-7.51%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-3.01%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-3.57%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-7.2%25-red.svg?style=flat) |
10
10
 
@@ -22,4 +22,5 @@ __exportStar(require("./withdrawPassivePoolAndBridge"), exports);
22
22
  __exportStar(require("./transferStakeToMarginAccount"), exports);
23
23
  __exportStar(require("./unstakeFromMarginAccount"), exports);
24
24
  __exportStar(require("./stakeFromMarginAccount"), exports);
25
+ __exportStar(require("./rescueFundsFromEmbeddedWallet"), exports);
25
26
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"/","sources":["services/lp/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,gEAA8C;AAC9C,yDAAuC;AACvC,yDAAuC;AACvC,iEAA+C;AAC/C,iEAA+C;AAC/C,6DAA2C;AAC3C,2DAAyC","sourcesContent":["export * from './types';\nexport * from './bridgeAndDepositPassivePool';\nexport * from './transferFromMAToPool';\nexport * from './transferFromPoolToMa';\nexport * from './withdrawPassivePoolAndBridge';\nexport * from './transferStakeToMarginAccount';\nexport * from './unstakeFromMarginAccount';\nexport * from './stakeFromMarginAccount';\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"/","sources":["services/lp/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,gEAA8C;AAC9C,yDAAuC;AACvC,yDAAuC;AACvC,iEAA+C;AAC/C,iEAA+C;AAC/C,6DAA2C;AAC3C,2DAAyC;AACzC,kEAAgD","sourcesContent":["export * from './types';\nexport * from './bridgeAndDepositPassivePool';\nexport * from './transferFromMAToPool';\nexport * from './transferFromPoolToMa';\nexport * from './withdrawPassivePoolAndBridge';\nexport * from './transferStakeToMarginAccount';\nexport * from './unstakeFromMarginAccount';\nexport * from './stakeFromMarginAccount';\nexport * from './rescueFundsFromEmbeddedWallet';\n"]}
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __generator = (this && this.__generator) || function (thisArg, body) {
12
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
+ function verb(n) { return function (v) { return step([n, v]); }; }
15
+ function step(op) {
16
+ if (f) throw new TypeError("Generator is already executing.");
17
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
+ if (y = 0, t) op = [op[0] & 2, t.value];
20
+ switch (op[0]) {
21
+ case 0: case 1: t = op; break;
22
+ case 4: _.label++; return { value: op[1], done: false };
23
+ case 5: _.label++; y = op[1]; op = [0]; continue;
24
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
+ default:
26
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
+ if (t[2]) _.ops.pop();
31
+ _.trys.pop(); continue;
32
+ }
33
+ op = body.call(thisArg, _);
34
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
+ }
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.rescueFundsFromEmbeddedWallet = void 0;
40
+ var ethers_1 = require("ethers");
41
+ var config_1 = require("../../config");
42
+ var common_1 = require("@reyaxyz/common");
43
+ var network_1 = require("../../utils/network");
44
+ var PassivePoolAbi = [
45
+ 'function getAccountBalance(uint128 poolId, address account) external view returns (uint256)',
46
+ 'function removeLiquidity(uint128,uint256,uint256,(uint8,address)) external',
47
+ 'function addLiquidity(uint128,address,uint256,uint256,(uint8,address)) external',
48
+ ];
49
+ var Erc20Abi = [
50
+ "function approve(address, uint256) external returns (bool)",
51
+ "function balanceOf(address) external view returns (uint256)",
52
+ ];
53
+ var validateInputs = function (_a) {
54
+ var embeddedWallet = _a.embeddedWallet, mainWallet = _a.mainWallet, embeddedWalletShares = _a.embeddedWalletShares;
55
+ return __awaiter(void 0, void 0, void 0, function () {
56
+ var embeddedWalletProvider, mainWalletProvider, embeddedWalletChainId, _b, mainWalletChainId, _c, doChainIdsMatch, isConnectedToReya;
57
+ return __generator(this, function (_d) {
58
+ switch (_d.label) {
59
+ case 0:
60
+ if (embeddedWalletShares === 0) {
61
+ throw new Error('No funds available to rescue');
62
+ }
63
+ embeddedWalletProvider = embeddedWallet.provider;
64
+ mainWalletProvider = mainWallet.provider;
65
+ if (!embeddedWalletProvider) {
66
+ throw new Error('Embedded wallet provider not found');
67
+ }
68
+ if (!mainWalletProvider) {
69
+ throw new Error('Main wallet provider not found');
70
+ }
71
+ _b = Number;
72
+ return [4 /*yield*/, embeddedWalletProvider.getNetwork()];
73
+ case 1:
74
+ embeddedWalletChainId = _b.apply(void 0, [(_d.sent()).chainId]);
75
+ _c = Number;
76
+ return [4 /*yield*/, mainWalletProvider.getNetwork()];
77
+ case 2:
78
+ mainWalletChainId = _c.apply(void 0, [(_d.sent()).chainId]);
79
+ doChainIdsMatch = embeddedWalletChainId === mainWalletChainId;
80
+ if (!doChainIdsMatch) {
81
+ throw new Error('Chain IDs do not match');
82
+ }
83
+ isConnectedToReya = embeddedWalletChainId === (0, config_1.getSdkConfig)().chain;
84
+ if (!isConnectedToReya) {
85
+ throw new Error("Wallet is not connected to Reya, ".concat(embeddedWalletChainId));
86
+ }
87
+ return [2 /*return*/];
88
+ }
89
+ });
90
+ });
91
+ };
92
+ var rescueFundsFromEmbeddedWallet = function (params) { return __awaiter(void 0, void 0, void 0, function () {
93
+ var embeddedWallet, mainWallet, embeddedWalletShares, chainId, embeddedWalletAddress, mainWalletAddress, passivePoolAddress, passivePool, rusdAddress, rusd, tx, rusdBalance, tx, tx;
94
+ return __generator(this, function (_a) {
95
+ switch (_a.label) {
96
+ case 0: return [4 /*yield*/, validateInputs(params)];
97
+ case 1:
98
+ _a.sent();
99
+ embeddedWallet = params.embeddedWallet, mainWallet = params.mainWallet, embeddedWalletShares = params.embeddedWalletShares;
100
+ chainId = (0, network_1.getReyaNetwork)();
101
+ return [4 /*yield*/, embeddedWallet.getAddress()];
102
+ case 2:
103
+ embeddedWalletAddress = _a.sent();
104
+ return [4 /*yield*/, mainWallet.getAddress()];
105
+ case 3:
106
+ mainWalletAddress = _a.sent();
107
+ passivePoolAddress = (0, common_1.getAddress)(chainId, common_1.ContractType.PASSIVE_POOL_PROXY);
108
+ passivePool = new ethers_1.Contract(passivePoolAddress, PassivePoolAbi, embeddedWallet);
109
+ rusdAddress = (0, common_1.getTokenInfoByName)('RUSD', chainId).address;
110
+ rusd = new ethers_1.Contract(rusdAddress, Erc20Abi, embeddedWallet);
111
+ return [4 /*yield*/, passivePool.removeLiquidity(1, (0, common_1.scale)(30)(embeddedWalletShares), 0, [2, embeddedWalletAddress])];
112
+ case 4:
113
+ tx = _a.sent();
114
+ return [4 /*yield*/, tx.wait()];
115
+ case 5:
116
+ _a.sent();
117
+ return [4 /*yield*/, rusd.balanceOf(embeddedWalletAddress)];
118
+ case 6:
119
+ rusdBalance = _a.sent();
120
+ return [4 /*yield*/, rusd.approve(passivePoolAddress, rusdBalance)];
121
+ case 7:
122
+ tx = _a.sent();
123
+ return [4 /*yield*/, tx.wait()];
124
+ case 8:
125
+ _a.sent();
126
+ return [4 /*yield*/, passivePool.addLiquidity(1, mainWalletAddress, rusdBalance, 0, [1, mainWalletAddress])];
127
+ case 9:
128
+ tx = _a.sent();
129
+ return [4 /*yield*/, tx.wait()];
130
+ case 10:
131
+ _a.sent();
132
+ return [2 /*return*/];
133
+ }
134
+ });
135
+ }); };
136
+ exports.rescueFundsFromEmbeddedWallet = rescueFundsFromEmbeddedWallet;
137
+ //# sourceMappingURL=rescueFundsFromEmbeddedWallet.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rescueFundsFromEmbeddedWallet.js","sourceRoot":"/","sources":["services/lp/rescueFundsFromEmbeddedWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAyD;AACzD,uCAA4C;AAC5C,0CAKyB;AACzB,+CAAqD;A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,CAAC,EAAA;;gBAAxD,EAAE,GAAG,SAAmD;gBAC9D,qBAAM,EAAE,CAAC,IAAI,EAAE,EAAA;;gBAAf,SAAe,CAAC;gBAIL,qBAAM,WAAW,CAAC,YAAY,CACvC,CAAC,EACD,iBAAiB,EACjB,WAAW,EACX,CAAC,EACD,CAAC,CAAC,EAAE,iBAAiB,CAAC,CACvB,EAAA;;gBANK,EAAE,GAAG,SAMV;gBAED,qBAAM,EAAE,CAAC,IAAI,EAAE,EAAA;;gBAAf,SAAe,CAAC;;;;KAEnB,CAAC;AAnDW,QAAA,6BAA6B,iCAmDxC","sourcesContent":["import { Contract, JsonRpcSigner, Signer } from 'ethers';\nimport { getSdkConfig } from '../../config';\nimport {\n ContractType,\n getAddress,\n getTokenInfoByName,\n scale,\n} from '@reyaxyz/common';\nimport { getReyaNetwork } from '../../utils/network';\n\nexport type RescueFundsFromEmbeddedWalletParams = {\n embeddedWallet: JsonRpcSigner | Signer;\n mainWallet: JsonRpcSigner | Signer;\n embeddedWalletShares: number;\n};\n\nconst PassivePoolAbi = [\n 'function getAccountBalance(uint128 poolId, address account) external view returns (uint256)',\n 'function removeLiquidity(uint128,uint256,uint256,(uint8,address)) external',\n 'function addLiquidity(uint128,address,uint256,uint256,(uint8,address)) external',\n];\n\nconst Erc20Abi = [\n `function approve(address, uint256) external returns (bool)`,\n `function balanceOf(address) external view returns (uint256)`,\n];\n\nconst validateInputs = async ({\n embeddedWallet,\n mainWallet,\n embeddedWalletShares,\n}: RescueFundsFromEmbeddedWalletParams): Promise<void> => {\n if (embeddedWalletShares === 0) {\n throw new Error('No funds available to rescue');\n }\n\n const embeddedWalletProvider = embeddedWallet.provider;\n const mainWalletProvider = mainWallet.provider;\n\n if (!embeddedWalletProvider) {\n throw new Error('Embedded wallet provider not found');\n }\n\n if (!mainWalletProvider) {\n throw new Error('Main wallet provider not found');\n }\n\n const embeddedWalletChainId = Number(\n (await embeddedWalletProvider.getNetwork()).chainId,\n );\n const mainWalletChainId = Number(\n (await mainWalletProvider.getNetwork()).chainId,\n );\n\n const doChainIdsMatch = embeddedWalletChainId === mainWalletChainId;\n if (!doChainIdsMatch) {\n throw new Error('Chain IDs do not match');\n }\n\n const isConnectedToReya = embeddedWalletChainId === getSdkConfig().chain;\n\n if (!isConnectedToReya) {\n throw new Error(\n `Wallet is not connected to Reya, ${embeddedWalletChainId}`,\n );\n }\n};\n\nexport const rescueFundsFromEmbeddedWallet = async (\n params: RescueFundsFromEmbeddedWalletParams,\n): Promise<void> => {\n await validateInputs(params);\n const { embeddedWallet, mainWallet, embeddedWalletShares } = params;\n const chainId = getReyaNetwork();\n const embeddedWalletAddress = await embeddedWallet.getAddress();\n const mainWalletAddress = await mainWallet.getAddress();\n\n const passivePoolAddress = getAddress(\n chainId,\n ContractType.PASSIVE_POOL_PROXY,\n );\n const passivePool = new Contract(\n passivePoolAddress,\n PassivePoolAbi,\n embeddedWallet,\n );\n\n const rusdAddress = getTokenInfoByName('RUSD', chainId).address;\n const rusd = new Contract(rusdAddress, Erc20Abi, embeddedWallet);\n\n {\n const tx = await passivePool.removeLiquidity(\n 1,\n scale(30)(embeddedWalletShares),\n 0,\n [2, embeddedWalletAddress],\n );\n\n await tx.wait();\n }\n\n const rusdBalance = await rusd.balanceOf(embeddedWalletAddress);\n\n {\n const tx = await rusd.approve(passivePoolAddress, rusdBalance);\n await tx.wait();\n }\n\n {\n const tx = await passivePool.addLiquidity(\n 1,\n mainWalletAddress,\n rusdBalance,\n 0,\n [1, mainWalletAddress],\n );\n\n await tx.wait();\n }\n};\n"]}
@@ -6,4 +6,5 @@ export * from './withdrawPassivePoolAndBridge';
6
6
  export * from './transferStakeToMarginAccount';
7
7
  export * from './unstakeFromMarginAccount';
8
8
  export * from './stakeFromMarginAccount';
9
+ export * from './rescueFundsFromEmbeddedWallet';
9
10
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["services/lp/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["services/lp/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,+BAA+B,CAAC;AAC9C,cAAc,wBAAwB,CAAC;AACvC,cAAc,wBAAwB,CAAC;AACvC,cAAc,gCAAgC,CAAC;AAC/C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,0BAA0B,CAAC;AACzC,cAAc,iCAAiC,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { JsonRpcSigner, Signer } from 'ethers';
2
+ export type RescueFundsFromEmbeddedWalletParams = {
3
+ embeddedWallet: JsonRpcSigner | Signer;
4
+ mainWallet: JsonRpcSigner | Signer;
5
+ embeddedWalletShares: number;
6
+ };
7
+ export declare const rescueFundsFromEmbeddedWallet: (params: RescueFundsFromEmbeddedWalletParams) => Promise<void>;
8
+ //# sourceMappingURL=rescueFundsFromEmbeddedWallet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rescueFundsFromEmbeddedWallet.d.ts","sourceRoot":"/","sources":["services/lp/rescueFundsFromEmbeddedWallet.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,aAAa,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAUzD,MAAM,MAAM,mCAAmC,GAAG;IAChD,cAAc,EAAE,aAAa,GAAG,MAAM,CAAC;IACvC,UAAU,EAAE,aAAa,GAAG,MAAM,CAAC;IACnC,oBAAoB,EAAE,MAAM,CAAC;CAC9B,CAAC;AAsDF,eAAO,MAAM,6BAA6B,WAChC,mCAAmC,KAC1C,QAAQ,IAAI,CAiDd,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/sdk",
3
- "version": "0.131.4",
3
+ "version": "0.131.5",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "registry": "https://registry.npmjs.org"
@@ -35,5 +35,5 @@
35
35
  "ethers": "6.9.0"
36
36
  },
37
37
  "packageManager": "pnpm@8.3.1",
38
- "gitHead": "7af610a38e30a79b09dc0b06c374f7deaa22b9a4"
38
+ "gitHead": "68a40db780abaf4b47522421d2f898a9a3ec2930"
39
39
  }
@@ -6,3 +6,4 @@ export * from './withdrawPassivePoolAndBridge';
6
6
  export * from './transferStakeToMarginAccount';
7
7
  export * from './unstakeFromMarginAccount';
8
8
  export * from './stakeFromMarginAccount';
9
+ export * from './rescueFundsFromEmbeddedWallet';
@@ -0,0 +1,120 @@
1
+ import { Contract, JsonRpcSigner, Signer } from 'ethers';
2
+ import { getSdkConfig } from '../../config';
3
+ import {
4
+ ContractType,
5
+ getAddress,
6
+ getTokenInfoByName,
7
+ scale,
8
+ } from '@reyaxyz/common';
9
+ import { getReyaNetwork } from '../../utils/network';
10
+
11
+ export type RescueFundsFromEmbeddedWalletParams = {
12
+ embeddedWallet: JsonRpcSigner | Signer;
13
+ mainWallet: JsonRpcSigner | Signer;
14
+ embeddedWalletShares: number;
15
+ };
16
+
17
+ const PassivePoolAbi = [
18
+ 'function getAccountBalance(uint128 poolId, address account) external view returns (uint256)',
19
+ 'function removeLiquidity(uint128,uint256,uint256,(uint8,address)) external',
20
+ 'function addLiquidity(uint128,address,uint256,uint256,(uint8,address)) external',
21
+ ];
22
+
23
+ const Erc20Abi = [
24
+ `function approve(address, uint256) external returns (bool)`,
25
+ `function balanceOf(address) external view returns (uint256)`,
26
+ ];
27
+
28
+ const validateInputs = async ({
29
+ embeddedWallet,
30
+ mainWallet,
31
+ embeddedWalletShares,
32
+ }: RescueFundsFromEmbeddedWalletParams): Promise<void> => {
33
+ if (embeddedWalletShares === 0) {
34
+ throw new Error('No funds available to rescue');
35
+ }
36
+
37
+ const embeddedWalletProvider = embeddedWallet.provider;
38
+ const mainWalletProvider = mainWallet.provider;
39
+
40
+ if (!embeddedWalletProvider) {
41
+ throw new Error('Embedded wallet provider not found');
42
+ }
43
+
44
+ if (!mainWalletProvider) {
45
+ throw new Error('Main wallet provider not found');
46
+ }
47
+
48
+ const embeddedWalletChainId = Number(
49
+ (await embeddedWalletProvider.getNetwork()).chainId,
50
+ );
51
+ const mainWalletChainId = Number(
52
+ (await mainWalletProvider.getNetwork()).chainId,
53
+ );
54
+
55
+ const doChainIdsMatch = embeddedWalletChainId === mainWalletChainId;
56
+ if (!doChainIdsMatch) {
57
+ throw new Error('Chain IDs do not match');
58
+ }
59
+
60
+ const isConnectedToReya = embeddedWalletChainId === getSdkConfig().chain;
61
+
62
+ if (!isConnectedToReya) {
63
+ throw new Error(
64
+ `Wallet is not connected to Reya, ${embeddedWalletChainId}`,
65
+ );
66
+ }
67
+ };
68
+
69
+ export const rescueFundsFromEmbeddedWallet = async (
70
+ params: RescueFundsFromEmbeddedWalletParams,
71
+ ): Promise<void> => {
72
+ await validateInputs(params);
73
+ const { embeddedWallet, mainWallet, embeddedWalletShares } = params;
74
+ const chainId = getReyaNetwork();
75
+ const embeddedWalletAddress = await embeddedWallet.getAddress();
76
+ const mainWalletAddress = await mainWallet.getAddress();
77
+
78
+ const passivePoolAddress = getAddress(
79
+ chainId,
80
+ ContractType.PASSIVE_POOL_PROXY,
81
+ );
82
+ const passivePool = new Contract(
83
+ passivePoolAddress,
84
+ PassivePoolAbi,
85
+ embeddedWallet,
86
+ );
87
+
88
+ const rusdAddress = getTokenInfoByName('RUSD', chainId).address;
89
+ const rusd = new Contract(rusdAddress, Erc20Abi, embeddedWallet);
90
+
91
+ {
92
+ const tx = await passivePool.removeLiquidity(
93
+ 1,
94
+ scale(30)(embeddedWalletShares),
95
+ 0,
96
+ [2, embeddedWalletAddress],
97
+ );
98
+
99
+ await tx.wait();
100
+ }
101
+
102
+ const rusdBalance = await rusd.balanceOf(embeddedWalletAddress);
103
+
104
+ {
105
+ const tx = await rusd.approve(passivePoolAddress, rusdBalance);
106
+ await tx.wait();
107
+ }
108
+
109
+ {
110
+ const tx = await passivePool.addLiquidity(
111
+ 1,
112
+ mainWalletAddress,
113
+ rusdBalance,
114
+ 0,
115
+ [1, mainWalletAddress],
116
+ );
117
+
118
+ await tx.wait();
119
+ }
120
+ };