@aurigami/sdk 0.3.5 → 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
@@ -46,8 +46,21 @@ export declare type UserMarketDetails = {
46
46
  isCollateral: boolean;
47
47
  maxWithdrawableAmount: TokenAmount;
48
48
  collateralRatio: number;
49
+ underlyingPrice: string;
49
50
  };
50
51
  export declare type PlyAuroraPoolDetails = {
51
52
  totalStakedLiquidity: TokenAmount;
52
53
  apy: string;
53
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
+ }
63
+ export declare type HypotheticalStats = {
64
+ newBorrowLimit: CurrencyAmount;
65
+ newBorrowUtilization: number;
66
+ };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.3.5",
2
+ "version": "0.4.4",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -60,6 +60,7 @@
60
60
  "@aurigami/contracts": "^1.1.1",
61
61
  "axios": "^0.24.0",
62
62
  "bignumber.js": "^9.0.2",
63
+ "dotenv": "^16.0.0",
63
64
  "ethers": "^5.0.0"
64
65
  }
65
66
  }
@@ -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,13 +7,15 @@ import {
7
7
  UserMarketDetails,
8
8
  Address,
9
9
  CurrencyAmount,
10
+ HypotheticalStats,
11
+ MarketAction
10
12
  } from './types';
11
- import { AVAX_DEFAULT_PROVIDER_URL, networkAddresses, BORROW_LIMIT_BUFFER, DECIMAL_PRECISION, REWARDCLAIMSTART, ONE_DAY, AurPlyPid } from './constants';
13
+ import { networkAddresses, BORROW_LIMIT_BUFFER, DECIMAL_PRECISION, REWARDCLAIMSTART, ONE_DAY, AurPlyPid } from './constants';
12
14
  import { TransactionResponse } from '@ethersproject/abstract-provider';
13
15
  import { Contract, BigNumber as BN, ethers, providers } from 'ethers';
14
16
  import { isSameAddress, decimalFactor, getCurrentTimestamp, calcLMRewardApr } from './helpers';
15
17
  import { MoneyMarketRead } from './MoneyMarket';
16
- import { fetchValuation, fetchPrice } from './priceFetcher';
18
+ import { fetchValuation, fetchPrice, calcValuation } from './priceFetcher';
17
19
  import { AuriFairLaunch, Comptroller, TokenLock, AuriLens } from '@aurigami/contracts/typechain';
18
20
  import { TokenAmount } from './tokenAmount';
19
21
  import { AURPLYToken, PLYToken, Token } from './token';
@@ -22,7 +24,6 @@ import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/Au
22
24
  import ComptrollerABI from '@aurigami/contracts/artifacts/contracts/Comptroller.sol/Comptroller.json';
23
25
  import TokenLockABI from "@aurigami/contracts/artifacts/contracts/TokenLock.sol/TokenLock.json";
24
26
  import AuriFairLaunchABI from "@aurigami/contracts/artifacts/contracts/AuriFairLaunch.sol/AuriFairLaunch.json";
25
- import dummyABI from "./abis/dummy.json";
26
27
 
27
28
  export class SdkRead extends BlockchainEntityRead {
28
29
 
@@ -69,15 +70,17 @@ export class SdkRead extends BlockchainEntityRead {
69
70
  promises.push(moneyMarket.getUserDepositBalance(userAddress).then((res: TokenAmount) => {userMarketDetail.depositBalance = res}));
70
71
  promises.push(moneyMarket.getUserBorrowBalance(userAddress).then((res: TokenAmount) => {userMarketDetail.borrowBalance = res}));
71
72
  promises.push(moneyMarket.getCollateralRatio().then((res: BigNumber) => {userMarketDetail.collateralRatio = res.toNumber()}));
72
- pricePromises.push(fetchPrice(moneyMarket.underlying.address, this._networkConnection.provider));
73
+ pricePromises.push(fetchPrice(moneyMarket.underlying.address, this._networkConnection.provider).then((res) => {
74
+ userMarketDetail.underlyingPrice = res.toString();
75
+ return res;
76
+ }));
73
77
  }
74
78
 
75
79
  promises.push(this._comptroller.getAssetsIn(userAddress).then((res: string[]) => {assetsIn = res}));
76
80
 
77
81
  await Promise.all(promises);
78
82
  var underlyingPrices = await Promise.all(pricePromises);
79
- var depositValuationPromises = [];
80
-
83
+ var depositValues = []
81
84
  for (var i = 0; i < marketCount; i ++) {
82
85
  const auToken = networkAddresses.auTokens[i];
83
86
  if (assetsIn!.find((auAddress: Address) => isSameAddress(auToken.address, auAddress)) !== undefined) {
@@ -85,10 +88,9 @@ export class SdkRead extends BlockchainEntityRead {
85
88
  } else {
86
89
  userMarketDetails[i].isCollateral = false;
87
90
  }
88
- depositValuationPromises.push(fetchValuation(userMarketDetails[i].depositBalance, this._networkConnection.provider, underlyingPrices[i]));
91
+ depositValues.push(calcValuation(userMarketDetails[i].depositBalance, underlyingPrices[i]));
89
92
  }
90
93
 
91
- var depositValues = await Promise.all(depositValuationPromises);
92
94
  totalBorrowLimit = depositValues.reduce((p: BigNumber, v: BigNumber, index: number)=> {
93
95
  if (userMarketDetails[index].isCollateral) return p.plus(v.multipliedBy(userMarketDetails[index].collateralRatio));
94
96
  return p;
@@ -101,7 +103,12 @@ export class SdkRead extends BlockchainEntityRead {
101
103
  promises.push(this._tokenLock.lockedAmounts(userAddress));
102
104
  var values = await Promise.all(promises);
103
105
  accountLiquidity = values[0]; rewardBalancesMetadata = values[1]; lockedAmount = values[2];
104
- const borrowedvaluation: BigNumber = totalBorrowLimit.minus(new BigNumber(accountLiquidity[1].toString()).div(decimalFactor(18)));
106
+ var borrowedvaluation: BigNumber = totalBorrowLimit.minus(new BigNumber(accountLiquidity[1].toString()).div(decimalFactor(18)));
107
+
108
+ if (borrowedvaluation.lt("0.00001")) { // Igore dust, dust could be the result of network delay causing collateral valuation calc to be different on vs off chain
109
+ borrowedvaluation = new BigNumber(0);
110
+ }
111
+
105
112
  const bufferedBorrowLimit: BigNumber = totalBorrowLimit.multipliedBy(BORROW_LIMIT_BUFFER);
106
113
  const minimumBorrowLimitAllowed = borrowedvaluation.div(BORROW_LIMIT_BUFFER);
107
114
 
@@ -187,6 +194,48 @@ export class SdkRead extends BlockchainEntityRead {
187
194
  userInfo.amount.toString()
188
195
  )
189
196
  }
197
+
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
+ }
231
+ return {
232
+ newBorrowLimit: {
233
+ amount: newBorrowLimit!.toFixed(DECIMAL_PRECISION),
234
+ currency: "USD"
235
+ },
236
+ newBorrowUtilization: newBorrowValuation!.div(newBorrowLimit!).toNumber()
237
+ }
238
+ }
190
239
  }
191
240
 
192
241
  export class SdkReadWrite extends SdkRead {
@@ -213,7 +262,7 @@ export class SDK extends BlockchainEntity {
213
262
  return new SdkRead(networkConnection);
214
263
  }
215
264
 
216
- public static defaultProvider(): providers.Provider {
217
- return new ethers.providers.JsonRpcProvider(AVAX_DEFAULT_PROVIDER_URL);
265
+ public readWrite(networkConnection: NetworkConnection): SdkReadWrite {
266
+ return new SdkReadWrite(networkConnection);
218
267
  }
219
268
  }
package/src/constants.ts CHANGED
@@ -1,29 +1,11 @@
1
1
  import BigNumber from 'bignumber.js';
2
- // import ethers from 'ethers';
3
- const ethers = require('ethers');
4
- import { Signer } from 'ethers';
5
- export const AVAX_DEFAULT_PROVIDER_URL =
6
- 'https://api.avax.network/ext/bc/C/rpc';
7
-
8
- export const AVAX_DEFAULT_PROVIDER = new ethers.providers.JsonRpcProvider(
9
- AVAX_DEFAULT_PROVIDER_URL
10
- );
11
-
12
- export const AURORA_DEFAULT_PROVIDER_URL = "https://mainnet.aurora.dev";
13
-
14
- export const AURORA_DEFAULT_PROVIDER = new ethers.providers.JsonRpcProvider(
15
- AURORA_DEFAULT_PROVIDER_URL
16
- );
2
+ import { ethers, Signer} from 'ethers';
17
3
  export const dummyAddress: string = "0xDEADbeEfEEeEEEeEEEeEEeeeeeEeEEeeeeEEEEeE";
18
4
  export const ETHAddress: string = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
19
5
 
20
- const dummyPrivateKey =
21
- '1111111111111111111111111111111111111111111111111111111111111111';
22
- export const AURORA_DEFAULT_SIGNER: Signer = new ethers.Wallet(dummyPrivateKey, AURORA_DEFAULT_PROVIDER);
23
-
24
6
  export const AURORA_CHAINID = 1313161554;
25
7
 
26
- export const DECIMAL_PRECISION = 6;
8
+ export const DECIMAL_PRECISION = 10;
27
9
 
28
10
  export type NetworkAddresses = {
29
11
  auTokens: {
package/src/dummy.ts CHANGED
@@ -39,5 +39,6 @@ export const dummyUserMarketDetails: UserMarketDetails = {
39
39
  borrowBalance: dummyZeroTokenAmount,
40
40
  isCollateral: true,
41
41
  maxWithdrawableAmount: dummyTokenAmount,
42
- collateralRatio: 0.5
42
+ collateralRatio: 0.5,
43
+ underlyingPrice: "1"
43
44
  };
@@ -8,8 +8,7 @@ import { TokenAmount } from "./tokenAmount";
8
8
  import { Address } from "./types";
9
9
  import IUniswapV2PairABI from './abis/IUniswapV2Pair.json';
10
10
  import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/AuriLens.json';
11
-
12
- const axios = require('axios')
11
+ import axios from 'axios';
13
12
 
14
13
  export async function fetchPriceFromCoingeckoAPI(id: string): Promise<BigNumber> {
15
14
  const price = await axios.get(
@@ -106,9 +105,11 @@ export async function fetchPrice(address: Address, provider: providers.Provider)
106
105
  return fetchPriceFromCoingecko(address);
107
106
  }
108
107
  }
109
- export async function fetchValuation(tokenAmount: TokenAmount, provider: providers.Provider, price?: BigNumber): Promise<BigNumber> {
110
- if (price === null) {
111
- price = await fetchPrice(tokenAmount.token.address, provider);
112
- }
113
- 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);
114
115
  }
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { providers, Signer } from 'ethers';
1
+ import type { BigNumber, providers, Signer } from 'ethers';
2
2
  import { TokenAmount } from './tokenAmount';
3
3
 
4
4
  export enum ComptrollerRewardType {
@@ -54,9 +54,23 @@ export type UserMarketDetails = {
54
54
  isCollateral: boolean;
55
55
  maxWithdrawableAmount: TokenAmount;
56
56
  collateralRatio: number;
57
+ underlyingPrice: string;
57
58
  };
58
59
 
59
60
  export type PlyAuroraPoolDetails = {
60
61
  totalStakedLiquidity: TokenAmount;
61
62
  apy: string;
62
- };
63
+ };
64
+
65
+ export enum MarketAction {
66
+ Deposit = 0,
67
+ Borrow,
68
+ Withdraw,
69
+ Repay,
70
+ EnableCollateral,
71
+ DisableCollateral
72
+ }
73
+ export type HypotheticalStats = {
74
+ newBorrowLimit: CurrencyAmount;
75
+ newBorrowUtilization: number;
76
+ }