@reyaxyz/sdk 0.1.0 → 0.1.1
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/dist/services/token/approve.js +95 -0
- package/dist/services/token/approve.js.map +1 -0
- package/dist/services/token/common.js +107 -0
- package/dist/services/token/common.js.map +1 -0
- package/dist/services/token/consts.js +29 -0
- package/dist/services/token/consts.js.map +1 -0
- package/dist/services/token/getAllowance.js +68 -0
- package/dist/services/token/getAllowance.js.map +1 -0
- package/dist/services/token/types.js +9 -0
- package/dist/services/token/types.js.map +1 -0
- package/dist/types/services/token/approve.d.ts +3 -0
- package/dist/types/services/token/approve.d.ts.map +1 -0
- package/dist/types/services/token/common.d.ts +14 -0
- package/dist/types/services/token/common.d.ts.map +1 -0
- package/dist/types/services/token/consts.d.ts +8 -0
- package/dist/types/services/token/consts.d.ts.map +1 -0
- package/dist/types/services/token/getAllowance.d.ts +3 -0
- package/dist/types/services/token/getAllowance.d.ts.map +1 -0
- package/dist/types/services/token/types.d.ts +24 -0
- package/dist/types/services/token/types.d.ts.map +1 -0
- package/dist/types/utils/contractAddresses.d.ts +5 -0
- package/dist/types/utils/contractAddresses.d.ts.map +1 -0
- package/dist/types/utils/retry.d.ts +2 -0
- package/dist/types/utils/retry.d.ts.map +1 -0
- package/dist/utils/contractAddresses.js +27 -0
- package/dist/utils/contractAddresses.js.map +1 -0
- package/dist/utils/retry.js +94 -0
- package/dist/utils/retry.js.map +1 -0
- package/package.json +2 -2
- package/src/services/token/approve.ts +52 -0
- package/src/services/token/common.ts +84 -0
- package/src/services/token/consts.ts +32 -0
- package/src/services/token/getAllowance.ts +24 -0
- package/src/services/token/types.ts +27 -0
- package/src/utils/contractAddresses.ts +37 -0
- package/src/utils/retry.ts +40 -0
|
@@ -0,0 +1,95 @@
|
|
|
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.approveTokenSpending = void 0;
|
|
40
|
+
var types_1 = require("./types");
|
|
41
|
+
var common_1 = require("./common");
|
|
42
|
+
var contractAddresses_1 = require("../../utils/contractAddresses");
|
|
43
|
+
var approveTokenSpending = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
44
|
+
var underLyingTokenAddress, tokenContract, contractProxyAddress, maxUint256Bn, approvalTransaction, error_1, _a, error_2;
|
|
45
|
+
var _b;
|
|
46
|
+
return __generator(this, function (_c) {
|
|
47
|
+
switch (_c.label) {
|
|
48
|
+
case 0:
|
|
49
|
+
underLyingTokenAddress = params.token;
|
|
50
|
+
tokenContract = (0, common_1.getERC20TokenContract)(underLyingTokenAddress, params.signer);
|
|
51
|
+
contractProxyAddress = params.type === types_1.ApprovalType.DEPOSIT
|
|
52
|
+
? (0, contractAddresses_1.getAddress)(params.chainId, 'core_proxy')
|
|
53
|
+
: (0, contractAddresses_1.getAddress)(params.chainId, 'passive_pool_proxy');
|
|
54
|
+
maxUint256Bn = BigInt('115792089237316195423570985008687907853269984665640564039457584007913129639935');
|
|
55
|
+
return [4 /*yield*/, tokenContract
|
|
56
|
+
.approve(contractProxyAddress, maxUint256Bn)
|
|
57
|
+
.catch(function (error) {
|
|
58
|
+
console.warn('Transaction Confirmation Error');
|
|
59
|
+
console.error(error);
|
|
60
|
+
throw new Error('Transaction Confirmation Error');
|
|
61
|
+
})];
|
|
62
|
+
case 1:
|
|
63
|
+
approvalTransaction = _c.sent();
|
|
64
|
+
_c.label = 2;
|
|
65
|
+
case 2:
|
|
66
|
+
_c.trys.push([2, 4, , 5]);
|
|
67
|
+
return [4 /*yield*/, approvalTransaction.wait()];
|
|
68
|
+
case 3:
|
|
69
|
+
_c.sent();
|
|
70
|
+
return [3 /*break*/, 5];
|
|
71
|
+
case 4:
|
|
72
|
+
error_1 = _c.sent();
|
|
73
|
+
console.warn('Token approval failed');
|
|
74
|
+
throw new Error('Token approval failed');
|
|
75
|
+
case 5:
|
|
76
|
+
_c.trys.push([5, 8, , 9]);
|
|
77
|
+
_a = common_1.getERC20Allowance;
|
|
78
|
+
_b = {};
|
|
79
|
+
return [4 /*yield*/, params.signer.getAddress()];
|
|
80
|
+
case 6: return [4 /*yield*/, _a.apply(void 0, [(_b.walletAddress = _c.sent(),
|
|
81
|
+
_b.tokenAddress = underLyingTokenAddress,
|
|
82
|
+
_b.spenderAddress = contractProxyAddress,
|
|
83
|
+
_b.subject = params.signer,
|
|
84
|
+
_b)])];
|
|
85
|
+
case 7: return [2 /*return*/, _c.sent()];
|
|
86
|
+
case 8:
|
|
87
|
+
error_2 = _c.sent();
|
|
88
|
+
console.warn('Fetching allowance failed');
|
|
89
|
+
return [3 /*break*/, 9];
|
|
90
|
+
case 9: return [2 /*return*/, 0];
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
}); };
|
|
94
|
+
exports.approveTokenSpending = approveTokenSpending;
|
|
95
|
+
//# sourceMappingURL=approve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approve.js","sourceRoot":"/","sources":["services/token/approve.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAyD;AACzD,mCAAoE;AACpE,mEAA2D;AAEpD,IAAM,oBAAoB,GAAG,UAClC,MAAwB;;;;;;gBAElB,sBAAsB,GAAG,MAAM,CAAC,KAAK,CAAC;gBAEtC,aAAa,GAAG,IAAA,8BAAqB,EACzC,sBAAsB,EACtB,MAAM,CAAC,MAAM,CACd,CAAC;gBAEI,oBAAoB,GACxB,MAAM,CAAC,IAAI,KAAK,oBAAY,CAAC,OAAO;oBAClC,CAAC,CAAC,IAAA,8BAAU,EAAC,MAAM,CAAC,OAAO,EAAE,YAAY,CAAC;oBAC1C,CAAC,CAAC,IAAA,8BAAU,EAAC,MAAM,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;gBAGjD,YAAY,GAAG,MAAM,CACzB,gFAAgF,CACjF,CAAC;gBAE0B,qBAAM,aAAa;yBAC5C,OAAO,CAAC,oBAAoB,EAAE,YAAY,CAAC;yBAC3C,KAAK,CAAC,UAAC,KAAK;wBACX,OAAO,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAC;wBAC/C,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;wBACrB,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;oBACpD,CAAC,CAAC,EAAA;;gBANE,mBAAmB,GAAG,SAMxB;;;;gBAGF,qBAAM,mBAAmB,CAAC,IAAI,EAAE,EAAA;;gBAAhC,SAAgC,CAAC;;;;gBAEjC,OAAO,CAAC,IAAI,CAAC,uBAAuB,CAAC,CAAC;gBACtC,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;;;gBAI5B,KAAA,0BAAiB,CAAA;;gBACb,qBAAM,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,EAAA;oBAD1C,qBAAM,mBACX,gBAAa,GAAE,SAAgC;wBAC/C,eAAY,GAAE,sBAAsB;wBACpC,iBAAc,GAAE,oBAAoB;wBACpC,UAAO,GAAE,MAAM,CAAC,MAAM;6BACtB,EAAA;oBALF,sBAAO,SAKL,EAAC;;;gBAEH,OAAO,CAAC,IAAI,CAAC,2BAA2B,CAAC,CAAC;;oBAG5C,sBAAO,CAAC,EAAC;;;KACV,CAAC;AA/CW,QAAA,oBAAoB,wBA+C/B","sourcesContent":["import { ApprovalType, ApproveTokenArgs } from './types';\nimport { getERC20Allowance, getERC20TokenContract } from './common';\nimport { getAddress } from '../../utils/contractAddresses';\n\nexport const approveTokenSpending = async (\n params: ApproveTokenArgs,\n): Promise<number> => {\n const underLyingTokenAddress = params.token;\n\n const tokenContract = getERC20TokenContract(\n underLyingTokenAddress,\n params.signer,\n );\n\n const contractProxyAddress =\n params.type === ApprovalType.DEPOSIT\n ? getAddress(params.chainId, 'core_proxy')\n : getAddress(params.chainId, 'passive_pool_proxy');\n\n // @todo validate if maxAllowance should be passed from UI\n const maxUint256Bn = BigInt(\n '115792089237316195423570985008687907853269984665640564039457584007913129639935',\n );\n\n const approvalTransaction = await tokenContract\n .approve(contractProxyAddress, maxUint256Bn)\n .catch((error) => {\n console.warn('Transaction Confirmation Error');\n console.error(error);\n throw new Error('Transaction Confirmation Error');\n });\n\n try {\n await approvalTransaction.wait();\n } catch (error) {\n console.warn('Token approval failed');\n throw new Error('Token approval failed');\n }\n\n try {\n return await getERC20Allowance({\n walletAddress: await params.signer.getAddress(),\n tokenAddress: underLyingTokenAddress,\n spenderAddress: contractProxyAddress,\n subject: params.signer,\n });\n } catch (error) {\n console.warn('Fetching allowance failed');\n }\n\n return 0;\n};\n"]}
|
|
@@ -0,0 +1,107 @@
|
|
|
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.getERC20Allowance = exports.getTokenDetails = exports.convertToAddress = exports.getERC20TokenContract = exports.scale = exports.descale = void 0;
|
|
40
|
+
var ethers_1 = require("ethers");
|
|
41
|
+
var consts_1 = require("./consts");
|
|
42
|
+
var retry_1 = require("../../utils/retry");
|
|
43
|
+
var descale = function (tokenDecimals) {
|
|
44
|
+
var f = function (value) {
|
|
45
|
+
return Number(ethers_1.ethers.formatUnits(value.toString(), tokenDecimals));
|
|
46
|
+
};
|
|
47
|
+
return f;
|
|
48
|
+
};
|
|
49
|
+
exports.descale = descale;
|
|
50
|
+
var scale = function (tokenDecimals) {
|
|
51
|
+
var f = function (value) {
|
|
52
|
+
return BigInt(ethers_1.ethers.parseUnits(value.toFixed(tokenDecimals), tokenDecimals));
|
|
53
|
+
};
|
|
54
|
+
return f;
|
|
55
|
+
};
|
|
56
|
+
exports.scale = scale;
|
|
57
|
+
var getERC20TokenContract = function (tokenAddress, subject) {
|
|
58
|
+
var abi = [
|
|
59
|
+
"function approve(address, uint256) external returns (bool)",
|
|
60
|
+
"function balanceOf(address) external view returns (uint256)",
|
|
61
|
+
"function allowance(address,address) external view returns (uint256)",
|
|
62
|
+
"function approve(address,uint256) external returns (bool)",
|
|
63
|
+
];
|
|
64
|
+
var contract = new ethers_1.Contract(tokenAddress, abi, subject);
|
|
65
|
+
return contract;
|
|
66
|
+
};
|
|
67
|
+
exports.getERC20TokenContract = getERC20TokenContract;
|
|
68
|
+
var convertToAddress = function (str) {
|
|
69
|
+
return str.toLowerCase();
|
|
70
|
+
};
|
|
71
|
+
exports.convertToAddress = convertToAddress;
|
|
72
|
+
var getTokenDetails = function (caseSensitiveAddress) {
|
|
73
|
+
var address = (0, exports.convertToAddress)(caseSensitiveAddress);
|
|
74
|
+
if (!Object.keys(consts_1.tokenDetails).includes(address)) {
|
|
75
|
+
throw new Error("Token details not found for ".concat(address, "."));
|
|
76
|
+
}
|
|
77
|
+
var _a = consts_1.tokenDetails[address], tokenName = _a.tokenName, tokenDecimals = _a.tokenDecimals;
|
|
78
|
+
return {
|
|
79
|
+
tokenName: tokenName,
|
|
80
|
+
tokenDecimals: tokenDecimals,
|
|
81
|
+
tokenDescaler: (0, exports.descale)(tokenDecimals),
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
exports.getTokenDetails = getTokenDetails;
|
|
85
|
+
var getERC20Allowance = function (_a) {
|
|
86
|
+
var walletAddress = _a.walletAddress, tokenAddress = _a.tokenAddress, spenderAddress = _a.spenderAddress, subject = _a.subject;
|
|
87
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
88
|
+
var tokenContract, tokenDecimals, allowance;
|
|
89
|
+
return __generator(this, function (_b) {
|
|
90
|
+
switch (_b.label) {
|
|
91
|
+
case 0:
|
|
92
|
+
tokenContract = (0, exports.getERC20TokenContract)(tokenAddress, subject);
|
|
93
|
+
tokenDecimals = (0, exports.getTokenDetails)(tokenAddress).tokenDecimals;
|
|
94
|
+
return [4 /*yield*/, (0, retry_1.exponentialBackoff)(function () {
|
|
95
|
+
return tokenContract.allowance(walletAddress, spenderAddress);
|
|
96
|
+
})];
|
|
97
|
+
case 1:
|
|
98
|
+
allowance = _b.sent();
|
|
99
|
+
return [2 /*return*/, BigInt(allowance) > (0, exports.scale)(tokenDecimals)(Number.MAX_SAFE_INTEGER)
|
|
100
|
+
? Number.MAX_SAFE_INTEGER
|
|
101
|
+
: (0, exports.descale)(tokenDecimals)(allowance)];
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
exports.getERC20Allowance = getERC20Allowance;
|
|
107
|
+
//# sourceMappingURL=common.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"/","sources":["services/token/common.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAkD;AAClD,mCAAwC;AACxC,2CAAuD;AAGhD,IAAM,OAAO,GAAG,UAAC,aAAqB;IAC3C,IAAM,CAAC,GAAG,UAAC,KAAa;QACtB,OAAO,MAAM,CAAC,eAAM,CAAC,WAAW,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,aAAa,CAAC,CAAC,CAAC;IACrE,CAAC,CAAC;IAEF,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;AANW,QAAA,OAAO,WAMlB;AAEK,IAAM,KAAK,GAAG,UAAC,aAAqB;IACzC,IAAM,CAAC,GAAG,UAAC,KAAa;QACtB,OAAO,MAAM,CACX,eAAM,CAAC,UAAU,CAAC,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE,aAAa,CAAC,CAC/D,CAAC;IACJ,CAAC,CAAC;IAEF,OAAO,CAAC,CAAC;AACX,CAAC,CAAC;AARW,QAAA,KAAK,SAQhB;AAEK,IAAM,qBAAqB,GAAG,UACnC,YAAoB,EACpB,OAAe;IAEf,IAAM,GAAG,GAAa;QACpB,4DAA4D;QAC5D,6DAA6D;QAC7D,qEAAqE;QACrE,2DAA2D;KAC5D,CAAC;IAEF,IAAM,QAAQ,GAAG,IAAI,iBAAQ,CAAC,YAAY,EAAE,GAAG,EAAE,OAAO,CAAC,CAAC;IAE1D,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAdW,QAAA,qBAAqB,yBAchC;AAIK,IAAM,gBAAgB,GAAG,UAAC,GAAW;IAC1C,OAAO,GAAG,CAAC,WAAW,EAAa,CAAC;AACtC,CAAC,CAAC;AAFW,QAAA,gBAAgB,oBAE3B;AAEK,IAAM,eAAe,GAAG,UAC7B,oBAA4B;IAM5B,IAAM,OAAO,GAAG,IAAA,wBAAgB,EAAC,oBAAoB,CAAC,CAAC;IAEvD,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,qBAAY,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;QACjD,MAAM,IAAI,KAAK,CAAC,sCAA+B,OAAO,MAAG,CAAC,CAAC;IAC7D,CAAC;IAEK,IAAA,KAA+B,qBAAY,CAAC,OAAO,CAAC,EAAlD,SAAS,eAAA,EAAE,aAAa,mBAA0B,CAAC;IAE3D,OAAO;QACL,SAAS,WAAA;QACT,aAAa,eAAA;QACb,aAAa,EAAE,IAAA,eAAO,EAAC,aAAa,CAAC;KACtC,CAAC;AACJ,CAAC,CAAC;AApBW,QAAA,eAAe,mBAoB1B;AAEK,IAAM,iBAAiB,GAAG,UAAO,EAKhB;QAJtB,aAAa,mBAAA,EACb,YAAY,kBAAA,EACZ,cAAc,oBAAA,EACd,OAAO,aAAA;;;;;;oBAED,aAAa,GAAG,IAAA,6BAAqB,EAAC,YAAY,EAAE,OAAO,CAAC,CAAC;oBAC3D,aAAa,GAAK,IAAA,uBAAe,EAAC,YAAY,CAAC,cAAlC,CAAmC;oBAEtC,qBAAM,IAAA,0BAAkB,EAAC;4BACzC,OAAA,aAAa,CAAC,SAAS,CAAC,aAAa,EAAE,cAAc,CAAC;wBAAtD,CAAsD,CACvD,EAAA;;oBAFK,SAAS,GAAG,SAEjB;oBAED,sBAAO,MAAM,CAAC,SAAS,CAAC,GAAG,IAAA,aAAK,EAAC,aAAa,CAAC,CAAC,MAAM,CAAC,gBAAgB,CAAC;4BACtE,CAAC,CAAC,MAAM,CAAC,gBAAgB;4BACzB,CAAC,CAAC,IAAA,eAAO,EAAC,aAAa,CAAC,CAAC,SAAS,CAAC,EAAC;;;;CACvC,CAAC;AAhBW,QAAA,iBAAiB,qBAgB5B","sourcesContent":["import { Contract, ethers, Signer } from 'ethers';\nimport { tokenDetails } from './consts';\nimport { exponentialBackoff } from '../../utils/retry';\nimport { GetERC20AllowanceArgs } from './types';\n\nexport const descale = (tokenDecimals: number) => {\n const f = (value: bigint) => {\n return Number(ethers.formatUnits(value.toString(), tokenDecimals));\n };\n\n return f;\n};\n\nexport const scale = (tokenDecimals: number) => {\n const f = (value: number) => {\n return BigInt(\n ethers.parseUnits(value.toFixed(tokenDecimals), tokenDecimals),\n );\n };\n\n return f;\n};\n\nexport const getERC20TokenContract = (\n tokenAddress: string,\n subject: Signer,\n): Contract => {\n const abi: string[] = [\n `function approve(address, uint256) external returns (bool)`,\n `function balanceOf(address) external view returns (uint256)`,\n `function allowance(address,address) external view returns (uint256)`,\n `function approve(address,uint256) external returns (bool)`,\n ];\n\n const contract = new Contract(tokenAddress, abi, subject);\n\n return contract;\n};\n\nexport type Address = Lowercase<string>;\n\nexport const convertToAddress = (str: string): Address => {\n return str.toLowerCase() as Address;\n};\n\nexport const getTokenDetails = (\n caseSensitiveAddress: string,\n): {\n tokenName: Uppercase<string>;\n tokenDecimals: number;\n tokenDescaler: (value: bigint) => number;\n} => {\n const address = convertToAddress(caseSensitiveAddress);\n\n if (!Object.keys(tokenDetails).includes(address)) {\n throw new Error(`Token details not found for ${address}.`);\n }\n\n const { tokenName, tokenDecimals } = tokenDetails[address];\n\n return {\n tokenName,\n tokenDecimals,\n tokenDescaler: descale(tokenDecimals),\n };\n};\n\nexport const getERC20Allowance = async ({\n walletAddress,\n tokenAddress,\n spenderAddress,\n subject,\n}: GetERC20AllowanceArgs): Promise<number> => {\n const tokenContract = getERC20TokenContract(tokenAddress, subject);\n const { tokenDecimals } = getTokenDetails(tokenAddress);\n\n const allowance = await exponentialBackoff(() =>\n tokenContract.allowance(walletAddress, spenderAddress),\n );\n\n return BigInt(allowance) > scale(tokenDecimals)(Number.MAX_SAFE_INTEGER)\n ? Number.MAX_SAFE_INTEGER\n : descale(tokenDecimals)(allowance);\n};\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.tokenDetails = void 0;
|
|
4
|
+
// @todo update values for mainnet
|
|
5
|
+
exports.tokenDetails = {
|
|
6
|
+
// ====== USDC ======
|
|
7
|
+
// mumbai
|
|
8
|
+
'0x52d800ca262522580cebad275395ca6e7598c014': {
|
|
9
|
+
tokenName: 'USDC',
|
|
10
|
+
tokenDecimals: 6,
|
|
11
|
+
},
|
|
12
|
+
// mainnet
|
|
13
|
+
'--': {
|
|
14
|
+
tokenName: 'USDC',
|
|
15
|
+
tokenDecimals: 6,
|
|
16
|
+
},
|
|
17
|
+
// ====== wETH ======
|
|
18
|
+
// mumbai
|
|
19
|
+
'0xc199807af4fedb02ee567ed0feb814a077de4802': {
|
|
20
|
+
tokenName: 'WETH',
|
|
21
|
+
tokenDecimals: 18,
|
|
22
|
+
},
|
|
23
|
+
// mainnet
|
|
24
|
+
'---': {
|
|
25
|
+
tokenName: 'WETH',
|
|
26
|
+
tokenDecimals: 18,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
//# sourceMappingURL=consts.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consts.js","sourceRoot":"/","sources":["services/token/consts.ts"],"names":[],"mappings":";;;AAEA,kCAAkC;AACrB,QAAA,YAAY,GAKrB;IACF,qBAAqB;IACrB,SAAS;IACT,4CAA4C,EAAE;QAC5C,SAAS,EAAE,MAAM;QACjB,aAAa,EAAE,CAAC;KACjB;IACD,UAAU;IACV,IAAI,EAAE;QACJ,SAAS,EAAE,MAAM;QACjB,aAAa,EAAE,CAAC;KACjB;IACD,qBAAqB;IACrB,SAAS;IACT,4CAA4C,EAAE;QAC5C,SAAS,EAAE,MAAM;QACjB,aAAa,EAAE,EAAE;KAClB;IACD,UAAU;IACV,KAAK,EAAE;QACL,SAAS,EAAE,MAAM;QACjB,aAAa,EAAE,EAAE;KAClB;CACF,CAAC","sourcesContent":["import { Address } from './common';\n\n// @todo update values for mainnet\nexport const tokenDetails: {\n [address: Address]: {\n tokenName: Uppercase<string>;\n tokenDecimals: number;\n };\n} = {\n // ====== USDC ======\n // mumbai\n '0x52d800ca262522580cebad275395ca6e7598c014': {\n tokenName: 'USDC',\n tokenDecimals: 6,\n },\n // mainnet\n '--': {\n tokenName: 'USDC',\n tokenDecimals: 6,\n },\n // ====== wETH ======\n // mumbai\n '0xc199807af4fedb02ee567ed0feb814a077de4802': {\n tokenName: 'WETH',\n tokenDecimals: 18,\n },\n // mainnet\n '---': {\n tokenName: 'WETH',\n tokenDecimals: 18,\n },\n};\n"]}
|
|
@@ -0,0 +1,68 @@
|
|
|
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.getAllowance = void 0;
|
|
40
|
+
var types_1 = require("./types");
|
|
41
|
+
var common_1 = require("./common");
|
|
42
|
+
var contractAddresses_1 = require("../../utils/contractAddresses");
|
|
43
|
+
var getAllowance = function (params) { return __awaiter(void 0, void 0, void 0, function () {
|
|
44
|
+
var underLyingTokenAddress, contractProxyAddress, walletAddress, allowance;
|
|
45
|
+
return __generator(this, function (_a) {
|
|
46
|
+
switch (_a.label) {
|
|
47
|
+
case 0:
|
|
48
|
+
underLyingTokenAddress = params.token;
|
|
49
|
+
contractProxyAddress = params.type === types_1.ApprovalType.DEPOSIT
|
|
50
|
+
? (0, contractAddresses_1.getAddress)(params.chainId, 'core_proxy')
|
|
51
|
+
: (0, contractAddresses_1.getAddress)(params.chainId, 'passive_pool_proxy');
|
|
52
|
+
return [4 /*yield*/, params.signer.getAddress()];
|
|
53
|
+
case 1:
|
|
54
|
+
walletAddress = _a.sent();
|
|
55
|
+
return [4 /*yield*/, (0, common_1.getERC20Allowance)({
|
|
56
|
+
walletAddress: walletAddress,
|
|
57
|
+
tokenAddress: underLyingTokenAddress,
|
|
58
|
+
spenderAddress: contractProxyAddress,
|
|
59
|
+
subject: params.signer,
|
|
60
|
+
})];
|
|
61
|
+
case 2:
|
|
62
|
+
allowance = _a.sent();
|
|
63
|
+
return [2 /*return*/, allowance];
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}); };
|
|
67
|
+
exports.getAllowance = getAllowance;
|
|
68
|
+
//# sourceMappingURL=getAllowance.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getAllowance.js","sourceRoot":"/","sources":["services/token/getAllowance.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAyD;AACzD,mCAA6C;AAC7C,mEAA2D;AAEpD,IAAM,YAAY,GAAG,UAC1B,MAAwB;;;;;gBAElB,sBAAsB,GAAG,MAAM,CAAC,KAAK,CAAC;gBAEtC,oBAAoB,GACxB,MAAM,CAAC,IAAI,KAAK,oBAAY,CAAC,OAAO;oBAClC,CAAC,CAAC,IAAA,8BAAU,EAAC,MAAM,CAAC,OAAO,EAAE,YAAY,CAAC;oBAC1C,CAAC,CAAC,IAAA,8BAAU,EAAC,MAAM,CAAC,OAAO,EAAE,oBAAoB,CAAC,CAAC;gBACzB,qBAAM,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,EAAA;;gBAAxD,aAAa,GAAW,SAAgC;gBAE5C,qBAAM,IAAA,0BAAiB,EAAC;wBACxC,aAAa,eAAA;wBACb,YAAY,EAAE,sBAAsB;wBACpC,cAAc,EAAE,oBAAoB;wBACpC,OAAO,EAAE,MAAM,CAAC,MAAM;qBACvB,CAAC,EAAA;;gBALI,SAAS,GAAG,SAKhB;gBAEF,sBAAO,SAAS,EAAC;;;KAClB,CAAC;AAnBW,QAAA,YAAY,gBAmBvB","sourcesContent":["import { ApprovalType, GetAllowanceArgs } from './types';\nimport { getERC20Allowance } from './common';\nimport { getAddress } from '../../utils/contractAddresses';\n\nexport const getAllowance = async (\n params: GetAllowanceArgs,\n): Promise<number> => {\n const underLyingTokenAddress = params.token;\n\n const contractProxyAddress =\n params.type === ApprovalType.DEPOSIT\n ? getAddress(params.chainId, 'core_proxy')\n : getAddress(params.chainId, 'passive_pool_proxy');\n const walletAddress: string = await params.signer.getAddress();\n\n const allowance = await getERC20Allowance({\n walletAddress,\n tokenAddress: underLyingTokenAddress,\n spenderAddress: contractProxyAddress,\n subject: params.signer,\n });\n\n return allowance;\n};\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ApprovalType = void 0;
|
|
4
|
+
var ApprovalType;
|
|
5
|
+
(function (ApprovalType) {
|
|
6
|
+
ApprovalType[ApprovalType["LP"] = 0] = "LP";
|
|
7
|
+
ApprovalType[ApprovalType["DEPOSIT"] = 1] = "DEPOSIT";
|
|
8
|
+
})(ApprovalType || (exports.ApprovalType = ApprovalType = {}));
|
|
9
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"/","sources":["services/token/types.ts"],"names":[],"mappings":";;;AAEA,IAAY,YAGX;AAHD,WAAY,YAAY;IACtB,2CAAE,CAAA;IACF,qDAAO,CAAA;AACT,CAAC,EAHW,YAAY,4BAAZ,YAAY,QAGvB","sourcesContent":["import { Signer } from 'ethers';\n\nexport enum ApprovalType {\n LP,\n DEPOSIT,\n}\n\nexport type ApproveTokenArgs = {\n signer: Signer;\n chainId: number;\n token: string;\n type: ApprovalType;\n};\n\nexport type GetAllowanceArgs = {\n signer: Signer;\n chainId: number;\n token: string;\n type: ApprovalType;\n};\n\nexport type GetERC20AllowanceArgs = {\n walletAddress: string;\n tokenAddress: string;\n spenderAddress: string;\n subject: Signer;\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"approve.d.ts","sourceRoot":"/","sources":["services/token/approve.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAIzD,eAAO,MAAM,oBAAoB,WACvB,gBAAgB,KACvB,QAAQ,MAAM,CA6ChB,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Contract, Signer } from 'ethers';
|
|
2
|
+
import { GetERC20AllowanceArgs } from './types';
|
|
3
|
+
export declare const descale: (tokenDecimals: number) => (value: bigint) => number;
|
|
4
|
+
export declare const scale: (tokenDecimals: number) => (value: number) => bigint;
|
|
5
|
+
export declare const getERC20TokenContract: (tokenAddress: string, subject: Signer) => Contract;
|
|
6
|
+
export type Address = Lowercase<string>;
|
|
7
|
+
export declare const convertToAddress: (str: string) => Address;
|
|
8
|
+
export declare const getTokenDetails: (caseSensitiveAddress: string) => {
|
|
9
|
+
tokenName: Uppercase<string>;
|
|
10
|
+
tokenDecimals: number;
|
|
11
|
+
tokenDescaler: (value: bigint) => number;
|
|
12
|
+
};
|
|
13
|
+
export declare const getERC20Allowance: ({ walletAddress, tokenAddress, spenderAddress, subject, }: GetERC20AllowanceArgs) => Promise<number>;
|
|
14
|
+
//# sourceMappingURL=common.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"/","sources":["services/token/common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAU,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGlD,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAC;AAEhD,eAAO,MAAM,OAAO,kBAAmB,MAAM,aACzB,MAAM,WAKzB,CAAC;AAEF,eAAO,MAAM,KAAK,kBAAmB,MAAM,aACvB,MAAM,WAOzB,CAAC;AAEF,eAAO,MAAM,qBAAqB,iBAClB,MAAM,WACX,MAAM,KACd,QAWF,CAAC;AAEF,MAAM,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;AAExC,eAAO,MAAM,gBAAgB,QAAS,MAAM,KAAG,OAE9C,CAAC;AAEF,eAAO,MAAM,eAAe,yBACJ,MAAM;eAEjB,UAAU,MAAM,CAAC;mBACb,MAAM;2BACE,MAAM,KAAK,MAAM;CAezC,CAAC;AAEF,eAAO,MAAM,iBAAiB,8DAK3B,qBAAqB,KAAG,QAAQ,MAAM,CAWxC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consts.d.ts","sourceRoot":"/","sources":["services/token/consts.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAGnC,eAAO,MAAM,YAAY,EAAE;IACzB,CAAC,OAAO,EAAE,OAAO,GAAG;QAClB,SAAS,EAAE,SAAS,CAAC,MAAM,CAAC,CAAC;QAC7B,aAAa,EAAE,MAAM,CAAC;KACvB,CAAC;CAwBH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getAllowance.d.ts","sourceRoot":"/","sources":["services/token/getAllowance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,gBAAgB,EAAE,MAAM,SAAS,CAAC;AAIzD,eAAO,MAAM,YAAY,WACf,gBAAgB,KACvB,QAAQ,MAAM,CAiBhB,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { Signer } from 'ethers';
|
|
2
|
+
export declare enum ApprovalType {
|
|
3
|
+
LP = 0,
|
|
4
|
+
DEPOSIT = 1
|
|
5
|
+
}
|
|
6
|
+
export type ApproveTokenArgs = {
|
|
7
|
+
signer: Signer;
|
|
8
|
+
chainId: number;
|
|
9
|
+
token: string;
|
|
10
|
+
type: ApprovalType;
|
|
11
|
+
};
|
|
12
|
+
export type GetAllowanceArgs = {
|
|
13
|
+
signer: Signer;
|
|
14
|
+
chainId: number;
|
|
15
|
+
token: string;
|
|
16
|
+
type: ApprovalType;
|
|
17
|
+
};
|
|
18
|
+
export type GetERC20AllowanceArgs = {
|
|
19
|
+
walletAddress: string;
|
|
20
|
+
tokenAddress: string;
|
|
21
|
+
spenderAddress: string;
|
|
22
|
+
subject: Signer;
|
|
23
|
+
};
|
|
24
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"/","sources":["services/token/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,oBAAY,YAAY;IACtB,EAAE,IAAA;IACF,OAAO,IAAA;CACR;AAED,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,YAAY,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,gBAAgB,GAAG;IAC7B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,YAAY,CAAC;CACpB,CAAC;AAEF,MAAM,MAAM,qBAAqB,GAAG;IAClC,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,cAAc,EAAE,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export type Address = Lowercase<string>;
|
|
2
|
+
export type SupportedChainId = 80001 | 999;
|
|
3
|
+
export type ContractType = 'core_proxy' | 'passive_pool_proxy';
|
|
4
|
+
export declare const getAddress: (chainId: number, contractName: ContractType) => string;
|
|
5
|
+
//# sourceMappingURL=contractAddresses.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contractAddresses.d.ts","sourceRoot":"/","sources":["utils/contractAddresses.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,OAAO,GAAG,SAAS,CAAC,MAAM,CAAC,CAAC;AAGxC,MAAM,MAAM,gBAAgB,GAAG,KAAK,GAAG,GAAG,CAAC;AAE3C,MAAM,MAAM,YAAY,GAAG,YAAY,GAAG,oBAAoB,CAAC;AAe/D,eAAO,MAAM,UAAU,YACZ,MAAM,gBACD,YAAY,KACzB,MAaF,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.d.ts","sourceRoot":"/","sources":["utils/retry.ts"],"names":[],"mappings":"AAgBA,eAAO,MAAM,kBAAkB,wFAuB9B,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAddress = void 0;
|
|
4
|
+
var addresses = {
|
|
5
|
+
80001: {
|
|
6
|
+
core_proxy: '0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4',
|
|
7
|
+
passive_pool_proxy: '0xbc8fb7d6ceace18ce15d473b8ae5849e2394f5c5',
|
|
8
|
+
},
|
|
9
|
+
999: {
|
|
10
|
+
core_proxy: '0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4',
|
|
11
|
+
passive_pool_proxy: '0xbc8fb7d6ceace18ce15d473b8ae5849e2394f5c5',
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
// @todo update values for mainnet
|
|
15
|
+
var getAddress = function (chainId, contractName) {
|
|
16
|
+
var keyChainId = chainId.toString();
|
|
17
|
+
if (!Object.keys(addresses).includes(keyChainId)) {
|
|
18
|
+
return "Unspecified addresses for chain id ".concat(keyChainId);
|
|
19
|
+
}
|
|
20
|
+
var networkAddresses = addresses[chainId];
|
|
21
|
+
if (!Object.keys(networkAddresses).includes(contractName)) {
|
|
22
|
+
return "Unspecified address for ".concat(contractName, " contract");
|
|
23
|
+
}
|
|
24
|
+
return networkAddresses[contractName];
|
|
25
|
+
};
|
|
26
|
+
exports.getAddress = getAddress;
|
|
27
|
+
//# sourceMappingURL=contractAddresses.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"contractAddresses.js","sourceRoot":"/","sources":["utils/contractAddresses.ts"],"names":[],"mappings":";;;AAOA,IAAM,SAAS,GAA4D;IACzE,KAAK,EAAE;QACL,UAAU,EAAE,4CAA4C;QACxD,kBAAkB,EAAE,4CAA4C;KACjE;IACD,GAAG,EAAE;QACH,UAAU,EAAE,4CAA4C;QACxD,kBAAkB,EAAE,4CAA4C;KACjE;CACF,CAAC;AAEF,kCAAkC;AAE3B,IAAM,UAAU,GAAG,UACxB,OAAe,EACf,YAA0B;IAE1B,IAAM,UAAU,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAEtC,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC;QACjD,OAAO,6CAAsC,UAAU,CAAE,CAAC;IAC5D,CAAC;IAED,IAAM,gBAAgB,GAAG,SAAS,CAAC,OAA2B,CAAC,CAAC;IAChE,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,QAAQ,CAAC,YAAY,CAAC,EAAE,CAAC;QAC1D,OAAO,kCAA2B,YAAY,cAAW,CAAC;IAC5D,CAAC;IAED,OAAO,gBAAgB,CAAC,YAAY,CAAC,CAAC;AACxC,CAAC,CAAC;AAhBW,QAAA,UAAU,cAgBrB","sourcesContent":["export type Address = Lowercase<string>;\n\n// @todo update with proper chainId for mainnet\nexport type SupportedChainId = 80001 | 999;\n\nexport type ContractType = 'core_proxy' | 'passive_pool_proxy';\n\nconst addresses: Record<SupportedChainId, Record<ContractType, Address>> = {\n 80001: {\n core_proxy: '0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4',\n passive_pool_proxy: '0xbc8fb7d6ceace18ce15d473b8ae5849e2394f5c5',\n },\n 999: {\n core_proxy: '0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4',\n passive_pool_proxy: '0xbc8fb7d6ceace18ce15d473b8ae5849e2394f5c5',\n },\n};\n\n// @todo update values for mainnet\n\nexport const getAddress = (\n chainId: number,\n contractName: ContractType,\n): string => {\n const keyChainId = chainId.toString();\n\n if (!Object.keys(addresses).includes(keyChainId)) {\n return `Unspecified addresses for chain id ${keyChainId}`;\n }\n\n const networkAddresses = addresses[chainId as SupportedChainId];\n if (!Object.keys(networkAddresses).includes(contractName)) {\n return `Unspecified address for ${contractName} contract`;\n }\n\n return networkAddresses[contractName];\n};\n"]}
|
|
@@ -0,0 +1,94 @@
|
|
|
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.exponentialBackoff = void 0;
|
|
40
|
+
// It introduces a delay of ms milliseconds
|
|
41
|
+
function delay(ms) {
|
|
42
|
+
return new Promise(function (resolve) { return setTimeout(resolve, ms); });
|
|
43
|
+
}
|
|
44
|
+
// It returns a random number between [0, max-1]
|
|
45
|
+
function getRandomNumber(max) {
|
|
46
|
+
return Math.floor(Math.random() * max);
|
|
47
|
+
}
|
|
48
|
+
// It retries a call for a few times while waiting an exponential time before each new attempt.
|
|
49
|
+
// You pass the query and the number of attempts you want to use and it performs as follows:
|
|
50
|
+
// 1. Initiates the waiting time to 1s
|
|
51
|
+
// 2. Tries to fetch the data
|
|
52
|
+
// 3.1. If it succeeds, it returns the data
|
|
53
|
+
// 3.2. If it fails, it waits, it doubles the waiting time and go to point 2. if the number of attemps is not reached
|
|
54
|
+
var exponentialBackoff = function (query, attempts, factor) {
|
|
55
|
+
if (attempts === void 0) { attempts = 5; }
|
|
56
|
+
if (factor === void 0) { factor = 2; }
|
|
57
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
58
|
+
var waitingTime, attempt, data, error_1;
|
|
59
|
+
return __generator(this, function (_a) {
|
|
60
|
+
switch (_a.label) {
|
|
61
|
+
case 0:
|
|
62
|
+
waitingTime = 1000;
|
|
63
|
+
attempt = 0;
|
|
64
|
+
_a.label = 1;
|
|
65
|
+
case 1:
|
|
66
|
+
if (!(attempt < attempts)) return [3 /*break*/, 8];
|
|
67
|
+
_a.label = 2;
|
|
68
|
+
case 2:
|
|
69
|
+
_a.trys.push([2, 4, , 5]);
|
|
70
|
+
return [4 /*yield*/, query()];
|
|
71
|
+
case 3:
|
|
72
|
+
data = _a.sent();
|
|
73
|
+
return [2 /*return*/, data];
|
|
74
|
+
case 4:
|
|
75
|
+
error_1 = _a.sent();
|
|
76
|
+
if (attempt + 1 === attempts) {
|
|
77
|
+
throw error_1;
|
|
78
|
+
}
|
|
79
|
+
return [3 /*break*/, 5];
|
|
80
|
+
case 5: return [4 /*yield*/, delay(waitingTime + getRandomNumber(100))];
|
|
81
|
+
case 6:
|
|
82
|
+
_a.sent();
|
|
83
|
+
waitingTime *= factor;
|
|
84
|
+
_a.label = 7;
|
|
85
|
+
case 7:
|
|
86
|
+
attempt += 1;
|
|
87
|
+
return [3 /*break*/, 1];
|
|
88
|
+
case 8: throw new Error('Retry loop failed unexpectedly');
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
};
|
|
93
|
+
exports.exponentialBackoff = exponentialBackoff;
|
|
94
|
+
//# sourceMappingURL=retry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"retry.js","sourceRoot":"/","sources":["utils/retry.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,2CAA2C;AAC3C,SAAS,KAAK,CAAC,EAAU;IACvB,OAAO,IAAI,OAAO,CAAC,UAAC,OAAO,IAAK,OAAA,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,EAAvB,CAAuB,CAAC,CAAC;AAC3D,CAAC;AAED,gDAAgD;AAChD,SAAS,eAAe,CAAC,GAAW;IAClC,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,GAAG,CAAC,CAAC;AACzC,CAAC;AAED,+FAA+F;AAC/F,4FAA4F;AAC5F,sCAAsC;AACtC,6BAA6B;AAC7B,2CAA2C;AAC3C,qHAAqH;AAC9G,IAAM,kBAAkB,GAAG,UAChC,KAAuB,EACvB,QAAY,EACZ,MAAU;IADV,yBAAA,EAAA,YAAY;IACZ,uBAAA,EAAA,UAAU;;;;;;oBAEN,WAAW,GAAG,IAAI,CAAC;oBAEd,OAAO,GAAG,CAAC;;;yBAAE,CAAA,OAAO,GAAG,QAAQ,CAAA;;;;oBAEvB,qBAAM,KAAK,EAAE,EAAA;;oBAApB,IAAI,GAAG,SAAa;oBAE1B,sBAAO,IAAI,EAAC;;;oBAEZ,IAAI,OAAO,GAAG,CAAC,KAAK,QAAQ,EAAE,CAAC;wBAC7B,MAAM,OAAK,CAAC;oBACd,CAAC;;wBAGH,qBAAM,KAAK,CAAC,WAAW,GAAG,eAAe,CAAC,GAAG,CAAC,CAAC,EAAA;;oBAA/C,SAA+C,CAAC;oBAChD,WAAW,IAAI,MAAM,CAAC;;;oBAZkB,OAAO,IAAI,CAAC,CAAA;;wBAetD,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;;;;CACnD,CAAC;AAvBW,QAAA,kBAAkB,sBAuB7B","sourcesContent":["// It introduces a delay of ms milliseconds\nfunction delay(ms: number): Promise<unknown> {\n return new Promise((resolve) => setTimeout(resolve, ms));\n}\n\n// It returns a random number between [0, max-1]\nfunction getRandomNumber(max: number): number {\n return Math.floor(Math.random() * max);\n}\n\n// It retries a call for a few times while waiting an exponential time before each new attempt.\n// You pass the query and the number of attempts you want to use and it performs as follows:\n// 1. Initiates the waiting time to 1s\n// 2. Tries to fetch the data\n// 3.1. If it succeeds, it returns the data\n// 3.2. If it fails, it waits, it doubles the waiting time and go to point 2. if the number of attemps is not reached\nexport const exponentialBackoff = async <T = never>(\n query: () => Promise<T>,\n attempts = 5,\n factor = 2,\n): Promise<T> => {\n let waitingTime = 1000;\n\n for (let attempt = 0; attempt < attempts; attempt += 1) {\n try {\n const data = await query();\n\n return data;\n } catch (error) {\n if (attempt + 1 === attempts) {\n throw error;\n }\n }\n\n await delay(waitingTime + getRandomNumber(100));\n waitingTime *= factor;\n }\n\n throw new Error('Retry loop failed unexpectedly');\n};\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/sdk",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"ethers": "6.9.0"
|
|
33
33
|
},
|
|
34
34
|
"packageManager": "pnpm@8.10.4",
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "1b1ef20464e203db8fc4fbf395e4218fd06119ae"
|
|
36
36
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { ApprovalType, ApproveTokenArgs } from './types';
|
|
2
|
+
import { getERC20Allowance, getERC20TokenContract } from './common';
|
|
3
|
+
import { getAddress } from '../../utils/contractAddresses';
|
|
4
|
+
|
|
5
|
+
export const approveTokenSpending = async (
|
|
6
|
+
params: ApproveTokenArgs,
|
|
7
|
+
): Promise<number> => {
|
|
8
|
+
const underLyingTokenAddress = params.token;
|
|
9
|
+
|
|
10
|
+
const tokenContract = getERC20TokenContract(
|
|
11
|
+
underLyingTokenAddress,
|
|
12
|
+
params.signer,
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
const contractProxyAddress =
|
|
16
|
+
params.type === ApprovalType.DEPOSIT
|
|
17
|
+
? getAddress(params.chainId, 'core_proxy')
|
|
18
|
+
: getAddress(params.chainId, 'passive_pool_proxy');
|
|
19
|
+
|
|
20
|
+
// @todo validate if maxAllowance should be passed from UI
|
|
21
|
+
const maxUint256Bn = BigInt(
|
|
22
|
+
'115792089237316195423570985008687907853269984665640564039457584007913129639935',
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
const approvalTransaction = await tokenContract
|
|
26
|
+
.approve(contractProxyAddress, maxUint256Bn)
|
|
27
|
+
.catch((error) => {
|
|
28
|
+
console.warn('Transaction Confirmation Error');
|
|
29
|
+
console.error(error);
|
|
30
|
+
throw new Error('Transaction Confirmation Error');
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
try {
|
|
34
|
+
await approvalTransaction.wait();
|
|
35
|
+
} catch (error) {
|
|
36
|
+
console.warn('Token approval failed');
|
|
37
|
+
throw new Error('Token approval failed');
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
try {
|
|
41
|
+
return await getERC20Allowance({
|
|
42
|
+
walletAddress: await params.signer.getAddress(),
|
|
43
|
+
tokenAddress: underLyingTokenAddress,
|
|
44
|
+
spenderAddress: contractProxyAddress,
|
|
45
|
+
subject: params.signer,
|
|
46
|
+
});
|
|
47
|
+
} catch (error) {
|
|
48
|
+
console.warn('Fetching allowance failed');
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return 0;
|
|
52
|
+
};
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Contract, ethers, Signer } from 'ethers';
|
|
2
|
+
import { tokenDetails } from './consts';
|
|
3
|
+
import { exponentialBackoff } from '../../utils/retry';
|
|
4
|
+
import { GetERC20AllowanceArgs } from './types';
|
|
5
|
+
|
|
6
|
+
export const descale = (tokenDecimals: number) => {
|
|
7
|
+
const f = (value: bigint) => {
|
|
8
|
+
return Number(ethers.formatUnits(value.toString(), tokenDecimals));
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
return f;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const scale = (tokenDecimals: number) => {
|
|
15
|
+
const f = (value: number) => {
|
|
16
|
+
return BigInt(
|
|
17
|
+
ethers.parseUnits(value.toFixed(tokenDecimals), tokenDecimals),
|
|
18
|
+
);
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
return f;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const getERC20TokenContract = (
|
|
25
|
+
tokenAddress: string,
|
|
26
|
+
subject: Signer,
|
|
27
|
+
): Contract => {
|
|
28
|
+
const abi: string[] = [
|
|
29
|
+
`function approve(address, uint256) external returns (bool)`,
|
|
30
|
+
`function balanceOf(address) external view returns (uint256)`,
|
|
31
|
+
`function allowance(address,address) external view returns (uint256)`,
|
|
32
|
+
`function approve(address,uint256) external returns (bool)`,
|
|
33
|
+
];
|
|
34
|
+
|
|
35
|
+
const contract = new Contract(tokenAddress, abi, subject);
|
|
36
|
+
|
|
37
|
+
return contract;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export type Address = Lowercase<string>;
|
|
41
|
+
|
|
42
|
+
export const convertToAddress = (str: string): Address => {
|
|
43
|
+
return str.toLowerCase() as Address;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export const getTokenDetails = (
|
|
47
|
+
caseSensitiveAddress: string,
|
|
48
|
+
): {
|
|
49
|
+
tokenName: Uppercase<string>;
|
|
50
|
+
tokenDecimals: number;
|
|
51
|
+
tokenDescaler: (value: bigint) => number;
|
|
52
|
+
} => {
|
|
53
|
+
const address = convertToAddress(caseSensitiveAddress);
|
|
54
|
+
|
|
55
|
+
if (!Object.keys(tokenDetails).includes(address)) {
|
|
56
|
+
throw new Error(`Token details not found for ${address}.`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const { tokenName, tokenDecimals } = tokenDetails[address];
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
tokenName,
|
|
63
|
+
tokenDecimals,
|
|
64
|
+
tokenDescaler: descale(tokenDecimals),
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const getERC20Allowance = async ({
|
|
69
|
+
walletAddress,
|
|
70
|
+
tokenAddress,
|
|
71
|
+
spenderAddress,
|
|
72
|
+
subject,
|
|
73
|
+
}: GetERC20AllowanceArgs): Promise<number> => {
|
|
74
|
+
const tokenContract = getERC20TokenContract(tokenAddress, subject);
|
|
75
|
+
const { tokenDecimals } = getTokenDetails(tokenAddress);
|
|
76
|
+
|
|
77
|
+
const allowance = await exponentialBackoff(() =>
|
|
78
|
+
tokenContract.allowance(walletAddress, spenderAddress),
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
return BigInt(allowance) > scale(tokenDecimals)(Number.MAX_SAFE_INTEGER)
|
|
82
|
+
? Number.MAX_SAFE_INTEGER
|
|
83
|
+
: descale(tokenDecimals)(allowance);
|
|
84
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { Address } from './common';
|
|
2
|
+
|
|
3
|
+
// @todo update values for mainnet
|
|
4
|
+
export const tokenDetails: {
|
|
5
|
+
[address: Address]: {
|
|
6
|
+
tokenName: Uppercase<string>;
|
|
7
|
+
tokenDecimals: number;
|
|
8
|
+
};
|
|
9
|
+
} = {
|
|
10
|
+
// ====== USDC ======
|
|
11
|
+
// mumbai
|
|
12
|
+
'0x52d800ca262522580cebad275395ca6e7598c014': {
|
|
13
|
+
tokenName: 'USDC',
|
|
14
|
+
tokenDecimals: 6,
|
|
15
|
+
},
|
|
16
|
+
// mainnet
|
|
17
|
+
'--': {
|
|
18
|
+
tokenName: 'USDC',
|
|
19
|
+
tokenDecimals: 6,
|
|
20
|
+
},
|
|
21
|
+
// ====== wETH ======
|
|
22
|
+
// mumbai
|
|
23
|
+
'0xc199807af4fedb02ee567ed0feb814a077de4802': {
|
|
24
|
+
tokenName: 'WETH',
|
|
25
|
+
tokenDecimals: 18,
|
|
26
|
+
},
|
|
27
|
+
// mainnet
|
|
28
|
+
'---': {
|
|
29
|
+
tokenName: 'WETH',
|
|
30
|
+
tokenDecimals: 18,
|
|
31
|
+
},
|
|
32
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ApprovalType, GetAllowanceArgs } from './types';
|
|
2
|
+
import { getERC20Allowance } from './common';
|
|
3
|
+
import { getAddress } from '../../utils/contractAddresses';
|
|
4
|
+
|
|
5
|
+
export const getAllowance = async (
|
|
6
|
+
params: GetAllowanceArgs,
|
|
7
|
+
): Promise<number> => {
|
|
8
|
+
const underLyingTokenAddress = params.token;
|
|
9
|
+
|
|
10
|
+
const contractProxyAddress =
|
|
11
|
+
params.type === ApprovalType.DEPOSIT
|
|
12
|
+
? getAddress(params.chainId, 'core_proxy')
|
|
13
|
+
: getAddress(params.chainId, 'passive_pool_proxy');
|
|
14
|
+
const walletAddress: string = await params.signer.getAddress();
|
|
15
|
+
|
|
16
|
+
const allowance = await getERC20Allowance({
|
|
17
|
+
walletAddress,
|
|
18
|
+
tokenAddress: underLyingTokenAddress,
|
|
19
|
+
spenderAddress: contractProxyAddress,
|
|
20
|
+
subject: params.signer,
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
return allowance;
|
|
24
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Signer } from 'ethers';
|
|
2
|
+
|
|
3
|
+
export enum ApprovalType {
|
|
4
|
+
LP,
|
|
5
|
+
DEPOSIT,
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type ApproveTokenArgs = {
|
|
9
|
+
signer: Signer;
|
|
10
|
+
chainId: number;
|
|
11
|
+
token: string;
|
|
12
|
+
type: ApprovalType;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type GetAllowanceArgs = {
|
|
16
|
+
signer: Signer;
|
|
17
|
+
chainId: number;
|
|
18
|
+
token: string;
|
|
19
|
+
type: ApprovalType;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type GetERC20AllowanceArgs = {
|
|
23
|
+
walletAddress: string;
|
|
24
|
+
tokenAddress: string;
|
|
25
|
+
spenderAddress: string;
|
|
26
|
+
subject: Signer;
|
|
27
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export type Address = Lowercase<string>;
|
|
2
|
+
|
|
3
|
+
// @todo update with proper chainId for mainnet
|
|
4
|
+
export type SupportedChainId = 80001 | 999;
|
|
5
|
+
|
|
6
|
+
export type ContractType = 'core_proxy' | 'passive_pool_proxy';
|
|
7
|
+
|
|
8
|
+
const addresses: Record<SupportedChainId, Record<ContractType, Address>> = {
|
|
9
|
+
80001: {
|
|
10
|
+
core_proxy: '0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4',
|
|
11
|
+
passive_pool_proxy: '0xbc8fb7d6ceace18ce15d473b8ae5849e2394f5c5',
|
|
12
|
+
},
|
|
13
|
+
999: {
|
|
14
|
+
core_proxy: '0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4',
|
|
15
|
+
passive_pool_proxy: '0xbc8fb7d6ceace18ce15d473b8ae5849e2394f5c5',
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
// @todo update values for mainnet
|
|
20
|
+
|
|
21
|
+
export const getAddress = (
|
|
22
|
+
chainId: number,
|
|
23
|
+
contractName: ContractType,
|
|
24
|
+
): string => {
|
|
25
|
+
const keyChainId = chainId.toString();
|
|
26
|
+
|
|
27
|
+
if (!Object.keys(addresses).includes(keyChainId)) {
|
|
28
|
+
return `Unspecified addresses for chain id ${keyChainId}`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const networkAddresses = addresses[chainId as SupportedChainId];
|
|
32
|
+
if (!Object.keys(networkAddresses).includes(contractName)) {
|
|
33
|
+
return `Unspecified address for ${contractName} contract`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return networkAddresses[contractName];
|
|
37
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// It introduces a delay of ms milliseconds
|
|
2
|
+
function delay(ms: number): Promise<unknown> {
|
|
3
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// It returns a random number between [0, max-1]
|
|
7
|
+
function getRandomNumber(max: number): number {
|
|
8
|
+
return Math.floor(Math.random() * max);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
// It retries a call for a few times while waiting an exponential time before each new attempt.
|
|
12
|
+
// You pass the query and the number of attempts you want to use and it performs as follows:
|
|
13
|
+
// 1. Initiates the waiting time to 1s
|
|
14
|
+
// 2. Tries to fetch the data
|
|
15
|
+
// 3.1. If it succeeds, it returns the data
|
|
16
|
+
// 3.2. If it fails, it waits, it doubles the waiting time and go to point 2. if the number of attemps is not reached
|
|
17
|
+
export const exponentialBackoff = async <T = never>(
|
|
18
|
+
query: () => Promise<T>,
|
|
19
|
+
attempts = 5,
|
|
20
|
+
factor = 2,
|
|
21
|
+
): Promise<T> => {
|
|
22
|
+
let waitingTime = 1000;
|
|
23
|
+
|
|
24
|
+
for (let attempt = 0; attempt < attempts; attempt += 1) {
|
|
25
|
+
try {
|
|
26
|
+
const data = await query();
|
|
27
|
+
|
|
28
|
+
return data;
|
|
29
|
+
} catch (error) {
|
|
30
|
+
if (attempt + 1 === attempts) {
|
|
31
|
+
throw error;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
await delay(waitingTime + getRandomNumber(100));
|
|
36
|
+
waitingTime *= factor;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
throw new Error('Retry loop failed unexpectedly');
|
|
40
|
+
};
|