@aurigami/sdk 1.5.1 → 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/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.5.1",
2
+ "version": "1.5.2",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
package/src/SDK.ts CHANGED
@@ -1,55 +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,
12
- TokenAPY,
35
+ UserDetails,
36
+ UserMarketDetails,
13
37
  } from './types';
14
- import {
15
- networkAddresses,
16
- BORROW_LIMIT_BUFFER,
17
- DECIMAL_PRECISION,
18
- ONE_DAY,
19
- PLYWNEAR_POOL_ID,
20
- ALL_STAKING_POOLS,
21
- PLYWNEAR_REWARD_TOKENS,
22
- } from './constants';
23
- import { TransactionResponse } from '@ethersproject/abstract-provider';
24
- import { Contract, BigNumber as BN } from 'ethers';
25
- import {
26
- isSameAddress,
27
- decimalFactor,
28
- calcLMRewardApr,
29
- getDecimal,
30
- } from './helpers';
31
- import { MoneyMarketRead } from './MoneyMarket';
32
- import {
33
- fetchValuation,
34
- fetchPrice,
35
- calcValuation,
36
- valuateToken,
37
- } from './priceFetcher';
38
- import {
39
- AuriFairLaunch,
40
- Comptroller,
41
- AuriLens,
42
- IERC20,
43
- } from '@aurigami/contracts/typechain';
44
- import { TokenAmount } from './tokenAmount';
45
- import { PLYWNEARToken, PLYToken, Token } from './token';
46
- import BigNumber from 'bignumber.js';
47
- import AuriLensABI from '@aurigami/contracts/artifacts/contracts/AuriLens.sol/AuriLens.json';
48
- import ComptrollerABI from '@aurigami/contracts/artifacts/contracts/Comptroller.sol/Comptroller.json';
49
- import AuriFairLaunchABI from '@aurigami/contracts/artifacts/contracts/AuriFairLaunch.sol/AuriFairLaunch.json';
50
- import FaucetABI from '@aurigami/contracts/artifacts/contracts/mock/Faucet.sol/Faucet.json';
51
- import EIP20InterfaceABI from '@aurigami/contracts/artifacts/contracts/interfaces/EIP20Interface.sol/EIP20Interface.json';
52
- import { PapermillRead } from './Papermill';
53
38
 
54
39
  export class SdkRead extends BlockchainEntityRead {
55
40
  protected _comptroller: Comptroller;
@@ -226,65 +211,7 @@ export class SdkRead extends BlockchainEntityRead {
226
211
  }
227
212
 
228
213
  public async getPLYWNEARPoolDetails(): Promise<PLYWNEARPoolDetails> {
229
- var stakedLiquidity: BN, rewardPerSeconds: BN[];
230
- await this._auriFairLaunch.getPoolInfo(PLYWNEAR_POOL_ID).then((res) => {
231
- stakedLiquidity = res.totalStake;
232
- rewardPerSeconds = res.rewardPerSeconds;
233
- });
234
-
235
- let stakedLiquidityAmount = new TokenAmount(
236
- PLYWNEARToken,
237
- stakedLiquidity!.toString()
238
- );
239
-
240
- var totalStakeValuation: BigNumber = await fetchValuation(
241
- stakedLiquidityAmount,
242
- this._networkConnection.provider
243
- );
244
-
245
- var promises = [];
246
-
247
- // Calculate the reward per second in USD
248
- for (let i = 0; i < rewardPerSeconds!.length; i++) {
249
- let rewardTokenAddress = PLYWNEAR_REWARD_TOKENS[i];
250
- let rewardToken = new Token(
251
- rewardTokenAddress,
252
- getDecimal(rewardTokenAddress)
253
- );
254
- let rewardPerSecond = rewardPerSeconds![i];
255
- promises.push(
256
- fetchValuation(
257
- new TokenAmount(rewardToken, rewardPerSecond!.toString()),
258
- this._networkConnection.provider
259
- )
260
- );
261
- }
262
-
263
- let APYs: TokenAPY[] = [];
264
- (await Promise.all(promises)).forEach((rewardPerSecondValuation, i) => {
265
- let rewardTokenAddress = PLYWNEAR_REWARD_TOKENS[i];
266
- let apy = calcLMRewardApr(
267
- rewardPerSecondValuation,
268
- totalStakeValuation,
269
- ONE_DAY * 365
270
- ).toFixed(DECIMAL_PRECISION);
271
-
272
- APYs.push({
273
- address: rewardTokenAddress,
274
- apy: apy,
275
- });
276
- });
277
-
278
- return {
279
- totalStakedLiquidity: {
280
- tokenAmount: stakedLiquidityAmount,
281
- currencyAmount: {
282
- currency: 'USD',
283
- amount: totalStakeValuation!.toFixed(DECIMAL_PRECISION),
284
- },
285
- },
286
- APYs: APYs,
287
- };
214
+ return new StakingRead(this._networkConnection).getPLYWNEARPoolDetails();
288
215
  }
289
216
 
290
217
  public async getTokenPrice(token: Token): Promise<CurrencyAmount> {
@@ -302,33 +229,18 @@ export class SdkRead extends BlockchainEntityRead {
302
229
  * Get amount and value in $ of staked LP tokens
303
230
  */
304
231
  public async stakeBalanceOf(user: string): Promise<TokenValuation> {
305
- const userInfo = await this._auriFairLaunch.getUserInfo(
306
- PLYWNEAR_POOL_ID,
307
- user
308
- );
309
- let stakeAmount = new TokenAmount(
310
- PLYWNEARToken,
311
- userInfo.amount.toString()
312
- );
313
-
314
- return valuateToken(stakeAmount, this._networkConnection.provider);
232
+ return new StakingRead(this._networkConnection).stakeBalanceOf(user);
315
233
  }
316
234
 
317
235
  /**
318
236
  * Get amount and value in $ of LP token in user wallet
319
237
  */
320
238
  public async LpBalanceOf(user: string): Promise<TokenValuation> {
321
- let lpContract = new Contract(
322
- networkAddresses.tokens.PLYWNEAR,
323
- EIP20InterfaceABI.abi,
324
- this._networkConnection.provider
325
- ) as IERC20;
326
- let balance = await lpContract.balanceOf(user);
239
+ return new StakingRead(this._networkConnection).LpBalanceOf(user);
240
+ }
327
241
 
328
- return valuateToken(
329
- new TokenAmount(PLYWNEARToken, balance.toString()),
330
- this._networkConnection.provider
331
- );
242
+ public async unclaimedRewardsOf(user: string): Promise<TokenAmount[]> {
243
+ return new StakingRead(this._networkConnection).unclaimedRewardsOf(user);
332
244
  }
333
245
 
334
246
  public calcHypotheticalStats({
@@ -416,15 +328,11 @@ export class SdkReadWrite extends SdkRead {
416
328
  }
417
329
 
418
330
  public async stake(amount: TokenAmount): Promise<TransactionResponse> {
419
- return this._auriFairLaunch
420
- .connect(this._networkConnection.signer!)
421
- .deposit(PLYWNEAR_POOL_ID, amount.rawAmount());
331
+ return new StakingReadWrite(this._networkConnection).stake(amount);
422
332
  }
423
333
 
424
334
  public async unstake(amount: TokenAmount): Promise<TransactionResponse> {
425
- return this._auriFairLaunch
426
- .connect(this._networkConnection.signer!)
427
- .withdraw(PLYWNEAR_POOL_ID, amount.rawAmount());
335
+ return new StakingReadWrite(this._networkConnection).unstake(amount);
428
336
  }
429
337
 
430
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
  }