@aurigami/sdk 1.5.0 → 1.5.2

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.
@@ -1,8 +1,10 @@
1
1
  import { Token } from './token';
2
+ import { Address } from './types';
2
3
  export declare class TokenAmount {
3
4
  readonly token: Token;
4
5
  private rawAmnt;
5
6
  constructor(token: Token, amount: string, isRaw?: boolean);
6
7
  formattedAmount(): string;
7
8
  rawAmount(): string;
9
+ static fromAddressAndAmount(tokenAddress: Address, amount: string, isRaw?: boolean): TokenAmount;
8
10
  }
package/dist/types.d.ts CHANGED
@@ -57,9 +57,13 @@ export declare type UserMarketDetails = {
57
57
  collateralRatio: number;
58
58
  underlyingPrice: string;
59
59
  };
60
+ export declare type TokenAPY = {
61
+ address: string;
62
+ apy: string;
63
+ };
60
64
  export declare type PLYWNEARPoolDetails = {
61
65
  totalStakedLiquidity: TokenValuation;
62
- apy: string;
66
+ APYs: TokenAPY[];
63
67
  };
64
68
  export declare enum MarketAction {
65
69
  Deposit = 0,
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.5.0",
2
+ "version": "1.5.2",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
package/src/Papermill.ts CHANGED
@@ -27,6 +27,7 @@ import { TokenAmount } from './tokenAmount';
27
27
  import { getDecimal } from './helpers';
28
28
  import BigNumber from 'bignumber.js';
29
29
 
30
+ const LOCKING_DENOMINATOR = BN.from(10000);
30
31
  export class PapermillRead extends BlockchainEntityRead {
31
32
  public plyToken: Token;
32
33
  public pulpToken: Token;
@@ -102,17 +103,18 @@ export class PapermillRead extends BlockchainEntityRead {
102
103
  );
103
104
  }
104
105
 
106
+ private getWeek(timestamp: number): number {
107
+ return BN.from(timestamp - REWARD_CLAIM_START)
108
+ .div(ONE_WEEK)
109
+ .toNumber();
110
+ }
105
111
  /**
106
112
  * Get all locking details of the user in all markets + all staking pools
107
113
  */
108
114
  public async getLockingDetails(
109
115
  userAddress: Address
110
116
  ): Promise<UserLockingDetails> {
111
- const currentTime = getCurrentTimestamp();
112
- const currentWeek = BN.from(currentTime - REWARD_CLAIM_START)
113
- .div(ONE_WEEK)
114
- .toNumber();
115
- const denominator = BN.from(10000);
117
+ const currentWeek = this.getWeek(getCurrentTimestamp());
116
118
  var promises: any[] = [];
117
119
 
118
120
  promises.push(
@@ -144,17 +146,18 @@ export class PapermillRead extends BlockchainEntityRead {
144
146
  // This doesn't take (the reward that user might earn in the next week) into account
145
147
  let plyAmountNextWeek = percentLockNextWeek
146
148
  .mul(totalRewards.rawAmount())
147
- .div(denominator);
149
+ .div(LOCKING_DENOMINATOR);
148
150
  let pulpAmountNextWeek = BN.from(totalRewards.rawAmount()).sub(
149
151
  plyAmountNextWeek
150
152
  );
151
153
 
152
154
  // we need to divide these two number by 10000 and multiple by 100 to get the percentage
153
155
  // I need to use BigNumber to do this because BN of ethers.js doesn't support floating point
154
- let currentUnlockPortion = denominator
155
- .sub(percentLockCurrentWeek)
156
- .toString();
157
- let nextUnlockPortion = denominator.sub(percentLockNextWeek).toString();
156
+ let currentUnlockPortion = LOCKING_DENOMINATOR.sub(
157
+ percentLockCurrentWeek
158
+ ).toString();
159
+ let nextUnlockPortion =
160
+ LOCKING_DENOMINATOR.sub(percentLockNextWeek).toString();
158
161
 
159
162
  return {
160
163
  vestingStart: REWARD_CLAIM_START,
@@ -197,6 +200,21 @@ export class PapermillRead extends BlockchainEntityRead {
197
200
 
198
201
  return REWARD_CLAIM_START + ONE_WEEK * weekId.toNumber();
199
202
  }
203
+
204
+ public async getUnlockPortionAt(
205
+ userAddress: Address,
206
+ timestamp: number
207
+ ): Promise<number> {
208
+ let lockPortion = await this._auriLens.getPercentLock(
209
+ this._pulp.address,
210
+ userAddress,
211
+ this.getWeek(timestamp)
212
+ );
213
+
214
+ let unlockPortion = LOCKING_DENOMINATOR.sub(lockPortion);
215
+
216
+ return unlockPortion.toNumber() / 100;
217
+ }
200
218
  }
201
219
 
202
220
  export class PapermillReadWrite extends PapermillRead {
package/src/SDK.ts CHANGED
@@ -1,54 +1,40 @@
1
+ import AuriFairLaunchABI from '@aurigami/contracts/artifacts/contracts/AuriFairLaunch.sol/AuriFairLaunch.json';
2
+ import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/AuriLens.json';
3
+ import ComptrollerABI from '@aurigami/contracts/artifacts/contracts/Comptroller.sol/Comptroller.json';
4
+ import FaucetABI from '@aurigami/contracts/artifacts/contracts/mock/Faucet.sol/Faucet.json';
5
+ import {
6
+ AuriFairLaunch,
7
+ AuriLens,
8
+ Comptroller,
9
+ } from '@aurigami/contracts/typechain';
10
+ import { TransactionResponse } from '@ethersproject/abstract-provider';
11
+ import BigNumber from 'bignumber.js';
12
+ import { BigNumber as BN, Contract } from 'ethers';
1
13
  import { BlockchainEntity, BlockchainEntityRead } from './BlockchainEntity';
2
14
  import {
3
- PLYWNEARPoolDetails,
4
- NetworkConnection,
5
- UserDetails,
6
- UserMarketDetails,
15
+ ALL_STAKING_POOLS,
16
+ BORROW_LIMIT_BUFFER,
17
+ DECIMAL_PRECISION,
18
+ networkAddresses,
19
+ } from './constants';
20
+ import { decimalFactor, isSameAddress } from './helpers';
21
+ import { MoneyMarketRead } from './MoneyMarket';
22
+ import { PapermillRead } from './Papermill';
23
+ import { calcValuation, fetchPrice } from './priceFetcher';
24
+ import { StakingRead, StakingReadWrite } from './Staking';
25
+ import { Token } from './token';
26
+ import { TokenAmount } from './tokenAmount';
27
+ import {
7
28
  Address,
8
29
  CurrencyAmount,
9
30
  HypotheticalStats,
10
31
  MarketAction,
32
+ NetworkConnection,
33
+ PLYWNEARPoolDetails,
11
34
  TokenValuation,
35
+ UserDetails,
36
+ UserMarketDetails,
12
37
  } from './types';
13
- import {
14
- networkAddresses,
15
- BORROW_LIMIT_BUFFER,
16
- DECIMAL_PRECISION,
17
- ONE_DAY,
18
- PLYWNEAR_POOL_ID,
19
- ALL_STAKING_POOLS,
20
- PLYWNEAR_REWARD_TOKENS,
21
- } from './constants';
22
- import { TransactionResponse } from '@ethersproject/abstract-provider';
23
- import { Contract, BigNumber as BN } from 'ethers';
24
- import {
25
- isSameAddress,
26
- decimalFactor,
27
- calcLMRewardApr,
28
- getDecimal,
29
- } from './helpers';
30
- import { MoneyMarketRead } from './MoneyMarket';
31
- import {
32
- fetchValuation,
33
- fetchPrice,
34
- calcValuation,
35
- valuateToken,
36
- } from './priceFetcher';
37
- import {
38
- AuriFairLaunch,
39
- Comptroller,
40
- AuriLens,
41
- IERC20,
42
- } from '@aurigami/contracts/typechain';
43
- import { TokenAmount } from './tokenAmount';
44
- import { PLYWNEARToken, PLYToken, Token } from './token';
45
- import BigNumber from 'bignumber.js';
46
- import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/AuriLens.json';
47
- import ComptrollerABI from '@aurigami/contracts/artifacts/contracts/Comptroller.sol/Comptroller.json';
48
- import AuriFairLaunchABI from '@aurigami/contracts/artifacts/contracts/AuriFairLaunch.sol/AuriFairLaunch.json';
49
- import FaucetABI from '@aurigami/contracts/artifacts/contracts/mock/Faucet.sol/Faucet.json';
50
- import EIP20InterfaceABI from '@aurigami/contracts/artifacts/contracts/interfaces/EIP20Interface.sol/EIP20Interface.json';
51
- import { PapermillRead } from './Papermill';
52
38
 
53
39
  export class SdkRead extends BlockchainEntityRead {
54
40
  protected _comptroller: Comptroller;
@@ -225,59 +211,7 @@ export class SdkRead extends BlockchainEntityRead {
225
211
  }
226
212
 
227
213
  public async getPLYWNEARPoolDetails(): Promise<PLYWNEARPoolDetails> {
228
- var stakedLiquidity: BN, rewardPerSeconds: BN[];
229
- await this._auriFairLaunch.getPoolInfo(PLYWNEAR_POOL_ID).then((res) => {
230
- stakedLiquidity = res.totalStake;
231
- rewardPerSeconds = res.rewardPerSeconds;
232
- });
233
-
234
- let stakedLiquidityAmount = new TokenAmount(
235
- PLYWNEARToken,
236
- stakedLiquidity!.toString()
237
- );
238
-
239
- var promises = [];
240
-
241
- // Calculate the reward per second in USD
242
- for (let i = 0; i < rewardPerSeconds!.length; i++) {
243
- let rewardTokenAddress = PLYWNEAR_REWARD_TOKENS[i];
244
- let rewardToken = new Token(
245
- rewardTokenAddress,
246
- getDecimal(rewardTokenAddress)
247
- );
248
- let rewardPerSecond = rewardPerSeconds![i];
249
- promises.push(
250
- fetchValuation(
251
- new TokenAmount(rewardToken, rewardPerSecond!.toString()),
252
- this._networkConnection.provider
253
- )
254
- );
255
- }
256
-
257
- const rewardPerSecondValuation = (await Promise.all(promises)).reduce(
258
- (p: BigNumber, v: BigNumber) => p.plus(v),
259
- new BigNumber(0)
260
- );
261
-
262
- var totalStakeValuation: BigNumber = await fetchValuation(
263
- stakedLiquidityAmount,
264
- this._networkConnection.provider
265
- );
266
-
267
- return {
268
- totalStakedLiquidity: {
269
- tokenAmount: stakedLiquidityAmount,
270
- currencyAmount: {
271
- currency: 'USD',
272
- amount: totalStakeValuation!.toFixed(DECIMAL_PRECISION),
273
- },
274
- },
275
- apy: calcLMRewardApr(
276
- rewardPerSecondValuation,
277
- totalStakeValuation,
278
- ONE_DAY * 365
279
- ).toFixed(DECIMAL_PRECISION),
280
- };
214
+ return new StakingRead(this._networkConnection).getPLYWNEARPoolDetails();
281
215
  }
282
216
 
283
217
  public async getTokenPrice(token: Token): Promise<CurrencyAmount> {
@@ -295,33 +229,18 @@ export class SdkRead extends BlockchainEntityRead {
295
229
  * Get amount and value in $ of staked LP tokens
296
230
  */
297
231
  public async stakeBalanceOf(user: string): Promise<TokenValuation> {
298
- const userInfo = await this._auriFairLaunch.getUserInfo(
299
- PLYWNEAR_POOL_ID,
300
- user
301
- );
302
- let stakeAmount = new TokenAmount(
303
- PLYWNEARToken,
304
- userInfo.amount.toString()
305
- );
306
-
307
- return valuateToken(stakeAmount, this._networkConnection.provider);
232
+ return new StakingRead(this._networkConnection).stakeBalanceOf(user);
308
233
  }
309
234
 
310
235
  /**
311
236
  * Get amount and value in $ of LP token in user wallet
312
237
  */
313
238
  public async LpBalanceOf(user: string): Promise<TokenValuation> {
314
- let lpContract = new Contract(
315
- networkAddresses.tokens.PLYWNEAR,
316
- EIP20InterfaceABI.abi,
317
- this._networkConnection.provider
318
- ) as IERC20;
319
- let balance = await lpContract.balanceOf(user);
239
+ return new StakingRead(this._networkConnection).LpBalanceOf(user);
240
+ }
320
241
 
321
- return valuateToken(
322
- new TokenAmount(PLYWNEARToken, balance.toString()),
323
- this._networkConnection.provider
324
- );
242
+ public async unclaimedRewardsOf(user: string): Promise<TokenAmount[]> {
243
+ return new StakingRead(this._networkConnection).unclaimedRewardsOf(user);
325
244
  }
326
245
 
327
246
  public calcHypotheticalStats({
@@ -409,15 +328,11 @@ export class SdkReadWrite extends SdkRead {
409
328
  }
410
329
 
411
330
  public async stake(amount: TokenAmount): Promise<TransactionResponse> {
412
- return this._auriFairLaunch
413
- .connect(this._networkConnection.signer!)
414
- .deposit(PLYWNEAR_POOL_ID, amount.rawAmount());
331
+ return new StakingReadWrite(this._networkConnection).stake(amount);
415
332
  }
416
333
 
417
334
  public async unstake(amount: TokenAmount): Promise<TransactionResponse> {
418
- return this._auriFairLaunch
419
- .connect(this._networkConnection.signer!)
420
- .withdraw(PLYWNEAR_POOL_ID, amount.rawAmount());
335
+ return new StakingReadWrite(this._networkConnection).unstake(amount);
421
336
  }
422
337
 
423
338
  public async faucetDrip(): Promise<TransactionResponse> {
package/src/Staking.ts ADDED
@@ -0,0 +1,193 @@
1
+ import AuriFairLaunchABI from '@aurigami/contracts/artifacts/contracts/AuriFairLaunch.sol/AuriFairLaunch.json';
2
+ import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/AuriLens.json';
3
+ import ComptrollerABI from '@aurigami/contracts/artifacts/contracts/Comptroller.sol/Comptroller.json';
4
+ import EIP20InterfaceABI from '@aurigami/contracts/artifacts/contracts/interfaces/EIP20Interface.sol/EIP20Interface.json';
5
+ import {
6
+ AuriFairLaunch,
7
+ AuriLens,
8
+ Comptroller,
9
+ IERC20,
10
+ } from '@aurigami/contracts/typechain';
11
+ import { TransactionResponse } from '@ethersproject/abstract-provider';
12
+ import BigNumber from 'bignumber.js';
13
+ import { BigNumber as BN, Contract } from 'ethers';
14
+ import { BlockchainEntity, BlockchainEntityRead } from './BlockchainEntity';
15
+ import {
16
+ DECIMAL_PRECISION,
17
+ networkAddresses,
18
+ ONE_DAY,
19
+ PLYWNEAR_POOL_ID,
20
+ PLYWNEAR_REWARD_TOKENS,
21
+ } from './constants';
22
+ import { calcLMRewardApr, getDecimal } from './helpers';
23
+ import { fetchValuation, valuateToken } from './priceFetcher';
24
+ import { PLYWNEARToken, Token } from './token';
25
+ import { TokenAmount } from './tokenAmount';
26
+ import {
27
+ NetworkConnection,
28
+ PLYWNEARPoolDetails,
29
+ TokenAPY,
30
+ TokenValuation,
31
+ } from './types';
32
+
33
+ export class StakingRead extends BlockchainEntityRead {
34
+ protected _comptroller: Comptroller;
35
+ protected _auriFairLaunch: AuriFairLaunch;
36
+ protected _auriLens: AuriLens;
37
+ constructor(networkConnection: NetworkConnection) {
38
+ super(networkConnection);
39
+ this._comptroller = new Contract(
40
+ networkAddresses.misc.COMPTROLLER,
41
+ ComptrollerABI.abi,
42
+ networkConnection.provider
43
+ ) as Comptroller;
44
+ this._auriFairLaunch = new Contract(
45
+ networkAddresses.misc.AURIFAIRLAUNCH,
46
+ AuriFairLaunchABI.abi,
47
+ networkConnection.provider
48
+ ) as AuriFairLaunch;
49
+ this._auriLens = new Contract(
50
+ networkAddresses.misc.AURILENS,
51
+ AuriLensABI.abi,
52
+ networkConnection.provider
53
+ ) as AuriLens;
54
+ }
55
+
56
+ public async getPLYWNEARPoolDetails(): Promise<PLYWNEARPoolDetails> {
57
+ var stakedLiquidity: BN, rewardPerSeconds: BN[];
58
+ await this._auriFairLaunch.getPoolInfo(PLYWNEAR_POOL_ID).then((res) => {
59
+ stakedLiquidity = res.totalStake;
60
+ rewardPerSeconds = res.rewardPerSeconds;
61
+ });
62
+
63
+ let stakedLiquidityAmount = new TokenAmount(
64
+ PLYWNEARToken,
65
+ stakedLiquidity!.toString()
66
+ );
67
+
68
+ var totalStakeValuation: BigNumber = await fetchValuation(
69
+ stakedLiquidityAmount,
70
+ this._networkConnection.provider
71
+ );
72
+
73
+ var promises = [];
74
+
75
+ // Calculate the reward per second in USD
76
+ for (let i = 0; i < rewardPerSeconds!.length; i++) {
77
+ let rewardTokenAddress = PLYWNEAR_REWARD_TOKENS[i];
78
+ let rewardToken = new Token(
79
+ rewardTokenAddress,
80
+ getDecimal(rewardTokenAddress)
81
+ );
82
+ let rewardPerSecond = rewardPerSeconds![i];
83
+ promises.push(
84
+ fetchValuation(
85
+ new TokenAmount(rewardToken, rewardPerSecond!.toString()),
86
+ this._networkConnection.provider
87
+ )
88
+ );
89
+ }
90
+
91
+ let APYs: TokenAPY[] = [];
92
+ (await Promise.all(promises)).forEach((rewardPerSecondValuation, i) => {
93
+ let rewardTokenAddress = PLYWNEAR_REWARD_TOKENS[i];
94
+ let apy = calcLMRewardApr(
95
+ rewardPerSecondValuation,
96
+ totalStakeValuation,
97
+ ONE_DAY * 365
98
+ ).toFixed(DECIMAL_PRECISION);
99
+
100
+ APYs.push({
101
+ address: rewardTokenAddress,
102
+ apy: apy,
103
+ });
104
+ });
105
+
106
+ return {
107
+ totalStakedLiquidity: {
108
+ tokenAmount: stakedLiquidityAmount,
109
+ currencyAmount: {
110
+ currency: 'USD',
111
+ amount: totalStakeValuation!.toFixed(DECIMAL_PRECISION),
112
+ },
113
+ },
114
+ APYs: APYs,
115
+ };
116
+ }
117
+
118
+ /**
119
+ * Get amount and value in $ of staked LP tokens
120
+ */
121
+ public async stakeBalanceOf(user: string): Promise<TokenValuation> {
122
+ const userInfo = await this._auriFairLaunch.getUserInfo(
123
+ PLYWNEAR_POOL_ID,
124
+ user
125
+ );
126
+ let stakeAmount = new TokenAmount(
127
+ PLYWNEARToken,
128
+ userInfo.amount.toString()
129
+ );
130
+
131
+ return valuateToken(stakeAmount, this._networkConnection.provider);
132
+ }
133
+
134
+ /**
135
+ * Get amount and value in $ of LP token in user wallet
136
+ */
137
+ public async LpBalanceOf(user: string): Promise<TokenValuation> {
138
+ let lpContract = new Contract(
139
+ networkAddresses.tokens.PLYWNEAR,
140
+ EIP20InterfaceABI.abi,
141
+ this._networkConnection.provider
142
+ ) as IERC20;
143
+ let balance = await lpContract.balanceOf(user);
144
+
145
+ return valuateToken(
146
+ new TokenAmount(PLYWNEARToken, balance.toString()),
147
+ this._networkConnection.provider
148
+ );
149
+ }
150
+
151
+ public async unclaimedRewardsOf(user: string): Promise<TokenAmount[]> {
152
+ let userInfo = await this._auriFairLaunch.callStatic.updateAndGetUserInfo(
153
+ PLYWNEAR_POOL_ID,
154
+ user,
155
+ { from: user }
156
+ );
157
+
158
+ let rewards = userInfo.unclaimedRewards;
159
+ return rewards.map((reward, i) =>
160
+ TokenAmount.fromAddressAndAmount(
161
+ PLYWNEAR_REWARD_TOKENS[i],
162
+ reward.toString()
163
+ )
164
+ );
165
+ }
166
+ }
167
+
168
+ export class StakingReadWrite extends StakingRead {
169
+ public async stake(amount: TokenAmount): Promise<TransactionResponse> {
170
+ return this._auriFairLaunch
171
+ .connect(this._networkConnection.signer!)
172
+ .deposit(PLYWNEAR_POOL_ID, amount.rawAmount());
173
+ }
174
+
175
+ public async unstake(amount: TokenAmount): Promise<TransactionResponse> {
176
+ return this._auriFairLaunch
177
+ .connect(this._networkConnection.signer!)
178
+ .withdraw(PLYWNEAR_POOL_ID, amount.rawAmount());
179
+ }
180
+ }
181
+
182
+ export class Staking extends BlockchainEntity {
183
+ constructor() {
184
+ super();
185
+ }
186
+ public read(networkConnection: NetworkConnection): StakingRead {
187
+ return new StakingRead(networkConnection);
188
+ }
189
+
190
+ public readWrite(networkConnection: NetworkConnection): StakingReadWrite {
191
+ return new StakingReadWrite(networkConnection);
192
+ }
193
+ }
package/src/constants.ts CHANGED
@@ -96,7 +96,7 @@ export const PRICE_WHITELISTED = new Set([
96
96
  networkAddresses.tokens.stNEAR,
97
97
  ]);
98
98
 
99
- export const REWARD_CLAIM_START = 1651762800; // 5 May 2022 15:00:00 UTC
99
+ export const REWARD_CLAIM_START = 1651748400; // 5 May 2022 11:00:00 UTC
100
100
 
101
101
  export const BORROW_LIMIT_BUFFER = new BigNumber(1);
102
102
 
@@ -114,7 +114,7 @@ export const PLYWNEAR_REWARD_TOKENS = [
114
114
  // could call RPC to get all the staking pools but it's not necessary for now
115
115
  // export const ALL_STAKING_POOLS = [PLYWNEAR_POOL_ID];
116
116
  // TODO: fix this
117
- export const ALL_STAKING_POOLS = [];
117
+ export const ALL_STAKING_POOLS: number[] = [];
118
118
  export const INF = BN.from(2).pow(256).sub(1);
119
119
 
120
120
  export const AIRDROP_START_TIMESTAMP = 1649858400;
package/src/helpers.ts CHANGED
@@ -1,12 +1,10 @@
1
- import EIP20InterfaceABI from '@aurigami/contracts/artifacts/contracts/interfaces/EIP20Interface.sol/EIP20Interface.json';
2
- import { EIP20Interface, IERC20 } from '@aurigami/contracts/typechain';
3
1
  import { Provider } from '@ethersproject/abstract-provider';
2
+ import assert from 'assert';
4
3
  import BigNumber from 'bignumber.js';
5
- import { BigNumber as BN, Contract, providers, utils } from 'ethers';
4
+ import { BigNumber as BN, utils } from 'ethers';
6
5
  import { getQuery as getAuroraAPIQuery } from './aurora-api-helpers';
7
6
  import { decimalRecords } from './decimals';
8
- import { Address, NetworkConnection } from './types';
9
- import assert from 'assert';
7
+ import { Address } from './types';
10
8
 
11
9
  export function formatObject(object: Object) {
12
10
  return JSON.stringify(object, null, ' ');
@@ -245,30 +245,28 @@ export async function fetchPLYPrice(
245
245
  provider: providers.Provider
246
246
  ): Promise<BigNumber> {
247
247
  //TODO: Fetch PLY price from CEX?
248
- return new BigNumber(0);
249
- const LPInfo = await fetchLPPositions(
250
- networkAddresses.tokens.PLYWNEAR,
251
- provider
252
- );
248
+ return fetchToken0PriceByLP(networkAddresses.tokens.PLYWNEAR, provider);
249
+ }
250
+
251
+ export async function fetchToken0PriceByLP(
252
+ LP: Address,
253
+ provider: providers.Provider
254
+ ): Promise<BigNumber> {
255
+ const LPInfo = await fetchLPPositions(LP, provider);
253
256
 
254
257
  if (LPInfo.reserve1.isZero()) {
255
258
  return new BigNumber(0);
256
259
  }
257
260
 
258
- const plyAddress = LPInfo.token0;
259
- assert(isSameAddress(plyAddress, networkAddresses.tokens.PLY));
260
- const plyDecimal = getDecimal(plyAddress);
261
-
262
- const wnearAddress = LPInfo.token1;
263
- assert(isSameAddress(wnearAddress, networkAddresses.tokens.WNEAR));
264
- const wnearDecimal = getDecimal(wnearAddress);
261
+ const token0Decimal = getDecimal(LPInfo.token0);
262
+ const token1Decimal = getDecimal(LPInfo.token1);
265
263
 
266
264
  // (token0Price * reserve0) / 10^decimal0 = (token1Price * reserve1) / 10^decimal1
267
265
  // token0Price = (token1Price * reserve1) * 10^decimal0 / 10^decimal1 / reserve0
268
- return (await fetchPrice(wnearAddress, provider))
266
+ return (await fetchPrice(LPInfo.token1, provider))
269
267
  .multipliedBy(LPInfo.reserve1.toString())
270
- .multipliedBy(decimalFactor(plyDecimal))
271
- .dividedBy(decimalFactor(wnearDecimal))
268
+ .multipliedBy(decimalFactor(token0Decimal))
269
+ .dividedBy(decimalFactor(token1Decimal))
272
270
  .dividedBy(LPInfo.reserve0.toString());
273
271
  }
274
272
 
@@ -1,6 +1,7 @@
1
1
  import { Token } from './token';
2
2
  import BigNumber from 'bignumber.js';
3
- import { decimalFactor } from './helpers';
3
+ import { decimalFactor, getDecimal } from './helpers';
4
+ import { Address } from './types';
4
5
 
5
6
  export class TokenAmount {
6
7
  public readonly token: Token;
@@ -26,4 +27,16 @@ export class TokenAmount {
26
27
  public rawAmount(): string {
27
28
  return this.rawAmnt;
28
29
  }
30
+
31
+ public static fromAddressAndAmount(
32
+ tokenAddress: Address,
33
+ amount: string,
34
+ isRaw: boolean = true
35
+ ): TokenAmount {
36
+ return new TokenAmount(
37
+ new Token(tokenAddress, getDecimal(tokenAddress)),
38
+ amount,
39
+ isRaw
40
+ );
41
+ }
29
42
  }
package/src/types.ts CHANGED
@@ -81,9 +81,14 @@ export type UserMarketDetails = {
81
81
  underlyingPrice: string;
82
82
  };
83
83
 
84
+ export type TokenAPY = {
85
+ address: string;
86
+ apy: string;
87
+ };
88
+
84
89
  export type PLYWNEARPoolDetails = {
85
90
  totalStakedLiquidity: TokenValuation;
86
- apy: string;
91
+ APYs: TokenAPY[];
87
92
  };
88
93
 
89
94
  export enum MarketAction {