@aurigami/sdk 1.4.7 → 1.5.0-dummy

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/src/helpers.ts CHANGED
@@ -6,6 +6,7 @@ import { BigNumber as BN, Contract, providers, utils } from "ethers";
6
6
  import { getQuery as getAuroraAPIQuery } from "./aurora-api-helpers";
7
7
  import { decimalRecords } from "./decimals";
8
8
  import { Address, NetworkConnection } from "./types";
9
+ import assert from 'assert';
9
10
 
10
11
  export function formatObject(object: Object) {
11
12
  return JSON.stringify(object, null, ' ');
@@ -32,7 +33,9 @@ export function validateAndParseAddress(address: Address): Address {
32
33
  }
33
34
 
34
35
  export function getDecimal(address: Address): number {
35
- return decimalRecords[address.toLowerCase()];
36
+ let addressLowercase = address.toLowerCase();
37
+ assert(decimalRecords[addressLowercase] != undefined, "Decimal not found for " + address);
38
+ return decimalRecords[addressLowercase];
36
39
  }
37
40
 
38
41
  export function calcLMRewardApr(rewardsValue: BigNumber, totalStakeValue: BigNumber, frequencyPerYear: number): BigNumber {
package/src/index.ts CHANGED
@@ -17,3 +17,4 @@ export * from './tokenAmount';
17
17
  export * from './types';
18
18
  export * from './airdrop-lottery';
19
19
  export * from './transfer-event-query';
20
+ export * from './Papermill';
@@ -1,14 +1,15 @@
1
1
  import BigNumber from "bignumber.js";
2
2
  import { Contract, providers, BigNumber as BN, ethers } from "ethers";
3
- import { networkAddresses } from "./constants";
3
+ import { DECIMAL_PRECISION, networkAddresses, PRICE_WHITELISTED } from "./constants";
4
4
  import OracleABI from "./abis/Oracle.json";
5
5
  import { isSameAddress, decimalFactor, getDecimal } from "./helpers";
6
- import { PriceOracle } from "@aurigami/contracts/typechain";
6
+ import { IUniswapV2Pair, PriceOracle } from "@aurigami/contracts/typechain";
7
7
  import { TokenAmount } from "./tokenAmount";
8
- import { Address } from "./types";
8
+ import { Address, TokenValuation } from "./types";
9
9
  import IUniswapV2PairABI from './abis/IUniswapV2Pair.json';
10
10
  import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/AuriLens.json';
11
11
  import axios from 'axios';
12
+ import assert from 'assert';
12
13
 
13
14
  const hardcodePLYPrice = true;
14
15
  export async function fetchPriceFromCoingeckoAPI(id: string): Promise<BigNumber> {
@@ -44,38 +45,56 @@ export async function fetchLPPositions(LPAddress: Address, provider: providers.P
44
45
  reserve1: BN,
45
46
  totalSupply: BN
46
47
  }> {
47
- var promises = [];
48
- const LPContract = new Contract(LPAddress, IUniswapV2PairABI.abi, provider);
48
+ const LPContract = new Contract(LPAddress, IUniswapV2PairABI.abi, provider) as IUniswapV2Pair;
49
+ var promises: any[] = [];
49
50
  promises.push(LPContract.token0());
50
- promises.push(LPContract.toekn1());
51
+ promises.push(LPContract.token1());
51
52
  promises.push(LPContract.getReserves());
52
53
  promises.push(LPContract.totalSupply());
53
- const values = await Promise.all(promises);
54
+ const values: any[] = await Promise.all(promises);
55
+
54
56
  return {
55
- token0: values[0],
56
- token1: values[1],
57
- reserve0: values[2].reserve0,
58
- reserve1: values[2].reserve1,
59
- totalSupply: values[3]
57
+ token0: values[0] as string,
58
+ token1: values[1] as string,
59
+ reserve0: (values[2] as {reserve0: BN, reserve1: BN}).reserve0,
60
+ reserve1: (values[2] as {reserve0: BN, reserve1: BN}).reserve1,
61
+ totalSupply: values[3] as BN,
60
62
  }
61
63
  }
62
64
 
63
65
  export async function fetchLPPrice(address: Address, provider: providers.Provider): Promise<BigNumber> {
64
66
  var LPInfo = await fetchLPPositions(address, provider);
67
+ const LPDecimal = getDecimal(address);
68
+
69
+ async function fetchLPPriceBy(tokenReverse: {address: string, reserve: BN}): Promise<BigNumber> {
70
+ if (tokenReverse.reserve.isZero()) return new BigNumber(0);
71
+
72
+ // Should NOT call `fetchPrice`, it will cause an infinite loop
73
+ // because the token can be PLY. To calculate PLY price, we need to
74
+ // call `fetchPrice` again...
75
+ const tokenPrice: BigNumber = await _fetchPrice(tokenReverse.address, provider);
76
+ const tokenDecimal = getDecimal(tokenReverse.address);
77
+
78
+ // reserveUSD will be divided by 10^tokenDecimal later for more precision
79
+ const reserveUSD: BigNumber = tokenPrice.multipliedBy(tokenReverse.reserve.toString()).multipliedBy(2);
80
+ return reserveUSD
81
+ .multipliedBy(decimalFactor(LPDecimal))
82
+ .dividedBy(new BigNumber(LPInfo.totalSupply.toString()))
83
+ .dividedBy(decimalFactor(tokenDecimal));
84
+ }
85
+
65
86
  try {
66
- const token0Price: BigNumber = await fetchPrice(LPInfo.token0, provider);
67
- const reserveUSD: BigNumber = token0Price.multipliedBy(LPInfo.reserve0.toString()).multipliedBy(2);
68
- return reserveUSD.multipliedBy(decimalFactor(18)).dividedBy(new BigNumber(LPInfo.totalSupply.toString()));
87
+ // MUST await here
88
+ return await fetchLPPriceBy({address: LPInfo.token0, reserve: LPInfo.reserve0});
69
89
  } catch {
70
- try {
71
- const token1Price: BigNumber = await fetchPrice(LPInfo.token1, provider);
72
- const reserveUSD: BigNumber = token1Price.multipliedBy(LPInfo.reserve1.toString()).multipliedBy(2);
73
- return reserveUSD.multipliedBy(decimalFactor(18)).dividedBy(new BigNumber(LPInfo.totalSupply.toString()));
74
- } catch {
75
- throw Error(`Unable to fetch price for both tokens of LP ${address}`);
76
- }
90
+ try {
91
+ // MUST await here
92
+ return await fetchLPPriceBy({address: LPInfo.token1, reserve: LPInfo.reserve1});
93
+ } catch {
94
+ throw Error(`Unable to fetch price for both tokens of LP ${address}`);
95
+ }
77
96
  }
78
- }
97
+ }
79
98
 
80
99
  function processPriceFromOracle(rawPrice: BN, underlyingDecimal: number) {
81
100
  return new BigNumber(rawPrice.toString()).div(decimalFactor(36 - underlyingDecimal));
@@ -117,8 +136,11 @@ function shouldFetchFromCoingecko(address: string) {
117
136
  address == networkAddresses.tokens.META;
118
137
  }
119
138
 
120
- export async function fetchPrice(address: Address, provider: providers.Provider): Promise<BigNumber> {
121
- if (hardcodePLYPrice && isSameAddress(address, networkAddresses.tokens.PLY)) return new BigNumber(0);
139
+ /**
140
+ * Fetch price from Oracle or Coingecko
141
+ */
142
+ async function _fetchPrice(address: Address, provider: providers.Provider): Promise<BigNumber> {
143
+ assert(PRICE_WHITELISTED.has(address.toLocaleLowerCase()));
122
144
 
123
145
  if (isSameAddress(address, networkAddresses.tokens.stNEAR)) return fetchStNEARPrice();
124
146
  if (shouldFetchFromCoingecko(address)) return fetchPriceFromCoingecko(address);
@@ -126,14 +148,48 @@ export async function fetchPrice(address: Address, provider: providers.Provider)
126
148
  const matchingAuToken = networkAddresses.auTokens.find((auToken) => {
127
149
  return isSameAddress(auToken.underlying, address)
128
150
  });
151
+
129
152
  if (matchingAuToken !== undefined) {
130
153
  return fetchPriceFromOracle(matchingAuToken.address, getDecimal(address), provider);
131
- } else if (isSameAddress(address, networkAddresses.tokens.AURPLY)) {
132
- return fetchLPPrice(address, provider);
133
154
  } else {
134
- return fetchPriceFromCoingecko(address);
155
+ throw Error(`Unable to fetch price for ${address}`)
156
+ }
157
+ }
158
+
159
+ export async function fetchPrice(address: Address, provider: providers.Provider): Promise<BigNumber> {
160
+ if (isSameAddress(address, networkAddresses.tokens.PLYUSDC)) {
161
+ return fetchLPPrice(address, provider)
135
162
  }
163
+ else if (isSameAddress(address, networkAddresses.tokens.PLY)) {
164
+ return fetchPLYPrice(provider)
165
+ }
166
+ return _fetchPrice(address, provider);
167
+ }
168
+
169
+ export async function fetchPLYPrice(provider: providers.Provider): Promise<BigNumber> {
170
+ const LPInfo = await fetchLPPositions(networkAddresses.tokens.PLYUSDC, provider);
171
+
172
+ if (LPInfo.reserve1.isZero()) {
173
+ return new BigNumber(0);
174
+ }
175
+
176
+ const plyAddress = LPInfo.token0;
177
+ assert(isSameAddress(plyAddress, networkAddresses.tokens.PLY));
178
+ const plyDecimal = getDecimal(plyAddress);
179
+
180
+ const usdcAddress = LPInfo.token1;
181
+ assert(isSameAddress(usdcAddress, networkAddresses.tokens.USDC));
182
+ const usdcDecimal = getDecimal(usdcAddress);
183
+
184
+ // (token0Price * reserve0) / 10^decimal0 = (token1Price * reserve1) / 10^decimal1
185
+ // token0Price = (token1Price * reserve1) * 10^decimal0 / 10^decimal1 / reserve0
186
+ return (await fetchPrice(usdcAddress, provider))
187
+ .multipliedBy(LPInfo.reserve1.toString())
188
+ .multipliedBy(decimalFactor(plyDecimal))
189
+ .dividedBy(decimalFactor(usdcDecimal))
190
+ .dividedBy(LPInfo.reserve0.toString());
136
191
  }
192
+
137
193
  export async function fetchValuation(tokenAmount: TokenAmount, provider: providers.Provider): Promise<BigNumber> {
138
194
  if (tokenAmount.rawAmount() == "0") return new BigNumber(0);
139
195
  var price = await fetchPrice(tokenAmount.token.address, provider);
@@ -143,3 +199,14 @@ export async function fetchValuation(tokenAmount: TokenAmount, provider: provide
143
199
  export function calcValuation(tokenAmount: TokenAmount, price: BigNumber) {
144
200
  return new BigNumber(tokenAmount.formattedAmount()).multipliedBy(price);
145
201
  }
202
+
203
+ export async function valuateToken(tokenAmount: TokenAmount, provider: providers.Provider): Promise<TokenValuation> {
204
+ let tokenValuation = await fetchValuation(tokenAmount, provider);
205
+ return {
206
+ tokenAmount: tokenAmount,
207
+ currencyAmount: {
208
+ currency: "USD",
209
+ amount: tokenValuation.toFixed(DECIMAL_PRECISION)
210
+ }
211
+ }
212
+ }
package/src/token.ts CHANGED
@@ -3,19 +3,19 @@ import { networkAddresses } from "./constants";
3
3
 
4
4
  export class Token {
5
5
  public readonly address: string;
6
- public readonly decimals: number;
6
+ public readonly decimals: number;
7
7
  public constructor(address: string, decimals: number) {
8
8
  this.address = address.toLowerCase();
9
9
  this.decimals = decimals;
10
- }
10
+ }
11
11
  }
12
12
 
13
13
  export const PLYToken = new Token(
14
14
  networkAddresses.tokens.PLY,
15
15
  getDecimal(networkAddresses.tokens.PLY)
16
16
  )
17
-
18
- export const AURPLYToken = new Token(
19
- networkAddresses.tokens.AURPLY,
20
- getDecimal(networkAddresses.tokens.AURPLY)
21
- )
17
+
18
+ export const PLYUSDCToken = new Token(
19
+ networkAddresses.tokens.PLYUSDC,
20
+ getDecimal(networkAddresses.tokens.PLYUSDC)
21
+ )
package/src/types.ts CHANGED
@@ -17,6 +17,11 @@ export type CurrencyAmount = {
17
17
  amount: string
18
18
  };
19
19
 
20
+ export type TokenValuation = {
21
+ tokenAmount: TokenAmount,
22
+ currencyAmount: CurrencyAmount,
23
+ }
24
+
20
25
  export type MoneyMarketDetails = {
21
26
  auTokenAddress: string;
22
27
  totalTokenDeposited: TokenAmount;
@@ -36,20 +41,34 @@ export type MoneyMarketDetails = {
36
41
  export type UserDetails = {
37
42
  markets: UserMarketDetails[];
38
43
  borrowLimit: CurrencyAmount;
39
- accruedPly: TokenAmount;
40
- lockedPly: TokenAmount;
44
+ userLockingDetails: UserLockingDetails;
41
45
  borrowableAmount: CurrencyAmount;
42
46
  };
43
47
 
44
- export type LockingDetails = {
48
+ export type UserLockingDetails = {
45
49
  vestingStart: number;
50
+ /** % of reward they will receive in PLY, if they claim this week */
46
51
  currentUnlockPortion: number;
52
+ //** Amount of PLY rewards that a user has. Equal to currentPly + currentPulp */
53
+ accruedPly: TokenAmount;
54
+ /** Amount of PLY they will receive, if they claim this week */
55
+ currentPly: TokenAmount;
56
+ /** Amount of PULP they will receive, if they claim this week */
57
+ currentPulp: TokenAmount;
58
+ /** % of reward they will receive in PLY, if they claim next week */
47
59
  nextUnlockPortion: number;
48
- };
49
-
50
- export type PlyAuroraPool = {
51
- totalStakedLiquidity: CurrencyAmount;
52
- apy: number;
60
+ /**
61
+ * Amount of PLY they will receive, if they claim next week.
62
+ * This doesn't include the additional reward that they will earn
63
+ * in the next week.
64
+ */
65
+ nextPly: TokenAmount;
66
+ /**
67
+ * Amount of PULP they will receive, if they claim next week.
68
+ * This doesn't include the additional reward that they will earn
69
+ * in the next week.
70
+ */
71
+ nextPulp: TokenAmount;
53
72
  };
54
73
 
55
74
  export type UserMarketDetails = {
@@ -62,8 +81,8 @@ export type UserMarketDetails = {
62
81
  underlyingPrice: string;
63
82
  };
64
83
 
65
- export type PlyAuroraPoolDetails = {
66
- totalStakedLiquidity: TokenAmount;
84
+ export type PlyUsdcPoolDetails = {
85
+ totalStakedLiquidity: TokenValuation;
67
86
  apy: string;
68
87
  };
69
88
 
@@ -75,7 +94,8 @@ export enum MarketAction {
75
94
  EnableCollateral,
76
95
  DisableCollateral
77
96
  }
97
+
78
98
  export type HypotheticalStats = {
79
99
  newBorrowLimit: CurrencyAmount;
80
100
  newBorrowUtilization: number;
81
- }
101
+ }