@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/dist/Papermill.d.ts +30 -0
- package/dist/SDK.d.ts +5 -7
- package/dist/constants.d.ts +5 -2
- package/dist/hardhat.config.d.ts +6 -0
- package/dist/index.d.ts +1 -0
- package/dist/priceFetcher.d.ts +3 -1
- package/dist/sdk.cjs.development.js +650 -234
- package/dist/sdk.cjs.development.js.map +1 -1
- package/dist/sdk.cjs.production.min.js +1 -1
- package/dist/sdk.cjs.production.min.js.map +1 -1
- package/dist/sdk.esm.js +640 -232
- package/dist/sdk.esm.js.map +1 -1
- package/dist/test/constants.d.ts +6 -0
- package/dist/test-hardhat/helper.d.ts +6 -0
- package/dist/token.d.ts +1 -1
- package/dist/types.d.ts +13 -9
- package/package.json +4 -4
- package/src/Papermill.ts +162 -0
- package/src/SDK.ts +90 -78
- package/src/constants.ts +28 -8
- package/src/decimals.ts +4 -2
- package/src/deployments/aurora_mainnet.json +10 -6
- package/src/helpers.ts +4 -1
- package/src/index.ts +1 -0
- package/src/priceFetcher.ts +95 -28
- package/src/token.ts +7 -7
- package/src/types.ts +31 -11
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
|
-
|
|
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
package/src/priceFetcher.ts
CHANGED
|
@@ -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
|
-
|
|
48
|
-
|
|
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.
|
|
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
|
-
|
|
67
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
121
|
-
|
|
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
|
-
|
|
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
|
|
19
|
-
networkAddresses.tokens.
|
|
20
|
-
getDecimal(networkAddresses.tokens.
|
|
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
|
-
|
|
40
|
-
lockedPly: TokenAmount;
|
|
44
|
+
userLockingDetails: UserLockingDetails;
|
|
41
45
|
borrowableAmount: CurrencyAmount;
|
|
42
46
|
};
|
|
43
47
|
|
|
44
|
-
export type
|
|
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
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
|
66
|
-
totalStakedLiquidity:
|
|
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
|
+
}
|