@aurigami/sdk 0.3.2 → 0.4.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.
package/dist/types.d.ts CHANGED
@@ -45,8 +45,14 @@ export declare type UserMarketDetails = {
45
45
  borrowBalance: TokenAmount;
46
46
  isCollateral: boolean;
47
47
  maxWithdrawableAmount: TokenAmount;
48
+ collateralRatio: number;
49
+ underlyingPrice: string;
48
50
  };
49
51
  export declare type PlyAuroraPoolDetails = {
50
52
  totalStakedLiquidity: TokenAmount;
51
53
  apy: string;
52
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.2",
2
+ "version": "0.4.0",
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,6 +7,7 @@ import {
7
7
  UserMarketDetails,
8
8
  Address,
9
9
  CurrencyAmount,
10
+ HypotheticalStats
10
11
  } from './types';
11
12
  import { AVAX_DEFAULT_PROVIDER_URL, networkAddresses, BORROW_LIMIT_BUFFER, DECIMAL_PRECISION, REWARDCLAIMSTART, ONE_DAY, AurPlyPid } from './constants';
12
13
  import { TransactionResponse } from '@ethersproject/abstract-provider';
@@ -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
 
@@ -39,7 +39,6 @@ export class SdkRead extends BlockchainEntityRead {
39
39
  }
40
40
 
41
41
  public async getLockingDetails(): Promise<LockingDetails> {
42
- console.log(this._networkConnection.provider)
43
42
  const currentTime = await getCurrentTimestamp(this._networkConnection.provider);
44
43
  const currentWeek = BN.from(currentTime - REWARDCLAIMSTART).div(7 * ONE_DAY).toNumber();
45
44
  const denominator = 10000;
@@ -59,7 +58,6 @@ export class SdkRead extends BlockchainEntityRead {
59
58
  public async getUserDetails(userAddress: string): Promise<UserDetails> {
60
59
  const marketCount = networkAddresses.auTokens.length;
61
60
  var userMarketDetails: UserMarketDetails[] = [], assetsIn: string[];
62
- var collateralRatio: BigNumber[] = new Array(marketCount);
63
61
  var promises = [];
64
62
  var totalBorrowLimit = new BigNumber(0);
65
63
  var pricePromises = [];
@@ -70,8 +68,11 @@ export class SdkRead extends BlockchainEntityRead {
70
68
  const moneyMarket: MoneyMarketRead = new MoneyMarketRead(this._networkConnection, auToken.address, auToken.underlying);
71
69
  promises.push(moneyMarket.getUserDepositBalance(userAddress).then((res: TokenAmount) => {userMarketDetail.depositBalance = res}));
72
70
  promises.push(moneyMarket.getUserBorrowBalance(userAddress).then((res: TokenAmount) => {userMarketDetail.borrowBalance = res}));
73
- promises.push(moneyMarket.getCollateralRatio().then((res: BigNumber) => {collateralRatio[i] = res}));
74
- pricePromises.push(fetchPrice(moneyMarket.underlying.address, this._networkConnection.provider));
71
+ promises.push(moneyMarket.getCollateralRatio().then((res: BigNumber) => {userMarketDetail.collateralRatio = res.toNumber()}));
72
+ pricePromises.push(fetchPrice(moneyMarket.underlying.address, this._networkConnection.provider).then((res) => {
73
+ userMarketDetail.underlyingPrice = res.toString();
74
+ return res;
75
+ }));
75
76
  }
76
77
 
77
78
  promises.push(this._comptroller.getAssetsIn(userAddress).then((res: string[]) => {assetsIn = res}));
@@ -82,19 +83,19 @@ export class SdkRead extends BlockchainEntityRead {
82
83
 
83
84
  for (var i = 0; i < marketCount; i ++) {
84
85
  const auToken = networkAddresses.auTokens[i];
85
- if (assetsIn!.filter((auAddress: Address) => {
86
- isSameAddress(auToken.address, auAddress)
87
- }).length > 0) {
86
+ if (assetsIn!.find((auAddress: Address) => isSameAddress(auToken.address, auAddress)) !== undefined) {
88
87
  userMarketDetails[i].isCollateral = true;
88
+ } else {
89
+ userMarketDetails[i].isCollateral = false;
89
90
  }
90
91
  depositValuationPromises.push(fetchValuation(userMarketDetails[i].depositBalance, this._networkConnection.provider, underlyingPrices[i]));
91
92
  }
92
93
 
93
94
  var depositValues = await Promise.all(depositValuationPromises);
94
95
  totalBorrowLimit = depositValues.reduce((p: BigNumber, v: BigNumber, index: number)=> {
95
- if (userMarketDetails[index].isCollateral) return p.plus(v.multipliedBy(collateralRatio[index]));
96
+ if (userMarketDetails[index].isCollateral) return p.plus(v.multipliedBy(userMarketDetails[index].collateralRatio));
96
97
  return p;
97
- })
98
+ }, new BigNumber(0))
98
99
 
99
100
  promises = [];
100
101
  var accountLiquidity: [BN, BN, BN], rewardBalancesMetadata, lockedAmount: BN;
@@ -105,6 +106,7 @@ export class SdkRead extends BlockchainEntityRead {
105
106
  accountLiquidity = values[0]; rewardBalancesMetadata = values[1]; lockedAmount = values[2];
106
107
  const borrowedvaluation: BigNumber = totalBorrowLimit.minus(new BigNumber(accountLiquidity[1].toString()).div(decimalFactor(18)));
107
108
  const bufferedBorrowLimit: BigNumber = totalBorrowLimit.multipliedBy(BORROW_LIMIT_BUFFER);
109
+ const minimumBorrowLimitAllowed = borrowedvaluation.div(BORROW_LIMIT_BUFFER);
108
110
 
109
111
  var borrowableAmount: BigNumber;
110
112
  if (bufferedBorrowLimit.lte(borrowedvaluation)) {
@@ -116,10 +118,17 @@ export class SdkRead extends BlockchainEntityRead {
116
118
  for (var i = 0; i < marketCount; i ++) {
117
119
  const auToken = networkAddresses.auTokens[i];
118
120
  const moneyMarket: MoneyMarketRead = new MoneyMarketRead(this._networkConnection, auToken.address, auToken.underlying);
119
- userMarketDetails[i].maxWithdrawableAmount = new TokenAmount(
120
- moneyMarket.underlying,
121
- borrowableAmount.div(underlyingPrices[i]).toFixed(18)
122
- )
121
+ const maxWithdrawCap: BigNumber = (totalBorrowLimit.minus(minimumBorrowLimitAllowed)).div(userMarketDetails[i].collateralRatio).div(underlyingPrices[i]);
122
+ userMarketDetails[i].maxWithdrawableAmount = userMarketDetails[i].isCollateral && maxWithdrawCap.lt(userMarketDetails[i].depositBalance.formattedAmount())
123
+ ? new TokenAmount(
124
+ moneyMarket.underlying,
125
+ maxWithdrawCap.toFixed(24),
126
+ false
127
+ )
128
+ : new TokenAmount(
129
+ moneyMarket.underlying,
130
+ userMarketDetails[i].depositBalance.rawAmount()
131
+ );
123
132
  }
124
133
 
125
134
  return {
@@ -181,6 +190,18 @@ export class SdkRead extends BlockchainEntityRead {
181
190
  userInfo.amount.toString()
182
191
  )
183
192
  }
193
+
194
+ public calcHypotheticalStats(args: {userMarketDetail: UserMarketDetails, currentBorrowLimit: CurrencyAmount, currentBorrowValuation: CurrencyAmount, isEnable: boolean}): HypotheticalStats {
195
+ const deltaBorrowLimit = new BigNumber(args.userMarketDetail.depositBalance.formattedAmount()).multipliedBy(args.userMarketDetail.underlyingPrice).multipliedBy(args.userMarketDetail.collateralRatio).multipliedBy(BORROW_LIMIT_BUFFER);
196
+ const newBorrowLimit = args.isEnable ? new BigNumber(args.currentBorrowLimit.amount).plus(deltaBorrowLimit) : new BigNumber(args.currentBorrowLimit.amount).minus(deltaBorrowLimit);
197
+ return {
198
+ newBorrowLimit: {
199
+ amount: newBorrowLimit.toFixed(DECIMAL_PRECISION),
200
+ currency: "USD"
201
+ },
202
+ newBorrowUtilization: new BigNumber(args.currentBorrowValuation.amount).div(newBorrowLimit).toNumber()
203
+ }
204
+ }
184
205
  }
185
206
 
186
207
  export class SdkReadWrite extends SdkRead {
package/src/constants.ts CHANGED
@@ -2,6 +2,9 @@ import BigNumber from 'bignumber.js';
2
2
  // import ethers from 'ethers';
3
3
  const ethers = require('ethers');
4
4
  import { Signer } from 'ethers';
5
+ import * as dotenv from "dotenv";
6
+ dotenv.config();
7
+
5
8
  export const AVAX_DEFAULT_PROVIDER_URL =
6
9
  'https://api.avax.network/ext/bc/C/rpc';
7
10
 
@@ -9,7 +12,7 @@ export const AVAX_DEFAULT_PROVIDER = new ethers.providers.JsonRpcProvider(
9
12
  AVAX_DEFAULT_PROVIDER_URL
10
13
  );
11
14
 
12
- export const AURORA_DEFAULT_PROVIDER_URL = "https://mainnet.aurora.dev";
15
+ export const AURORA_DEFAULT_PROVIDER_URL = `https://mainnet.aurora.dev/${process.env.AURORA_API_KEY}`;
13
16
 
14
17
  export const AURORA_DEFAULT_PROVIDER = new ethers.providers.JsonRpcProvider(
15
18
  AURORA_DEFAULT_PROVIDER_URL
package/src/dummy.ts CHANGED
@@ -39,4 +39,5 @@ export const dummyUserMarketDetails: UserMarketDetails = {
39
39
  borrowBalance: dummyZeroTokenAmount,
40
40
  isCollateral: true,
41
41
  maxWithdrawableAmount: dummyTokenAmount,
42
+ collateralRatio: 0.5
42
43
  };
@@ -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);
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 {
@@ -53,9 +53,16 @@ export type UserMarketDetails = {
53
53
  borrowBalance: TokenAmount;
54
54
  isCollateral: boolean;
55
55
  maxWithdrawableAmount: TokenAmount;
56
+ collateralRatio: number;
57
+ underlyingPrice: string;
56
58
  };
57
59
 
58
60
  export type PlyAuroraPoolDetails = {
59
61
  totalStakedLiquidity: TokenAmount;
60
62
  apy: string;
61
- };
63
+ };
64
+
65
+ export type HypotheticalStats = {
66
+ newBorrowLimit: CurrencyAmount;
67
+ newBorrowUtilization: number;
68
+ }