@aurigami/sdk 0.4.2 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/types.d.ts CHANGED
@@ -52,6 +52,14 @@ export declare type PlyAuroraPoolDetails = {
52
52
  totalStakedLiquidity: TokenAmount;
53
53
  apy: string;
54
54
  };
55
+ export declare enum MarketAction {
56
+ Deposit = 0,
57
+ Borrow = 1,
58
+ Withdraw = 2,
59
+ Repay = 3,
60
+ EnableCollateral = 4,
61
+ DisableCollateral = 5
62
+ }
55
63
  export declare type HypotheticalStats = {
56
64
  newBorrowLimit: CurrencyAmount;
57
65
  newBorrowUtilization: number;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.4.2",
2
+ "version": "0.4.4",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -13,7 +13,7 @@ import { decimalFactor, isSameAddress, calcLMRewardApr } from './helpers';
13
13
  import { Token, PLYToken } from './token';
14
14
  import { TokenAmount } from './tokenAmount';
15
15
  import { getDecimal, validateAndParseAddress } from './helpers';
16
- import { fetchPrice, fetchValuation } from "./priceFetcher";
16
+ import { fetchPrice, calcValuation } from "./priceFetcher";
17
17
 
18
18
 
19
19
  type RewardSpeeds = {
@@ -138,24 +138,22 @@ export class MoneyMarketRead extends BlockchainEntityRead {
138
138
  await Promise.all(promises);
139
139
 
140
140
  var borrowPlyApy = calcLMRewardApr(
141
- await fetchValuation(
141
+ await calcValuation(
142
142
  new TokenAmount(
143
143
  PLYToken,
144
144
  rewardSpeeds!.plyRewardBorrowSpeed.toString()
145
145
  ),
146
- this._networkConnection.provider,
147
146
  plyPrice!
148
147
  ),
149
148
  underlyingPrice!.multipliedBy(totalBorrowed!.formattedAmount()),
150
149
  ONE_YEAR
151
150
  );
152
151
  var depositPlyApy = calcLMRewardApr(
153
- await fetchValuation(
152
+ await calcValuation(
154
153
  new TokenAmount(
155
154
  PLYToken,
156
155
  rewardSpeeds!.plyRewardSupplySpeed.toString()
157
156
  ),
158
- this._networkConnection.provider,
159
157
  plyPrice!
160
158
  ),
161
159
  underlyingPrice!.multipliedBy(totalDeposited!.formattedAmount()),
package/src/SDK.ts CHANGED
@@ -7,14 +7,15 @@ import {
7
7
  UserMarketDetails,
8
8
  Address,
9
9
  CurrencyAmount,
10
- HypotheticalStats
10
+ HypotheticalStats,
11
+ MarketAction
11
12
  } from './types';
12
13
  import { networkAddresses, BORROW_LIMIT_BUFFER, DECIMAL_PRECISION, REWARDCLAIMSTART, ONE_DAY, AurPlyPid } from './constants';
13
14
  import { TransactionResponse } from '@ethersproject/abstract-provider';
14
15
  import { Contract, BigNumber as BN, ethers, providers } from 'ethers';
15
16
  import { isSameAddress, decimalFactor, getCurrentTimestamp, calcLMRewardApr } from './helpers';
16
17
  import { MoneyMarketRead } from './MoneyMarket';
17
- import { fetchValuation, fetchPrice } from './priceFetcher';
18
+ import { fetchValuation, fetchPrice, calcValuation } from './priceFetcher';
18
19
  import { AuriFairLaunch, Comptroller, TokenLock, AuriLens } from '@aurigami/contracts/typechain';
19
20
  import { TokenAmount } from './tokenAmount';
20
21
  import { AURPLYToken, PLYToken, Token } from './token';
@@ -79,8 +80,7 @@ export class SdkRead extends BlockchainEntityRead {
79
80
 
80
81
  await Promise.all(promises);
81
82
  var underlyingPrices = await Promise.all(pricePromises);
82
- var depositValuationPromises = [];
83
-
83
+ var depositValues = []
84
84
  for (var i = 0; i < marketCount; i ++) {
85
85
  const auToken = networkAddresses.auTokens[i];
86
86
  if (assetsIn!.find((auAddress: Address) => isSameAddress(auToken.address, auAddress)) !== undefined) {
@@ -88,10 +88,9 @@ export class SdkRead extends BlockchainEntityRead {
88
88
  } else {
89
89
  userMarketDetails[i].isCollateral = false;
90
90
  }
91
- depositValuationPromises.push(fetchValuation(userMarketDetails[i].depositBalance, this._networkConnection.provider, underlyingPrices[i]));
91
+ depositValues.push(calcValuation(userMarketDetails[i].depositBalance, underlyingPrices[i]));
92
92
  }
93
93
 
94
- var depositValues = await Promise.all(depositValuationPromises);
95
94
  totalBorrowLimit = depositValues.reduce((p: BigNumber, v: BigNumber, index: number)=> {
96
95
  if (userMarketDetails[index].isCollateral) return p.plus(v.multipliedBy(userMarketDetails[index].collateralRatio));
97
96
  return p;
@@ -196,15 +195,45 @@ export class SdkRead extends BlockchainEntityRead {
196
195
  )
197
196
  }
198
197
 
199
- public calcHypotheticalStats(args: {userMarketDetail: UserMarketDetails, currentBorrowLimit: CurrencyAmount, currentBorrowValuation: CurrencyAmount, isEnable: boolean}): HypotheticalStats {
200
- const deltaBorrowLimit = new BigNumber(args.userMarketDetail.depositBalance.formattedAmount()).multipliedBy(args.userMarketDetail.underlyingPrice).multipliedBy(args.userMarketDetail.collateralRatio).multipliedBy(BORROW_LIMIT_BUFFER);
201
- const newBorrowLimit = args.isEnable ? new BigNumber(args.currentBorrowLimit.amount).plus(deltaBorrowLimit) : new BigNumber(args.currentBorrowLimit.amount).minus(deltaBorrowLimit);
198
+ public calcHypotheticalStats({
199
+ userMarketDetail,
200
+ currentBorrowLimit,
201
+ currentBorrowValuation,
202
+ action,
203
+ tokenAmount
204
+ }: {
205
+ userMarketDetail: UserMarketDetails,
206
+ currentBorrowLimit: CurrencyAmount,
207
+ currentBorrowValuation: CurrencyAmount,
208
+ action: MarketAction,
209
+ tokenAmount?: TokenAmount
210
+ }): HypotheticalStats {
211
+ var newBorrowLimit = new BigNumber(currentBorrowLimit.amount), newBorrowValuation = new BigNumber(currentBorrowValuation.amount);
212
+ switch (action) {
213
+ case MarketAction.EnableCollateral:
214
+ case MarketAction.DisableCollateral:
215
+ var deltaBorrowLimit = calcValuation(userMarketDetail.depositBalance, new BigNumber(userMarketDetail.underlyingPrice)).multipliedBy(userMarketDetail.collateralRatio).multipliedBy(BORROW_LIMIT_BUFFER);
216
+ newBorrowLimit = action == MarketAction.EnableCollateral ? newBorrowLimit.plus(deltaBorrowLimit) : newBorrowLimit.minus(deltaBorrowLimit);
217
+ break;
218
+
219
+ case MarketAction.Deposit:
220
+ case MarketAction.Withdraw:
221
+ deltaBorrowLimit = calcValuation(tokenAmount!, new BigNumber(userMarketDetail.underlyingPrice)).multipliedBy(userMarketDetail.collateralRatio).multipliedBy(BORROW_LIMIT_BUFFER);
222
+ newBorrowLimit = action == MarketAction.Deposit ? newBorrowLimit.plus(deltaBorrowLimit) : newBorrowLimit.minus(deltaBorrowLimit);
223
+ break;
224
+
225
+ case MarketAction.Borrow:
226
+ case MarketAction.Repay:
227
+ var deltaBorrowValuation = calcValuation(tokenAmount!, new BigNumber(userMarketDetail.underlyingPrice));
228
+ newBorrowValuation = action == MarketAction.Borrow ? newBorrowValuation.plus(deltaBorrowValuation) : newBorrowValuation.minus(deltaBorrowValuation);
229
+ break;
230
+ }
202
231
  return {
203
232
  newBorrowLimit: {
204
- amount: newBorrowLimit.toFixed(DECIMAL_PRECISION),
233
+ amount: newBorrowLimit!.toFixed(DECIMAL_PRECISION),
205
234
  currency: "USD"
206
235
  },
207
- newBorrowUtilization: new BigNumber(args.currentBorrowValuation.amount).div(newBorrowLimit).toNumber()
236
+ newBorrowUtilization: newBorrowValuation!.div(newBorrowLimit!).toNumber()
208
237
  }
209
238
  }
210
239
  }
package/src/constants.ts CHANGED
@@ -5,7 +5,7 @@ export const ETHAddress: string = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
5
5
 
6
6
  export const AURORA_CHAINID = 1313161554;
7
7
 
8
- export const DECIMAL_PRECISION = 6;
8
+ export const DECIMAL_PRECISION = 10;
9
9
 
10
10
  export type NetworkAddresses = {
11
11
  auTokens: {
@@ -105,9 +105,11 @@ export async function fetchPrice(address: Address, provider: providers.Provider)
105
105
  return fetchPriceFromCoingecko(address);
106
106
  }
107
107
  }
108
- export async function fetchValuation(tokenAmount: TokenAmount, provider: providers.Provider, price?: BigNumber): Promise<BigNumber> {
109
- if (price === undefined) {
110
- price = await fetchPrice(tokenAmount.token.address, provider);
111
- }
112
- return new BigNumber(tokenAmount.formattedAmount()).multipliedBy(price!);
108
+ export async function fetchValuation(tokenAmount: TokenAmount, provider: providers.Provider): Promise<BigNumber> {
109
+ var price = await fetchPrice(tokenAmount.token.address, provider);
110
+ return calcValuation(tokenAmount, price);
111
+ }
112
+
113
+ export function calcValuation(tokenAmount: TokenAmount, price: BigNumber) {
114
+ return new BigNumber(tokenAmount.formattedAmount()).multipliedBy(price);
113
115
  }
package/src/types.ts CHANGED
@@ -62,6 +62,14 @@ export type PlyAuroraPoolDetails = {
62
62
  apy: string;
63
63
  };
64
64
 
65
+ export enum MarketAction {
66
+ Deposit = 0,
67
+ Borrow,
68
+ Withdraw,
69
+ Repay,
70
+ EnableCollateral,
71
+ DisableCollateral
72
+ }
65
73
  export type HypotheticalStats = {
66
74
  newBorrowLimit: CurrencyAmount;
67
75
  newBorrowUtilization: number;