@aurigami/sdk 0.4.0 → 0.4.5

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.0",
2
+ "version": "0.4.5",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -7,13 +7,13 @@ import ComptrollerABI from '@aurigami/contracts/artifacts/contracts/Comptroller.
7
7
  import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/AuriLens.json';
8
8
  import { AuErc20, AuETH, AuriLens, Comptroller } from "@aurigami/contracts/typechain";
9
9
  import { TransactionResponse } from '@ethersproject/abstract-provider';
10
- import { networkAddresses, ETHAddress, ONE_YEAR } from "./constants";
10
+ import { networkAddresses, ETHAddress, ONE_YEAR, INF } from "./constants";
11
11
  import BigNumber from "bignumber.js";
12
12
  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()),
@@ -205,6 +203,9 @@ export class MoneyMarketReadWrite extends MoneyMarketRead {
205
203
  return this.auToken.connect(this._networkConnection.signer!).borrow(amount.rawAmount());
206
204
  }
207
205
  public async withdraw(amount: TokenAmount): Promise<TransactionResponse> {
206
+ if (new BigNumber(amount.rawAmount()).lt(0)) {
207
+ return this.auToken.connect(this._networkConnection.signer!).redeemUnderlying(INF);
208
+ }
208
209
  return this.auToken.connect(this._networkConnection.signer!).redeemUnderlying(amount.rawAmount());
209
210
  }
210
211
  public async repay(amount: TokenAmount): Promise<TransactionResponse> {
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
- 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';
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;
@@ -104,7 +103,12 @@ export class SdkRead extends BlockchainEntityRead {
104
103
  promises.push(this._tokenLock.lockedAmounts(userAddress));
105
104
  var values = await Promise.all(promises);
106
105
  accountLiquidity = values[0]; rewardBalancesMetadata = values[1]; lockedAmount = values[2];
107
- 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
+
108
112
  const bufferedBorrowLimit: BigNumber = totalBorrowLimit.multipliedBy(BORROW_LIMIT_BUFFER);
109
113
  const minimumBorrowLimitAllowed = borrowedvaluation.div(BORROW_LIMIT_BUFFER);
110
114
 
@@ -191,15 +195,45 @@ export class SdkRead extends BlockchainEntityRead {
191
195
  )
192
196
  }
193
197
 
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);
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
+ }
197
231
  return {
198
232
  newBorrowLimit: {
199
- amount: newBorrowLimit.toFixed(DECIMAL_PRECISION),
233
+ amount: newBorrowLimit!.toFixed(DECIMAL_PRECISION),
200
234
  currency: "USD"
201
235
  },
202
- newBorrowUtilization: new BigNumber(args.currentBorrowValuation.amount).div(newBorrowLimit).toNumber()
236
+ newBorrowUtilization: newBorrowValuation!.div(newBorrowLimit!).toNumber()
203
237
  }
204
238
  }
205
239
  }
@@ -228,7 +262,7 @@ export class SDK extends BlockchainEntity {
228
262
  return new SdkRead(networkConnection);
229
263
  }
230
264
 
231
- public static defaultProvider(): providers.Provider {
232
- return new ethers.providers.JsonRpcProvider(AVAX_DEFAULT_PROVIDER_URL);
265
+ public readWrite(networkConnection: NetworkConnection): SdkReadWrite {
266
+ return new SdkReadWrite(networkConnection);
233
267
  }
234
268
  }
package/src/constants.ts CHANGED
@@ -1,32 +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
- import * as dotenv from "dotenv";
6
- dotenv.config();
7
-
8
- export const AVAX_DEFAULT_PROVIDER_URL =
9
- 'https://api.avax.network/ext/bc/C/rpc';
10
-
11
- export const AVAX_DEFAULT_PROVIDER = new ethers.providers.JsonRpcProvider(
12
- AVAX_DEFAULT_PROVIDER_URL
13
- );
14
-
15
- export const AURORA_DEFAULT_PROVIDER_URL = `https://mainnet.aurora.dev/${process.env.AURORA_API_KEY}`;
16
-
17
- export const AURORA_DEFAULT_PROVIDER = new ethers.providers.JsonRpcProvider(
18
- AURORA_DEFAULT_PROVIDER_URL
19
- );
2
+ import { ethers, Signer, BigNumber as BN} from 'ethers';
20
3
  export const dummyAddress: string = "0xDEADbeEfEEeEEEeEEEeEEeeeeeEeEEeeeeEEEEeE";
21
4
  export const ETHAddress: string = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
22
5
 
23
- const dummyPrivateKey =
24
- '1111111111111111111111111111111111111111111111111111111111111111';
25
- export const AURORA_DEFAULT_SIGNER: Signer = new ethers.Wallet(dummyPrivateKey, AURORA_DEFAULT_PROVIDER);
26
-
27
6
  export const AURORA_CHAINID = 1313161554;
28
7
 
29
- export const DECIMAL_PRECISION = 6;
8
+ export const DECIMAL_PRECISION = 10;
30
9
 
31
10
  export type NetworkAddresses = {
32
11
  auTokens: {
@@ -94,4 +73,6 @@ export const ONE_DAY = 86400;
94
73
  export const ONE_YEAR = ONE_DAY * 365;
95
74
 
96
75
  export const AurPlyPid = 0;
76
+ export const INF = BN.from(2).pow(256).sub(1)
77
+
97
78
 
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
  };
@@ -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 === null) {
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;