@aurigami/sdk 1.24.0 → 1.24.1
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/helpers/priceFetcher.js +9 -13
- package/package.json +2 -14
- package/src/SDK.ts +0 -223
- package/src/abis/index.ts +0 -38
- package/src/consts/constants.ts +0 -210
- package/src/consts/decimals.ts +0 -31
- package/src/consts/deployments.ts +0 -4
- package/src/consts/dummy.ts +0 -51
- package/src/consts/index.ts +0 -5
- package/src/consts/symbols.ts +0 -9
- package/src/entities/BlockchainEntity.ts +0 -29
- package/src/entities/auriEnv.ts +0 -180
- package/src/entities/index.ts +0 -4
- package/src/entities/token.ts +0 -21
- package/src/entities/tokenAmount.ts +0 -48
- package/src/helpers/aurigami-api.ts +0 -42
- package/src/helpers/aurora-api-helpers.ts +0 -17
- package/src/helpers/graphql-helpers.ts +0 -8
- package/src/helpers/helpers.ts +0 -212
- package/src/helpers/historicalPriceFetcher.ts +0 -200
- package/src/helpers/index.ts +0 -7
- package/src/helpers/kyber-aggregator-api.ts +0 -37
- package/src/helpers/multicall.ts +0 -251
- package/src/helpers/notification-api.ts +0 -22
- package/src/helpers/one-inch-aggregator-api.ts +0 -45
- package/src/helpers/priceFetcher.ts +0 -388
- package/src/helpers/subgraphQuery.ts +0 -17
- package/src/helpers/transfer-event-query.ts +0 -68
- package/src/index.ts +0 -14
- package/src/interactors/Airdrop.ts +0 -72
- package/src/interactors/MoneyMarket.ts +0 -486
- package/src/interactors/Multicall.ts +0 -50
- package/src/interactors/Papermill.ts +0 -185
- package/src/interactors/PlyGame.ts +0 -172
- package/src/interactors/PlyTokenLock.ts +0 -46
- package/src/interactors/Pulp.ts +0 -41
- package/src/interactors/Referral.ts +0 -214
- package/src/interactors/Staking.ts +0 -156
- package/src/interactors/index.ts +0 -9
- package/src/interactors/misc.ts +0 -433
- package/src/types/index.ts +0 -252
package/src/consts/dummy.ts
DELETED
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { Token, TokenAmount } from '../entities';
|
|
2
|
-
import { ReferralReward, UserMarketDetails } from '../types';
|
|
3
|
-
import { networkAddresses } from './constants';
|
|
4
|
-
|
|
5
|
-
export const dummyToken = new Token(networkAddresses.tokens.WNEAR, 24);
|
|
6
|
-
|
|
7
|
-
export const dummyPly = new Token('0x8729438eb15e2c8b576fcc6aecda6a148776c0f5', 18);
|
|
8
|
-
|
|
9
|
-
export const dummyPlyAurora = new Token('0xE530dC2095Ef5653205CF5ea79F8979a7028065c', 18);
|
|
10
|
-
|
|
11
|
-
export const dummyTokenAmount = new TokenAmount(dummyToken, '123.456', false);
|
|
12
|
-
|
|
13
|
-
export const dummyZeroTokenAmount = new TokenAmount(dummyToken, '0');
|
|
14
|
-
|
|
15
|
-
export const dummyTokenAmount2 = new TokenAmount(dummyToken, '456.123', false);
|
|
16
|
-
|
|
17
|
-
export const dummyTokenAmount3 = new TokenAmount(dummyToken, '1000.123', false);
|
|
18
|
-
|
|
19
|
-
export const dummyMarketAddress = '0xbeb5d47a3f720ec0a390d04b4d41ed7d9688bc7f'; // qiUSDC
|
|
20
|
-
|
|
21
|
-
export const dummyUserMarketDetails: UserMarketDetails = {
|
|
22
|
-
market: dummyMarketAddress,
|
|
23
|
-
depositBalance: dummyTokenAmount2,
|
|
24
|
-
borrowBalance: dummyZeroTokenAmount,
|
|
25
|
-
isCollateral: true,
|
|
26
|
-
maxWithdrawableAmount: dummyTokenAmount,
|
|
27
|
-
collateralRatio: 0.5,
|
|
28
|
-
underlyingPrice: '1',
|
|
29
|
-
};
|
|
30
|
-
|
|
31
|
-
export const referralRewardDummy1: ReferralReward = {
|
|
32
|
-
campaign: 'testa',
|
|
33
|
-
startTime: Math.trunc(new Date(2022, 8, 1).getTime() / 1000),
|
|
34
|
-
endTime: Math.trunc(new Date(2022, 9, 30).getTime() / 1000),
|
|
35
|
-
isReferrer: false,
|
|
36
|
-
rewardTokenAmount: dummyTokenAmount,
|
|
37
|
-
transactionHash: null,
|
|
38
|
-
userAddr: '0x...123',
|
|
39
|
-
refereeAddress: '0x123..123',
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
export const referralRewardDummy2: ReferralReward = {
|
|
43
|
-
campaign: 'testb',
|
|
44
|
-
startTime: Math.trunc(new Date(2022, 8, 1).getTime() / 1000),
|
|
45
|
-
endTime: Math.trunc(new Date(2022, 8, 31).getTime() / 1000),
|
|
46
|
-
isReferrer: true,
|
|
47
|
-
rewardTokenAmount: dummyTokenAmount2,
|
|
48
|
-
transactionHash: '0x4da9657663b46ef18d150b897b28c7b717f16421978b52e3a06d91b54ca3548b',
|
|
49
|
-
userAddr: '0x...456',
|
|
50
|
-
refereeAddress: '0x123..123',
|
|
51
|
-
};
|
package/src/consts/index.ts
DELETED
package/src/consts/symbols.ts
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { networkAddresses } from './constants';
|
|
2
|
-
|
|
3
|
-
export const symbolRecords: Record<string, string> = Object.fromEntries(
|
|
4
|
-
networkAddresses.auTokens
|
|
5
|
-
.map((e) => [e.address, e.name]) // AuToken
|
|
6
|
-
.concat(networkAddresses.auTokens.map((e) => [e.underlying, e.name.slice(2)])) // underlying token
|
|
7
|
-
.concat([['0x04ac48711bcdc45b4d223fb021e09da73c71095e', 'PULP']]) // others
|
|
8
|
-
.map(([address, name]) => [address.toLowerCase(), name])
|
|
9
|
-
);
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { Signer } from '@ethersproject/abstract-signer';
|
|
2
|
-
import { NetworkConnection } from '../types';
|
|
3
|
-
|
|
4
|
-
export class BlockchainEntityRead {
|
|
5
|
-
protected _networkConnection: NetworkConnection;
|
|
6
|
-
public constructor(networkConnection: NetworkConnection) {
|
|
7
|
-
this._networkConnection = networkConnection;
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export class BlockchainEntityReadWrite extends BlockchainEntityRead {
|
|
12
|
-
public constructor(networkConnection: NetworkConnection) {
|
|
13
|
-
if (!(networkConnection.signer instanceof Signer)) {
|
|
14
|
-
throw Error('Signer is not provided when constructing BlockchainEntityReadWrite');
|
|
15
|
-
}
|
|
16
|
-
super(networkConnection);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export class BlockchainEntity {
|
|
21
|
-
constructor() {}
|
|
22
|
-
public read(networkConnection: NetworkConnection): BlockchainEntityRead {
|
|
23
|
-
return new BlockchainEntityRead(networkConnection);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
public readWrite(networkConnection: NetworkConnection): BlockchainEntityReadWrite {
|
|
27
|
-
return new BlockchainEntityReadWrite(networkConnection);
|
|
28
|
-
}
|
|
29
|
-
}
|
package/src/entities/auriEnv.ts
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AuErc20,
|
|
3
|
-
AuToken,
|
|
4
|
-
AuriAirdrop,
|
|
5
|
-
AuriFairLaunch,
|
|
6
|
-
AuriInternalLens,
|
|
7
|
-
AuriLens,
|
|
8
|
-
AuriOracle,
|
|
9
|
-
Comptroller,
|
|
10
|
-
IERC20,
|
|
11
|
-
PULP,
|
|
12
|
-
PlyGame,
|
|
13
|
-
PlyTokenLock,
|
|
14
|
-
ReferralDirectoryV2,
|
|
15
|
-
} from '@aurigami/contracts/typechain';
|
|
16
|
-
import { Contract } from 'ethers';
|
|
17
|
-
import * as abis from '../abis';
|
|
18
|
-
import { networkAddresses } from '../consts/constants';
|
|
19
|
-
import { assert } from '../helpers/helpers';
|
|
20
|
-
import { createContract } from '../helpers/multicall';
|
|
21
|
-
import { AuTokenWithUnderlying, MulticallStatic } from '../types';
|
|
22
|
-
import { BlockchainEntityRead } from './BlockchainEntity';
|
|
23
|
-
|
|
24
|
-
export class AuriEnv extends BlockchainEntityRead {
|
|
25
|
-
private _comptroller: MulticallStatic<Comptroller> | null = null;
|
|
26
|
-
private _auriFairLaunch: MulticallStatic<AuriFairLaunch> | null = null;
|
|
27
|
-
private _auriLens: MulticallStatic<AuriLens> | null = null;
|
|
28
|
-
private _ply: MulticallStatic<IERC20> | null = null;
|
|
29
|
-
private _pulp: MulticallStatic<PULP> | null = null;
|
|
30
|
-
private _auriInternalLens: MulticallStatic<AuriInternalLens> | null = null;
|
|
31
|
-
private _auriAirdrop: MulticallStatic<AuriAirdrop> | null = null;
|
|
32
|
-
private _referralDirectoryV2: MulticallStatic<ReferralDirectoryV2> | null = null;
|
|
33
|
-
private _oracle: MulticallStatic<AuriOracle> | null = null;
|
|
34
|
-
private _plygame: MulticallStatic<PlyGame> | null = null;
|
|
35
|
-
private _plyTokenLock: MulticallStatic<PlyTokenLock> | null = null;
|
|
36
|
-
|
|
37
|
-
public getContract<CType extends Contract>(address: string, abi: any) {
|
|
38
|
-
return createContract<CType>(address, abi, this._networkConnection.provider);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public getAuTokenContracts(): AuTokenWithUnderlying[] {
|
|
42
|
-
return networkAddresses.auTokens.map((auToken) => {
|
|
43
|
-
const contract = this.getContract<AuToken>(
|
|
44
|
-
auToken.address,
|
|
45
|
-
abis.AuTokenABI.abi
|
|
46
|
-
) as AuTokenWithUnderlying;
|
|
47
|
-
contract.underlying = auToken.underlying;
|
|
48
|
-
return contract;
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* Get AuErc20 contract by its address or underlying name.
|
|
54
|
-
*
|
|
55
|
-
* Either address or underlyingName must be provided
|
|
56
|
-
*
|
|
57
|
-
*/
|
|
58
|
-
public getAuErc20Contract({
|
|
59
|
-
address,
|
|
60
|
-
underlyingName,
|
|
61
|
-
}: {
|
|
62
|
-
address?: string;
|
|
63
|
-
underlyingName?: string;
|
|
64
|
-
}) {
|
|
65
|
-
assert(
|
|
66
|
-
address !== undefined || underlyingName !== undefined,
|
|
67
|
-
'Either address or underlyingName must be provided'
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
if (underlyingName) {
|
|
71
|
-
address = networkAddresses.auTokens.find(
|
|
72
|
-
(auToken) => auToken.name == 'au' + underlyingName
|
|
73
|
-
)?.address;
|
|
74
|
-
assert(address !== undefined, `AuToken with underlying ${underlyingName} not found`);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return this.getContract<AuErc20>(address!, abis.AuErc20ABI.abi);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
get comptroller() {
|
|
81
|
-
if (!this._comptroller) {
|
|
82
|
-
this._comptroller = this.getContract<Comptroller>(
|
|
83
|
-
networkAddresses.misc.COMPTROLLER,
|
|
84
|
-
abis.ComptrollerABI.abi
|
|
85
|
-
);
|
|
86
|
-
}
|
|
87
|
-
return this._comptroller;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
get auriFairLaunch() {
|
|
91
|
-
if (!this._auriFairLaunch) {
|
|
92
|
-
this._auriFairLaunch = this.getContract<AuriFairLaunch>(
|
|
93
|
-
networkAddresses.misc.AURIFAIRLAUNCH,
|
|
94
|
-
abis.AuriFairLaunchABI.abi
|
|
95
|
-
);
|
|
96
|
-
}
|
|
97
|
-
return this._auriFairLaunch;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
get auriLens() {
|
|
101
|
-
if (!this._auriLens) {
|
|
102
|
-
this._auriLens = this.getContract<AuriLens>(
|
|
103
|
-
networkAddresses.misc.AURILENS,
|
|
104
|
-
abis.AuriLensABI.abi
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
return this._auriLens;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
get ply() {
|
|
111
|
-
if (!this._ply) {
|
|
112
|
-
this._ply = this.getContract<IERC20>(networkAddresses.tokens.PLY, abis.EIP20InterfaceABI.abi);
|
|
113
|
-
}
|
|
114
|
-
return this._ply;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
get pulp() {
|
|
118
|
-
if (!this._pulp) {
|
|
119
|
-
this._pulp = this.getContract<PULP>(networkAddresses.tokens.PULP, abis.PulpABI.abi);
|
|
120
|
-
}
|
|
121
|
-
return this._pulp;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
get auriInternalLens() {
|
|
125
|
-
if (!this._auriInternalLens) {
|
|
126
|
-
this._auriInternalLens = this.getContract<AuriInternalLens>(
|
|
127
|
-
networkAddresses.misc.AURI_INTERNAL_LENS,
|
|
128
|
-
abis.AuriInternalLensABI.abi
|
|
129
|
-
);
|
|
130
|
-
}
|
|
131
|
-
return this._auriInternalLens;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
get auriAirdrop() {
|
|
135
|
-
if (!this._auriAirdrop) {
|
|
136
|
-
this._auriAirdrop = this.getContract<AuriAirdrop>(
|
|
137
|
-
networkAddresses.misc.AURI_AIRDROP,
|
|
138
|
-
abis.AuriAirdropABI.abi
|
|
139
|
-
);
|
|
140
|
-
}
|
|
141
|
-
return this._auriAirdrop;
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
get referralDirectoryV2() {
|
|
145
|
-
if (!this._referralDirectoryV2) {
|
|
146
|
-
this._referralDirectoryV2 = this.getContract<ReferralDirectoryV2>(
|
|
147
|
-
networkAddresses.misc.REFERRAL_V2,
|
|
148
|
-
abis.ReferralDirectoryV2ABI.abi
|
|
149
|
-
);
|
|
150
|
-
}
|
|
151
|
-
return this._referralDirectoryV2;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
get oracle() {
|
|
155
|
-
if (!this._oracle) {
|
|
156
|
-
this._oracle = this.getContract<AuriOracle>(
|
|
157
|
-
networkAddresses.misc.PYTH_ORACLE,
|
|
158
|
-
abis.AuriOracleABI.abi
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
return this._oracle;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
get plygame() {
|
|
165
|
-
if (!this._plygame) {
|
|
166
|
-
this._plygame = this.getContract<PlyGame>(networkAddresses.misc.PLYGAME, abis.PlyGameABI.abi);
|
|
167
|
-
}
|
|
168
|
-
return this._plygame;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
get plyTokenLock() {
|
|
172
|
-
if (!this._plyTokenLock) {
|
|
173
|
-
this._plyTokenLock = this.getContract<PlyTokenLock>(
|
|
174
|
-
networkAddresses.misc.PLY_TOKEN_LOCK,
|
|
175
|
-
abis.PlyTokenLockABI.abi
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
return this._plyTokenLock;
|
|
179
|
-
}
|
|
180
|
-
}
|
package/src/entities/index.ts
DELETED
package/src/entities/token.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { networkAddresses } from '../consts/constants';
|
|
2
|
-
import { getDecimal } from '../helpers/helpers';
|
|
3
|
-
|
|
4
|
-
export class Token {
|
|
5
|
-
public readonly address: string;
|
|
6
|
-
public readonly decimals: number;
|
|
7
|
-
public constructor(address: string, decimals: number) {
|
|
8
|
-
this.address = address.toLowerCase();
|
|
9
|
-
this.decimals = decimals;
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export const PLYToken = new Token(
|
|
14
|
-
networkAddresses.tokens.PLY,
|
|
15
|
-
getDecimal(networkAddresses.tokens.PLY)
|
|
16
|
-
);
|
|
17
|
-
|
|
18
|
-
export const PLYWNEARToken = new Token(
|
|
19
|
-
networkAddresses.tokens.PLYWNEAR,
|
|
20
|
-
getDecimal(networkAddresses.tokens.PLYWNEAR)
|
|
21
|
-
);
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import BigNumber from 'bignumber.js';
|
|
2
|
-
import { decimalFactor, getDecimal, getSymbol } from '../helpers/helpers';
|
|
3
|
-
import { Address } from '../types';
|
|
4
|
-
import { Token } from './token';
|
|
5
|
-
|
|
6
|
-
export class TokenAmount {
|
|
7
|
-
public readonly token: Token;
|
|
8
|
-
private rawAmnt: string;
|
|
9
|
-
|
|
10
|
-
public constructor(token: Token, amount: string, isRaw: boolean = true) {
|
|
11
|
-
if (isRaw) {
|
|
12
|
-
this.rawAmnt = amount;
|
|
13
|
-
} else {
|
|
14
|
-
this.rawAmnt = new BigNumber(amount).times(decimalFactor(token.decimals)).toFixed(0);
|
|
15
|
-
}
|
|
16
|
-
this.token = token;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
public formattedAmount(decimal?: number, fmt?: BigNumber.Format): string {
|
|
20
|
-
if (decimal !== undefined && fmt !== undefined) {
|
|
21
|
-
return new BigNumber(this.rawAmnt)
|
|
22
|
-
.div(decimalFactor(this.token.decimals))
|
|
23
|
-
.toFormat(decimal, fmt);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
return new BigNumber(this.rawAmnt).div(decimalFactor(this.token.decimals)).toString();
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
public formattedAmountWithSymbol(decimal?: number, fmt?: BigNumber.Format): string {
|
|
30
|
-
return `${this.formattedAmount(decimal, fmt)} ${getSymbol(this.token.address)}`;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
public rawAmount(): string {
|
|
34
|
-
return this.rawAmnt;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
public compare(other: TokenAmount): number {
|
|
38
|
-
return new BigNumber(this.rawAmnt).comparedTo(new BigNumber(other.rawAmnt));
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
public static fromAddressAndAmount(
|
|
42
|
-
tokenAddress: Address,
|
|
43
|
-
amount: string,
|
|
44
|
-
isRaw: boolean = true
|
|
45
|
-
): TokenAmount {
|
|
46
|
-
return new TokenAmount(new Token(tokenAddress, getDecimal(tokenAddress)), amount, isRaw);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
|
|
3
|
-
const AURIGAMI_API_END_POINT = 'https://api.aurigami.finance/app/';
|
|
4
|
-
|
|
5
|
-
export async function getApi(url: string, params: Record<string, any> = {}): Promise<any> {
|
|
6
|
-
const resp = await axios.get(AURIGAMI_API_END_POINT + url + '?' + new URLSearchParams(params), {
|
|
7
|
-
headers: { 'Content-Type': 'application/json' },
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
return resp.data;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export type MetaApiResponse = {
|
|
14
|
-
totalItems: number;
|
|
15
|
-
itemCount: number;
|
|
16
|
-
itemsPerPage: number;
|
|
17
|
-
totalPages: number;
|
|
18
|
-
currentPage: number;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type PaginagedApiResponse<T> = {
|
|
22
|
-
items: T[];
|
|
23
|
-
meta: MetaApiResponse;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export async function getPaginatedApi<T = any>(
|
|
27
|
-
url: string,
|
|
28
|
-
params: Record<string, any> = {},
|
|
29
|
-
page: number = 1,
|
|
30
|
-
pageSize: number = 50
|
|
31
|
-
): Promise<PaginagedApiResponse<T>> {
|
|
32
|
-
params = {
|
|
33
|
-
...params,
|
|
34
|
-
page,
|
|
35
|
-
limit: pageSize,
|
|
36
|
-
};
|
|
37
|
-
const resp = await axios.get(AURIGAMI_API_END_POINT + url + '?' + new URLSearchParams(params), {
|
|
38
|
-
headers: { 'Content-Type': 'application/json' },
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
return resp.data;
|
|
42
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import axios from 'axios';
|
|
2
|
-
|
|
3
|
-
const AURORA_API_BASE_URL = 'https://explorer.mainnet.aurora.dev/api';
|
|
4
|
-
|
|
5
|
-
export async function getQuery(
|
|
6
|
-
module: string,
|
|
7
|
-
action: string,
|
|
8
|
-
params: Record<string, any> = {}
|
|
9
|
-
): Promise<any> {
|
|
10
|
-
params = { ...params, module: module, action: action };
|
|
11
|
-
|
|
12
|
-
let resp = await axios.get(AURORA_API_BASE_URL + '?' + new URLSearchParams(params), {
|
|
13
|
-
headers: { 'Content-Type': 'application/json' },
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
return resp.data.result!;
|
|
17
|
-
}
|
package/src/helpers/helpers.ts
DELETED
|
@@ -1,212 +0,0 @@
|
|
|
1
|
-
import { TypedEvent } from '@aurigami/contracts/typechain/common';
|
|
2
|
-
import { Provider } from '@ethersproject/abstract-provider';
|
|
3
|
-
import BigNumber from 'bignumber.js';
|
|
4
|
-
import { BigNumber as BN, utils } from 'ethers';
|
|
5
|
-
import { Result } from 'ethers/lib/utils';
|
|
6
|
-
import { GRAPHQL_URL, networkAddresses } from '../consts/constants';
|
|
7
|
-
import { decimalRecords } from '../consts/decimals';
|
|
8
|
-
import { symbolRecords } from '../consts/symbols';
|
|
9
|
-
import { Address } from '../types';
|
|
10
|
-
import { getQuery as getAuroraAPIQuery } from './aurora-api-helpers';
|
|
11
|
-
import { queryGraphQL } from './graphql-helpers';
|
|
12
|
-
|
|
13
|
-
export function assert(condition: boolean, message: string = ''): void {
|
|
14
|
-
if (!condition) {
|
|
15
|
-
message = 'Assertion failed' + (message ? ': ' + message : '');
|
|
16
|
-
throw new Error(message);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
export function formatObject(object: Object) {
|
|
21
|
-
return JSON.stringify(object, null, ' ');
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export function decimalFactor(decimal: number) {
|
|
25
|
-
return BN.from(10).pow(decimal).toString();
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export const isSameAddress = (address1: Address, address2: Address): boolean => {
|
|
29
|
-
return address1.toLowerCase() == address2.toLowerCase();
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
export const getCurrentTimestamp = (): number => {
|
|
33
|
-
return Math.trunc(Date.now() / 1000);
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
export async function sleep(duration: number): Promise<void> {
|
|
37
|
-
return new Promise((resolve) => {
|
|
38
|
-
setTimeout(resolve, duration);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function validateAndParseAddress(address: Address): Address {
|
|
43
|
-
try {
|
|
44
|
-
return utils.getAddress(address);
|
|
45
|
-
} catch (error) {
|
|
46
|
-
throw new Error('Invalid address: ' + address);
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function getDecimal(address: Address): number {
|
|
51
|
-
let addressLowercase = address.toLowerCase();
|
|
52
|
-
assert(decimalRecords[addressLowercase] != undefined, 'Decimal not found for ' + address);
|
|
53
|
-
return decimalRecords[addressLowercase];
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export function getSymbol(address: Address): string {
|
|
57
|
-
let addressLowercase = address.toLowerCase();
|
|
58
|
-
assert(symbolRecords[addressLowercase] != undefined, 'Symbol not found for ' + address);
|
|
59
|
-
return symbolRecords[addressLowercase];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function getUnderlying(address: Address): Address {
|
|
63
|
-
let addressLowercase = address.toLowerCase();
|
|
64
|
-
let matching = networkAddresses.auTokens.filter((e) => e.address == addressLowercase);
|
|
65
|
-
if (matching.length == 0) {
|
|
66
|
-
throw new Error('Underlying not found for ' + address);
|
|
67
|
-
}
|
|
68
|
-
return matching[0].underlying;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
export function calcLMRewardApr(
|
|
72
|
-
rewardsValue: BigNumber,
|
|
73
|
-
totalStakeValue: BigNumber,
|
|
74
|
-
frequencyPerYear: number
|
|
75
|
-
): BigNumber {
|
|
76
|
-
if (rewardsValue.isZero()) {
|
|
77
|
-
return new BigNumber(0);
|
|
78
|
-
}
|
|
79
|
-
if (totalStakeValue.lte(0)) {
|
|
80
|
-
// avoid division by zero when there is no stake
|
|
81
|
-
return rewardsValue;
|
|
82
|
-
}
|
|
83
|
-
return rewardsValue.multipliedBy(frequencyPerYear).dividedBy(totalStakeValue);
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export function calcNetApy(
|
|
87
|
-
suppliedValue: BigNumber,
|
|
88
|
-
supplyApy: BigNumber,
|
|
89
|
-
borrowedValue: BigNumber,
|
|
90
|
-
borrowApy: BigNumber
|
|
91
|
-
) {
|
|
92
|
-
return suppliedValue.multipliedBy(supplyApy).minus(borrowedValue.multipliedBy(borrowApy));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
export async function getBlockTimestamp(provider: Provider, blockNumber: number): Promise<number> {
|
|
96
|
-
const block = await provider.getBlock(blockNumber);
|
|
97
|
-
return block.timestamp;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export async function getBlocksTimestampViaRPC(
|
|
101
|
-
provider: Provider,
|
|
102
|
-
blocksNumber: number[]
|
|
103
|
-
): Promise<number[]> {
|
|
104
|
-
// Promise all 200 blocks at a time, using too many blocks at once will cause the RPC call timeout.
|
|
105
|
-
const CHUNK_SIZE = 200;
|
|
106
|
-
let results: number[] = [];
|
|
107
|
-
for (let i = 0; i < blocksNumber.length; i += CHUNK_SIZE) {
|
|
108
|
-
let slice = blocksNumber.slice(i, i + CHUNK_SIZE);
|
|
109
|
-
let promises = slice.map(async (blockNumber) => getBlockTimestamp(provider, blockNumber));
|
|
110
|
-
results = results.concat(await Promise.all(promises));
|
|
111
|
-
}
|
|
112
|
-
return results;
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* Get timestamp of multiple blocks.
|
|
117
|
-
* @note the getBlockTimestamp function is too slow because of the RPC call, even with Promise.all.
|
|
118
|
-
* So I use the GraphQL endpoint of Aurora instead. You can check it here: https://explorer.mainnet.aurora.dev/graphiql
|
|
119
|
-
*/
|
|
120
|
-
export async function getBlocksTimestamp(
|
|
121
|
-
provider: Provider,
|
|
122
|
-
blockNumbers: number[]
|
|
123
|
-
): Promise<number[]> {
|
|
124
|
-
if (blockNumbers.length == 0) return [];
|
|
125
|
-
const CHUNK_SIZE = 100; // graphql query limit
|
|
126
|
-
|
|
127
|
-
let promises = [];
|
|
128
|
-
let lines: string[] = blockNumbers.map((x) => `b${x}: block(number: ${x}) { timestamp }`);
|
|
129
|
-
|
|
130
|
-
for (let i = 0; i < lines.length; i += CHUNK_SIZE) {
|
|
131
|
-
let query = `{ ${lines.slice(i, i + CHUNK_SIZE).join('\n')} }`;
|
|
132
|
-
promises.push(queryGraphQL(query, GRAPHQL_URL));
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// merge all the json objects
|
|
136
|
-
let results = (await Promise.all(promises)).reduce((acc, cur) => {
|
|
137
|
-
return { ...acc, ...cur };
|
|
138
|
-
}, {});
|
|
139
|
-
|
|
140
|
-
let missingBlocks = blockNumbers.filter((blockNumber) => !results[`b${blockNumber}`]);
|
|
141
|
-
if (missingBlocks.length > 0) {
|
|
142
|
-
devLog(`These blocks are missing from Aurora GraphQL endpoint: ${missingBlocks.join(', ')}`);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
let missingBlocksTimestamps = await getBlocksTimestampViaRPC(provider, missingBlocks);
|
|
146
|
-
|
|
147
|
-
missingBlocksTimestamps.forEach((timestamp, index) => {
|
|
148
|
-
results[`b${missingBlocks[index]}`] = { timestamp: new Date(timestamp * 1000).toISOString() };
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
return blockNumbers.map((blockNumber) =>
|
|
152
|
-
Math.trunc(new Date(results[`b${blockNumber}`]!.timestamp).getTime() / 1000)
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/**
|
|
157
|
-
* Get the block number of the last block that has a timestamp before the given timestamp
|
|
158
|
-
* @param timestamp input timestamp
|
|
159
|
-
* @returns return the last block that its timestamp <= the give timestamp
|
|
160
|
-
* @note Check this API for more information: https://aurorascan.dev/apis#blocks
|
|
161
|
-
*
|
|
162
|
-
* This function is much faster than using binary search.
|
|
163
|
-
*/
|
|
164
|
-
export async function getBlockBeforeTimestamp(provider: Provider, timestamp: number) {
|
|
165
|
-
let result = await getAuroraAPIQuery('block', 'getblocknobytime', {
|
|
166
|
-
timestamp: timestamp,
|
|
167
|
-
closest: 'before',
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
// In case the API return `Block timestamp too far in the future`
|
|
171
|
-
// use the last block number
|
|
172
|
-
return parseInt(result.blockNumber) || (await provider.getBlockNumber());
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
export function devLog(message?: any, ...optionalParams: any[]): void {
|
|
176
|
-
if ('production' !== process.env.NODE_ENV) {
|
|
177
|
-
console.log('[DEV LOG]', message, ...optionalParams);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Sorts (in place) an array of Events by blocknumber.
|
|
183
|
-
* This method mutates the array and returns a reference to the same array.
|
|
184
|
-
**/
|
|
185
|
-
export function sortEvents<T extends Result>(events: TypedEvent<T>[]): TypedEvent<T>[] {
|
|
186
|
-
return events.sort((a, b) => {
|
|
187
|
-
if (a.blockNumber == b.blockNumber) {
|
|
188
|
-
return a.logIndex - b.logIndex;
|
|
189
|
-
}
|
|
190
|
-
return a.blockNumber - b.blockNumber;
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
// export function Cache(cacheTimeout: number) {
|
|
195
|
-
// return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
196
|
-
// const originalMethod = descriptor.value;
|
|
197
|
-
// let cache = new Map<string, any>();
|
|
198
|
-
// descriptor.value = function (...args: any[]) {
|
|
199
|
-
// let key = JSON.stringify(args);
|
|
200
|
-
// if (cache.has(key)) {
|
|
201
|
-
// let [timestamp, value] = cache.get(key);
|
|
202
|
-
// if (Date.now() - timestamp < cacheTimeout) {
|
|
203
|
-
// return value;
|
|
204
|
-
// }
|
|
205
|
-
// }
|
|
206
|
-
// let result = originalMethod.apply(this, args);
|
|
207
|
-
// cache.set(key, [Date.now(), result]);
|
|
208
|
-
// return result;
|
|
209
|
-
// };
|
|
210
|
-
// return descriptor;
|
|
211
|
-
// };
|
|
212
|
-
// }
|