@mento-protocol/mento-sdk 1.18.0 → 1.19.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 (117) hide show
  1. package/dist/cjs/ChainClient.d.ts +9 -0
  2. package/dist/cjs/ChainClient.js +58 -0
  3. package/dist/cjs/TestChainClient.d.ts +7 -0
  4. package/dist/cjs/TestChainClient.js +41 -0
  5. package/dist/cjs/constants/addresses.d.ts +4 -0
  6. package/dist/cjs/constants/addresses.js +59 -0
  7. package/dist/cjs/constants/currencies.d.ts +5 -0
  8. package/dist/cjs/constants/currencies.js +29 -0
  9. package/dist/cjs/constants/index.d.ts +4 -0
  10. package/dist/cjs/constants/index.js +20 -0
  11. package/dist/cjs/constants/tokens.11142220.d.ts +2 -0
  12. package/dist/cjs/constants/tokens.11142220.js +128 -0
  13. package/dist/cjs/constants/tokens.42220.d.ts +2 -0
  14. package/dist/cjs/constants/tokens.42220.js +128 -0
  15. package/dist/cjs/constants/tokens.d.ts +50 -0
  16. package/dist/cjs/constants/tokens.js +155 -0
  17. package/dist/cjs/constants/tradablePairs.11142220.d.ts +2 -0
  18. package/dist/cjs/constants/tradablePairs.11142220.js +7391 -0
  19. package/dist/cjs/constants/tradablePairs.42220.d.ts +2 -0
  20. package/dist/cjs/constants/tradablePairs.42220.js +7391 -0
  21. package/dist/cjs/constants/tradablePairs.d.ts +16 -0
  22. package/dist/cjs/constants/tradablePairs.js +53 -0
  23. package/dist/cjs/enums/chainId.d.ts +4 -0
  24. package/dist/cjs/enums/chainId.js +8 -0
  25. package/dist/cjs/enums/index.d.ts +3 -0
  26. package/dist/cjs/enums/index.js +19 -0
  27. package/dist/cjs/enums/proposalState.d.ts +10 -0
  28. package/dist/cjs/enums/proposalState.js +14 -0
  29. package/dist/cjs/enums/tradingMode.d.ts +17 -0
  30. package/dist/cjs/enums/tradingMode.js +21 -0
  31. package/dist/cjs/governance.d.ts +62 -0
  32. package/dist/cjs/governance.js +151 -0
  33. package/dist/cjs/index.d.ts +7 -0
  34. package/dist/cjs/index.js +23 -0
  35. package/dist/cjs/interfaces/IChainClient.d.ts +6 -0
  36. package/dist/cjs/interfaces/IChainClient.js +2 -0
  37. package/dist/cjs/interfaces/index.d.ts +4 -0
  38. package/dist/cjs/interfaces/index.js +20 -0
  39. package/dist/cjs/interfaces/tradingLimit.d.ts +7 -0
  40. package/dist/cjs/interfaces/tradingLimit.js +2 -0
  41. package/dist/cjs/interfaces/tradingLimitsConfig.d.ts +10 -0
  42. package/dist/cjs/interfaces/tradingLimitsConfig.js +2 -0
  43. package/dist/cjs/interfaces/tradingLimitsState.d.ts +9 -0
  44. package/dist/cjs/interfaces/tradingLimitsState.js +2 -0
  45. package/dist/cjs/limits.d.ts +33 -0
  46. package/dist/cjs/limits.js +130 -0
  47. package/dist/cjs/mento.d.ts +287 -0
  48. package/dist/cjs/mento.js +699 -0
  49. package/dist/cjs/routeUtils.d.ts +304 -0
  50. package/dist/cjs/routeUtils.js +372 -0
  51. package/dist/cjs/types/contractAddressMap.d.ts +4 -0
  52. package/dist/cjs/types/contractAddressMap.js +2 -0
  53. package/dist/cjs/types/contractAddresses.d.ts +23 -0
  54. package/dist/cjs/types/contractAddresses.js +2 -0
  55. package/dist/cjs/types/index.d.ts +2 -0
  56. package/dist/cjs/types/index.js +18 -0
  57. package/dist/cjs/utils.d.ts +80 -0
  58. package/dist/cjs/utils.js +177 -0
  59. package/dist/esm/ChainClient.d.ts +9 -0
  60. package/dist/esm/ChainClient.js +54 -0
  61. package/dist/esm/TestChainClient.d.ts +7 -0
  62. package/dist/esm/TestChainClient.js +37 -0
  63. package/dist/esm/constants/addresses.d.ts +4 -0
  64. package/dist/esm/constants/addresses.js +55 -0
  65. package/dist/esm/constants/currencies.d.ts +5 -0
  66. package/dist/esm/constants/currencies.js +26 -0
  67. package/dist/esm/constants/index.d.ts +4 -0
  68. package/dist/esm/constants/index.js +4 -0
  69. package/dist/esm/constants/tokens.11142220.d.ts +2 -0
  70. package/dist/esm/constants/tokens.11142220.js +125 -0
  71. package/dist/esm/constants/tokens.42220.d.ts +2 -0
  72. package/dist/esm/constants/tokens.42220.js +125 -0
  73. package/dist/esm/constants/tokens.d.ts +50 -0
  74. package/dist/esm/constants/tokens.js +127 -0
  75. package/dist/esm/constants/tradablePairs.11142220.d.ts +2 -0
  76. package/dist/esm/constants/tradablePairs.11142220.js +7388 -0
  77. package/dist/esm/constants/tradablePairs.42220.d.ts +2 -0
  78. package/dist/esm/constants/tradablePairs.42220.js +7388 -0
  79. package/dist/esm/constants/tradablePairs.d.ts +16 -0
  80. package/dist/esm/constants/tradablePairs.js +26 -0
  81. package/dist/esm/enums/chainId.d.ts +4 -0
  82. package/dist/esm/enums/chainId.js +5 -0
  83. package/dist/esm/enums/index.d.ts +3 -0
  84. package/dist/esm/enums/index.js +3 -0
  85. package/dist/esm/enums/proposalState.d.ts +10 -0
  86. package/dist/esm/enums/proposalState.js +11 -0
  87. package/dist/esm/enums/tradingMode.d.ts +17 -0
  88. package/dist/esm/enums/tradingMode.js +18 -0
  89. package/dist/esm/governance.d.ts +62 -0
  90. package/dist/esm/governance.js +147 -0
  91. package/dist/esm/index.d.ts +7 -0
  92. package/dist/esm/index.js +7 -0
  93. package/dist/esm/interfaces/IChainClient.d.ts +6 -0
  94. package/dist/esm/interfaces/IChainClient.js +1 -0
  95. package/dist/esm/interfaces/index.d.ts +4 -0
  96. package/dist/esm/interfaces/index.js +4 -0
  97. package/dist/esm/interfaces/tradingLimit.d.ts +7 -0
  98. package/dist/esm/interfaces/tradingLimit.js +1 -0
  99. package/dist/esm/interfaces/tradingLimitsConfig.d.ts +10 -0
  100. package/dist/esm/interfaces/tradingLimitsConfig.js +1 -0
  101. package/dist/esm/interfaces/tradingLimitsState.d.ts +9 -0
  102. package/dist/esm/interfaces/tradingLimitsState.js +1 -0
  103. package/dist/esm/limits.d.ts +33 -0
  104. package/dist/esm/limits.js +123 -0
  105. package/dist/esm/mento.d.ts +287 -0
  106. package/dist/esm/mento.js +671 -0
  107. package/dist/esm/routeUtils.d.ts +304 -0
  108. package/dist/esm/routeUtils.js +362 -0
  109. package/dist/esm/types/contractAddressMap.d.ts +4 -0
  110. package/dist/esm/types/contractAddressMap.js +1 -0
  111. package/dist/esm/types/contractAddresses.d.ts +23 -0
  112. package/dist/esm/types/contractAddresses.js +1 -0
  113. package/dist/esm/types/index.d.ts +2 -0
  114. package/dist/esm/types/index.js +2 -0
  115. package/dist/esm/utils.d.ts +80 -0
  116. package/dist/esm/utils.js +162 -0
  117. package/package.json +1 -1
@@ -0,0 +1,16 @@
1
+ import { TradablePair } from '../mento';
2
+ export interface TradablePairWithSpread extends TradablePair {
3
+ spreadData: {
4
+ totalSpreadPercent: number;
5
+ hops: Array<{
6
+ exchangeId: string;
7
+ spreadPercent: number;
8
+ }>;
9
+ };
10
+ }
11
+ /**
12
+ * Gets cached tradable pairs for a specific chain ID
13
+ * @param chainId - The chain ID to get cached pairs for
14
+ * @returns Promise resolving to the cached tradable pairs or undefined if not available
15
+ */
16
+ export declare function getCachedTradablePairs(chainId: number): Promise<readonly (TradablePair | TradablePairWithSpread)[] | undefined>;
@@ -0,0 +1,26 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ /**
11
+ * Gets cached tradable pairs for a specific chain ID
12
+ * @param chainId - The chain ID to get cached pairs for
13
+ * @returns Promise resolving to the cached tradable pairs or undefined if not available
14
+ */
15
+ export function getCachedTradablePairs(chainId) {
16
+ return __awaiter(this, void 0, void 0, function* () {
17
+ switch (chainId) {
18
+ case 42220:
19
+ return yield import('./tradablePairs.42220').then((module) => module.tradablePairs42220);
20
+ case 11142220:
21
+ return yield import('./tradablePairs.11142220').then((module) => module.tradablePairs11142220);
22
+ default:
23
+ return undefined;
24
+ }
25
+ });
26
+ }
@@ -0,0 +1,4 @@
1
+ export declare enum ChainId {
2
+ CELO = 42220,
3
+ CELO_SEPOLIA = 11142220
4
+ }
@@ -0,0 +1,5 @@
1
+ export var ChainId;
2
+ (function (ChainId) {
3
+ ChainId[ChainId["CELO"] = 42220] = "CELO";
4
+ ChainId[ChainId["CELO_SEPOLIA"] = 11142220] = "CELO_SEPOLIA";
5
+ })(ChainId || (ChainId = {}));
@@ -0,0 +1,3 @@
1
+ export * from './chainId';
2
+ export * from './proposalState';
3
+ export * from './tradingMode';
@@ -0,0 +1,3 @@
1
+ export * from './chainId';
2
+ export * from './proposalState';
3
+ export * from './tradingMode';
@@ -0,0 +1,10 @@
1
+ export declare enum ProposalState {
2
+ PENDING = 0,
3
+ ACTIVE = 1,
4
+ CANCELED = 2,
5
+ DEFEATED = 3,
6
+ SUCCEEDED = 4,
7
+ QUEUED = 5,
8
+ EXPIRED = 6,
9
+ EXECUTED = 7
10
+ }
@@ -0,0 +1,11 @@
1
+ export var ProposalState;
2
+ (function (ProposalState) {
3
+ ProposalState[ProposalState["PENDING"] = 0] = "PENDING";
4
+ ProposalState[ProposalState["ACTIVE"] = 1] = "ACTIVE";
5
+ ProposalState[ProposalState["CANCELED"] = 2] = "CANCELED";
6
+ ProposalState[ProposalState["DEFEATED"] = 3] = "DEFEATED";
7
+ ProposalState[ProposalState["SUCCEEDED"] = 4] = "SUCCEEDED";
8
+ ProposalState[ProposalState["QUEUED"] = 5] = "QUEUED";
9
+ ProposalState[ProposalState["EXPIRED"] = 6] = "EXPIRED";
10
+ ProposalState[ProposalState["EXECUTED"] = 7] = "EXECUTED";
11
+ })(ProposalState || (ProposalState = {}));
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Trading modes for rate feeds in the BreakerBox
3
+ */
4
+ export declare enum TradingMode {
5
+ /**
6
+ * Bidirectional trading is enabled
7
+ */
8
+ BIDIRECTIONAL = 0,
9
+ /**
10
+ * Trading is temporarily halted (circuit breaker tripped)
11
+ */
12
+ HALTED = 1,
13
+ /**
14
+ * Trading is permanently disabled
15
+ */
16
+ DISABLED = 2
17
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Trading modes for rate feeds in the BreakerBox
3
+ */
4
+ export var TradingMode;
5
+ (function (TradingMode) {
6
+ /**
7
+ * Bidirectional trading is enabled
8
+ */
9
+ TradingMode[TradingMode["BIDIRECTIONAL"] = 0] = "BIDIRECTIONAL";
10
+ /**
11
+ * Trading is temporarily halted (circuit breaker tripped)
12
+ */
13
+ TradingMode[TradingMode["HALTED"] = 1] = "HALTED";
14
+ /**
15
+ * Trading is permanently disabled
16
+ */
17
+ TradingMode[TradingMode["DISABLED"] = 2] = "DISABLED";
18
+ })(TradingMode || (TradingMode = {}));
@@ -0,0 +1,62 @@
1
+ import { BigNumberish, Signer, providers } from 'ethers';
2
+ import { MentoGovernor } from '@mento-protocol/mento-core-ts';
3
+ import { IChainClient } from './interfaces';
4
+ export declare class Governance {
5
+ private chainClient;
6
+ constructor(chainClient: IChainClient);
7
+ constructor(signerOrProvider: Signer | providers.Provider);
8
+ /**
9
+ * This function retrieves the MentoGovernor contract.
10
+ * @returns The MentoGovernor contract.
11
+ */
12
+ getGovernorContract(): Promise<MentoGovernor>;
13
+ /**
14
+ * Generates a transaction that submits a proposal to be created to the Mento Governor contract using the specified values.
15
+ * @param targets The addresses of the contracts to be called during proposal execution.
16
+ * @param values The values to be passed to the calls to the target contracts.
17
+ * @param calldatas The calldata to be passed to the calls to the target contracts.
18
+ * @param description A human readable description of the proposal.
19
+ * @returns The transaction request.
20
+ */
21
+ createProposal(targets: string[], values: BigNumberish[], calldatas: string[], description: string): Promise<providers.TransactionRequest>;
22
+ /**
23
+ * Generates a transaction that will queue the proposal with the specified id to be executed.
24
+ * @param proposalId The id of the proposal to queue.
25
+ * @returns The transaction request.
26
+ */
27
+ queueProposal(proposalId: BigNumberish): Promise<providers.TransactionRequest>;
28
+ /**
29
+ * Executes the proposal with the specified id.
30
+ * @param proposalId The id of the proposal to execute.
31
+ * @returns The transaction request.
32
+ */
33
+ executeProposal(proposalId: BigNumberish): Promise<providers.TransactionRequest>;
34
+ /**
35
+ * Submits a vote to the Mento Governor contract for the specified proposal.
36
+ * @param proposalId The id of the proposal to vote on.
37
+ * @param support Whether or not to support the proposal.
38
+ * @returns The transaction request.
39
+ */
40
+ castVote(proposalId: BigNumberish, support: BigNumberish): Promise<providers.TransactionRequest>;
41
+ /**
42
+ * Cancels the proposal with the specified id.
43
+ * @param proposalId The id of the proposal to vote on.
44
+ * @param support Whether or not to support the proposal.
45
+ * @returns The transaction request.
46
+ */
47
+ cancelProposal(proposalId: BigNumberish): Promise<providers.TransactionRequest>;
48
+ /**
49
+ * Returns the state of the proposal with the specified id.
50
+ * @param proposalId The id of the proposal to get the state of.
51
+ * @returns The state of the proposal.
52
+ */
53
+ getProposalState(proposalId: BigNumberish): Promise<string>;
54
+ /**
55
+ * This function validates the args that are to be used in the createProposal function.
56
+ * @param targets The addresses of the contracts to be called during proposal execution.
57
+ * @param values The values to be passed to the calls to the target contracts.
58
+ * @param calldatas The calldata to be passed to the calls to the target contracts.
59
+ * @param description A human readable description of the proposal.
60
+ */
61
+ private validateProposalArgs;
62
+ }
@@ -0,0 +1,147 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { Signer, providers } from 'ethers';
11
+ import { MentoGovernor__factory, } from '@mento-protocol/mento-core-ts';
12
+ import { ChainClient } from './ChainClient';
13
+ import { TestChainClient } from './TestChainClient';
14
+ import { ProposalState } from './enums';
15
+ import { addresses } from './constants';
16
+ export class Governance {
17
+ constructor(arg) {
18
+ // TODO: Remove use of TestChainClient in future this is only meant for testing
19
+ if (arg instanceof ChainClient || arg instanceof TestChainClient) {
20
+ this.chainClient = arg;
21
+ }
22
+ else if (Signer.isSigner(arg) || providers.Provider.isProvider(arg)) {
23
+ this.chainClient = new ChainClient(arg);
24
+ }
25
+ else {
26
+ throw new Error('Invalid constructor argument');
27
+ }
28
+ }
29
+ /**
30
+ * This function retrieves the MentoGovernor contract.
31
+ * @returns The MentoGovernor contract.
32
+ */
33
+ getGovernorContract() {
34
+ return __awaiter(this, void 0, void 0, function* () {
35
+ const chainId = yield this.chainClient.getChainId();
36
+ const contracts = addresses[chainId];
37
+ if (!contracts) {
38
+ throw new Error(`Contracts not deployed on network with chain id ${chainId}`);
39
+ }
40
+ const mentoGovernorAddress = contracts.MentoGovernor;
41
+ return MentoGovernor__factory.connect(mentoGovernorAddress, yield this.chainClient.getSigner());
42
+ });
43
+ }
44
+ /**
45
+ * Generates a transaction that submits a proposal to be created to the Mento Governor contract using the specified values.
46
+ * @param targets The addresses of the contracts to be called during proposal execution.
47
+ * @param values The values to be passed to the calls to the target contracts.
48
+ * @param calldatas The calldata to be passed to the calls to the target contracts.
49
+ * @param description A human readable description of the proposal.
50
+ * @returns The transaction request.
51
+ */
52
+ createProposal(targets, values, calldatas, description) {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ this.validateProposalArgs(targets, values, calldatas, description);
55
+ const governor = yield this.getGovernorContract();
56
+ const tx = yield governor.populateTransaction['propose(address[],uint256[],bytes[],string)'](targets, values, calldatas, description);
57
+ return yield this.chainClient.populateTransaction(tx);
58
+ });
59
+ }
60
+ /**
61
+ * Generates a transaction that will queue the proposal with the specified id to be executed.
62
+ * @param proposalId The id of the proposal to queue.
63
+ * @returns The transaction request.
64
+ */
65
+ queueProposal(proposalId) {
66
+ return __awaiter(this, void 0, void 0, function* () {
67
+ const governor = yield this.getGovernorContract();
68
+ const tx = yield governor.populateTransaction['queue(uint256)'](proposalId);
69
+ return yield this.chainClient.populateTransaction(tx);
70
+ });
71
+ }
72
+ /**
73
+ * Executes the proposal with the specified id.
74
+ * @param proposalId The id of the proposal to execute.
75
+ * @returns The transaction request.
76
+ */
77
+ executeProposal(proposalId) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ const governor = yield this.getGovernorContract();
80
+ const tx = yield governor.populateTransaction['execute(uint256)'](proposalId);
81
+ return yield this.chainClient.populateTransaction(tx);
82
+ });
83
+ }
84
+ /**
85
+ * Submits a vote to the Mento Governor contract for the specified proposal.
86
+ * @param proposalId The id of the proposal to vote on.
87
+ * @param support Whether or not to support the proposal.
88
+ * @returns The transaction request.
89
+ */
90
+ castVote(proposalId, support) {
91
+ return __awaiter(this, void 0, void 0, function* () {
92
+ const governor = yield this.getGovernorContract();
93
+ const tx = yield governor.populateTransaction.castVote(proposalId, support);
94
+ return yield this.chainClient.populateTransaction(tx);
95
+ });
96
+ }
97
+ /**
98
+ * Cancels the proposal with the specified id.
99
+ * @param proposalId The id of the proposal to vote on.
100
+ * @param support Whether or not to support the proposal.
101
+ * @returns The transaction request.
102
+ */
103
+ cancelProposal(proposalId) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const governor = yield this.getGovernorContract();
106
+ const tx = yield governor.populateTransaction.cancel(proposalId);
107
+ return yield this.chainClient.populateTransaction(tx);
108
+ });
109
+ }
110
+ /**
111
+ * Returns the state of the proposal with the specified id.
112
+ * @param proposalId The id of the proposal to get the state of.
113
+ * @returns The state of the proposal.
114
+ */
115
+ getProposalState(proposalId) {
116
+ return __awaiter(this, void 0, void 0, function* () {
117
+ const governor = yield this.getGovernorContract();
118
+ const state = (yield governor.functions.state(proposalId))[0];
119
+ return ProposalState[state];
120
+ });
121
+ }
122
+ /**
123
+ * This function validates the args that are to be used in the createProposal function.
124
+ * @param targets The addresses of the contracts to be called during proposal execution.
125
+ * @param values The values to be passed to the calls to the target contracts.
126
+ * @param calldatas The calldata to be passed to the calls to the target contracts.
127
+ * @param description A human readable description of the proposal.
128
+ */
129
+ validateProposalArgs(targets, values, calldatas, description) {
130
+ if (!targets || targets.length === 0) {
131
+ throw new Error('Targets must be specified');
132
+ }
133
+ if (!values || values.length === 0) {
134
+ throw new Error('Values must be specified');
135
+ }
136
+ if (!calldatas || calldatas.length === 0) {
137
+ throw new Error('Calldatas must be specified');
138
+ }
139
+ if (!description) {
140
+ throw new Error('Description must be specified');
141
+ }
142
+ if (targets.length !== values.length ||
143
+ targets.length !== calldatas.length) {
144
+ throw new Error('Targets, values, and calldatas must all have the same length');
145
+ }
146
+ }
147
+ }
@@ -0,0 +1,7 @@
1
+ export * from './constants';
2
+ export * from './enums';
3
+ export * from './governance';
4
+ export * from './mento';
5
+ export * from './routeUtils';
6
+ export { ContractAddresses } from './types';
7
+ export * from './utils';
@@ -0,0 +1,7 @@
1
+ /* istanbul ignore file */
2
+ export * from './constants';
3
+ export * from './enums';
4
+ export * from './governance';
5
+ export * from './mento';
6
+ export * from './routeUtils';
7
+ export * from './utils';
@@ -0,0 +1,6 @@
1
+ import { ethers, providers } from 'ethers';
2
+ export interface IChainClient {
3
+ getSigner(): Promise<ethers.Signer | providers.Provider>;
4
+ getChainId(): Promise<number>;
5
+ populateTransaction(tx: ethers.PopulatedTransaction): Promise<providers.TransactionRequest>;
6
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,4 @@
1
+ export * from './tradingLimit';
2
+ export * from './tradingLimitsConfig';
3
+ export * from './tradingLimitsState';
4
+ export * from './IChainClient';
@@ -0,0 +1,4 @@
1
+ export * from './tradingLimit';
2
+ export * from './tradingLimitsConfig';
3
+ export * from './tradingLimitsState';
4
+ export * from './IChainClient';
@@ -0,0 +1,7 @@
1
+ export type Address = string;
2
+ export interface TradingLimit {
3
+ asset: Address;
4
+ maxIn: number;
5
+ maxOut: number;
6
+ until: number;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import { Address } from './tradingLimit';
2
+ export interface TradingLimitsConfig {
3
+ asset: Address;
4
+ timestep0: number;
5
+ timestep1: number;
6
+ limit0: number;
7
+ limit1: number;
8
+ limitGlobal: number;
9
+ flags: number;
10
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { Address } from './tradingLimit';
2
+ export interface TradingLimitsState {
3
+ asset: Address;
4
+ lastUpdated0: number;
5
+ lastUpdated1: number;
6
+ netflow0: number;
7
+ netflow1: number;
8
+ netflowGlobal: number;
9
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,33 @@
1
+ import { Address, TradingLimit, TradingLimitsConfig, TradingLimitsState } from './interfaces';
2
+ import { Broker } from '@mento-protocol/mento-core-ts';
3
+ /**
4
+ * Returns the limit configuration in the broker for the given exchange and asset
5
+ * @param broker an instance of the broker
6
+ * @param exchangeId the id of the exchange
7
+ * @param asset the address of the limited asset
8
+ * @returns the limit configuration
9
+ */
10
+ export declare function getLimitsConfig(broker: Broker, exchangeId: string, asset: Address): Promise<TradingLimitsConfig>;
11
+ /**
12
+ * Returns the limit state in the broker for the given exchange and asset
13
+ * @param broker an instance of the broker
14
+ * @param exchangeId the id of the exchange
15
+ * @param asset the address of the limited asset
16
+ * @returns the limit state
17
+ */
18
+ export declare function getLimitsState(broker: Broker, exchangeId: string, asset: Address): Promise<TradingLimitsState>;
19
+ /**
20
+ * Returns a human-friendly representation of the limits for the given exchange and asset
21
+ * @param broker an instance of the broker
22
+ * @param exchangeId the id of the exchange
23
+ * @param asset the address of the asset with the limit
24
+ * @returns a list of TradingLimit objects
25
+ */
26
+ export declare function getLimits(broker: Broker, exchangeId: string, asset: Address): Promise<TradingLimit[]>;
27
+ /**
28
+ * Returns the limit id for the given exchange and asset
29
+ * @param exchangeId the id of the exchange
30
+ * @param asset the address of the asset with the limit
31
+ * @returns the limit id
32
+ */
33
+ export declare function getLimitId(exchangeId: string, asset: Address): string;
@@ -0,0 +1,123 @@
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ import { strict as assert } from 'assert';
11
+ import { utils } from 'ethers';
12
+ /**
13
+ * Returns the limit configuration in the broker for the given exchange and asset
14
+ * @param broker an instance of the broker
15
+ * @param exchangeId the id of the exchange
16
+ * @param asset the address of the limited asset
17
+ * @returns the limit configuration
18
+ */
19
+ export function getLimitsConfig(broker, exchangeId, asset) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ const limitId = getLimitId(exchangeId, asset);
22
+ const cfg = yield broker.tradingLimitsConfig(limitId);
23
+ return {
24
+ asset,
25
+ timestep0: cfg['timestep0'],
26
+ timestep1: cfg['timestep1'],
27
+ limit0: cfg['limit0'],
28
+ limit1: cfg['limit1'],
29
+ limitGlobal: cfg['limitGlobal'],
30
+ flags: cfg['flags'],
31
+ };
32
+ });
33
+ }
34
+ /**
35
+ * Returns the limit state in the broker for the given exchange and asset
36
+ * @param broker an instance of the broker
37
+ * @param exchangeId the id of the exchange
38
+ * @param asset the address of the limited asset
39
+ * @returns the limit state
40
+ */
41
+ export function getLimitsState(broker, exchangeId, asset) {
42
+ return __awaiter(this, void 0, void 0, function* () {
43
+ const limitId = getLimitId(exchangeId, asset);
44
+ const [cfg, state] = yield Promise.all([
45
+ getLimitsConfig(broker, exchangeId, asset),
46
+ broker.tradingLimitsState(limitId),
47
+ ]);
48
+ const isL0Enabled = cfg.timestep0 > 0;
49
+ const isL1Enabled = cfg.timestep1 > 0;
50
+ const nowEpoch = Math.floor(Date.now() / 1000);
51
+ const isL0Outdated = isL0Enabled && nowEpoch > state['lastUpdated0'] + cfg.timestep0;
52
+ const isL1Outdated = isL1Enabled && nowEpoch > state['lastUpdated1'] + cfg.timestep1;
53
+ return {
54
+ asset,
55
+ lastUpdated0: isL0Outdated ? nowEpoch : state['lastUpdated0'],
56
+ lastUpdated1: isL1Outdated ? nowEpoch : state['lastUpdated1'],
57
+ netflow0: isL0Outdated ? 0 : state['netflow0'],
58
+ netflow1: isL1Outdated ? 0 : state['netflow1'],
59
+ netflowGlobal: state['netflowGlobal'],
60
+ };
61
+ });
62
+ }
63
+ /**
64
+ * Returns a human-friendly representation of the limits for the given exchange and asset
65
+ * @param broker an instance of the broker
66
+ * @param exchangeId the id of the exchange
67
+ * @param asset the address of the asset with the limit
68
+ * @returns a list of TradingLimit objects
69
+ */
70
+ export function getLimits(broker, exchangeId, asset) {
71
+ return __awaiter(this, void 0, void 0, function* () {
72
+ const [cfg, state] = yield Promise.all([
73
+ getLimitsConfig(broker, exchangeId, asset),
74
+ getLimitsState(broker, exchangeId, asset),
75
+ ]);
76
+ const limits = [];
77
+ if (cfg.limit0 > 0) {
78
+ limits.push({
79
+ asset: asset,
80
+ maxIn: cfg.limit0 - state.netflow0,
81
+ maxOut: cfg.limit0 + state.netflow0,
82
+ until: state.lastUpdated0 + cfg.timestep0,
83
+ });
84
+ }
85
+ if (cfg.limit1 > 0) {
86
+ limits.push({
87
+ asset: asset,
88
+ maxIn: cfg.limit1 - state.netflow1,
89
+ maxOut: cfg.limit1 + state.netflow1,
90
+ until: state.lastUpdated1 + cfg.timestep1,
91
+ });
92
+ }
93
+ if (cfg.limitGlobal > 0) {
94
+ const timestampIn2030 = 1893456000; // a far away timestamp
95
+ limits.push({
96
+ asset: asset,
97
+ maxIn: cfg.limitGlobal - state.netflowGlobal,
98
+ maxOut: cfg.limitGlobal + state.netflowGlobal,
99
+ until: timestampIn2030,
100
+ });
101
+ }
102
+ // Limits with a smaller timeframe are restricted by the ones with a larger one
103
+ // e.g: if maxIn is 0 in LG, it should also be 0 in L1 and L0
104
+ for (let i = limits.length - 1; i > 0; i--) {
105
+ limits[i - 1].maxIn = Math.min(limits[i - 1].maxIn, limits[i].maxIn);
106
+ limits[i - 1].maxOut = Math.min(limits[i - 1].maxOut, limits[i].maxOut);
107
+ }
108
+ return limits;
109
+ });
110
+ }
111
+ /**
112
+ * Returns the limit id for the given exchange and asset
113
+ * @param exchangeId the id of the exchange
114
+ * @param asset the address of the asset with the limit
115
+ * @returns the limit id
116
+ */
117
+ export function getLimitId(exchangeId, asset) {
118
+ const assetBytes32 = utils.zeroPad(asset, 32);
119
+ const exchangeIdBytes = utils.arrayify(exchangeId);
120
+ const assetBytes = utils.arrayify(assetBytes32);
121
+ assert(exchangeIdBytes.length === assetBytes.length, 'exchangeId and asset0 must be the same length');
122
+ return utils.hexlify(exchangeIdBytes.map((b, i) => b ^ assetBytes[i]));
123
+ }