@mento-protocol/mento-sdk 0.1.4 → 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.
@@ -0,0 +1,115 @@
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
+ timestep0: cfg['timestep0'],
25
+ timestep1: cfg['timestep1'],
26
+ limit0: cfg['limit0'],
27
+ limit1: cfg['limit1'],
28
+ limitGlobal: cfg['limitGlobal'],
29
+ flags: cfg['flags'],
30
+ };
31
+ });
32
+ }
33
+ /**
34
+ * Returns the limit state in the broker for the given exchange and asset
35
+ * @param broker an instance of the broker
36
+ * @param exchangeId the id of the exchange
37
+ * @param asset the address of the limited asset
38
+ * @returns the limit state
39
+ */
40
+ export function getLimitsState(broker, exchangeId, asset) {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ const limitId = getLimitId(exchangeId, asset);
43
+ const [cfg, state] = yield Promise.all([
44
+ getLimitsConfig(broker, exchangeId, asset),
45
+ broker.tradingLimitsState(limitId),
46
+ ]);
47
+ const isL0Enabled = cfg.timestep0 > 0;
48
+ const isL1Enabled = cfg.timestep1 > 0;
49
+ const nowEpoch = Math.floor(Date.now() / 1000);
50
+ const isL0Outdated = isL0Enabled && nowEpoch > state['lastUpdated0'] + cfg.timestep0;
51
+ const isL1Outdated = isL1Enabled && nowEpoch > state['lastUpdated1'] + cfg.timestep1;
52
+ return {
53
+ lastUpdated0: isL0Outdated ? nowEpoch : state['lastUpdated0'],
54
+ lastUpdated1: isL1Outdated ? nowEpoch : state['lastUpdated1'],
55
+ netflow0: isL0Outdated ? 0 : state['netflow0'],
56
+ netflow1: isL1Outdated ? 0 : state['netflow1'],
57
+ netflowGlobal: state['netflowGlobal'],
58
+ };
59
+ });
60
+ }
61
+ /**
62
+ * Returns a human-friendly representation of the limits for the given exchange and asset
63
+ * @param broker an instance of the broker
64
+ * @param exchangeId the id of the exchange
65
+ * @param asset the address of the asset with the limit
66
+ * @returns a list of TradingLimit objects
67
+ */
68
+ export function getLimits(broker, exchangeId, asset) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ const [cfg, state] = yield Promise.all([
71
+ getLimitsConfig(broker, exchangeId, asset),
72
+ getLimitsState(broker, exchangeId, asset),
73
+ ]);
74
+ const limits = [];
75
+ if (cfg.limit0 > 0) {
76
+ limits.push({
77
+ asset: asset,
78
+ maxIn: cfg.limit0 - state.netflow0,
79
+ maxOut: cfg.limit0 + state.netflow0,
80
+ until: state.lastUpdated0 + cfg.timestep0,
81
+ });
82
+ }
83
+ if (cfg.limit1 > 0) {
84
+ limits.push({
85
+ asset: asset,
86
+ maxIn: cfg.limit1 - state.netflow1,
87
+ maxOut: cfg.limit1 + state.netflow1,
88
+ until: state.lastUpdated1 + cfg.timestep1,
89
+ });
90
+ }
91
+ if (cfg.limitGlobal > 0) {
92
+ const timestampIn2030 = 1893456000; // a far away timestamp
93
+ limits.push({
94
+ asset: asset,
95
+ maxIn: cfg.limitGlobal - state.netflowGlobal,
96
+ maxOut: cfg.limitGlobal + state.netflowGlobal,
97
+ until: timestampIn2030,
98
+ });
99
+ }
100
+ return limits;
101
+ });
102
+ }
103
+ /**
104
+ * Returns the limit id for the given exchange and asset
105
+ * @param exchangeId the id of the exchange
106
+ * @param asset the address of the asset with the limit
107
+ * @returns the limit id
108
+ */
109
+ export function getLimitId(exchangeId, asset) {
110
+ const assetBytes32 = utils.zeroPad(asset, 32);
111
+ const exchangeIdBytes = utils.arrayify(exchangeId);
112
+ const assetBytes = utils.arrayify(assetBytes32);
113
+ assert(exchangeIdBytes.length === assetBytes.length, 'exchangeId and asset0 must be the same length');
114
+ return utils.hexlify(exchangeIdBytes.map((b, i) => b ^ assetBytes[i]));
115
+ }
@@ -1,6 +1,6 @@
1
+ import { Address, TradingLimit, TradingLimitsConfig, TradingLimitsState } from './types';
1
2
  import { IBroker } from '@mento-protocol/mento-core-ts';
2
- import { BigNumber, BigNumberish, providers, Signer } from 'ethers';
3
- import { Address } from './types';
3
+ import { BigNumber, BigNumberish, Signer, providers } from 'ethers';
4
4
  export interface Exchange {
5
5
  providerAddr: Address;
6
6
  id: string;
@@ -109,9 +109,44 @@ export declare class Mento {
109
109
  getExchangesForProvider(exchangeProviderAddr: Address): Promise<Exchange[]>;
110
110
  /**
111
111
  * Returns the Mento exchange (if any) for a given pair of tokens
112
- * @param token0 the first token
113
- * @param token1 the second token
112
+ * @param token0 the address of the first token
113
+ * @param token1 the address of the second token
114
114
  * @returns exchange
115
115
  */
116
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 configuration 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>;
117
152
  }
package/dist/esm/mento.js CHANGED
@@ -7,9 +7,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- import { IBroker__factory, IExchangeProvider__factory, } from '@mento-protocol/mento-core-ts';
10
+ import { BiPoolManager__factory, Broker__factory, IBreakerBox__factory, IBroker__factory, IExchangeProvider__factory, } from '@mento-protocol/mento-core-ts';
11
11
  import { Signer } from 'ethers';
12
12
  import { getBrokerAddressFromRegistry, getSymbolFromTokenAddress, increaseAllowance, validateSigner, validateSignerOrProvider, } from './utils';
13
+ import { getLimits, getLimitsConfig, getLimitsState } from './limits';
13
14
  import { strict as assert } from 'assert';
14
15
  export class Mento {
15
16
  /**
@@ -211,8 +212,8 @@ export class Mento {
211
212
  }
212
213
  /**
213
214
  * Returns the Mento exchange (if any) for a given pair of tokens
214
- * @param token0 the first token
215
- * @param token1 the second token
215
+ * @param token0 the address of the first token
216
+ * @param token1 the address of the second token
216
217
  * @returns exchange
217
218
  */
218
219
  getExchangeForTokens(token0, token1) {
@@ -225,4 +226,82 @@ export class Mento {
225
226
  return exchanges[0];
226
227
  });
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 assetWithLimit = exchange.assets[0]; // currently limits are configured only on asset0
278
+ return getLimits(broker, exchangeId, assetWithLimit);
279
+ });
280
+ }
281
+ /**
282
+ * Returns the trading limits configuration for a given exchange id
283
+ * @param exchangeId the id of the exchange
284
+ * @returns the trading limits configuration
285
+ */
286
+ getTradingLimitConfig(exchangeId) {
287
+ return __awaiter(this, void 0, void 0, function* () {
288
+ const exchange = yield this.getExchangeById(exchangeId);
289
+ const broker = Broker__factory.connect(this.broker.address, this.signerOrProvider);
290
+ const assetWithLimit = exchange.assets[0]; // currently limits are configured only on asset0
291
+ return getLimitsConfig(broker, exchangeId, assetWithLimit);
292
+ });
293
+ }
294
+ /**
295
+ * Returns the trading limits state for a given exchange id
296
+ * @param exchangeId the id of the exchange
297
+ * @returns the trading limits state
298
+ */
299
+ getTradingLimitState(exchangeId) {
300
+ return __awaiter(this, void 0, void 0, function* () {
301
+ const exchange = yield this.getExchangeById(exchangeId);
302
+ const broker = Broker__factory.connect(this.broker.address, this.signerOrProvider);
303
+ const assetWithLimit = exchange.assets[0]; // currently limits are configured only on asset0
304
+ return getLimitsState(broker, exchangeId, assetWithLimit);
305
+ });
306
+ }
228
307
  }
@@ -1 +1,49 @@
1
+ import { ethers, providers } from "ethers";
1
2
  export type Address = string;
3
+ export interface TradingLimit {
4
+ asset: Address;
5
+ maxIn: number;
6
+ maxOut: number;
7
+ until: number;
8
+ }
9
+ export interface TradingLimitsConfig {
10
+ timestep0: number;
11
+ timestep1: number;
12
+ limit0: number;
13
+ limit1: number;
14
+ limitGlobal: number;
15
+ flags: number;
16
+ }
17
+ export interface TradingLimitsState {
18
+ lastUpdated0: number;
19
+ lastUpdated1: number;
20
+ netflow0: number;
21
+ netflow1: number;
22
+ netflowGlobal: number;
23
+ }
24
+ export interface ContractAddressMap {
25
+ [chainId: string]: ContractAddresses;
26
+ }
27
+ export interface ContractAddresses {
28
+ Airgrab: string;
29
+ Emission: string;
30
+ MentoGovernor: string;
31
+ MentoToken: string;
32
+ TimelockController: string;
33
+ Locking: string;
34
+ }
35
+ export declare enum ProposalState {
36
+ PENDING = 0,
37
+ ACTIVE = 1,
38
+ CANCELED = 2,
39
+ DEFEATED = 3,
40
+ SUCCEEDED = 4,
41
+ QUEUED = 5,
42
+ EXPIRED = 6,
43
+ EXECUTED = 7
44
+ }
45
+ export interface IChainClient {
46
+ getSigner(): Promise<ethers.Signer | providers.Provider>;
47
+ getChainId(): Promise<number>;
48
+ populateTransaction(tx: ethers.PopulatedTransaction): Promise<providers.TransactionRequest>;
49
+ }
package/dist/esm/types.js CHANGED
@@ -1 +1,11 @@
1
- export {};
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 = {}));
@@ -1,5 +1,5 @@
1
1
  import { BigNumberish, providers, Signer } from 'ethers';
2
- import { Address } from './types';
2
+ import { Address, ContractAddresses } from './types';
3
3
  /**
4
4
  * Ensures that given signer is truly a a connected signer
5
5
  * @param signer an ethers signer
@@ -34,3 +34,4 @@ export declare function getSymbolFromTokenAddress(tokenAddr: Address, signerOrPr
34
34
  * @returns the populated TransactionRequest object
35
35
  */
36
36
  export declare function increaseAllowance(tokenAddr: string, spender: string, amount: BigNumberish, signerOrProvider: Signer | providers.Provider): Promise<providers.TransactionRequest>;
37
+ export declare function getContractsByChainId(chainId: number): ContractAddresses;
package/dist/esm/utils.js CHANGED
@@ -8,6 +8,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { constants, Contract, providers, Signer } from 'ethers';
11
+ import contractAddresses from './contracts.json';
11
12
  /**
12
13
  * Ensures that given signer is truly a a connected signer
13
14
  * @param signer an ethers signer
@@ -88,3 +89,11 @@ export function increaseAllowance(tokenAddr, spender, amount, signerOrProvider)
88
89
  return yield contract.populateTransaction.increaseAllowance(spender, amount);
89
90
  });
90
91
  }
92
+ export function getContractsByChainId(chainId) {
93
+ const addresses = contractAddresses;
94
+ const contracts = addresses[chainId];
95
+ if (!contracts) {
96
+ throw new Error(`No contracts found for chainId ${chainId}`);
97
+ }
98
+ return contracts;
99
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@mento-protocol/mento-sdk",
3
3
  "description": "Official SDK for interacting with the Mento Protocol",
4
- "version": "0.1.4",
4
+ "version": "0.2.0",
5
5
  "license": "MIT",
6
6
  "author": "Mento Labs",
7
7
  "keywords": [
@@ -77,7 +77,7 @@
77
77
  "typescript": "^4.9.5"
78
78
  },
79
79
  "dependencies": {
80
- "@mento-protocol/mento-core-ts": "^0.1.0"
80
+ "@mento-protocol/mento-core-ts": "^0.2.0"
81
81
  },
82
82
  "peerDependencies": {
83
83
  "ethers": "^5.7"