@reyaxyz/sdk 0.1.0 → 0.2.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.
Files changed (66) hide show
  1. package/dist/services/executeTransaction.js +9 -7
  2. package/dist/services/executeTransaction.js.map +1 -1
  3. package/dist/services/lp/lp.js +3 -2
  4. package/dist/services/lp/lp.js.map +1 -1
  5. package/dist/services/margin-accounts/account.js +2 -1
  6. package/dist/services/margin-accounts/account.js.map +1 -1
  7. package/dist/services/margin-accounts/deposit.js +2 -1
  8. package/dist/services/margin-accounts/deposit.js.map +1 -1
  9. package/dist/services/margin-accounts/withdraw.js +2 -1
  10. package/dist/services/margin-accounts/withdraw.js.map +1 -1
  11. package/dist/services/orders/order.js +2 -1
  12. package/dist/services/orders/order.js.map +1 -1
  13. package/dist/services/token/approve.js +95 -0
  14. package/dist/services/token/approve.js.map +1 -0
  15. package/dist/services/token/common.js +107 -0
  16. package/dist/services/token/common.js.map +1 -0
  17. package/dist/services/token/consts.js +29 -0
  18. package/dist/services/token/consts.js.map +1 -0
  19. package/dist/services/token/getAllowance.js +68 -0
  20. package/dist/services/token/getAllowance.js.map +1 -0
  21. package/dist/services/token/types.js +9 -0
  22. package/dist/services/token/types.js.map +1 -0
  23. package/dist/types/services/executeTransaction.d.ts +3 -2
  24. package/dist/types/services/executeTransaction.d.ts.map +1 -1
  25. package/dist/types/services/lp/lp.d.ts.map +1 -1
  26. package/dist/types/services/margin-accounts/account.d.ts.map +1 -1
  27. package/dist/types/services/margin-accounts/deposit.d.ts.map +1 -1
  28. package/dist/types/services/margin-accounts/withdraw.d.ts.map +1 -1
  29. package/dist/types/services/orders/order.d.ts.map +1 -1
  30. package/dist/types/services/token/approve.d.ts +3 -0
  31. package/dist/types/services/token/approve.d.ts.map +1 -0
  32. package/dist/types/services/token/common.d.ts +14 -0
  33. package/dist/types/services/token/common.d.ts.map +1 -0
  34. package/dist/types/services/token/consts.d.ts +8 -0
  35. package/dist/types/services/token/consts.d.ts.map +1 -0
  36. package/dist/types/services/token/getAllowance.d.ts +3 -0
  37. package/dist/types/services/token/getAllowance.d.ts.map +1 -0
  38. package/dist/types/services/token/types.d.ts +24 -0
  39. package/dist/types/services/token/types.d.ts.map +1 -0
  40. package/dist/types/utils/consts.d.ts +8 -0
  41. package/dist/types/utils/consts.d.ts.map +1 -0
  42. package/dist/types/utils/contractAddresses.d.ts +5 -0
  43. package/dist/types/utils/contractAddresses.d.ts.map +1 -0
  44. package/dist/types/utils/retry.d.ts +2 -0
  45. package/dist/types/utils/retry.d.ts.map +1 -0
  46. package/dist/utils/consts.js +12 -0
  47. package/dist/utils/consts.js.map +1 -0
  48. package/dist/utils/contractAddresses.js +27 -0
  49. package/dist/utils/contractAddresses.js.map +1 -0
  50. package/dist/utils/retry.js +94 -0
  51. package/dist/utils/retry.js.map +1 -0
  52. package/package.json +2 -2
  53. package/src/services/executeTransaction.ts +15 -3
  54. package/src/services/lp/lp.ts +3 -0
  55. package/src/services/margin-accounts/account.ts +2 -0
  56. package/src/services/margin-accounts/deposit.ts +2 -0
  57. package/src/services/margin-accounts/withdraw.ts +2 -0
  58. package/src/services/orders/order.ts +2 -0
  59. package/src/services/token/approve.ts +52 -0
  60. package/src/services/token/common.ts +84 -0
  61. package/src/services/token/consts.ts +32 -0
  62. package/src/services/token/getAllowance.ts +24 -0
  63. package/src/services/token/types.ts +27 -0
  64. package/src/utils/consts.ts +7 -0
  65. package/src/utils/contractAddresses.ts +37 -0
  66. package/src/utils/retry.ts +40 -0
@@ -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,8 @@
1
+ import { Address } from './common';
2
+ export declare const tokenDetails: {
3
+ [address: Address]: {
4
+ tokenName: Uppercase<string>;
5
+ tokenDecimals: number;
6
+ };
7
+ };
8
+ //# sourceMappingURL=consts.d.ts.map
@@ -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,3 @@
1
+ import { GetAllowanceArgs } from './types';
2
+ export declare const getAllowance: (params: GetAllowanceArgs) => Promise<number>;
3
+ //# sourceMappingURL=getAllowance.d.ts.map
@@ -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,8 @@
1
+ export declare enum TransactionType {
2
+ CREATE_ACCOUNT = 0,
3
+ DEPOSIT = 1,
4
+ WITHDRAW = 2,
5
+ LP = 3,
6
+ TRADE = 4
7
+ }
8
+ //# sourceMappingURL=consts.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consts.d.ts","sourceRoot":"/","sources":["utils/consts.ts"],"names":[],"mappings":"AAAA,oBAAY,eAAe;IACzB,cAAc,IAAA;IACd,OAAO,IAAA;IACP,QAAQ,IAAA;IACR,EAAE,IAAA;IACF,KAAK,IAAA;CACN"}
@@ -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,2 @@
1
+ export declare const exponentialBackoff: <T = never>(query: () => Promise<T>, attempts?: number, factor?: number) => Promise<T>;
2
+ //# sourceMappingURL=retry.d.ts.map
@@ -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,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TransactionType = void 0;
4
+ var TransactionType;
5
+ (function (TransactionType) {
6
+ TransactionType[TransactionType["CREATE_ACCOUNT"] = 0] = "CREATE_ACCOUNT";
7
+ TransactionType[TransactionType["DEPOSIT"] = 1] = "DEPOSIT";
8
+ TransactionType[TransactionType["WITHDRAW"] = 2] = "WITHDRAW";
9
+ TransactionType[TransactionType["LP"] = 3] = "LP";
10
+ TransactionType[TransactionType["TRADE"] = 4] = "TRADE";
11
+ })(TransactionType || (exports.TransactionType = TransactionType = {}));
12
+ //# sourceMappingURL=consts.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"consts.js","sourceRoot":"/","sources":["utils/consts.ts"],"names":[],"mappings":";;;AAAA,IAAY,eAMX;AAND,WAAY,eAAe;IACzB,yEAAc,CAAA;IACd,2DAAO,CAAA;IACP,6DAAQ,CAAA;IACR,iDAAE,CAAA;IACF,uDAAK,CAAA;AACP,CAAC,EANW,eAAe,+BAAf,eAAe,QAM1B","sourcesContent":["export enum TransactionType {\n CREATE_ACCOUNT,\n DEPOSIT,\n WITHDRAW,\n LP,\n TRADE,\n}\n"]}
@@ -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.0",
3
+ "version": "0.2.0",
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": "12b257e116bed35f8bb69fcd44e5619715228906"
35
+ "gitHead": "0cd4bd2ca7ec708fcf7ef2198aa7abdc4dbbc83c"
36
36
  }
@@ -1,5 +1,7 @@
1
1
  import { Signer } from 'ethers';
2
2
  import { getGasBuffer } from '../utils/txHelpers';
3
+ import { getAddress } from '../utils/contractAddresses';
4
+ import { TransactionType } from '../utils/consts';
3
5
 
4
6
  export type Transaction = {
5
7
  from: string;
@@ -13,10 +15,13 @@ export async function estimateGas(
13
15
  data: string,
14
16
  value: string,
15
17
  chainId: number,
18
+ transactionType: TransactionType,
16
19
  ): Promise<Transaction & { gasLimit: bigint }> {
17
- console.log(chainId); // @todo remove chainId if obsolete
18
20
  const accountAddress = await signer.getAddress();
19
- const contractAddress = `0xdb8cd625873ca9d7b6529ffafe5ecdd3746708b4`; // @todo Define after deployment
21
+ const contractAddress =
22
+ transactionType === TransactionType.LP
23
+ ? getAddress(chainId, 'passive_pool_proxy')
24
+ : getAddress(chainId, 'core_proxy');
20
25
  const tx = {
21
26
  from: accountAddress,
22
27
  to: contractAddress,
@@ -41,8 +46,15 @@ export async function executeTransaction(
41
46
  data: string,
42
47
  value: string,
43
48
  chainId: number,
49
+ transactionType: TransactionType,
44
50
  ) {
45
- const txData = await estimateGas(signer, data, value, chainId);
51
+ const txData = await estimateGas(
52
+ signer,
53
+ data,
54
+ value,
55
+ chainId,
56
+ transactionType,
57
+ );
46
58
  try {
47
59
  const txResponse = await signer.sendTransaction(txData);
48
60
  const txReceipt = await txResponse.wait(); // @todo Check what was reasoning behind this since this function waits until transaction is minted
@@ -4,6 +4,7 @@ import {
4
4
  encodeRemoveLiquidityCall,
5
5
  } from './encode';
6
6
  import { ProvideLiquidityArgs, RemoveLiquidityArgs } from './types';
7
+ import { TransactionType } from '../../utils/consts';
7
8
 
8
9
  export const provideLiquidity = async (
9
10
  params: ProvideLiquidityArgs,
@@ -21,6 +22,7 @@ export const provideLiquidity = async (
21
22
  data,
22
23
  value,
23
24
  params.chainId,
25
+ TransactionType.LP,
24
26
  );
25
27
 
26
28
  return result;
@@ -41,6 +43,7 @@ export const removeLiquidity = async (
41
43
  data,
42
44
  value,
43
45
  params.chainId,
46
+ TransactionType.LP,
44
47
  );
45
48
 
46
49
  return result;
@@ -1,6 +1,7 @@
1
1
  import { executeTransaction } from '../executeTransaction';
2
2
  import { encodeCreateAccountCall } from './encode';
3
3
  import { CreateAccountArgs } from './types';
4
+ import { TransactionType } from '../../utils/consts';
4
5
 
5
6
  export const createAccount = async (
6
7
  params: CreateAccountArgs,
@@ -15,6 +16,7 @@ export const createAccount = async (
15
16
  data,
16
17
  value,
17
18
  params.chainId,
19
+ TransactionType.CREATE_ACCOUNT,
18
20
  );
19
21
 
20
22
  return result;
@@ -1,6 +1,7 @@
1
1
  import { DepositArgs } from './types';
2
2
  import { encodeDeposit } from './encode';
3
3
  import { executeTransaction } from '../executeTransaction';
4
+ import { TransactionType } from '../../utils/consts';
4
5
 
5
6
  export const deposit = async (params: DepositArgs): Promise<unknown> => {
6
7
  // @todo update type once we agree on the structure
@@ -17,6 +18,7 @@ export const deposit = async (params: DepositArgs): Promise<unknown> => {
17
18
  data,
18
19
  value,
19
20
  params.chainId,
21
+ TransactionType.DEPOSIT,
20
22
  );
21
23
 
22
24
  return result;
@@ -1,6 +1,7 @@
1
1
  import { WithdrawArgs } from './types';
2
2
  import { encodeWithdraw } from './encode';
3
3
  import { executeTransaction } from '../executeTransaction';
4
+ import { TransactionType } from '../../utils/consts';
4
5
 
5
6
  export const withdraw = async (params: WithdrawArgs): Promise<unknown> => {
6
7
  // @todo update type once we agree on the structure
@@ -17,6 +18,7 @@ export const withdraw = async (params: WithdrawArgs): Promise<unknown> => {
17
18
  data,
18
19
  value,
19
20
  params.chainId,
21
+ TransactionType.WITHDRAW,
20
22
  );
21
23
 
22
24
  return result;
@@ -1,6 +1,7 @@
1
1
  import { MatchOrderArgs } from './types';
2
2
  import { encodeMatchOrder } from './encode';
3
3
  import { executeTransaction } from '../executeTransaction';
4
+ import { TransactionType } from '../../utils/consts';
4
5
 
5
6
  export const matchOrder = async (params: MatchOrderArgs): Promise<unknown> => {
6
7
  // @todo update type once we agree on the structure
@@ -18,6 +19,7 @@ export const matchOrder = async (params: MatchOrderArgs): Promise<unknown> => {
18
19
  data,
19
20
  value,
20
21
  params.chainId,
22
+ TransactionType.TRADE,
21
23
  );
22
24
 
23
25
  return result;
@@ -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,7 @@
1
+ export enum TransactionType {
2
+ CREATE_ACCOUNT,
3
+ DEPOSIT,
4
+ WITHDRAW,
5
+ LP,
6
+ TRADE,
7
+ }