@aurigami/sdk 0.3.4 → 0.4.2

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,13 @@ 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 type HypotheticalStats = {
56
+ newBorrowLimit: CurrencyAmount;
57
+ newBorrowUtilization: number;
58
+ };
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.3.4",
2
+ "version": "0.4.2",
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
  }
@@ -32,7 +32,7 @@ export class MoneyMarketRead extends BlockchainEntityRead {
32
32
  constructor(networkConnection: NetworkConnection, auToken: Address, underlyingAsset: Address) {
33
33
  super(networkConnection);
34
34
  if (isSameAddress(underlyingAsset, ETHAddress)) {
35
- this.auToken = new Contract(auToken, AuETHABI.abi, networkConnection.provider) as AuETH
35
+ this.auToken = new Contract(auToken, AuETHABI.abi, networkConnection.provider) as AuETH;
36
36
  } else {
37
37
  this.auToken = new Contract(auToken, AuErc20ABI.abi, networkConnection.provider) as AuErc20;
38
38
  }
package/src/SDK.ts CHANGED
@@ -7,8 +7,9 @@ import {
7
7
  UserMarketDetails,
8
8
  Address,
9
9
  CurrencyAmount,
10
+ HypotheticalStats
10
11
  } from './types';
11
- import { AVAX_DEFAULT_PROVIDER_URL, networkAddresses, BORROW_LIMIT_BUFFER, DECIMAL_PRECISION, REWARDCLAIMSTART, ONE_DAY, AurPlyPid } from './constants';
12
+ import { networkAddresses, BORROW_LIMIT_BUFFER, DECIMAL_PRECISION, REWARDCLAIMSTART, ONE_DAY, AurPlyPid } from './constants';
12
13
  import { TransactionResponse } from '@ethersproject/abstract-provider';
13
14
  import { Contract, BigNumber as BN, ethers, providers } from 'ethers';
14
15
  import { isSameAddress, decimalFactor, getCurrentTimestamp, calcLMRewardApr } from './helpers';
@@ -22,7 +23,6 @@ import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/Au
22
23
  import ComptrollerABI from '@aurigami/contracts/artifacts/contracts/Comptroller.sol/Comptroller.json';
23
24
  import TokenLockABI from "@aurigami/contracts/artifacts/contracts/TokenLock.sol/TokenLock.json";
24
25
  import AuriFairLaunchABI from "@aurigami/contracts/artifacts/contracts/AuriFairLaunch.sol/AuriFairLaunch.json";
25
- import dummyABI from "./abis/dummy.json";
26
26
 
27
27
  export class SdkRead extends BlockchainEntityRead {
28
28
 
@@ -69,7 +69,10 @@ export class SdkRead extends BlockchainEntityRead {
69
69
  promises.push(moneyMarket.getUserDepositBalance(userAddress).then((res: TokenAmount) => {userMarketDetail.depositBalance = res}));
70
70
  promises.push(moneyMarket.getUserBorrowBalance(userAddress).then((res: TokenAmount) => {userMarketDetail.borrowBalance = res}));
71
71
  promises.push(moneyMarket.getCollateralRatio().then((res: BigNumber) => {userMarketDetail.collateralRatio = res.toNumber()}));
72
- pricePromises.push(fetchPrice(moneyMarket.underlying.address, this._networkConnection.provider));
72
+ pricePromises.push(fetchPrice(moneyMarket.underlying.address, this._networkConnection.provider).then((res) => {
73
+ userMarketDetail.underlyingPrice = res.toString();
74
+ return res;
75
+ }));
73
76
  }
74
77
 
75
78
  promises.push(this._comptroller.getAssetsIn(userAddress).then((res: string[]) => {assetsIn = res}));
@@ -101,8 +104,14 @@ export class SdkRead extends BlockchainEntityRead {
101
104
  promises.push(this._tokenLock.lockedAmounts(userAddress));
102
105
  var values = await Promise.all(promises);
103
106
  accountLiquidity = values[0]; rewardBalancesMetadata = values[1]; lockedAmount = values[2];
104
- const borrowedvaluation: BigNumber = totalBorrowLimit.minus(new BigNumber(accountLiquidity[1].toString()).div(decimalFactor(18)));
107
+ var borrowedvaluation: BigNumber = totalBorrowLimit.minus(new BigNumber(accountLiquidity[1].toString()).div(decimalFactor(18)));
108
+
109
+ 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
110
+ borrowedvaluation = new BigNumber(0);
111
+ }
112
+
105
113
  const bufferedBorrowLimit: BigNumber = totalBorrowLimit.multipliedBy(BORROW_LIMIT_BUFFER);
114
+ const minimumBorrowLimitAllowed = borrowedvaluation.div(BORROW_LIMIT_BUFFER);
106
115
 
107
116
  var borrowableAmount: BigNumber;
108
117
  if (bufferedBorrowLimit.lte(borrowedvaluation)) {
@@ -114,10 +123,17 @@ export class SdkRead extends BlockchainEntityRead {
114
123
  for (var i = 0; i < marketCount; i ++) {
115
124
  const auToken = networkAddresses.auTokens[i];
116
125
  const moneyMarket: MoneyMarketRead = new MoneyMarketRead(this._networkConnection, auToken.address, auToken.underlying);
117
- userMarketDetails[i].maxWithdrawableAmount = new TokenAmount(
118
- moneyMarket.underlying,
119
- borrowableAmount.div(underlyingPrices[i]).toFixed(18)
120
- )
126
+ const maxWithdrawCap: BigNumber = (totalBorrowLimit.minus(minimumBorrowLimitAllowed)).div(userMarketDetails[i].collateralRatio).div(underlyingPrices[i]);
127
+ userMarketDetails[i].maxWithdrawableAmount = userMarketDetails[i].isCollateral && maxWithdrawCap.lt(userMarketDetails[i].depositBalance.formattedAmount())
128
+ ? new TokenAmount(
129
+ moneyMarket.underlying,
130
+ maxWithdrawCap.toFixed(24),
131
+ false
132
+ )
133
+ : new TokenAmount(
134
+ moneyMarket.underlying,
135
+ userMarketDetails[i].depositBalance.rawAmount()
136
+ );
121
137
  }
122
138
 
123
139
  return {
@@ -179,6 +195,18 @@ export class SdkRead extends BlockchainEntityRead {
179
195
  userInfo.amount.toString()
180
196
  )
181
197
  }
198
+
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);
202
+ return {
203
+ newBorrowLimit: {
204
+ amount: newBorrowLimit.toFixed(DECIMAL_PRECISION),
205
+ currency: "USD"
206
+ },
207
+ newBorrowUtilization: new BigNumber(args.currentBorrowValuation.amount).div(newBorrowLimit).toNumber()
208
+ }
209
+ }
182
210
  }
183
211
 
184
212
  export class SdkReadWrite extends SdkRead {
@@ -205,7 +233,7 @@ export class SDK extends BlockchainEntity {
205
233
  return new SdkRead(networkConnection);
206
234
  }
207
235
 
208
- public static defaultProvider(): providers.Provider {
209
- return new ethers.providers.JsonRpcProvider(AVAX_DEFAULT_PROVIDER_URL);
236
+ public readWrite(networkConnection: NetworkConnection): SdkReadWrite {
237
+ return new SdkReadWrite(networkConnection);
210
238
  }
211
239
  }
package/src/constants.ts CHANGED
@@ -1,26 +1,8 @@
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
8
  export const DECIMAL_PRECISION = 6;
package/src/dummy.ts CHANGED
@@ -39,4 +39,6 @@ export const dummyUserMarketDetails: UserMarketDetails = {
39
39
  borrowBalance: dummyZeroTokenAmount,
40
40
  isCollateral: true,
41
41
  maxWithdrawableAmount: dummyTokenAmount,
42
+ collateralRatio: 0.5,
43
+ underlyingPrice: "1"
42
44
  };
@@ -7,9 +7,8 @@ import { PriceOracle } from "@aurigami/contracts/typechain";
7
7
  import { TokenAmount } from "./tokenAmount";
8
8
  import { Address } from "./types";
9
9
  import IUniswapV2PairABI from './abis/IUniswapV2Pair.json';
10
- import dummyABI from "./abis/dummy.json";
11
-
12
- const axios = require('axios')
10
+ import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/AuriLens.json';
11
+ import axios from 'axios';
13
12
 
14
13
  export async function fetchPriceFromCoingeckoAPI(id: string): Promise<BigNumber> {
15
14
  const price = await axios.get(
@@ -78,8 +77,11 @@ export async function fetchPriceFromOracle(auTokenAddress: Address, underlyingDe
78
77
  return processPriceFromOracle(rawPrice, underlyingDecimal)
79
78
  }
80
79
 
81
- export async function fetchUnderlyingTokensPrices(provider: providers.Provider) {
82
- const auriLens: Contract = new Contract(networkAddresses.misc.AURILENS, dummyABI.abi, provider);
80
+ export async function fetchUnderlyingTokensPrices(provider: providers.Provider): Promise<{
81
+ auToken: string,
82
+ underlyingPrice: BigNumber
83
+ }[]> {
84
+ const auriLens: Contract = new Contract(networkAddresses.misc.AURILENS, AuriLensABI.abi, provider);
83
85
  const auTokenAddresses = networkAddresses.auTokens.map((auToken) => auToken.address);
84
86
  const underlyingDecimals = networkAddresses.auTokens.map((auToken) => getDecimal(auToken.underlying));
85
87
  const rawPrices = await auriLens.auTokenUnderlyingPriceAll(auTokenAddresses);
@@ -104,7 +106,7 @@ export async function fetchPrice(address: Address, provider: providers.Provider)
104
106
  }
105
107
  }
106
108
  export async function fetchValuation(tokenAmount: TokenAmount, provider: providers.Provider, price?: BigNumber): Promise<BigNumber> {
107
- if (price === null) {
109
+ if (price === undefined) {
108
110
  price = await fetchPrice(tokenAmount.token.address, provider);
109
111
  }
110
112
  return new BigNumber(tokenAmount.formattedAmount()).multipliedBy(price!);
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,15 @@ 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 type HypotheticalStats = {
66
+ newBorrowLimit: CurrencyAmount;
67
+ newBorrowUtilization: number;
68
+ }