@aurigami/sdk 1.12.7 → 1.12.9-hf
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/ChangeLog.md +371 -0
- package/dist/helpers/historicalPriceFetcher.d.ts +15 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/interactors/MoneyMarket.d.ts +2 -0
- package/dist/interactors/PlyGame.d.ts +1 -0
- package/dist/sdk.cjs.development.js +557 -95
- 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 +569 -113
- package/dist/sdk.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/.DS_Store +0 -0
- package/src/consts/constants.ts +3 -0
- package/src/consts/decimals.ts +2 -0
- package/src/helpers/historicalPriceFetcher.ts +196 -0
- package/src/helpers/index.ts +1 -0
- package/src/interactors/MoneyMarket.ts +12 -0
- package/src/interactors/PlyGame.ts +10 -1
- package/src/interactors/misc.ts +8 -5
package/package.json
CHANGED
package/src/.DS_Store
ADDED
|
Binary file
|
package/src/consts/constants.ts
CHANGED
|
@@ -106,6 +106,9 @@ export const networkAddresses: NetworkAddresses = {
|
|
|
106
106
|
PULP: MAINNET_ADDRESSES.PULP.toLowerCase(),
|
|
107
107
|
PLYWNEAR: MAINNET_ADDRESSES.ply_wnear_lp.toLowerCase(),
|
|
108
108
|
USN: '0x5183e1b1091804bc2602586919e6880ac1cf2896'.toLowerCase(),
|
|
109
|
+
PLY_WNEAR: '0x044b6b0cd3bb13d2b9057781df4459c66781dce7'.toLowerCase(),
|
|
110
|
+
WNEAR_TRI: '0x84b123875f0f36b966d0b6ca14b31121bd9676ad'.toLowerCase(),
|
|
111
|
+
AURORA_WNEAR: '0x1e0e812fbcd3eb75d8562ad6f310ed94d258d008'.toLowerCase(),
|
|
109
112
|
},
|
|
110
113
|
};
|
|
111
114
|
|
package/src/consts/decimals.ts
CHANGED
|
@@ -22,4 +22,6 @@ export const decimalRecords: Record<string, number> = {
|
|
|
22
22
|
'0x04ac48711bcdc45b4d223fb021e09da73c71095e': 18, // PULP
|
|
23
23
|
'0x044b6b0cd3bb13d2b9057781df4459c66781dce7': 18, // PLYWNEAR
|
|
24
24
|
'0x5183e1b1091804bc2602586919e6880ac1cf2896': 18, // USN
|
|
25
|
+
'0x84b123875f0f36b966d0b6ca14b31121bd9676ad': 18, //WNEAR_TRI,
|
|
26
|
+
'0x1e0e812fbcd3eb75d8562ad6f310ed94d258d008': 18, //AURORA_WNEAR
|
|
25
27
|
};
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { IUniswapV2Pair, PriceOracle } from '@aurigami/contracts/typechain';
|
|
2
|
+
import BigNumber from 'bignumber.js';
|
|
3
|
+
import { BigNumber as BN, Contract, providers } from 'ethers';
|
|
4
|
+
import { AuriOracleABI, IUniswapV2PairABI } from '../abis';
|
|
5
|
+
import { HARDCODE_PRICE_TOKENS, networkAddresses } from '../consts/constants';
|
|
6
|
+
import { Address } from '../types';
|
|
7
|
+
import { decimalFactor, devLog, getDecimal, isSameAddress } from './helpers';
|
|
8
|
+
|
|
9
|
+
export async function fetchHistoricalLPPositions(
|
|
10
|
+
LPAddress: Address,
|
|
11
|
+
provider: providers.Provider,
|
|
12
|
+
block: number
|
|
13
|
+
): Promise<{
|
|
14
|
+
token0: Address;
|
|
15
|
+
token1: Address;
|
|
16
|
+
reserve0: BN;
|
|
17
|
+
reserve1: BN;
|
|
18
|
+
totalSupply: BN;
|
|
19
|
+
}> {
|
|
20
|
+
const LPContract = new Contract(LPAddress, IUniswapV2PairABI.abi, provider) as IUniswapV2Pair;
|
|
21
|
+
var promises: any[] = [];
|
|
22
|
+
promises.push(LPContract.token0());
|
|
23
|
+
promises.push(LPContract.token1());
|
|
24
|
+
promises.push(LPContract.getReserves({ blockTag: block }));
|
|
25
|
+
promises.push(LPContract.totalSupply({ blockTag: block }));
|
|
26
|
+
const values: any[] = await Promise.all(promises);
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
token0: values[0] as string,
|
|
30
|
+
token1: values[1] as string,
|
|
31
|
+
reserve0: (values[2] as { reserve0: BN; reserve1: BN }).reserve0,
|
|
32
|
+
reserve1: (values[2] as { reserve0: BN; reserve1: BN }).reserve1,
|
|
33
|
+
totalSupply: values[3] as BN,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function fetchHistoricalLPPrice(
|
|
38
|
+
address: Address,
|
|
39
|
+
provider: providers.Provider,
|
|
40
|
+
block: number
|
|
41
|
+
): Promise<BigNumber> {
|
|
42
|
+
var LPInfo = await fetchHistoricalLPPositions(address, provider, block);
|
|
43
|
+
const LPDecimal = getDecimal(address);
|
|
44
|
+
|
|
45
|
+
async function fetchLPPriceBy(
|
|
46
|
+
tokenReverse: {
|
|
47
|
+
address: string;
|
|
48
|
+
reserve: BN;
|
|
49
|
+
},
|
|
50
|
+
block: number
|
|
51
|
+
): Promise<BigNumber> {
|
|
52
|
+
if (tokenReverse.reserve.isZero()) return new BigNumber(0);
|
|
53
|
+
|
|
54
|
+
const tokenPrice: BigNumber = await fetchHistoricalPrice(tokenReverse.address, provider, block);
|
|
55
|
+
const tokenDecimal = getDecimal(tokenReverse.address);
|
|
56
|
+
|
|
57
|
+
// reserveUSD will be divided by 10^tokenDecimal later for more precision
|
|
58
|
+
const reserveUSD: BigNumber = tokenPrice
|
|
59
|
+
.multipliedBy(tokenReverse.reserve.toString())
|
|
60
|
+
.multipliedBy(2);
|
|
61
|
+
return reserveUSD
|
|
62
|
+
.multipliedBy(decimalFactor(LPDecimal))
|
|
63
|
+
.dividedBy(new BigNumber(LPInfo.totalSupply.toString()))
|
|
64
|
+
.dividedBy(decimalFactor(tokenDecimal));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
// MUST await here
|
|
69
|
+
const lpPrice = await fetchLPPriceBy(
|
|
70
|
+
{
|
|
71
|
+
address: LPInfo.token0,
|
|
72
|
+
reserve: LPInfo.reserve0,
|
|
73
|
+
},
|
|
74
|
+
block
|
|
75
|
+
);
|
|
76
|
+
if (lpPrice.isZero()) {
|
|
77
|
+
throw Error(`Price is zero`);
|
|
78
|
+
}
|
|
79
|
+
return lpPrice;
|
|
80
|
+
} catch (e) {
|
|
81
|
+
devLog(`fetchLPPriceBy ${LPInfo.token0} failed`, e);
|
|
82
|
+
try {
|
|
83
|
+
// MUST await here
|
|
84
|
+
return await fetchLPPriceBy(
|
|
85
|
+
{
|
|
86
|
+
address: LPInfo.token1,
|
|
87
|
+
reserve: LPInfo.reserve1,
|
|
88
|
+
},
|
|
89
|
+
block
|
|
90
|
+
);
|
|
91
|
+
} catch (e) {
|
|
92
|
+
devLog(`fetchLPPriceBy ${LPInfo.token1} failed`, e);
|
|
93
|
+
throw Error(`Unable to fetch price for both tokens of LP ${address}`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function processPriceFromOracle(rawPrice: BN, underlyingDecimal: number) {
|
|
99
|
+
return new BigNumber(rawPrice.toString()).div(decimalFactor(36 - underlyingDecimal));
|
|
100
|
+
}
|
|
101
|
+
export async function fetchHistoricalPriceFromOracle(
|
|
102
|
+
auTokenAddress: Address,
|
|
103
|
+
underlyingDecimal: number,
|
|
104
|
+
provider: providers.Provider,
|
|
105
|
+
block: number
|
|
106
|
+
): Promise<BigNumber> {
|
|
107
|
+
const oracleContract: PriceOracle = new Contract(
|
|
108
|
+
networkAddresses.misc.oracle,
|
|
109
|
+
AuriOracleABI.abi,
|
|
110
|
+
provider
|
|
111
|
+
) as PriceOracle;
|
|
112
|
+
const rawPrice = await oracleContract
|
|
113
|
+
.getUnderlyingPrice(auTokenAddress, { blockTag: block })
|
|
114
|
+
.catch((e: any) => {
|
|
115
|
+
console.log(`Unable to fetch price from oracle for ${auTokenAddress}`);
|
|
116
|
+
return BN.from(0);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
return processPriceFromOracle(rawPrice, underlyingDecimal);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export async function fetchHistoricalPrice(
|
|
123
|
+
address: Address,
|
|
124
|
+
provider: providers.Provider,
|
|
125
|
+
block: number
|
|
126
|
+
): Promise<BigNumber> {
|
|
127
|
+
if (address.toLowerCase() in HARDCODE_PRICE_TOKENS) {
|
|
128
|
+
return new BigNumber(HARDCODE_PRICE_TOKENS[address.toLowerCase()]);
|
|
129
|
+
} else if (
|
|
130
|
+
isSameAddress(address, networkAddresses.tokens.PLY_WNEAR) ||
|
|
131
|
+
isSameAddress(address, networkAddresses.tokens.WNEAR_TRI) ||
|
|
132
|
+
isSameAddress(address, networkAddresses.tokens.AURORA_WNEAR)
|
|
133
|
+
) {
|
|
134
|
+
return fetchHistoricalLPPrice(address, provider, block);
|
|
135
|
+
} else if (isSameAddress(address, networkAddresses.tokens.PLY)) {
|
|
136
|
+
return fetchHistoricalPriceByLP(networkAddresses.tokens.PLY_WNEAR, provider, block);
|
|
137
|
+
} else if (isSameAddress(address, networkAddresses.tokens.TRI)) {
|
|
138
|
+
return fetchHistoricalPriceByLP(networkAddresses.tokens.WNEAR_TRI, provider, block, 1);
|
|
139
|
+
} else if (isSameAddress(address, networkAddresses.tokens.AURORA)) {
|
|
140
|
+
return fetchHistoricalPriceByLP(networkAddresses.tokens.AURORA_WNEAR, provider, block);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
const matchingAuToken = networkAddresses.auTokens.find((auToken) => {
|
|
144
|
+
return isSameAddress(auToken.underlying, address);
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
if (matchingAuToken !== undefined) {
|
|
148
|
+
return fetchHistoricalPriceFromOracle(
|
|
149
|
+
matchingAuToken.address,
|
|
150
|
+
getDecimal(address),
|
|
151
|
+
provider,
|
|
152
|
+
block
|
|
153
|
+
);
|
|
154
|
+
} else {
|
|
155
|
+
throw Error(`Unable to fetch price for ${address}`);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export async function fetchHistoricalPULPPrice(provider: providers.Provider): Promise<BigNumber> {
|
|
160
|
+
//TODO: Calculate PULP price
|
|
161
|
+
return new BigNumber(0);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export async function fetchHistoricalPriceByLP(
|
|
165
|
+
LP: Address,
|
|
166
|
+
provider: providers.Provider,
|
|
167
|
+
block: number,
|
|
168
|
+
tokenNumber: 0 | 1 = 0
|
|
169
|
+
): Promise<BigNumber> {
|
|
170
|
+
let LPInfo = await fetchHistoricalLPPositions(LP, provider, block);
|
|
171
|
+
|
|
172
|
+
if (tokenNumber === 1) {
|
|
173
|
+
LPInfo = {
|
|
174
|
+
token0: LPInfo.token1,
|
|
175
|
+
token1: LPInfo.token0,
|
|
176
|
+
reserve0: LPInfo.reserve1,
|
|
177
|
+
reserve1: LPInfo.reserve0,
|
|
178
|
+
totalSupply: LPInfo.totalSupply,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
if (LPInfo.reserve1.isZero()) {
|
|
183
|
+
return new BigNumber(0);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const token0Decimal = getDecimal(LPInfo.token0);
|
|
187
|
+
const token1Decimal = getDecimal(LPInfo.token1);
|
|
188
|
+
|
|
189
|
+
// (token0Price * reserve0) / 10^decimal0 = (token1Price * reserve1) / 10^decimal1
|
|
190
|
+
// token0Price = (token1Price * reserve1) * 10^decimal0 / 10^decimal1 / reserve0
|
|
191
|
+
return (await fetchHistoricalPrice(LPInfo.token1, provider, block))
|
|
192
|
+
.multipliedBy(LPInfo.reserve1.toString())
|
|
193
|
+
.multipliedBy(decimalFactor(token0Decimal))
|
|
194
|
+
.dividedBy(decimalFactor(token1Decimal))
|
|
195
|
+
.dividedBy(LPInfo.reserve0.toString());
|
|
196
|
+
}
|
package/src/helpers/index.ts
CHANGED
|
@@ -332,6 +332,18 @@ export class MoneyMarketRead extends BlockchainEntityRead {
|
|
|
332
332
|
return new TokenAmount(NEARToken, '0');
|
|
333
333
|
}
|
|
334
334
|
}
|
|
335
|
+
public async mintCap(): Promise<TokenAmount> {
|
|
336
|
+
return new TokenAmount(
|
|
337
|
+
this.underlying,
|
|
338
|
+
(await this.env.comptroller.mintCaps(this.auToken.address)).toString()
|
|
339
|
+
);
|
|
340
|
+
}
|
|
341
|
+
public async borrowCap(): Promise<TokenAmount> {
|
|
342
|
+
return new TokenAmount(
|
|
343
|
+
this.underlying,
|
|
344
|
+
(await this.env.comptroller.borrowCaps(this.auToken.address)).toString()
|
|
345
|
+
);
|
|
346
|
+
}
|
|
335
347
|
}
|
|
336
348
|
|
|
337
349
|
export class MoneyMarketReadWrite extends MoneyMarketRead {
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
NetworkConnection,
|
|
11
11
|
PlyBetMadeEvent,
|
|
12
12
|
PlyGameBetResult,
|
|
13
|
-
PlyGameLeaderboardItem
|
|
13
|
+
PlyGameLeaderboardItem
|
|
14
14
|
} from '../types';
|
|
15
15
|
|
|
16
16
|
export class PlyGameRead extends BlockchainEntityRead {
|
|
@@ -131,6 +131,15 @@ export class PlyGameRead extends BlockchainEntityRead {
|
|
|
131
131
|
amount: new TokenAmount(this.plyToken, result.amount.toString()),
|
|
132
132
|
};
|
|
133
133
|
}
|
|
134
|
+
|
|
135
|
+
public async overallLeaderboard(): Promise<PlyGameLeaderboardItem[]> {
|
|
136
|
+
const result = await AuriAPI.getPaginatedApi('/plygame/overallLeaderboard');
|
|
137
|
+
return result.items.map((item) => ({
|
|
138
|
+
player: item.user,
|
|
139
|
+
unlockedPulpAmount: new TokenAmount(this.pulpToken, item.unlockedAmount!),
|
|
140
|
+
gamesPlayed: item.count!,
|
|
141
|
+
}));
|
|
142
|
+
}
|
|
134
143
|
}
|
|
135
144
|
|
|
136
145
|
export class PlyGameReadWrite extends PlyGameRead {
|
package/src/interactors/misc.ts
CHANGED
|
@@ -88,7 +88,10 @@ export class MiscRead extends BlockchainEntityRead {
|
|
|
88
88
|
return p;
|
|
89
89
|
}, new BigNumber(0));
|
|
90
90
|
|
|
91
|
-
var accountLiquidity: [BN, BN] = await this.env.comptroller.getAccountLiquidity(userAddress)
|
|
91
|
+
var accountLiquidity: [BN, BN] = await this.env.comptroller.getAccountLiquidity(userAddress).catch((e) => {
|
|
92
|
+
console.log(e);
|
|
93
|
+
return [BN.from(0), BN.from(0)]
|
|
94
|
+
});
|
|
92
95
|
|
|
93
96
|
var borrowedvaluation: BigNumber = totalBorrowLimit.minus(
|
|
94
97
|
new BigNumber(accountLiquidity[0].toString()).div(decimalFactor(18))
|
|
@@ -126,12 +129,12 @@ export class MiscRead extends BlockchainEntityRead {
|
|
|
126
129
|
.div(underlyingPrices[i]);
|
|
127
130
|
userMarketDetails[i].maxWithdrawableAmount =
|
|
128
131
|
userMarketDetails[i].isCollateral &&
|
|
129
|
-
|
|
132
|
+
maxWithdrawCap.lt(userMarketDetails[i].depositBalance.formattedAmount())
|
|
130
133
|
? new TokenAmount(moneyMarket.underlying, maxWithdrawCap.toFixed(24), false)
|
|
131
134
|
: new TokenAmount(
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
+
moneyMarket.underlying,
|
|
136
|
+
userMarketDetails[i].depositBalance.rawAmount()
|
|
137
|
+
);
|
|
135
138
|
}
|
|
136
139
|
|
|
137
140
|
return {
|