@mento-protocol/mento-sdk 1.0.0 → 2.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (196) hide show
  1. package/README.md +124 -8
  2. package/dist/abis/bipoolmanager.d.ts +15 -0
  3. package/dist/abis/bipoolmanager.d.ts.map +1 -0
  4. package/dist/abis/bipoolmanager.js +26 -0
  5. package/dist/abis/bipoolmanager.js.map +1 -0
  6. package/dist/abis/broker.d.ts +2 -0
  7. package/dist/abis/broker.d.ts.map +1 -0
  8. package/dist/abis/broker.js +8 -0
  9. package/dist/abis/broker.js.map +1 -0
  10. package/dist/abis/erc20.d.ts +2 -0
  11. package/dist/abis/erc20.d.ts.map +1 -0
  12. package/dist/abis/erc20.js +11 -0
  13. package/dist/abis/erc20.js.map +1 -0
  14. package/dist/abis/index.d.ts +4 -0
  15. package/dist/abis/index.d.ts.map +1 -0
  16. package/dist/{cjs → abis}/index.js +4 -5
  17. package/dist/abis/index.js.map +1 -0
  18. package/dist/abis/pricingmodule.d.ts +2 -0
  19. package/dist/abis/pricingmodule.d.ts.map +1 -0
  20. package/dist/abis/pricingmodule.js +5 -0
  21. package/dist/abis/pricingmodule.js.map +1 -0
  22. package/dist/abis/reserve.d.ts +2 -0
  23. package/dist/abis/reserve.d.ts.map +1 -0
  24. package/dist/abis/reserve.js +9 -0
  25. package/dist/abis/reserve.js.map +1 -0
  26. package/dist/adapters/implementations/ethersAdapter.d.ts +10 -0
  27. package/dist/adapters/implementations/ethersAdapter.d.ts.map +1 -0
  28. package/dist/adapters/implementations/ethersAdapter.js +22 -0
  29. package/dist/adapters/implementations/ethersAdapter.js.map +1 -0
  30. package/dist/adapters/implementations/ethersV5Adapter.d.ts +10 -0
  31. package/dist/adapters/implementations/ethersV5Adapter.d.ts.map +1 -0
  32. package/dist/adapters/implementations/ethersV5Adapter.js +22 -0
  33. package/dist/adapters/implementations/ethersV5Adapter.js.map +1 -0
  34. package/dist/adapters/implementations/viemAdapter.d.ts +10 -0
  35. package/dist/adapters/implementations/viemAdapter.d.ts.map +1 -0
  36. package/dist/adapters/implementations/viemAdapter.js +29 -0
  37. package/dist/adapters/implementations/viemAdapter.js.map +1 -0
  38. package/dist/adapters/index.d.ts +5 -0
  39. package/dist/adapters/index.d.ts.map +1 -0
  40. package/dist/adapters/index.js +10 -0
  41. package/dist/adapters/index.js.map +1 -0
  42. package/dist/adapters/proxies/ethersAdapterProxy.d.ts +27 -0
  43. package/dist/adapters/proxies/ethersAdapterProxy.d.ts.map +1 -0
  44. package/dist/adapters/proxies/ethersAdapterProxy.js +77 -0
  45. package/dist/adapters/proxies/ethersAdapterProxy.js.map +1 -0
  46. package/dist/adapters/proxies/ethersV5AdapterProxy.d.ts +27 -0
  47. package/dist/adapters/proxies/ethersV5AdapterProxy.d.ts.map +1 -0
  48. package/dist/adapters/proxies/ethersV5AdapterProxy.js +77 -0
  49. package/dist/adapters/proxies/ethersV5AdapterProxy.js.map +1 -0
  50. package/dist/adapters/proxies/viemAdapterProxy.d.ts +28 -0
  51. package/dist/adapters/proxies/viemAdapterProxy.d.ts.map +1 -0
  52. package/dist/adapters/proxies/viemAdapterProxy.js +77 -0
  53. package/dist/adapters/proxies/viemAdapterProxy.js.map +1 -0
  54. package/dist/constants/addresses.d.ts +14 -0
  55. package/dist/constants/addresses.d.ts.map +1 -0
  56. package/dist/{cjs/constants → constants}/addresses.js +14 -25
  57. package/dist/constants/addresses.js.map +1 -0
  58. package/dist/constants/chainId.d.ts +5 -0
  59. package/dist/constants/chainId.d.ts.map +1 -0
  60. package/dist/{cjs/enums → constants}/chainId.js +2 -2
  61. package/dist/constants/chainId.js.map +1 -0
  62. package/dist/constants/contractNames.d.ts +21 -0
  63. package/dist/constants/contractNames.d.ts.map +1 -0
  64. package/dist/constants/contractNames.js +24 -0
  65. package/dist/constants/contractNames.js.map +1 -0
  66. package/dist/constants/index.d.ts +6 -0
  67. package/dist/constants/index.d.ts.map +1 -0
  68. package/dist/{cjs/interfaces → constants}/index.js +6 -4
  69. package/dist/constants/index.js.map +1 -0
  70. package/dist/{cjs/enums → constants}/proposalState.d.ts +1 -0
  71. package/dist/constants/proposalState.d.ts.map +1 -0
  72. package/dist/{cjs/enums → constants}/proposalState.js +2 -1
  73. package/dist/constants/proposalState.js.map +1 -0
  74. package/dist/constants/stableTokenMetadata.d.ts +13 -0
  75. package/dist/constants/stableTokenMetadata.d.ts.map +1 -0
  76. package/dist/constants/stableTokenMetadata.js +30 -0
  77. package/dist/constants/stableTokenMetadata.js.map +1 -0
  78. package/dist/index.d.ts +67 -0
  79. package/dist/index.d.ts.map +1 -0
  80. package/dist/index.js +122 -0
  81. package/dist/index.js.map +1 -0
  82. package/dist/services/collateralAssetService.d.ts +8 -0
  83. package/dist/services/collateralAssetService.d.ts.map +1 -0
  84. package/dist/services/collateralAssetService.js +44 -0
  85. package/dist/services/collateralAssetService.js.map +1 -0
  86. package/dist/services/exchangeService.d.ts +18 -0
  87. package/dist/services/exchangeService.d.ts.map +1 -0
  88. package/dist/services/exchangeService.js +78 -0
  89. package/dist/services/exchangeService.js.map +1 -0
  90. package/dist/services/index.d.ts +3 -0
  91. package/dist/services/index.d.ts.map +1 -0
  92. package/dist/{cjs/enums → services}/index.js +3 -2
  93. package/dist/services/index.js.map +1 -0
  94. package/dist/services/stableTokenService.d.ts +8 -0
  95. package/dist/services/stableTokenService.d.ts.map +1 -0
  96. package/dist/services/stableTokenService.js +35 -0
  97. package/dist/services/stableTokenService.js.map +1 -0
  98. package/dist/services/tokenMetadataService.d.ts +8 -0
  99. package/dist/services/tokenMetadataService.d.ts.map +1 -0
  100. package/dist/services/tokenMetadataService.js +45 -0
  101. package/dist/services/tokenMetadataService.js.map +1 -0
  102. package/dist/{esm/types → types}/contractAddresses.d.ts +1 -0
  103. package/dist/types/contractAddresses.d.ts.map +1 -0
  104. package/dist/{cjs/types → types}/contractAddresses.js +1 -0
  105. package/dist/types/contractAddresses.js.map +1 -0
  106. package/dist/types/exchange.d.ts +12 -0
  107. package/dist/types/exchange.d.ts.map +1 -0
  108. package/dist/{cjs/interfaces/tradingLimit.js → types/exchange.js} +1 -0
  109. package/dist/types/exchange.js.map +1 -0
  110. package/dist/types/index.d.ts +5 -0
  111. package/dist/types/index.d.ts.map +1 -0
  112. package/dist/{cjs/types → types}/index.js +4 -1
  113. package/dist/types/index.js.map +1 -0
  114. package/dist/types/provider.d.ts +12 -0
  115. package/dist/types/provider.d.ts.map +1 -0
  116. package/dist/{cjs/interfaces/tradingLimitsConfig.js → types/provider.js} +1 -0
  117. package/dist/types/provider.js.map +1 -0
  118. package/dist/types/token.d.ts +12 -0
  119. package/dist/types/token.d.ts.map +1 -0
  120. package/dist/{cjs/interfaces/IChainClient.js → types/token.js} +1 -0
  121. package/dist/types/token.js.map +1 -0
  122. package/dist/utils/index.d.ts +2 -0
  123. package/dist/utils/index.d.ts.map +1 -0
  124. package/dist/{cjs/constants → utils}/index.js +2 -1
  125. package/dist/utils/index.js.map +1 -0
  126. package/dist/utils/retry.d.ts +12 -0
  127. package/dist/utils/retry.d.ts.map +1 -0
  128. package/dist/utils/retry.js +28 -0
  129. package/dist/utils/retry.js.map +1 -0
  130. package/package.json +31 -43
  131. package/dist/cjs/ChainClient.d.ts +0 -9
  132. package/dist/cjs/ChainClient.js +0 -58
  133. package/dist/cjs/TestChainClient.d.ts +0 -7
  134. package/dist/cjs/TestChainClient.js +0 -41
  135. package/dist/cjs/constants/addresses.d.ts +0 -2
  136. package/dist/cjs/constants/index.d.ts +0 -1
  137. package/dist/cjs/enums/chainId.d.ts +0 -5
  138. package/dist/cjs/enums/index.d.ts +0 -2
  139. package/dist/cjs/governance.d.ts +0 -62
  140. package/dist/cjs/governance.js +0 -151
  141. package/dist/cjs/index.d.ts +0 -5
  142. package/dist/cjs/interfaces/IChainClient.d.ts +0 -6
  143. package/dist/cjs/interfaces/index.d.ts +0 -4
  144. package/dist/cjs/interfaces/tradingLimit.d.ts +0 -7
  145. package/dist/cjs/interfaces/tradingLimitsConfig.d.ts +0 -10
  146. package/dist/cjs/interfaces/tradingLimitsState.d.ts +0 -9
  147. package/dist/cjs/interfaces/tradingLimitsState.js +0 -2
  148. package/dist/cjs/limits.d.ts +0 -33
  149. package/dist/cjs/limits.js +0 -130
  150. package/dist/cjs/mento.d.ts +0 -152
  151. package/dist/cjs/mento.js +0 -318
  152. package/dist/cjs/types/contractAddressMap.d.ts +0 -4
  153. package/dist/cjs/types/contractAddressMap.js +0 -2
  154. package/dist/cjs/types/contractAddresses.d.ts +0 -22
  155. package/dist/cjs/types/index.d.ts +0 -2
  156. package/dist/cjs/utils.d.ts +0 -36
  157. package/dist/cjs/utils.js +0 -98
  158. package/dist/esm/ChainClient.d.ts +0 -9
  159. package/dist/esm/ChainClient.js +0 -54
  160. package/dist/esm/TestChainClient.d.ts +0 -7
  161. package/dist/esm/TestChainClient.js +0 -37
  162. package/dist/esm/constants/addresses.d.ts +0 -2
  163. package/dist/esm/constants/addresses.js +0 -69
  164. package/dist/esm/constants/index.d.ts +0 -1
  165. package/dist/esm/constants/index.js +0 -1
  166. package/dist/esm/enums/chainId.d.ts +0 -5
  167. package/dist/esm/enums/chainId.js +0 -6
  168. package/dist/esm/enums/index.d.ts +0 -2
  169. package/dist/esm/enums/index.js +0 -2
  170. package/dist/esm/enums/proposalState.d.ts +0 -10
  171. package/dist/esm/enums/proposalState.js +0 -11
  172. package/dist/esm/governance.d.ts +0 -62
  173. package/dist/esm/governance.js +0 -147
  174. package/dist/esm/index.d.ts +0 -5
  175. package/dist/esm/index.js +0 -5
  176. package/dist/esm/interfaces/IChainClient.d.ts +0 -6
  177. package/dist/esm/interfaces/IChainClient.js +0 -1
  178. package/dist/esm/interfaces/index.d.ts +0 -4
  179. package/dist/esm/interfaces/index.js +0 -4
  180. package/dist/esm/interfaces/tradingLimit.d.ts +0 -7
  181. package/dist/esm/interfaces/tradingLimit.js +0 -1
  182. package/dist/esm/interfaces/tradingLimitsConfig.d.ts +0 -10
  183. package/dist/esm/interfaces/tradingLimitsConfig.js +0 -1
  184. package/dist/esm/interfaces/tradingLimitsState.d.ts +0 -9
  185. package/dist/esm/interfaces/tradingLimitsState.js +0 -1
  186. package/dist/esm/limits.d.ts +0 -33
  187. package/dist/esm/limits.js +0 -123
  188. package/dist/esm/mento.d.ts +0 -152
  189. package/dist/esm/mento.js +0 -314
  190. package/dist/esm/types/contractAddressMap.d.ts +0 -4
  191. package/dist/esm/types/contractAddressMap.js +0 -1
  192. package/dist/esm/types/contractAddresses.js +0 -1
  193. package/dist/esm/types/index.d.ts +0 -2
  194. package/dist/esm/types/index.js +0 -2
  195. package/dist/esm/utils.d.ts +0 -36
  196. package/dist/esm/utils.js +0 -90
@@ -1,33 +0,0 @@
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;
@@ -1,123 +0,0 @@
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
- }
@@ -1,152 +0,0 @@
1
- import { Address, TradingLimit, TradingLimitsConfig, TradingLimitsState } from './interfaces';
2
- import { IBroker } from '@mento-protocol/mento-core-ts';
3
- import { BigNumber, BigNumberish, Signer, providers } from 'ethers';
4
- export interface Exchange {
5
- providerAddr: Address;
6
- id: string;
7
- assets: Address[];
8
- }
9
- export interface Asset {
10
- address: Address;
11
- symbol: string;
12
- }
13
- export declare class Mento {
14
- private readonly signerOrProvider;
15
- private readonly broker;
16
- private exchanges;
17
- /**
18
- * This constructor is private, use the static create or createWithParams methods
19
- * to create a new Mento instance
20
- * @param signerOrProvider an ethers provider or connected signer
21
- * @param brokerAddress the address of the broker contract
22
- * @param exchanges exchange data for the broker
23
- */
24
- private constructor();
25
- /**
26
- * Creates a new Mento object instance.
27
- * When constructed with only a Provider only read-only operations are supported
28
- * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
29
- * @returns a new Mento object instance
30
- */
31
- static create(signerOrProvider: Signer | providers.Provider): Promise<Mento>;
32
- /**
33
- * Create a new Mento object instance given a broker address and optional exchanges data
34
- * When constructed with a Provider, only read-only operations are supported
35
- * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
36
- * @param brokerAddr the address of the broker contract
37
- * @param exchanges the exchanges data for the broker
38
- * @returns a new Mento object instance
39
- */
40
- static createWithParams(signerOrProvider: Signer | providers.Provider, brokerAddr: Address, exchanges?: Exchange[]): Mento;
41
- /**
42
- * Returns a new Mento instance connected to the given signer
43
- * @param signer an ethers signer
44
- * @returns new Mento object instance
45
- */
46
- connectSigner(signer: Signer): Mento;
47
- /**
48
- * Returns a list of all the pairs that can be traded on Mento
49
- * @returns The list of tradeable pairs in the form of [{address, symbol}]
50
- */
51
- getTradeablePairs(): Promise<[Asset, Asset][]>;
52
- /**
53
- * Returns the amount of tokenIn to be sold to buy amountOut of tokenOut
54
- * @param tokenIn the token to be sold
55
- * @param tokenOut the token to be bought
56
- * @param amountOut the amount of tokenOut to be bought
57
- * @returns the amount of tokenIn to be sold
58
- */
59
- getAmountIn(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish): Promise<BigNumber>;
60
- /**
61
- * Returns the amount of tokenOut to be bought by selling amountIn of tokenIn
62
- * @param tokenIn the token to be sold
63
- * @param tokenOut the token to be bought
64
- * @param amountIn the amount of tokenIn to be sold
65
- * @returns the amount of tokenOut to be bought
66
- */
67
- getAmountOut(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish): Promise<BigNumber>;
68
- /**
69
- * Increases the broker's trading allowance for the given token
70
- * @param token the token to increase the allowance for
71
- * @param amount the amount to increase the allowance by
72
- * @returns the populated TransactionRequest object
73
- */
74
- increaseTradingAllowance(token: Address, amount: BigNumberish): Promise<providers.TransactionRequest>;
75
- /**
76
- * Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut
77
- * Submitting the transaction to execute the swap is left to the consumer
78
- * @param tokenIn the token to be sold
79
- * @param tokenOut the token to be bought
80
- * @param amountIn the amount of tokenIn to be sold
81
- * @param amountOutMin the minimum amount of tokenOut to be bought
82
- * @returns the populated TransactionRequest object
83
- */
84
- swapIn(tokenIn: Address, tokenOut: Address, amountIn: BigNumberish, amountOutMin: BigNumberish): Promise<providers.TransactionRequest>;
85
- /**
86
- * Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut
87
- * Submitting the transaction to execute the swap is left to the consumer
88
- * @param tokenIn the token to be sold
89
- * @param tokenOut the token to be bought
90
- * @param amountOut the amount of tokenOut to be bought
91
- * @param amountInMax the maximum amount of tokenIn to be sold
92
- * @returns the populated TransactionRequest object
93
- */
94
- swapOut(tokenIn: Address, tokenOut: Address, amountOut: BigNumberish, amountInMax: BigNumberish): Promise<providers.TransactionRequest>;
95
- /**
96
- * Returns the mento instance's broker contract
97
- * @returns broker contract
98
- */
99
- getBroker(): IBroker;
100
- /**
101
- * Returns the list of exchanges available in Mento (cached)
102
- * @returns the list of exchanges
103
- */
104
- getExchanges(): Promise<Exchange[]>;
105
- /**
106
- * Returns the list of exchanges for a given exchange provider address
107
- * @returns list of exchanges
108
- */
109
- getExchangesForProvider(exchangeProviderAddr: Address): Promise<Exchange[]>;
110
- /**
111
- * Returns the Mento exchange (if any) for a given pair of tokens
112
- * @param token0 the address of the first token
113
- * @param token1 the address of the second token
114
- * @returns exchange
115
- */
116
- getExchangeForTokens(token0: Address, token1: Address): Promise<Exchange>;
117
- /**
118
- * Returns the Mento exchange for a given exchange id
119
- * @param exchangeId the id of the exchange
120
- * @returns the exchange with the given id
121
- */
122
- getExchangeById(exchangeId: string): Promise<Exchange>;
123
- /**
124
- * Returns whether trading is enabled in the given mode for a given exchange id
125
- * @param exchangeId the id of the exchange
126
- * @param mode the trading mode
127
- * @returns true if trading is enabled in the given mode, false otherwise
128
- */
129
- isTradingEnabled(exchangeId: string): Promise<boolean>;
130
- /**
131
- * Return the trading limits for a given exchange id. Each limit is an object with the following fields:
132
- * asset: the address of the asset with the limit
133
- * maxIn: the maximum amount of the asset that can be sold
134
- * maxOut: the maximum amount of the asset that can be bought
135
- * until: the timestamp until which the limit is valid
136
- * @param exchangeId the id of the exchange
137
- * @returns the list of trading limits
138
- */
139
- getTradingLimits(exchangeId: string): Promise<TradingLimit[]>;
140
- /**
141
- * Returns the trading limits configurations for a given exchange id
142
- * @param exchangeId the id of the exchange
143
- * @returns the trading limits configuration
144
- */
145
- getTradingLimitConfig(exchangeId: string): Promise<TradingLimitsConfig[]>;
146
- /**
147
- * Returns the trading limits state for a given exchange id
148
- * @param exchangeId the id of the exchange
149
- * @returns the trading limits state
150
- */
151
- getTradingLimitState(exchangeId: string): Promise<TradingLimitsState[]>;
152
- }
package/dist/esm/mento.js DELETED
@@ -1,314 +0,0 @@
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 { BiPoolManager__factory, Broker__factory, IBreakerBox__factory, IBroker__factory, IExchangeProvider__factory, } from '@mento-protocol/mento-core-ts';
11
- import { Signer } from 'ethers';
12
- import { getBrokerAddressFromRegistry, getSymbolFromTokenAddress, increaseAllowance, validateSigner, validateSignerOrProvider, } from './utils';
13
- import { getLimits, getLimitsConfig, getLimitsState } from './limits';
14
- import { strict as assert } from 'assert';
15
- export class Mento {
16
- /**
17
- * This constructor is private, use the static create or createWithParams methods
18
- * to create a new Mento instance
19
- * @param signerOrProvider an ethers provider or connected signer
20
- * @param brokerAddress the address of the broker contract
21
- * @param exchanges exchange data for the broker
22
- */
23
- constructor(signerOrProvider, brokerAddress, exchanges) {
24
- this.signerOrProvider = signerOrProvider;
25
- this.broker = IBroker__factory.connect(brokerAddress, signerOrProvider);
26
- this.exchanges = exchanges || [];
27
- }
28
- /**
29
- * Creates a new Mento object instance.
30
- * When constructed with only a Provider only read-only operations are supported
31
- * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
32
- * @returns a new Mento object instance
33
- */
34
- static create(signerOrProvider) {
35
- return __awaiter(this, void 0, void 0, function* () {
36
- validateSignerOrProvider(signerOrProvider);
37
- return new Mento(signerOrProvider, yield getBrokerAddressFromRegistry(signerOrProvider));
38
- });
39
- }
40
- /**
41
- * Create a new Mento object instance given a broker address and optional exchanges data
42
- * When constructed with a Provider, only read-only operations are supported
43
- * @param signerOrProvider an ethers signer or provider. A signer is required to execute swaps
44
- * @param brokerAddr the address of the broker contract
45
- * @param exchanges the exchanges data for the broker
46
- * @returns a new Mento object instance
47
- */
48
- static createWithParams(signerOrProvider, brokerAddr, exchanges) {
49
- validateSignerOrProvider(signerOrProvider);
50
- return new Mento(signerOrProvider, brokerAddr, exchanges);
51
- }
52
- /**
53
- * Returns a new Mento instance connected to the given signer
54
- * @param signer an ethers signer
55
- * @returns new Mento object instance
56
- */
57
- connectSigner(signer) {
58
- validateSigner(signer);
59
- return new Mento(signer, this.broker.address, this.exchanges);
60
- }
61
- /**
62
- * Returns a list of all the pairs that can be traded on Mento
63
- * @returns The list of tradeable pairs in the form of [{address, symbol}]
64
- */
65
- getTradeablePairs() {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- const exchanges = yield this.getExchanges();
68
- const pairs = [];
69
- for (const exchange of exchanges) {
70
- const asset0 = exchange.assets[0];
71
- const asset1 = exchange.assets[1];
72
- const symbols = yield Promise.all([
73
- getSymbolFromTokenAddress(asset0, this.signerOrProvider),
74
- getSymbolFromTokenAddress(asset1, this.signerOrProvider),
75
- ]);
76
- pairs.push([
77
- { address: asset0, symbol: symbols[0] },
78
- { address: asset1, symbol: symbols[1] },
79
- ]);
80
- }
81
- return pairs;
82
- });
83
- }
84
- /**
85
- * Returns the amount of tokenIn to be sold to buy amountOut of tokenOut
86
- * @param tokenIn the token to be sold
87
- * @param tokenOut the token to be bought
88
- * @param amountOut the amount of tokenOut to be bought
89
- * @returns the amount of tokenIn to be sold
90
- */
91
- getAmountIn(tokenIn, tokenOut, amountOut) {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
94
- return this.broker.getAmountIn(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountOut);
95
- });
96
- }
97
- /**
98
- * Returns the amount of tokenOut to be bought by selling amountIn of tokenIn
99
- * @param tokenIn the token to be sold
100
- * @param tokenOut the token to be bought
101
- * @param amountIn the amount of tokenIn to be sold
102
- * @returns the amount of tokenOut to be bought
103
- */
104
- getAmountOut(tokenIn, tokenOut, amountIn) {
105
- return __awaiter(this, void 0, void 0, function* () {
106
- const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
107
- return this.broker.getAmountOut(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountIn);
108
- });
109
- }
110
- /**
111
- * Increases the broker's trading allowance for the given token
112
- * @param token the token to increase the allowance for
113
- * @param amount the amount to increase the allowance by
114
- * @returns the populated TransactionRequest object
115
- */
116
- increaseTradingAllowance(token, amount) {
117
- return __awaiter(this, void 0, void 0, function* () {
118
- const spender = this.broker.address;
119
- const tx = yield increaseAllowance(token, spender, amount, this.signerOrProvider);
120
- if (Signer.isSigner(this.signerOrProvider)) {
121
- // The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
122
- return this.signerOrProvider.populateTransaction(tx);
123
- }
124
- else {
125
- return tx;
126
- }
127
- });
128
- }
129
- /**
130
- * Returns a token swap populated tx object with a fixed amount of tokenIn and a minimum amount of tokenOut
131
- * Submitting the transaction to execute the swap is left to the consumer
132
- * @param tokenIn the token to be sold
133
- * @param tokenOut the token to be bought
134
- * @param amountIn the amount of tokenIn to be sold
135
- * @param amountOutMin the minimum amount of tokenOut to be bought
136
- * @returns the populated TransactionRequest object
137
- */
138
- swapIn(tokenIn, tokenOut, amountIn, amountOutMin) {
139
- return __awaiter(this, void 0, void 0, function* () {
140
- const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
141
- const tx = yield this.broker.populateTransaction.swapIn(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountIn, amountOutMin);
142
- if (Signer.isSigner(this.signerOrProvider)) {
143
- // The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
144
- return this.signerOrProvider.populateTransaction(tx);
145
- }
146
- else {
147
- return tx;
148
- }
149
- });
150
- }
151
- /**
152
- * Returns a token swap populated tx object with a maximum amount of tokenIn and a fixed amount of tokenOut
153
- * Submitting the transaction to execute the swap is left to the consumer
154
- * @param tokenIn the token to be sold
155
- * @param tokenOut the token to be bought
156
- * @param amountOut the amount of tokenOut to be bought
157
- * @param amountInMax the maximum amount of tokenIn to be sold
158
- * @returns the populated TransactionRequest object
159
- */
160
- swapOut(tokenIn, tokenOut, amountOut, amountInMax) {
161
- return __awaiter(this, void 0, void 0, function* () {
162
- const exchange = yield this.getExchangeForTokens(tokenIn, tokenOut);
163
- const tx = yield this.broker.populateTransaction.swapOut(exchange.providerAddr, exchange.id, tokenIn, tokenOut, amountOut, amountInMax);
164
- if (Signer.isSigner(this.signerOrProvider)) {
165
- // The contract call doesn't populate all of the signer fields, so we need an extra call for the signer
166
- return this.signerOrProvider.populateTransaction(tx);
167
- }
168
- else {
169
- return tx;
170
- }
171
- });
172
- }
173
- /**
174
- * Returns the mento instance's broker contract
175
- * @returns broker contract
176
- */
177
- getBroker() {
178
- return this.broker;
179
- }
180
- /**
181
- * Returns the list of exchanges available in Mento (cached)
182
- * @returns the list of exchanges
183
- */
184
- getExchanges() {
185
- return __awaiter(this, void 0, void 0, function* () {
186
- if (this.exchanges.length > 0) {
187
- return this.exchanges;
188
- }
189
- const exchangeProvidersAddresses = yield this.broker.getExchangeProviders();
190
- const exchanges = (yield Promise.all(exchangeProvidersAddresses.map((a) => this.getExchangesForProvider(a)))).flat();
191
- this.exchanges = exchanges;
192
- return exchanges;
193
- });
194
- }
195
- /**
196
- * Returns the list of exchanges for a given exchange provider address
197
- * @returns list of exchanges
198
- */
199
- getExchangesForProvider(exchangeProviderAddr) {
200
- return __awaiter(this, void 0, void 0, function* () {
201
- const exchangeProvider = IExchangeProvider__factory.connect(exchangeProviderAddr, this.signerOrProvider);
202
- const exchangesInProvider = yield exchangeProvider.getExchanges();
203
- return exchangesInProvider.map((e) => {
204
- assert(e.assets.length === 2, 'Exchange must have 2 assets');
205
- return {
206
- providerAddr: exchangeProviderAddr,
207
- id: e.exchangeId,
208
- assets: e.assets,
209
- };
210
- });
211
- });
212
- }
213
- /**
214
- * Returns the Mento exchange (if any) for a given pair of tokens
215
- * @param token0 the address of the first token
216
- * @param token1 the address of the second token
217
- * @returns exchange
218
- */
219
- getExchangeForTokens(token0, token1) {
220
- return __awaiter(this, void 0, void 0, function* () {
221
- const exchanges = (yield this.getExchanges()).filter((e) => e.assets.includes(token0) && e.assets.includes(token1));
222
- if (exchanges.length === 0) {
223
- throw Error(`No exchange found for ${token0} and ${token1}`);
224
- }
225
- assert(exchanges.length === 1, `More than one exchange found for ${token0} and ${token1}`);
226
- return exchanges[0];
227
- });
228
- }
229
- /**
230
- * Returns the Mento exchange for a given exchange id
231
- * @param exchangeId the id of the exchange
232
- * @returns the exchange with the given id
233
- */
234
- getExchangeById(exchangeId) {
235
- return __awaiter(this, void 0, void 0, function* () {
236
- const exchanges = (yield this.getExchanges()).filter((e) => e.id === exchangeId);
237
- if (exchanges.length === 0) {
238
- throw Error(`No exchange found for id ${exchangeId}`);
239
- }
240
- assert(exchanges.length === 1, `More than one exchange found with id ${exchangeId}`);
241
- return exchanges[0];
242
- });
243
- }
244
- /**
245
- * Returns whether trading is enabled in the given mode for a given exchange id
246
- * @param exchangeId the id of the exchange
247
- * @param mode the trading mode
248
- * @returns true if trading is enabled in the given mode, false otherwise
249
- */
250
- isTradingEnabled(exchangeId) {
251
- return __awaiter(this, void 0, void 0, function* () {
252
- const exchange = yield this.getExchangeById(exchangeId);
253
- const biPoolManager = BiPoolManager__factory.connect(exchange.providerAddr, this.signerOrProvider);
254
- const [breakerBoxAddr, exchangeConfig] = yield Promise.all([
255
- biPoolManager.breakerBox(),
256
- biPoolManager.getPoolExchange(exchangeId),
257
- ]);
258
- const breakerBox = IBreakerBox__factory.connect(breakerBoxAddr, this.signerOrProvider);
259
- const currentMode = yield breakerBox.getRateFeedTradingMode(exchangeConfig.config.referenceRateFeedID);
260
- const BI_DIRECTIONAL_TRADING_MODE = 0;
261
- return currentMode == BI_DIRECTIONAL_TRADING_MODE;
262
- });
263
- }
264
- /**
265
- * Return the trading limits for a given exchange id. Each limit is an object with the following fields:
266
- * asset: the address of the asset with the limit
267
- * maxIn: the maximum amount of the asset that can be sold
268
- * maxOut: the maximum amount of the asset that can be bought
269
- * until: the timestamp until which the limit is valid
270
- * @param exchangeId the id of the exchange
271
- * @returns the list of trading limits
272
- */
273
- getTradingLimits(exchangeId) {
274
- return __awaiter(this, void 0, void 0, function* () {
275
- const exchange = yield this.getExchangeById(exchangeId);
276
- const broker = Broker__factory.connect(this.broker.address, this.signerOrProvider);
277
- const asset0Limits = yield getLimits(broker, exchangeId, exchange.assets[0]);
278
- const asset1Limits = yield getLimits(broker, exchangeId, exchange.assets[1]);
279
- return asset0Limits.concat(asset1Limits);
280
- });
281
- }
282
- /**
283
- * Returns the trading limits configurations for a given exchange id
284
- * @param exchangeId the id of the exchange
285
- * @returns the trading limits configuration
286
- */
287
- getTradingLimitConfig(exchangeId) {
288
- return __awaiter(this, void 0, void 0, function* () {
289
- const exchange = yield this.getExchangeById(exchangeId);
290
- const broker = Broker__factory.connect(this.broker.address, this.signerOrProvider);
291
- const cfgs = [];
292
- for (const asset of exchange.assets) {
293
- const limitCfg = yield getLimitsConfig(broker, exchangeId, asset);
294
- const isLimitConfigured = limitCfg.flags > 0;
295
- if (isLimitConfigured) {
296
- cfgs.push(limitCfg);
297
- }
298
- }
299
- return cfgs;
300
- });
301
- }
302
- /**
303
- * Returns the trading limits state for a given exchange id
304
- * @param exchangeId the id of the exchange
305
- * @returns the trading limits state
306
- */
307
- getTradingLimitState(exchangeId) {
308
- return __awaiter(this, void 0, void 0, function* () {
309
- const broker = Broker__factory.connect(this.broker.address, this.signerOrProvider);
310
- const configuredLimitCfgs = yield this.getTradingLimitConfig(exchangeId);
311
- return yield Promise.all(configuredLimitCfgs.map((cfg) => __awaiter(this, void 0, void 0, function* () { return yield getLimitsState(broker, exchangeId, cfg.asset); })));
312
- });
313
- }
314
- }
@@ -1,4 +0,0 @@
1
- import { ContractAddresses } from '.';
2
- export type ContractAddressMap = {
3
- [chainId: number]: ContractAddresses;
4
- };
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- export * from './contractAddresses';
2
- export * from './contractAddressMap';
@@ -1,2 +0,0 @@
1
- export * from './contractAddresses';
2
- export * from './contractAddressMap';
@@ -1,36 +0,0 @@
1
- import { BigNumberish, providers, Signer } from 'ethers';
2
- import { Address } from './interfaces';
3
- /**
4
- * Ensures that given signer is truly a a connected signer
5
- * @param signer an ethers signer
6
- * @throws if signer is invalid or not connected
7
- */
8
- export declare function validateSigner(signer: Signer): void;
9
- /**
10
- * Ensures that given signerOrProvider is truly a provider or a connected signer
11
- * @param signerOrProvider an ethers provider or signer
12
- * @throws if signerOrProvider is invalid or not connected
13
- */
14
- export declare function validateSignerOrProvider(signerOrProvider: Signer | providers.Provider): void;
15
- /**
16
- * Returns the broker address from the Celo registry
17
- * @param signerOrProvider an ethers provider or signer
18
- * @returns the broker address
19
- */
20
- export declare function getBrokerAddressFromRegistry(signerOrProvider: Signer | providers.Provider): Promise<Address>;
21
- /**
22
- * Returns the symbol of an erc20 token
23
- * @param tokenAddr the address of the erc20 token
24
- * @param signerOrProvider an ethers provider or signer
25
- * @returns the symbol of the erc20 token
26
- */
27
- export declare function getSymbolFromTokenAddress(tokenAddr: Address, signerOrProvider: Signer | providers.Provider): Promise<string>;
28
- /**
29
- * Returns a populated tx obj for increasing the allowance of a spender for a given erc20 token by a given amount
30
- * @param tokenAddr the address of the erc20 token
31
- * @param spender the address of the spender
32
- * @param amount the amount to increase the allowance by
33
- * @param signerOrProvider an ethers signer or provider
34
- * @returns the populated TransactionRequest object
35
- */
36
- export declare function increaseAllowance(tokenAddr: string, spender: string, amount: BigNumberish, signerOrProvider: Signer | providers.Provider): Promise<providers.TransactionRequest>;