@aurigami/sdk 0.2.2 → 0.2.3

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,6 +1,5 @@
1
1
  import { BlockchainEntity, BlockchainEntityRead } from './BlockchainEntity';
2
- import { MoneyMarketDetails, NetworkConnection, Address } from './types';
3
-
2
+ import { MoneyMarketDetails, NetworkConnection, Address, ComptrollerRewardType } from './types';
4
3
  import { Contract, BigNumber as BN } from 'ethers';
5
4
  import AuErc20ABI from '@aurigami/contracts/artifacts/contracts/AuErc20.sol/AuErc20.json';
6
5
  import AuETHABI from '@aurigami/contracts/artifacts/contracts/AuETH.sol/AuETH.json';
@@ -13,6 +12,7 @@ import { decimalFactor, isSameAddress } from './helpers';
13
12
  import { Token } from './token';
14
13
  import { TokenAmount } from './tokenAmount';
15
14
  import { getDecimal, validateAndParseAddress } from './helpers';
15
+ import { calcLMRewardApr, DECIMAL_PRECISION, fetchPrice, fetchPriceFromCoingecko, ONE_YEAR, PLYToken } from '.';
16
16
 
17
17
  export class MoneyMarketRead extends BlockchainEntityRead {
18
18
  public underlying: Token;
@@ -70,6 +70,40 @@ export class MoneyMarketRead extends BlockchainEntityRead {
70
70
  return new BigNumber(res.collateralFactorMantissa.toString()).div(decimalFactor(18));
71
71
  })
72
72
  }
73
+
74
+ public async getBorrowPlyAPY(): Promise<BigNumber> {
75
+ var promises = [];
76
+ var totalBorrowed: TokenAmount, rewardSpeed: TokenAmount, plyPrice: BigNumber, underlyingPrice: BigNumber;
77
+ promises.push(this.getTotalTokenBorrowed().then((res: TokenAmount) => {totalBorrowed = res}));
78
+ promises.push(this._comptroller.rewardSpeeds(ComptrollerRewardType.PLY, this.auToken.address).then((res) => {
79
+ rewardSpeed = new TokenAmount(
80
+ PLYToken,
81
+ res.toString()
82
+ )
83
+ }));
84
+ promises.push(fetchPrice(networkAddresses.tokens.PLY, this._networkConnection.provider).then((res) => {plyPrice = res}));
85
+ promises.push(fetchPrice(this.underlying.address, this._networkConnection.provider).then((res) => {underlyingPrice = res}));
86
+ await Promise.all(promises);
87
+ return calcLMRewardApr(plyPrice!.multipliedBy(rewardSpeed!.formattedAmount()),
88
+ underlyingPrice!.multipliedBy(totalBorrowed!.formattedAmount()), ONE_YEAR);
89
+ }
90
+
91
+ public async getDepositPlyAPY(): Promise<BigNumber> {
92
+ var promises = [];
93
+ var totalDeposited: TokenAmount, rewardSpeed: TokenAmount, plyPrice: BigNumber, underlyingPrice: BigNumber;
94
+ promises.push(this.getTotalTokenDeposited().then((res: TokenAmount) => {totalDeposited = res}));
95
+ promises.push(this._comptroller.rewardSpeeds(ComptrollerRewardType.PLY, this.auToken.address).then((res) => {
96
+ rewardSpeed = new TokenAmount(
97
+ PLYToken,
98
+ res.toString()
99
+ )
100
+ }));
101
+ promises.push(fetchPrice(networkAddresses.tokens.PLY, this._networkConnection.provider).then((res) => {plyPrice = res}));
102
+ promises.push(fetchPrice(this.underlying.address, this._networkConnection.provider).then((res) => {underlyingPrice = res}));
103
+ await Promise.all(promises);
104
+ return calcLMRewardApr(plyPrice!.multipliedBy(rewardSpeed!.formattedAmount()),
105
+ underlyingPrice!.multipliedBy(totalDeposited!.formattedAmount()), ONE_YEAR);
106
+ }
73
107
  public async getDetails(): Promise<MoneyMarketDetails> {
74
108
  var promises = [], totalDeposited: TokenAmount, totalBorrowed: TokenAmount, depositAPY: number, borrowAPY: number, collateralRatio: number;
75
109
  promises.push(this.getTotalTokenDeposited().then((res: TokenAmount) => {totalDeposited = res}));
@@ -77,16 +111,32 @@ export class MoneyMarketRead extends BlockchainEntityRead {
77
111
  promises.push(this.getDepositAPY().then((res: BigNumber) => {depositAPY = res.toNumber()}));
78
112
  promises.push(this.getBorrowAPY().then((res: BigNumber) => {borrowAPY = res.toNumber()}));
79
113
  promises.push(this.getCollateralRatio().then((res: BigNumber) => {collateralRatio = res.toNumber()}));
80
-
114
+
115
+ var rewardSpeed: TokenAmount, plyPrice: BigNumber, underlyingPrice: BigNumber;
116
+ promises.push(this._comptroller.rewardSpeeds(ComptrollerRewardType.PLY, this.auToken.address).then((res) => {
117
+ rewardSpeed = new TokenAmount(
118
+ PLYToken,
119
+ res.toString()
120
+ )
121
+ }));
122
+ promises.push(fetchPrice(networkAddresses.tokens.PLY, this._networkConnection.provider).then((res) => {plyPrice = res}));
123
+ promises.push(fetchPrice(this.underlying.address, this._networkConnection.provider).then((res) => {underlyingPrice = res}));
124
+
81
125
  await Promise.all(promises);
126
+
127
+ var borrowPlyApy = calcLMRewardApr(plyPrice!.multipliedBy(rewardSpeed!.formattedAmount()),
128
+ underlyingPrice!.multipliedBy(totalBorrowed!.formattedAmount()), ONE_YEAR);
129
+ var depositPlyApy = calcLMRewardApr(plyPrice!.multipliedBy(rewardSpeed!.formattedAmount()),
130
+ underlyingPrice!.multipliedBy(totalDeposited!.formattedAmount()), ONE_YEAR);
131
+
82
132
  return {
83
133
  auTokenAddress: this.auToken.address,
84
134
  totalTokenDeposited: totalDeposited!,
85
135
  totalTokenBorrowed: totalBorrowed!,
86
136
  depositApy: depositAPY!,
87
- depositPlyApy: 0.18, //To-do
137
+ depositPlyApy: depositPlyApy.toNumber(),
88
138
  borrowApy: borrowAPY!,
89
- borrowPlyApy: 0.18, // To-d0
139
+ borrowPlyApy: borrowPlyApy.toNumber(),
90
140
  collateralRatio: collateralRatio!
91
141
  };
92
142
  }
package/src/constants.ts CHANGED
@@ -82,5 +82,7 @@ export const REWARDCLAIMSTART = 1673280000;
82
82
 
83
83
  export const ONE_DAY = 86400;
84
84
 
85
+ export const ONE_YEAR = ONE_DAY * 365;
86
+
85
87
  export const AurPlyPid = 1;
86
88
 
@@ -11,7 +11,7 @@ import dummyABI from "./abis/dummy.json";
11
11
 
12
12
  const axios = require('axios')
13
13
 
14
- export async function fetchPriceFromCoingecko(id: string): Promise<BigNumber> {
14
+ export async function fetchPriceFromCoingeckoAPI(id: string): Promise<BigNumber> {
15
15
  const price = await axios.get(
16
16
  `https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=usd`
17
17
  ).then((res: any) => {
@@ -21,11 +21,11 @@ export async function fetchPriceFromCoingecko(id: string): Promise<BigNumber> {
21
21
  return new BigNumber(price[id].usd)
22
22
  }
23
23
 
24
- export async function fetchBasicTokenPrice(address: Address): Promise<BigNumber> {
24
+ export async function fetchPriceFromCoingecko(address: Address): Promise<BigNumber> {
25
25
  if (isSameAddress(address, networkAddresses.tokens.AURORA)) {
26
- return fetchPriceFromCoingecko('aurora-near');
26
+ return fetchPriceFromCoingeckoAPI('aurora-near');
27
27
  } else {
28
- throw Error(`Unknown token ${address} in fetchBasicTokenPrice`);
28
+ throw Error(`Unknown token ${address} in fetchPriceFromCoingecko`);
29
29
  }
30
30
  }
31
31
 
@@ -100,7 +100,7 @@ export async function fetchPrice(address: Address, provider: providers.Provider)
100
100
  } else if (isSameAddress(address, networkAddresses.misc.AUPLYLP)) {
101
101
  return fetchLPPrice(address, provider);
102
102
  } else {
103
- return fetchBasicTokenPrice(address);
103
+ return fetchPriceFromCoingecko(address);
104
104
  }
105
105
  }
106
106
  export async function fetchValuation(tokenAmount: TokenAmount, provider: providers.Provider, price?: BigNumber): Promise<BigNumber> {
package/src/types.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  import type { providers, Signer } from 'ethers';
2
2
  import { TokenAmount } from './tokenAmount';
3
3
 
4
+ export enum ComptrollerRewardType {
5
+ PLY = 0,
6
+ AURORA
7
+ };
4
8
  export type NetworkConnection = {
5
9
  provider: providers.Provider,
6
10
  signer?: Signer