@aurigami/sdk 0.1.6 → 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.
- package/dist/BlockchainEntity.d.ts +6 -7
- package/dist/MoneyMarket.d.ts +25 -12
- package/dist/SDK.d.ts +15 -9
- package/dist/constants.d.ts +19 -2
- package/dist/decimals.d.ts +1 -0
- package/dist/dummy.d.ts +6 -5
- package/dist/helpers.d.ts +9 -0
- package/dist/index.d.ts +4 -1
- package/dist/priceFetcher.d.ts +18 -0
- package/dist/sdk.cjs.development.js +2210 -312
- 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 +2184 -310
- package/dist/sdk.esm.js.map +1 -1
- package/dist/token.d.ts +7 -0
- package/dist/tokenAmount.d.ts +8 -0
- package/dist/types.d.ts +23 -11
- package/package.json +8 -7
- package/src/BlockchainEntity.ts +13 -10
- package/src/MoneyMarket.ts +178 -47
- package/src/SDK.ts +163 -72
- package/src/abis/IUniswapV2Pair.json +663 -0
- package/src/abis/Oracle.json +247 -0
- package/src/abis/dummy.json +3 -0
- package/src/constants.ts +75 -8
- package/src/decimals.ts +18 -0
- package/src/dummy.ts +30 -44
- package/src/helpers.ts +37 -0
- package/src/index.ts +4 -4
- package/src/priceFetcher.ts +108 -0
- package/src/token.ts +21 -0
- package/src/tokenAmount.ts +29 -0
- package/src/types.ts +26 -12
package/src/MoneyMarket.ts
CHANGED
|
@@ -1,73 +1,204 @@
|
|
|
1
1
|
import { BlockchainEntity, BlockchainEntityRead } from './BlockchainEntity';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
} from
|
|
8
|
-
import { Contract } from 'ethers';
|
|
9
|
-
import AuToken from '@aurigami/contracts/artifacts/contracts/AuToken.sol/AuToken.json';
|
|
10
|
-
import IERC20 from '@aurigami/contracts/artifacts/contracts/EIP20Interface.sol/EIP20Interface.json';
|
|
2
|
+
import { MoneyMarketDetails, NetworkConnection, Address, ComptrollerRewardType } from './types';
|
|
3
|
+
import { Contract, BigNumber as BN } from 'ethers';
|
|
4
|
+
import AuErc20ABI from '@aurigami/contracts/artifacts/contracts/AuErc20.sol/AuErc20.json';
|
|
5
|
+
import AuETHABI from '@aurigami/contracts/artifacts/contracts/AuETH.sol/AuETH.json';
|
|
6
|
+
import ComptrollerABI from '@aurigami/contracts/artifacts/contracts/Comptroller.sol/Comptroller.json';
|
|
7
|
+
import { AuErc20, AuETH, Comptroller } from "@aurigami/contracts/typechain";
|
|
11
8
|
import { TransactionResponse } from '@ethersproject/abstract-provider';
|
|
12
|
-
import {
|
|
9
|
+
import { networkAddresses, ETHAddress } from "./constants";
|
|
10
|
+
import BigNumber from "bignumber.js";
|
|
11
|
+
import { decimalFactor, isSameAddress } from './helpers';
|
|
12
|
+
import { Token } from './token';
|
|
13
|
+
import { TokenAmount } from './tokenAmount';
|
|
14
|
+
import { getDecimal, validateAndParseAddress } from './helpers';
|
|
15
|
+
import { calcLMRewardApr, DECIMAL_PRECISION, fetchPrice, fetchPriceFromCoingecko, ONE_YEAR, PLYToken } from '.';
|
|
13
16
|
|
|
14
17
|
export class MoneyMarketRead extends BlockchainEntityRead {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
18
|
+
public underlying: Token;
|
|
19
|
+
public auToken: AuErc20 | AuETH;
|
|
20
|
+
protected _comptroller: Comptroller;
|
|
21
|
+
constructor(networkConnection: NetworkConnection, auToken: Address, underlyingAsset: Address) {
|
|
22
|
+
super(networkConnection);
|
|
23
|
+
if (isSameAddress(underlyingAsset, ETHAddress)) {
|
|
24
|
+
this.auToken = new Contract(auToken, AuETHABI.abi, networkConnection.provider) as AuETH
|
|
25
|
+
} else {
|
|
26
|
+
this.auToken = new Contract(auToken, AuErc20ABI.abi, networkConnection.provider) as AuErc20;
|
|
27
|
+
}
|
|
28
|
+
this.underlying = new Token(
|
|
29
|
+
underlyingAsset,
|
|
30
|
+
getDecimal(underlyingAsset),
|
|
31
|
+
);
|
|
32
|
+
this._comptroller = new Contract(networkAddresses.misc.COMPTROLLER, ComptrollerABI.abi, networkConnection.provider) as Comptroller;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
public async getTotalTokenBorrowed(): Promise<TokenAmount> {
|
|
36
|
+
return new TokenAmount(
|
|
37
|
+
this.underlying,
|
|
38
|
+
(await this.auToken.totalBorrows()).toString()
|
|
39
|
+
)
|
|
40
|
+
}
|
|
41
|
+
public async getTotalTokenDeposited(): Promise<TokenAmount> {
|
|
42
|
+
var promises = [];
|
|
43
|
+
var totalBorrow: TokenAmount, totalCash: TokenAmount;
|
|
44
|
+
promises.push(this.getTotalTokenBorrowed().then((res: TokenAmount) => {totalBorrow = res}));
|
|
45
|
+
promises.push(this.auToken.getCash().then((res: BN) => {
|
|
46
|
+
totalCash = new TokenAmount(
|
|
47
|
+
this.underlying,
|
|
48
|
+
res.toString()
|
|
49
|
+
)
|
|
50
|
+
}))
|
|
51
|
+
await Promise.all(promises);
|
|
52
|
+
return new TokenAmount(
|
|
53
|
+
this.underlying,
|
|
54
|
+
BN.from(totalCash!.rawAmount).add(totalBorrow!.rawAmount()).toString()
|
|
55
|
+
)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
public async getDepositAPY(): Promise<BigNumber> {
|
|
59
|
+
const supplyRatePerTimestamp = await this.auToken.supplyRatePerTimestamp();
|
|
60
|
+
return new BigNumber(supplyRatePerTimestamp.toString()).multipliedBy(60*60*24*365).div(decimalFactor(18));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
public async getBorrowAPY(): Promise<BigNumber> {
|
|
64
|
+
const borrowRatePerTimestamp = await this.auToken.borrowRatePerTimestamp();
|
|
65
|
+
return new BigNumber(borrowRatePerTimestamp.toString()).multipliedBy(60*60*24*365).div(decimalFactor(18));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
public async getCollateralRatio(): Promise<BigNumber> {
|
|
69
|
+
return this._comptroller.markets(this.auToken.address).then((res: any) => {
|
|
70
|
+
return new BigNumber(res.collateralFactorMantissa.toString()).div(decimalFactor(18));
|
|
71
|
+
})
|
|
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);
|
|
19
106
|
}
|
|
20
107
|
public async getDetails(): Promise<MoneyMarketDetails> {
|
|
108
|
+
var promises = [], totalDeposited: TokenAmount, totalBorrowed: TokenAmount, depositAPY: number, borrowAPY: number, collateralRatio: number;
|
|
109
|
+
promises.push(this.getTotalTokenDeposited().then((res: TokenAmount) => {totalDeposited = res}));
|
|
110
|
+
promises.push(this.getTotalTokenBorrowed().then((res: TokenAmount) => {totalBorrowed = res}));
|
|
111
|
+
promises.push(this.getDepositAPY().then((res: BigNumber) => {depositAPY = res.toNumber()}));
|
|
112
|
+
promises.push(this.getBorrowAPY().then((res: BigNumber) => {borrowAPY = res.toNumber()}));
|
|
113
|
+
promises.push(this.getCollateralRatio().then((res: BigNumber) => {collateralRatio = res.toNumber()}));
|
|
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
|
+
|
|
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
|
+
|
|
21
132
|
return {
|
|
22
|
-
auTokenAddress:
|
|
23
|
-
totalTokenDeposited:
|
|
24
|
-
totalTokenBorrowed:
|
|
25
|
-
depositApy:
|
|
26
|
-
depositPlyApy:
|
|
27
|
-
borrowApy:
|
|
28
|
-
borrowPlyApy:
|
|
29
|
-
collateralRatio:
|
|
133
|
+
auTokenAddress: this.auToken.address,
|
|
134
|
+
totalTokenDeposited: totalDeposited!,
|
|
135
|
+
totalTokenBorrowed: totalBorrowed!,
|
|
136
|
+
depositApy: depositAPY!,
|
|
137
|
+
depositPlyApy: depositPlyApy.toNumber(),
|
|
138
|
+
borrowApy: borrowAPY!,
|
|
139
|
+
borrowPlyApy: borrowPlyApy.toNumber(),
|
|
140
|
+
collateralRatio: collateralRatio!
|
|
30
141
|
};
|
|
31
142
|
}
|
|
143
|
+
|
|
144
|
+
public async getUserBorrowBalance(userAddress: string): Promise<TokenAmount> {
|
|
145
|
+
const totalBorrow: BN = await this.auToken.callStatic.borrowBalanceCurrent(userAddress);
|
|
146
|
+
return new TokenAmount(
|
|
147
|
+
this.underlying,
|
|
148
|
+
totalBorrow.toString()
|
|
149
|
+
)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
public async getUserDepositBalance(userAddress: string): Promise<TokenAmount> {
|
|
153
|
+
const totalDeposit: BN = await this.auToken.callStatic.balanceOfUnderlying(userAddress);
|
|
154
|
+
return new TokenAmount(
|
|
155
|
+
this.underlying,
|
|
156
|
+
totalDeposit.toString()
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
|
|
32
161
|
}
|
|
33
162
|
|
|
34
163
|
export class MoneyMarketReadWrite extends MoneyMarketRead {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
return this.dummyTransaction();
|
|
164
|
+
public async deposit(amount: TokenAmount): Promise<TransactionResponse> {
|
|
165
|
+
if (isSameAddress(amount.token.address, ETHAddress)) {
|
|
166
|
+
return (this.auToken as AuETH).connect(this._networkConnection.signer!).mint({value: amount.rawAmount()});
|
|
167
|
+
} else {
|
|
168
|
+
return (this.auToken as AuErc20).connect(this._networkConnection.signer!).mint(amount.rawAmount());
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
public async borrow(amount: TokenAmount): Promise<TransactionResponse> {
|
|
172
|
+
return this.auToken.connect(this._networkConnection.signer!).borrow(amount.rawAmount());
|
|
173
|
+
}
|
|
174
|
+
public async withdraw(amount: TokenAmount): Promise<TransactionResponse> {
|
|
175
|
+
return this.auToken.connect(this._networkConnection.signer!).redeemUnderlying(amount.rawAmount());
|
|
176
|
+
}
|
|
177
|
+
public async repay(amount: TokenAmount): Promise<TransactionResponse> {
|
|
178
|
+
if (isSameAddress(amount.token.address, ETHAddress)) {
|
|
179
|
+
return (this.auToken as AuETH).connect(this._networkConnection.signer!).repayBorrow({value: amount.rawAmount()});
|
|
180
|
+
} else {
|
|
181
|
+
return (this.auToken as AuErc20).connect(this._networkConnection.signer!).repayBorrow(amount.rawAmount());
|
|
182
|
+
}
|
|
55
183
|
}
|
|
56
184
|
public async enableCollateral(): Promise<TransactionResponse> {
|
|
57
|
-
return this.
|
|
185
|
+
return this._comptroller.connect(this._networkConnection.signer!).enterMarkets([this.auToken.address]);
|
|
58
186
|
}
|
|
59
187
|
public async disableCollateral(): Promise<TransactionResponse> {
|
|
60
|
-
return this.
|
|
188
|
+
return this._comptroller.connect(this._networkConnection.signer!).exitMarket(this.auToken.address);
|
|
61
189
|
}
|
|
62
190
|
}
|
|
63
191
|
|
|
64
192
|
export class MoneyMarket extends BlockchainEntity {
|
|
65
193
|
protected address: Address;
|
|
66
|
-
|
|
194
|
+
|
|
195
|
+
protected underlying: Address;
|
|
196
|
+
constructor(address: string, underlying: string) {
|
|
67
197
|
super();
|
|
68
198
|
this.address = validateAndParseAddress(address);
|
|
199
|
+
this.underlying = validateAndParseAddress(underlying);
|
|
69
200
|
}
|
|
70
|
-
public read(
|
|
71
|
-
return new MoneyMarketRead(
|
|
201
|
+
public read(networkConnection: NetworkConnection): MoneyMarketRead {
|
|
202
|
+
return new MoneyMarketRead(networkConnection, this.address, this.underlying);
|
|
72
203
|
}
|
|
73
204
|
}
|
package/src/SDK.ts
CHANGED
|
@@ -1,109 +1,200 @@
|
|
|
1
1
|
import { BlockchainEntity, BlockchainEntityRead } from './BlockchainEntity';
|
|
2
|
-
import {
|
|
3
|
-
dummyPly,
|
|
4
|
-
dummyPlyAurora,
|
|
5
|
-
dummyToken,
|
|
6
|
-
dummyTokenAmount2,
|
|
7
|
-
dummyTokenAmount3,
|
|
8
|
-
dummyUserMarketDetails,
|
|
9
|
-
} from './dummy';
|
|
10
2
|
import {
|
|
11
3
|
LockingDetails,
|
|
12
4
|
PlyAuroraPoolDetails,
|
|
13
|
-
|
|
5
|
+
NetworkConnection,
|
|
14
6
|
UserDetails,
|
|
7
|
+
UserMarketDetails,
|
|
8
|
+
Address,
|
|
9
|
+
CurrencyAmount,
|
|
15
10
|
} from './types';
|
|
16
|
-
import {
|
|
17
|
-
import { AVAX_DEFAULT_PROVIDER_URL, DOLLAR } from './constants';
|
|
11
|
+
import { AVAX_DEFAULT_PROVIDER_URL, networkAddresses, BORROW_LIMIT_BUFFER, DECIMAL_PRECISION, REWARDCLAIMSTART, ONE_DAY, AurPlyPid } from './constants';
|
|
18
12
|
import { TransactionResponse } from '@ethersproject/abstract-provider';
|
|
13
|
+
import { Contract, BigNumber as BN, ethers, providers } from 'ethers';
|
|
14
|
+
import { isSameAddress, decimalFactor, getCurrentTimestamp, calcLMRewardApr } from './helpers';
|
|
15
|
+
import { MoneyMarketRead } from './MoneyMarket';
|
|
16
|
+
import { fetchValuation, fetchPrice } from './priceFetcher';
|
|
17
|
+
import { AuriFairLaunch, Comptroller, TokenLock } from '@aurigami/contracts/typechain';
|
|
18
|
+
import { TokenAmount } from './tokenAmount';
|
|
19
|
+
import { AURPLYToken, PLYToken, Token } from './token';
|
|
20
|
+
import BigNumber from 'bignumber.js';
|
|
19
21
|
import IERC20 from '@aurigami/contracts/artifacts/contracts/EIP20Interface.sol/EIP20Interface.json';
|
|
20
|
-
import
|
|
21
|
-
|
|
22
|
+
import ComptrollerABI from '@aurigami/contracts/artifacts/contracts/Comptroller.sol/Comptroller.json';
|
|
23
|
+
import TokenLockABI from "@aurigami/contracts/artifacts/contracts/TokenLock.sol/TokenLock.json";
|
|
24
|
+
import AuriFairLaunchABI from "@aurigami/contracts/artifacts/contracts/AuriFairLaunch.sol/AuriFairLaunch.json";
|
|
25
|
+
import dummyABI from "./abis/dummy.json";
|
|
22
26
|
|
|
23
27
|
export class SdkRead extends BlockchainEntityRead {
|
|
24
|
-
|
|
25
|
-
|
|
28
|
+
|
|
29
|
+
protected _comptroller: Comptroller;
|
|
30
|
+
protected _tokenLock: TokenLock;
|
|
31
|
+
protected _auriFairLaunch: AuriFairLaunch;
|
|
32
|
+
protected _auriLens: Contract; // To-do
|
|
33
|
+
constructor(networkConnection: NetworkConnection) {
|
|
34
|
+
super(networkConnection);
|
|
35
|
+
this._comptroller = new Contract(networkAddresses.misc.COMPTROLLER, ComptrollerABI.abi, networkConnection.provider) as Comptroller;
|
|
36
|
+
this._tokenLock = new Contract(networkAddresses.misc.TOKENLOCK, TokenLockABI.abi, networkConnection.provider) as TokenLock;
|
|
37
|
+
this._auriFairLaunch = new Contract(networkAddresses.misc.AURIFAIRLAUNCH, AuriFairLaunchABI.abi, networkConnection.provider) as AuriFairLaunch;
|
|
38
|
+
this._auriLens = new Contract(networkAddresses.misc.AURILENS, dummyABI.abi, networkConnection.provider); //To-do
|
|
26
39
|
}
|
|
27
40
|
|
|
28
41
|
public async getLockingDetails(): Promise<LockingDetails> {
|
|
42
|
+
const currentTime = await getCurrentTimestamp(this._networkConnection.provider);
|
|
43
|
+
const currentWeek = BN.from(currentTime - REWARDCLAIMSTART).div(7 * ONE_DAY).toNumber();
|
|
44
|
+
const denominator = 10000;
|
|
45
|
+
var promises = [];
|
|
46
|
+
promises.push(this._tokenLock.percentageLock(currentWeek));
|
|
47
|
+
promises.push(this._tokenLock.percentageLock(currentWeek + 1).catch((err: any) => {return denominator}));
|
|
48
|
+
var values: string[] = await Promise.all(promises).then((res) => {
|
|
49
|
+
return res.map((v) => {return v.toString()})
|
|
50
|
+
});
|
|
29
51
|
return {
|
|
30
|
-
vestingStart:
|
|
31
|
-
currentUnlockPortion: 0.
|
|
32
|
-
nextUnlockPortion:
|
|
52
|
+
vestingStart: REWARDCLAIMSTART, // 10 Jan 2023
|
|
53
|
+
currentUnlockPortion: new BigNumber(values[0]).div(denominator).toNumber(),
|
|
54
|
+
nextUnlockPortion: new BigNumber(values[1]).div(denominator).toNumber(),
|
|
33
55
|
};
|
|
34
56
|
}
|
|
35
57
|
|
|
36
58
|
public async getUserDetails(userAddress: string): Promise<UserDetails> {
|
|
59
|
+
const marketCount = networkAddresses.auTokens.length;
|
|
60
|
+
var userMarketDetails: UserMarketDetails[] = [], assetsIn: string[];
|
|
61
|
+
var collateralRatio: BigNumber[];
|
|
62
|
+
var promises = [];
|
|
63
|
+
var totalBorrowLimit = new BigNumber(0);
|
|
64
|
+
var pricePromises = [];
|
|
65
|
+
for (const auToken of networkAddresses.auTokens) {
|
|
66
|
+
const userMarketDetail: UserMarketDetails = {} as UserMarketDetails;
|
|
67
|
+
userMarketDetails.push(userMarketDetail);
|
|
68
|
+
userMarketDetail.market = auToken.underlying;
|
|
69
|
+
const moneyMarket: MoneyMarketRead = new MoneyMarketRead(this._networkConnection, auToken.address, auToken.underlying);
|
|
70
|
+
promises.push(moneyMarket.getUserDepositBalance(userAddress).then((res: TokenAmount) => {userMarketDetail.depositBalance = res}));
|
|
71
|
+
promises.push(moneyMarket.getUserBorrowBalance(userAddress).then((res: TokenAmount) => {userMarketDetail.borrowBalance = res}));
|
|
72
|
+
promises.push(moneyMarket.getCollateralRatio().then((res: BigNumber) => {collateralRatio[i] = res}));
|
|
73
|
+
pricePromises.push(fetchPrice(moneyMarket.underlying.address, this._networkConnection.provider));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
promises.push(this._comptroller.getAssetsIn(userAddress).then((res: string[]) => {assetsIn = res}));
|
|
77
|
+
|
|
78
|
+
await Promise.all(promises);
|
|
79
|
+
var underlyingPrices = await Promise.all(pricePromises);
|
|
80
|
+
var depositValuationPromises = [];
|
|
81
|
+
|
|
82
|
+
for (var i = 0; i < marketCount; i ++) {
|
|
83
|
+
const auToken = networkAddresses.auTokens[i];
|
|
84
|
+
if (assetsIn!.filter((auAddress: Address) => {
|
|
85
|
+
isSameAddress(auToken.address, auAddress)
|
|
86
|
+
}).length > 0) {
|
|
87
|
+
userMarketDetails[i].isCollateral = true;
|
|
88
|
+
}
|
|
89
|
+
depositValuationPromises.push(fetchValuation(userMarketDetails[i].depositBalance, this._networkConnection.provider, underlyingPrices[i]));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
var depositValues = await Promise.all(depositValuationPromises);
|
|
93
|
+
totalBorrowLimit = depositValues.reduce((p: BigNumber, v: BigNumber, index: number)=> {
|
|
94
|
+
if (userMarketDetails[index].isCollateral) return p.plus(v.multipliedBy(collateralRatio[index]));
|
|
95
|
+
return p;
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
promises = [];
|
|
99
|
+
var accountLiquidity: [BN, BN, BN], rewardBalancesMetadata, lockedAmount: BN;
|
|
100
|
+
promises.push(this._comptroller.getAccountLiquidity(userAddress));
|
|
101
|
+
promises.push(this._auriLens.getRewardBalancesMetadata(this._comptroller.address, userAddress));
|
|
102
|
+
promises.push(this._tokenLock.lockedAmounts(userAddress));
|
|
103
|
+
var values = await Promise.all(promises);
|
|
104
|
+
accountLiquidity = values[0]; rewardBalancesMetadata = values[1]; lockedAmount = values[2];
|
|
105
|
+
const borrowedvaluation: BigNumber = totalBorrowLimit.minus(new BigNumber(accountLiquidity[1].toString()).div(decimalFactor(18)));
|
|
106
|
+
const bufferedBorrowLimit: BigNumber = totalBorrowLimit.multipliedBy(BORROW_LIMIT_BUFFER);
|
|
107
|
+
|
|
108
|
+
var borrowableAmount: BigNumber;
|
|
109
|
+
if (bufferedBorrowLimit.lte(borrowedvaluation)) {
|
|
110
|
+
borrowableAmount = new BigNumber(0)
|
|
111
|
+
} else {
|
|
112
|
+
borrowableAmount = bufferedBorrowLimit.minus(borrowedvaluation);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
for (var i = 0; i < marketCount; i ++) {
|
|
116
|
+
const auToken = networkAddresses.auTokens[i];
|
|
117
|
+
const moneyMarket: MoneyMarketRead = new MoneyMarketRead(this._networkConnection, auToken.address, auToken.underlying);
|
|
118
|
+
userMarketDetails[i].maxWithdrawableAmount = new TokenAmount(
|
|
119
|
+
moneyMarket.underlying,
|
|
120
|
+
borrowableAmount.div(underlyingPrices[i]).toFixed(18)
|
|
121
|
+
)
|
|
122
|
+
}
|
|
123
|
+
|
|
37
124
|
return {
|
|
38
|
-
markets:
|
|
39
|
-
borrowLimit:
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
currency:
|
|
47
|
-
amount:
|
|
48
|
-
|
|
49
|
-
}),
|
|
50
|
-
borrowableAmount: dummyTokenAmount2,
|
|
125
|
+
markets: userMarketDetails,
|
|
126
|
+
borrowLimit: {
|
|
127
|
+
currency: "USD",
|
|
128
|
+
amount: bufferedBorrowLimit.toFixed(DECIMAL_PRECISION)
|
|
129
|
+
},
|
|
130
|
+
accruedPly: new TokenAmount(PLYToken, rewardBalancesMetadata.plyAccrued.toString()),
|
|
131
|
+
lockedPly: new TokenAmount(PLYToken, lockedAmount.toString()),
|
|
132
|
+
borrowableAmount: {
|
|
133
|
+
currency: "USD",
|
|
134
|
+
amount: borrowableAmount.toFixed(DECIMAL_PRECISION)
|
|
135
|
+
},
|
|
51
136
|
};
|
|
52
137
|
}
|
|
53
138
|
|
|
54
139
|
public async getPlyAuroraPoolDetails(): Promise<PlyAuroraPoolDetails> {
|
|
140
|
+
var stakedLiquidity: BN, rewardPerSecond: BN;
|
|
141
|
+
var promises = [];
|
|
142
|
+
promises.push(this._auriFairLaunch.getPoolInfo(AurPlyPid).then((res) => {
|
|
143
|
+
stakedLiquidity = res.totalStake;
|
|
144
|
+
rewardPerSecond = res.rewardPerSecond;
|
|
145
|
+
}));
|
|
146
|
+
await Promise.all(promises);
|
|
147
|
+
|
|
148
|
+
promises = [];
|
|
149
|
+
var rewardPerSecondValuation: BigNumber, totalStakeValuation: BigNumber;
|
|
150
|
+
promises.push(fetchValuation(new TokenAmount(
|
|
151
|
+
PLYToken,
|
|
152
|
+
rewardPerSecond!.toString()
|
|
153
|
+
), this._networkConnection.provider).then((res) => rewardPerSecondValuation = res));
|
|
154
|
+
promises.push(fetchValuation(new TokenAmount(
|
|
155
|
+
AURPLYToken,
|
|
156
|
+
stakedLiquidity!.toString()
|
|
157
|
+
), this._networkConnection.provider).then((res) => totalStakeValuation = res));
|
|
158
|
+
|
|
55
159
|
return {
|
|
56
|
-
totalStakedLiquidity: new
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
apy: 1.21,
|
|
160
|
+
totalStakedLiquidity: new TokenAmount(
|
|
161
|
+
AURPLYToken,
|
|
162
|
+
stakedLiquidity!.toString()
|
|
163
|
+
),
|
|
164
|
+
apy: calcLMRewardApr(rewardPerSecondValuation!, totalStakeValuation!, ONE_DAY * 365).toFixed(DECIMAL_PRECISION),
|
|
62
165
|
};
|
|
63
166
|
}
|
|
64
167
|
|
|
65
168
|
public async getTokenPrice(token: Token): Promise<CurrencyAmount> {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
isRaw: false,
|
|
71
|
-
});
|
|
169
|
+
const price = await fetchPrice(token.address, this._networkConnection.provider);
|
|
170
|
+
return {
|
|
171
|
+
currency: "USD",
|
|
172
|
+
amount: price.toFixed(DECIMAL_PRECISION)
|
|
72
173
|
}
|
|
73
|
-
|
|
74
|
-
return new CurrencyAmount({
|
|
75
|
-
currency: DOLLAR,
|
|
76
|
-
amount: '12.1',
|
|
77
|
-
isRaw: false,
|
|
78
|
-
});
|
|
79
174
|
}
|
|
80
|
-
public async stakeBalanceOf(user: string): Promise<
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
175
|
+
public async stakeBalanceOf(user: string): Promise<TokenAmount> {
|
|
176
|
+
const userInfo = await this._auriFairLaunch.getUserInfo(AurPlyPid, user);
|
|
177
|
+
return new TokenAmount(
|
|
178
|
+
AURPLYToken,
|
|
179
|
+
userInfo.amount.toString()
|
|
180
|
+
)
|
|
86
181
|
}
|
|
87
182
|
}
|
|
88
183
|
|
|
89
184
|
export class SdkReadWrite extends SdkRead {
|
|
90
|
-
dummyTransaction = async (): Promise<TransactionResponse> => {
|
|
91
|
-
// const dummyErc20Token = new Contract(
|
|
92
|
-
// dummyToken.address,
|
|
93
|
-
// IERC20.abi,
|
|
94
|
-
// this._providerOrSigner
|
|
95
|
-
// );
|
|
96
|
-
// return await dummyErc20Token.approve(dummyToken.address, 123);
|
|
97
|
-
return {} as TransactionResponse;
|
|
98
|
-
};
|
|
99
185
|
public async claim(): Promise<TransactionResponse> {
|
|
100
|
-
return this.
|
|
186
|
+
return this._auriLens.connect(this._networkConnection.signer!).claimRewards(
|
|
187
|
+
this._comptroller.address,
|
|
188
|
+
this._auriFairLaunch.address,
|
|
189
|
+
await this._networkConnection.signer!.getAddress(),
|
|
190
|
+
[AurPlyPid]
|
|
191
|
+
);
|
|
101
192
|
}
|
|
102
|
-
public async stake(amount:
|
|
103
|
-
return this.
|
|
193
|
+
public async stake(amount: TokenAmount): Promise<TransactionResponse> {
|
|
194
|
+
return this._auriFairLaunch.connect(this._networkConnection.signer!).deposit(AurPlyPid, amount.rawAmount(), false);
|
|
104
195
|
}
|
|
105
|
-
public async unstake(amount:
|
|
106
|
-
return this.
|
|
196
|
+
public async unstake(amount: TokenAmount): Promise<TransactionResponse> {
|
|
197
|
+
return this._auriFairLaunch.connect(this._networkConnection.signer!).withdraw(AurPlyPid, amount.rawAmount());
|
|
107
198
|
}
|
|
108
199
|
}
|
|
109
200
|
|
|
@@ -111,11 +202,11 @@ export class SDK extends BlockchainEntity {
|
|
|
111
202
|
constructor() {
|
|
112
203
|
super();
|
|
113
204
|
}
|
|
114
|
-
public read(
|
|
115
|
-
return new SdkRead(
|
|
205
|
+
public read(networkConnection: NetworkConnection): SdkRead {
|
|
206
|
+
return new SdkRead(networkConnection);
|
|
116
207
|
}
|
|
117
208
|
|
|
118
|
-
public static defaultProvider():
|
|
209
|
+
public static defaultProvider(): providers.Provider {
|
|
119
210
|
return new ethers.providers.JsonRpcProvider(AVAX_DEFAULT_PROVIDER_URL);
|
|
120
211
|
}
|
|
121
212
|
}
|