@reyaxyz/sdk 0.25.1 → 0.26.0
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/lp/bridgeAndDepositPassivePool.js +1 -2
- package/dist/services/lp/bridgeAndDepositPassivePool.js.map +1 -1
- package/dist/services/lp/simulateWithdrawPassivePoolAndBridge.js +2 -0
- package/dist/services/lp/simulateWithdrawPassivePoolAndBridge.js.map +1 -1
- package/dist/services/lp/transferFromMAToPool.js +1 -2
- package/dist/services/lp/transferFromMAToPool.js.map +1 -1
- package/dist/services/lp/types.js.map +1 -1
- package/dist/services/margin-accounts/bridgeAndDepositExistingMA.js +1 -2
- package/dist/services/margin-accounts/bridgeAndDepositExistingMA.js.map +1 -1
- package/dist/services/margin-accounts/bridgeAndDepositNewMA.js +1 -2
- package/dist/services/margin-accounts/bridgeAndDepositNewMA.js.map +1 -1
- package/dist/services/margin-accounts/deposit.js +2 -3
- package/dist/services/margin-accounts/deposit.js.map +1 -1
- package/dist/services/margin-accounts/transferMarginBetweenAccounts.js +1 -2
- package/dist/services/margin-accounts/transferMarginBetweenAccounts.js.map +1 -1
- package/dist/services/margin-accounts/withdraw.js +2 -3
- package/dist/services/margin-accounts/withdraw.js.map +1 -1
- package/dist/services/margin-accounts/withdrawMAAndBridge.js +1 -2
- package/dist/services/margin-accounts/withdrawMAAndBridge.js.map +1 -1
- package/dist/services/token/approve.js +1 -1
- package/dist/services/token/approve.js.map +1 -1
- package/dist/services/token/getAllowance.js +1 -1
- package/dist/services/token/getAllowance.js.map +1 -1
- package/dist/services/token/getBalanceByTokenAddress.js +1 -1
- package/dist/services/token/getBalanceByTokenAddress.js.map +1 -1
- package/dist/services/token/types.js.map +1 -1
- package/dist/types/services/lp/simulateWithdrawPassivePoolAndBridge.d.ts.map +1 -1
- package/dist/types/services/lp/transferFromMAToPool.d.ts.map +1 -1
- package/dist/types/services/lp/types.d.ts +2 -1
- package/dist/types/services/lp/types.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/deposit.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/transferMarginBetweenAccounts.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/withdraw.d.ts.map +1 -1
- package/dist/types/services/token/types.d.ts +1 -7
- package/dist/types/services/token/types.d.ts.map +1 -1
- package/package.json +3 -3
- package/src/services/lp/bridgeAndDepositPassivePool.ts +1 -1
- package/src/services/lp/simulateWithdrawPassivePoolAndBridge.ts +8 -1
- package/src/services/lp/transferFromMAToPool.ts +5 -2
- package/src/services/lp/types.ts +2 -1
- package/src/services/margin-accounts/bridgeAndDepositExistingMA.ts +1 -1
- package/src/services/margin-accounts/bridgeAndDepositNewMA.ts +1 -1
- package/src/services/margin-accounts/deposit.ts +1 -2
- package/src/services/margin-accounts/transferMarginBetweenAccounts.ts +1 -2
- package/src/services/margin-accounts/withdraw.ts +1 -2
- package/src/services/margin-accounts/withdrawMAAndBridge.ts +1 -1
- package/src/services/token/approve.ts +1 -1
- package/src/services/token/getAllowance.ts +1 -1
- package/src/services/token/getBalanceByTokenAddress.ts +1 -1
- package/src/services/token/types.ts +1 -8
- package/dist/services/token/common.js +0 -114
- package/dist/services/token/common.js.map +0 -1
- package/dist/services/token/consts.js +0 -52
- package/dist/services/token/consts.js.map +0 -1
- package/dist/types/services/token/common.d.ts +0 -18
- package/dist/types/services/token/common.d.ts.map +0 -1
- package/dist/types/services/token/consts.d.ts +0 -8
- package/dist/types/services/token/consts.d.ts.map +0 -1
- package/dist/types/utils/retry.d.ts +0 -2
- package/dist/types/utils/retry.d.ts.map +0 -1
- package/dist/utils/retry.js +0 -94
- package/dist/utils/retry.js.map +0 -1
- package/src/services/token/common.ts +0 -89
- package/src/services/token/consts.ts +0 -57
- package/src/utils/retry.ts +0 -40
|
@@ -3,10 +3,10 @@ import {
|
|
|
3
3
|
getSocketDepositFeesAsBigInt,
|
|
4
4
|
getSocketVault,
|
|
5
5
|
scale,
|
|
6
|
+
getTokenDetails,
|
|
6
7
|
} from '@reyaxyz/common';
|
|
7
8
|
import { ContractType, getAddress } from '../../utils/contractAddresses';
|
|
8
9
|
import { executeTransaction } from '../executeTransaction';
|
|
9
|
-
import { getTokenDetails } from '../token/common';
|
|
10
10
|
import { encodeBridgeAndDepositNewMA } from './encode';
|
|
11
11
|
import {
|
|
12
12
|
BridgeAndDepositNewMAParams,
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { DepositParams, DepositResult } from './types';
|
|
2
2
|
import { encodeDeposit } from './encode';
|
|
3
3
|
import { executeTransaction } from '../executeTransaction';
|
|
4
|
-
import { getTokenDetails } from '../token/common';
|
|
5
4
|
import { ContractType } from '../../utils/contractAddresses';
|
|
6
|
-
import { scale } from '@reyaxyz/common';
|
|
5
|
+
import { scale, getTokenDetails } from '@reyaxyz/common';
|
|
7
6
|
|
|
8
7
|
export const deposit = async (
|
|
9
8
|
params: DepositParams,
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { scale } from '@reyaxyz/common';
|
|
1
|
+
import { scale, getTokenDetails } from '@reyaxyz/common';
|
|
2
2
|
import { ContractType } from '../../utils/contractAddresses';
|
|
3
3
|
import { executeTransaction } from '../executeTransaction';
|
|
4
|
-
import { getTokenDetails } from '../token/common';
|
|
5
4
|
import { encodeTransferMargin } from './encode';
|
|
6
5
|
import {
|
|
7
6
|
TransferMarginBetweenAccountsParams,
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { WithdrawParams, WithdrawResult } from './types';
|
|
2
2
|
import { encodeWithdraw } from './encode';
|
|
3
3
|
import { executeTransaction } from '../executeTransaction';
|
|
4
|
-
import { getTokenDetails } from '../token/common';
|
|
5
4
|
import { ContractType } from '../../utils/contractAddresses';
|
|
6
|
-
import { scale } from '@reyaxyz/common';
|
|
5
|
+
import { scale, getTokenDetails } from '@reyaxyz/common';
|
|
7
6
|
|
|
8
7
|
export const withdraw = async (
|
|
9
8
|
params: WithdrawParams,
|
|
@@ -3,11 +3,11 @@ import {
|
|
|
3
3
|
getSocketConnector,
|
|
4
4
|
getSocketController,
|
|
5
5
|
scale,
|
|
6
|
+
getTokenDetails,
|
|
6
7
|
} from '@reyaxyz/common';
|
|
7
8
|
import { ContractType } from '../../utils/contractAddresses';
|
|
8
9
|
import { executeTransaction } from '../executeTransaction';
|
|
9
10
|
import { signCoreCommands } from '../sign';
|
|
10
|
-
import { getTokenDetails } from '../token/common';
|
|
11
11
|
import { encodeWithdrawMAAndBridge } from './encode';
|
|
12
12
|
import {
|
|
13
13
|
WithdrawMAAndBridgeParams,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ApprovalType, ApproveTokenParams } from './types';
|
|
2
|
-
import { getERC20Allowance, getERC20TokenContract } from '
|
|
2
|
+
import { getERC20Allowance, getERC20TokenContract } from '@reyaxyz/common';
|
|
3
3
|
import { ContractType, getAddress } from '../../utils/contractAddresses';
|
|
4
4
|
|
|
5
5
|
export const approveTokenSpending = async (
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ApprovalType, GetAllowanceParams } from './types';
|
|
2
|
-
import { getERC20Allowance } from '
|
|
2
|
+
import { getERC20Allowance } from '@reyaxyz/common';
|
|
3
3
|
import { ContractType, getAddress } from '../../utils/contractAddresses';
|
|
4
4
|
|
|
5
5
|
export const getAllowance = async (
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { JsonRpcSigner, Signer } from 'ethers';
|
|
2
|
-
import {
|
|
2
|
+
import { TokenEntity } from '@reyaxyz/common';
|
|
3
3
|
import { MoneyInOutChainId, ReyaChainId } from '@reyaxyz/common';
|
|
4
4
|
|
|
5
5
|
export enum ApprovalType {
|
|
@@ -19,13 +19,6 @@ export type GetAllowanceParams = {
|
|
|
19
19
|
type: ApprovalType;
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
-
export type GetERC20AllowanceParams = {
|
|
23
|
-
walletAddress: string;
|
|
24
|
-
tokenAddress: LpPoolEntity['tokenAddress'];
|
|
25
|
-
spenderAddress: string;
|
|
26
|
-
subject: Signer;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
22
|
export type GetBalanceByTokenAddressParams = {
|
|
30
23
|
signer: Signer | JsonRpcSigner;
|
|
31
24
|
tokenAddress: TokenEntity['address'];
|
|
@@ -1,114 +0,0 @@
|
|
|
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.getERC20Balance = exports.getERC20Allowance = exports.getTokenDetails = exports.convertToAddress = exports.getERC20TokenContract = void 0;
|
|
40
|
-
var ethers_1 = require("ethers");
|
|
41
|
-
var consts_1 = require("./consts");
|
|
42
|
-
var retry_1 = require("../../utils/retry");
|
|
43
|
-
var common_1 = require("@reyaxyz/common");
|
|
44
|
-
var getERC20TokenContract = function (tokenAddress, subject) {
|
|
45
|
-
var abi = [
|
|
46
|
-
"function approve(address, uint256) external returns (bool)",
|
|
47
|
-
"function balanceOf(address) external view returns (uint256)",
|
|
48
|
-
"function allowance(address,address) external view returns (uint256)",
|
|
49
|
-
"function approve(address,uint256) external returns (bool)",
|
|
50
|
-
];
|
|
51
|
-
var contract = new ethers_1.Contract(tokenAddress, abi, subject);
|
|
52
|
-
return contract;
|
|
53
|
-
};
|
|
54
|
-
exports.getERC20TokenContract = getERC20TokenContract;
|
|
55
|
-
var convertToAddress = function (str) {
|
|
56
|
-
return str.toLowerCase();
|
|
57
|
-
};
|
|
58
|
-
exports.convertToAddress = convertToAddress;
|
|
59
|
-
var getTokenDetails = function (caseSensitiveAddress) {
|
|
60
|
-
var address = (0, exports.convertToAddress)(caseSensitiveAddress);
|
|
61
|
-
if (!Object.keys(consts_1.tokenDetails).includes(address)) {
|
|
62
|
-
throw new Error("Token details not found for ".concat(address, "."));
|
|
63
|
-
}
|
|
64
|
-
var _a = consts_1.tokenDetails[address], tokenName = _a.tokenName, tokenDecimals = _a.tokenDecimals;
|
|
65
|
-
return {
|
|
66
|
-
tokenName: tokenName,
|
|
67
|
-
tokenDecimals: tokenDecimals,
|
|
68
|
-
tokenDescaler: (0, common_1.descale)(tokenDecimals),
|
|
69
|
-
};
|
|
70
|
-
};
|
|
71
|
-
exports.getTokenDetails = getTokenDetails;
|
|
72
|
-
var getERC20Allowance = function (_a) {
|
|
73
|
-
var walletAddress = _a.walletAddress, tokenAddress = _a.tokenAddress, spenderAddress = _a.spenderAddress, subject = _a.subject;
|
|
74
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
75
|
-
var tokenContract, tokenDecimals, allowance;
|
|
76
|
-
return __generator(this, function (_b) {
|
|
77
|
-
switch (_b.label) {
|
|
78
|
-
case 0:
|
|
79
|
-
tokenContract = (0, exports.getERC20TokenContract)(tokenAddress, subject);
|
|
80
|
-
tokenDecimals = (0, exports.getTokenDetails)(tokenAddress).tokenDecimals;
|
|
81
|
-
return [4 /*yield*/, (0, retry_1.exponentialBackoff)(function () {
|
|
82
|
-
return tokenContract.allowance(walletAddress, spenderAddress);
|
|
83
|
-
})];
|
|
84
|
-
case 1:
|
|
85
|
-
allowance = _b.sent();
|
|
86
|
-
return [2 /*return*/, BigInt(allowance) > (0, common_1.scale)(tokenDecimals)(Number.MAX_SAFE_INTEGER)
|
|
87
|
-
? Number.MAX_SAFE_INTEGER
|
|
88
|
-
: (0, common_1.descale)(tokenDecimals)(allowance)];
|
|
89
|
-
}
|
|
90
|
-
});
|
|
91
|
-
});
|
|
92
|
-
};
|
|
93
|
-
exports.getERC20Allowance = getERC20Allowance;
|
|
94
|
-
var getERC20Balance = function (_a) {
|
|
95
|
-
var tokenAddress = _a.tokenAddress, walletAddress = _a.walletAddress, subject = _a.subject;
|
|
96
|
-
return __awaiter(void 0, void 0, void 0, function () {
|
|
97
|
-
var token, tokenDescaler, currentBalance;
|
|
98
|
-
return __generator(this, function (_b) {
|
|
99
|
-
switch (_b.label) {
|
|
100
|
-
case 0:
|
|
101
|
-
token = (0, exports.getERC20TokenContract)(tokenAddress, subject);
|
|
102
|
-
tokenDescaler = (0, exports.getTokenDetails)(tokenAddress).tokenDescaler;
|
|
103
|
-
return [4 /*yield*/, (0, retry_1.exponentialBackoff)(function () {
|
|
104
|
-
return token.balanceOf(walletAddress);
|
|
105
|
-
})];
|
|
106
|
-
case 1:
|
|
107
|
-
currentBalance = _b.sent();
|
|
108
|
-
return [2 /*return*/, tokenDescaler(currentBalance)];
|
|
109
|
-
}
|
|
110
|
-
});
|
|
111
|
-
});
|
|
112
|
-
};
|
|
113
|
-
exports.getERC20Balance = getERC20Balance;
|
|
114
|
-
//# sourceMappingURL=common.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"/","sources":["services/token/common.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAA0C;AAC1C,mCAAwC;AACxC,2CAAuD;AAEvD,0CAAiD;AAE1C,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,gBAAO,EAAC,aAAa,CAAC;KACtC,CAAC;AACJ,CAAC,CAAC;AApBW,QAAA,eAAe,mBAoB1B;AAEK,IAAM,iBAAiB,GAAG,UAAO,EAKd;QAJxB,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,cAAK,EAAC,aAAa,CAAC,CAAC,MAAM,CAAC,gBAAgB,CAAC;4BACtE,CAAC,CAAC,MAAM,CAAC,gBAAgB;4BACzB,CAAC,CAAC,IAAA,gBAAO,EAAC,aAAa,CAAC,CAAC,SAAS,CAAC,EAAC;;;;CACvC,CAAC;AAhBW,QAAA,iBAAiB,qBAgB5B;AAQK,IAAM,eAAe,GAAG,UAAO,EAIhB;QAHpB,YAAY,kBAAA,EACZ,aAAa,mBAAA,EACb,OAAO,aAAA;;;;;;oBAED,KAAK,GAAG,IAAA,6BAAqB,EAAC,YAAY,EAAE,OAAO,CAAC,CAAC;oBAEnD,aAAa,GAAK,IAAA,uBAAe,EAAC,YAAY,CAAC,cAAlC,CAAmC;oBAEjC,qBAAM,IAAA,0BAAkB,EAAC;4BAC9C,OAAA,KAAK,CAAC,SAAS,CAAC,aAAa,CAAC;wBAA9B,CAA8B,CAC/B,EAAA;;oBAFK,cAAc,GAAG,SAEtB;oBAED,sBAAO,aAAa,CAAC,cAAc,CAAC,EAAC;;;;CACtC,CAAC;AAdW,QAAA,eAAe,mBAc1B","sourcesContent":["import { Contract, Signer } from 'ethers';\nimport { tokenDetails } from './consts';\nimport { exponentialBackoff } from '../../utils/retry';\nimport { GetERC20AllowanceParams } from './types';\nimport { descale, scale } from '@reyaxyz/common';\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}: GetERC20AllowanceParams): 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\nexport type GetERC20BalanceArgs = {\n tokenAddress: string;\n walletAddress: string;\n subject: Signer;\n};\n\nexport const getERC20Balance = async ({\n tokenAddress,\n walletAddress,\n subject,\n}: GetERC20BalanceArgs): Promise<number> => {\n const token = getERC20TokenContract(tokenAddress, subject);\n\n const { tokenDescaler } = getTokenDetails(tokenAddress);\n\n const currentBalance = await exponentialBackoff(() =>\n token.balanceOf(walletAddress),\n );\n\n return tokenDescaler(currentBalance);\n};\n"]}
|
|
@@ -1,52 +0,0 @@
|
|
|
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
|
-
// reya cronos
|
|
13
|
-
'0x51b0b7f083e563d5cf9d0b5f3916b59fc49c7e5f': {
|
|
14
|
-
tokenName: 'USDC',
|
|
15
|
-
tokenDecimals: 6,
|
|
16
|
-
},
|
|
17
|
-
// ====== wETH ======
|
|
18
|
-
// mumbai
|
|
19
|
-
'0xc199807af4fedb02ee567ed0feb814a077de4802': {
|
|
20
|
-
tokenName: 'WETH',
|
|
21
|
-
tokenDecimals: 18,
|
|
22
|
-
},
|
|
23
|
-
// reya cronos
|
|
24
|
-
'0xcbdba2ac8f840711948ccd7a1b9ec0226d641693': {
|
|
25
|
-
tokenName: 'WETH',
|
|
26
|
-
tokenDecimals: 18,
|
|
27
|
-
},
|
|
28
|
-
// ====== wBTC ======
|
|
29
|
-
// mumbai
|
|
30
|
-
'0x2fa2e7a6deb7bb51b625336dbe1da23511914a8a': {
|
|
31
|
-
tokenName: 'WBTC',
|
|
32
|
-
tokenDecimals: 8,
|
|
33
|
-
},
|
|
34
|
-
// mainnet
|
|
35
|
-
'0xaf21abab55a7c0ab143e0379a3c01054e4ebb514': {
|
|
36
|
-
tokenName: 'WBTC',
|
|
37
|
-
tokenDecimals: 8,
|
|
38
|
-
},
|
|
39
|
-
// ====== USDT ======
|
|
40
|
-
// reya cronos
|
|
41
|
-
'0xeba6f8d7d4d4787f29441db1164548299ef4641f': {
|
|
42
|
-
tokenName: 'USDT',
|
|
43
|
-
tokenDecimals: 6,
|
|
44
|
-
},
|
|
45
|
-
// ====== dai ======
|
|
46
|
-
// reya cronos
|
|
47
|
-
'0x0d4fad8b258a467286fc2419ffc19015fdcae7f9': {
|
|
48
|
-
tokenName: 'USDT',
|
|
49
|
-
tokenDecimals: 18,
|
|
50
|
-
},
|
|
51
|
-
};
|
|
52
|
-
//# sourceMappingURL=consts.js.map
|
|
@@ -1 +0,0 @@
|
|
|
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,cAAc;IACd,4CAA4C,EAAE;QAC5C,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,cAAc;IACd,4CAA4C,EAAE;QAC5C,SAAS,EAAE,MAAM;QACjB,aAAa,EAAE,EAAE;KAClB;IAED,qBAAqB;IACrB,SAAS;IACT,4CAA4C,EAAE;QAC5C,SAAS,EAAE,MAAM;QACjB,aAAa,EAAE,CAAC;KACjB;IACD,UAAU;IACV,4CAA4C,EAAE;QAC5C,SAAS,EAAE,MAAM;QACjB,aAAa,EAAE,CAAC;KACjB;IACD,qBAAqB;IACrB,cAAc;IACd,4CAA4C,EAAE;QAC5C,SAAS,EAAE,MAAM;QACjB,aAAa,EAAE,CAAC;KACjB;IAED,oBAAoB;IACpB,cAAc;IACd,4CAA4C,EAAE;QAC5C,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 // reya cronos\n '0x51b0b7f083e563d5cf9d0b5f3916b59fc49c7e5f': {\n tokenName: 'USDC',\n tokenDecimals: 6,\n },\n // ====== wETH ======\n // mumbai\n '0xc199807af4fedb02ee567ed0feb814a077de4802': {\n tokenName: 'WETH',\n tokenDecimals: 18,\n },\n // reya cronos\n '0xcbdba2ac8f840711948ccd7a1b9ec0226d641693': {\n tokenName: 'WETH',\n tokenDecimals: 18,\n },\n\n // ====== wBTC ======\n // mumbai\n '0x2fa2e7a6deb7bb51b625336dbe1da23511914a8a': {\n tokenName: 'WBTC',\n tokenDecimals: 8,\n },\n // mainnet\n '0xaf21abab55a7c0ab143e0379a3c01054e4ebb514': {\n tokenName: 'WBTC',\n tokenDecimals: 8,\n },\n // ====== USDT ======\n // reya cronos\n '0xeba6f8d7d4d4787f29441db1164548299ef4641f': {\n tokenName: 'USDT',\n tokenDecimals: 6,\n },\n\n // ====== dai ======\n // reya cronos\n '0x0d4fad8b258a467286fc2419ffc19015fdcae7f9': {\n tokenName: 'USDT',\n tokenDecimals: 18,\n },\n};\n"]}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { Contract, Signer } from 'ethers';
|
|
2
|
-
import { GetERC20AllowanceParams } from './types';
|
|
3
|
-
export declare const getERC20TokenContract: (tokenAddress: string, subject: Signer) => Contract;
|
|
4
|
-
export type Address = Lowercase<string>;
|
|
5
|
-
export declare const convertToAddress: (str: string) => Address;
|
|
6
|
-
export declare const getTokenDetails: (caseSensitiveAddress: string) => {
|
|
7
|
-
tokenName: Uppercase<string>;
|
|
8
|
-
tokenDecimals: number;
|
|
9
|
-
tokenDescaler: (value: bigint) => number;
|
|
10
|
-
};
|
|
11
|
-
export declare const getERC20Allowance: ({ walletAddress, tokenAddress, spenderAddress, subject, }: GetERC20AllowanceParams) => Promise<number>;
|
|
12
|
-
export type GetERC20BalanceArgs = {
|
|
13
|
-
tokenAddress: string;
|
|
14
|
-
walletAddress: string;
|
|
15
|
-
subject: Signer;
|
|
16
|
-
};
|
|
17
|
-
export declare const getERC20Balance: ({ tokenAddress, walletAddress, subject, }: GetERC20BalanceArgs) => Promise<number>;
|
|
18
|
-
//# sourceMappingURL=common.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"common.d.ts","sourceRoot":"/","sources":["services/token/common.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAG1C,OAAO,EAAE,uBAAuB,EAAE,MAAM,SAAS,CAAC;AAGlD,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,uBAAuB,KAAG,QAAQ,MAAM,CAW1C,CAAC;AAEF,MAAM,MAAM,mBAAmB,GAAG;IAChC,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,eAAO,MAAM,eAAe,8CAIzB,mBAAmB,KAAG,QAAQ,MAAM,CAUtC,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
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;CAiDH,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"retry.d.ts","sourceRoot":"/","sources":["utils/retry.ts"],"names":[],"mappings":"AAgBA,eAAO,MAAM,kBAAkB,wFAuB9B,CAAC"}
|
package/dist/utils/retry.js
DELETED
|
@@ -1,94 +0,0 @@
|
|
|
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
|
package/dist/utils/retry.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
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"]}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { Contract, Signer } from 'ethers';
|
|
2
|
-
import { tokenDetails } from './consts';
|
|
3
|
-
import { exponentialBackoff } from '../../utils/retry';
|
|
4
|
-
import { GetERC20AllowanceParams } from './types';
|
|
5
|
-
import { descale, scale } from '@reyaxyz/common';
|
|
6
|
-
|
|
7
|
-
export const getERC20TokenContract = (
|
|
8
|
-
tokenAddress: string,
|
|
9
|
-
subject: Signer,
|
|
10
|
-
): Contract => {
|
|
11
|
-
const abi: string[] = [
|
|
12
|
-
`function approve(address, uint256) external returns (bool)`,
|
|
13
|
-
`function balanceOf(address) external view returns (uint256)`,
|
|
14
|
-
`function allowance(address,address) external view returns (uint256)`,
|
|
15
|
-
`function approve(address,uint256) external returns (bool)`,
|
|
16
|
-
];
|
|
17
|
-
|
|
18
|
-
const contract = new Contract(tokenAddress, abi, subject);
|
|
19
|
-
|
|
20
|
-
return contract;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type Address = Lowercase<string>;
|
|
24
|
-
|
|
25
|
-
export const convertToAddress = (str: string): Address => {
|
|
26
|
-
return str.toLowerCase() as Address;
|
|
27
|
-
};
|
|
28
|
-
|
|
29
|
-
export const getTokenDetails = (
|
|
30
|
-
caseSensitiveAddress: string,
|
|
31
|
-
): {
|
|
32
|
-
tokenName: Uppercase<string>;
|
|
33
|
-
tokenDecimals: number;
|
|
34
|
-
tokenDescaler: (value: bigint) => number;
|
|
35
|
-
} => {
|
|
36
|
-
const address = convertToAddress(caseSensitiveAddress);
|
|
37
|
-
|
|
38
|
-
if (!Object.keys(tokenDetails).includes(address)) {
|
|
39
|
-
throw new Error(`Token details not found for ${address}.`);
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
const { tokenName, tokenDecimals } = tokenDetails[address];
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
tokenName,
|
|
46
|
-
tokenDecimals,
|
|
47
|
-
tokenDescaler: descale(tokenDecimals),
|
|
48
|
-
};
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export const getERC20Allowance = async ({
|
|
52
|
-
walletAddress,
|
|
53
|
-
tokenAddress,
|
|
54
|
-
spenderAddress,
|
|
55
|
-
subject,
|
|
56
|
-
}: GetERC20AllowanceParams): Promise<number> => {
|
|
57
|
-
const tokenContract = getERC20TokenContract(tokenAddress, subject);
|
|
58
|
-
const { tokenDecimals } = getTokenDetails(tokenAddress);
|
|
59
|
-
|
|
60
|
-
const allowance = await exponentialBackoff(() =>
|
|
61
|
-
tokenContract.allowance(walletAddress, spenderAddress),
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
return BigInt(allowance) > scale(tokenDecimals)(Number.MAX_SAFE_INTEGER)
|
|
65
|
-
? Number.MAX_SAFE_INTEGER
|
|
66
|
-
: descale(tokenDecimals)(allowance);
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
export type GetERC20BalanceArgs = {
|
|
70
|
-
tokenAddress: string;
|
|
71
|
-
walletAddress: string;
|
|
72
|
-
subject: Signer;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
export const getERC20Balance = async ({
|
|
76
|
-
tokenAddress,
|
|
77
|
-
walletAddress,
|
|
78
|
-
subject,
|
|
79
|
-
}: GetERC20BalanceArgs): Promise<number> => {
|
|
80
|
-
const token = getERC20TokenContract(tokenAddress, subject);
|
|
81
|
-
|
|
82
|
-
const { tokenDescaler } = getTokenDetails(tokenAddress);
|
|
83
|
-
|
|
84
|
-
const currentBalance = await exponentialBackoff(() =>
|
|
85
|
-
token.balanceOf(walletAddress),
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
return tokenDescaler(currentBalance);
|
|
89
|
-
};
|
|
@@ -1,57 +0,0 @@
|
|
|
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
|
-
// reya cronos
|
|
17
|
-
'0x51b0b7f083e563d5cf9d0b5f3916b59fc49c7e5f': {
|
|
18
|
-
tokenName: 'USDC',
|
|
19
|
-
tokenDecimals: 6,
|
|
20
|
-
},
|
|
21
|
-
// ====== wETH ======
|
|
22
|
-
// mumbai
|
|
23
|
-
'0xc199807af4fedb02ee567ed0feb814a077de4802': {
|
|
24
|
-
tokenName: 'WETH',
|
|
25
|
-
tokenDecimals: 18,
|
|
26
|
-
},
|
|
27
|
-
// reya cronos
|
|
28
|
-
'0xcbdba2ac8f840711948ccd7a1b9ec0226d641693': {
|
|
29
|
-
tokenName: 'WETH',
|
|
30
|
-
tokenDecimals: 18,
|
|
31
|
-
},
|
|
32
|
-
|
|
33
|
-
// ====== wBTC ======
|
|
34
|
-
// mumbai
|
|
35
|
-
'0x2fa2e7a6deb7bb51b625336dbe1da23511914a8a': {
|
|
36
|
-
tokenName: 'WBTC',
|
|
37
|
-
tokenDecimals: 8,
|
|
38
|
-
},
|
|
39
|
-
// mainnet
|
|
40
|
-
'0xaf21abab55a7c0ab143e0379a3c01054e4ebb514': {
|
|
41
|
-
tokenName: 'WBTC',
|
|
42
|
-
tokenDecimals: 8,
|
|
43
|
-
},
|
|
44
|
-
// ====== USDT ======
|
|
45
|
-
// reya cronos
|
|
46
|
-
'0xeba6f8d7d4d4787f29441db1164548299ef4641f': {
|
|
47
|
-
tokenName: 'USDT',
|
|
48
|
-
tokenDecimals: 6,
|
|
49
|
-
},
|
|
50
|
-
|
|
51
|
-
// ====== dai ======
|
|
52
|
-
// reya cronos
|
|
53
|
-
'0x0d4fad8b258a467286fc2419ffc19015fdcae7f9': {
|
|
54
|
-
tokenName: 'USDT',
|
|
55
|
-
tokenDecimals: 18,
|
|
56
|
-
},
|
|
57
|
-
};
|
package/src/utils/retry.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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
|
-
};
|