@aurigami/sdk 1.23.1 → 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.
Files changed (123) hide show
  1. package/dist/SDK.d.ts +48 -65
  2. package/dist/SDK.js +165 -0
  3. package/dist/abis/index.d.ts +18 -18
  4. package/dist/abis/index.js +39 -0
  5. package/dist/consts/constants.d.ts +139 -139
  6. package/dist/consts/constants.js +185 -0
  7. package/dist/consts/decimals.d.ts +1 -1
  8. package/dist/consts/decimals.js +35 -0
  9. package/dist/consts/deployments.d.ts +3 -3
  10. package/dist/consts/deployments.js +9 -0
  11. package/dist/consts/dummy.d.ts +13 -13
  12. package/dist/consts/dummy.js +43 -0
  13. package/dist/consts/index.d.ts +5 -5
  14. package/dist/consts/index.js +9 -0
  15. package/dist/consts/symbols.d.ts +1 -1
  16. package/dist/consts/symbols.js +10 -0
  17. package/dist/entities/BlockchainEntity.d.ts +13 -13
  18. package/dist/entities/BlockchainEntity.js +36 -0
  19. package/dist/entities/auriEnv.d.ts +34 -40
  20. package/dist/entities/auriEnv.js +166 -0
  21. package/dist/entities/index.d.ts +4 -4
  22. package/dist/entities/index.js +8 -0
  23. package/dist/entities/token.d.ts +7 -7
  24. package/dist/entities/token.js +27 -0
  25. package/dist/entities/tokenAmount.d.ts +13 -13
  26. package/dist/entities/tokenAmount.js +52 -0
  27. package/dist/helpers/aurigami-api.d.ts +13 -13
  28. package/dist/helpers/aurigami-api.js +26 -0
  29. package/dist/helpers/aurora-api-helpers.d.ts +1 -1
  30. package/dist/helpers/aurora-api-helpers.js +15 -0
  31. package/dist/helpers/graphql-helpers.d.ts +1 -1
  32. package/dist/helpers/graphql-helpers.js +13 -0
  33. package/dist/helpers/helpers.d.ts +23 -40
  34. package/dist/helpers/helpers.js +148 -0
  35. package/dist/helpers/historicalPriceFetcher.d.ts +15 -15
  36. package/dist/helpers/historicalPriceFetcher.js +142 -0
  37. package/dist/helpers/index.d.ts +7 -7
  38. package/dist/helpers/index.js +11 -0
  39. package/dist/helpers/kyber-aggregator-api.d.ts +5 -5
  40. package/dist/helpers/kyber-aggregator-api.js +27 -0
  41. package/dist/helpers/multicall.d.ts +40 -0
  42. package/dist/helpers/multicall.js +203 -0
  43. package/dist/helpers/notification-api.d.ts +2 -2
  44. package/dist/helpers/notification-api.js +21 -0
  45. package/dist/helpers/one-inch-aggregator-api.d.ts +21 -21
  46. package/dist/helpers/one-inch-aggregator-api.js +19 -0
  47. package/dist/helpers/priceFetcher.d.ts +36 -36
  48. package/dist/helpers/priceFetcher.js +292 -0
  49. package/dist/helpers/subgraphQuery.d.ts +1 -1
  50. package/dist/helpers/subgraphQuery.js +22 -0
  51. package/dist/helpers/transfer-event-query.d.ts +17 -26
  52. package/dist/helpers/transfer-event-query.js +41 -0
  53. package/dist/index.d.ts +7 -7
  54. package/dist/index.js +17 -8
  55. package/dist/interactors/Airdrop.d.ts +17 -26
  56. package/dist/interactors/Airdrop.js +48 -0
  57. package/dist/interactors/MoneyMarket.d.ts +63 -63
  58. package/dist/interactors/MoneyMarket.js +392 -0
  59. package/dist/interactors/Multicall.d.ts +15 -18
  60. package/dist/interactors/Multicall.js +36 -0
  61. package/dist/interactors/Papermill.d.ts +27 -57
  62. package/dist/interactors/Papermill.js +125 -0
  63. package/dist/interactors/PlyGame.d.ts +30 -42
  64. package/dist/interactors/PlyGame.js +146 -0
  65. package/dist/interactors/PlyTokenLock.d.ts +16 -16
  66. package/dist/interactors/PlyTokenLock.js +44 -0
  67. package/dist/interactors/Pulp.d.ts +17 -17
  68. package/dist/interactors/Pulp.js +43 -0
  69. package/dist/interactors/Referral.d.ts +35 -78
  70. package/dist/interactors/Referral.js +158 -0
  71. package/dist/interactors/Staking.d.ts +20 -26
  72. package/dist/interactors/Staking.js +106 -0
  73. package/dist/interactors/index.d.ts +9 -9
  74. package/dist/interactors/index.js +13 -0
  75. package/dist/interactors/misc.d.ts +31 -39
  76. package/dist/interactors/misc.js +318 -0
  77. package/dist/types/index.d.ts +175 -197
  78. package/dist/types/index.js +18 -0
  79. package/package.json +4 -16
  80. package/dist/sdk.cjs.development.js +0 -6325
  81. package/dist/sdk.cjs.development.js.map +0 -1
  82. package/dist/sdk.cjs.production.min.js +0 -2
  83. package/dist/sdk.cjs.production.min.js.map +0 -1
  84. package/dist/sdk.esm.js +0 -6193
  85. package/dist/sdk.esm.js.map +0 -1
  86. package/src/SDK.ts +0 -223
  87. package/src/abis/index.ts +0 -38
  88. package/src/consts/constants.ts +0 -210
  89. package/src/consts/decimals.ts +0 -31
  90. package/src/consts/deployments.ts +0 -4
  91. package/src/consts/dummy.ts +0 -51
  92. package/src/consts/index.ts +0 -5
  93. package/src/consts/symbols.ts +0 -9
  94. package/src/entities/BlockchainEntity.ts +0 -29
  95. package/src/entities/auriEnv.ts +0 -178
  96. package/src/entities/index.ts +0 -4
  97. package/src/entities/token.ts +0 -21
  98. package/src/entities/tokenAmount.ts +0 -48
  99. package/src/helpers/aurigami-api.ts +0 -42
  100. package/src/helpers/aurora-api-helpers.ts +0 -17
  101. package/src/helpers/graphql-helpers.ts +0 -8
  102. package/src/helpers/helpers.ts +0 -212
  103. package/src/helpers/historicalPriceFetcher.ts +0 -203
  104. package/src/helpers/index.ts +0 -7
  105. package/src/helpers/kyber-aggregator-api.ts +0 -37
  106. package/src/helpers/notification-api.ts +0 -22
  107. package/src/helpers/one-inch-aggregator-api.ts +0 -45
  108. package/src/helpers/priceFetcher.ts +0 -389
  109. package/src/helpers/subgraphQuery.ts +0 -17
  110. package/src/helpers/transfer-event-query.ts +0 -68
  111. package/src/index.ts +0 -14
  112. package/src/interactors/Airdrop.ts +0 -72
  113. package/src/interactors/MoneyMarket.ts +0 -482
  114. package/src/interactors/Multicall.ts +0 -50
  115. package/src/interactors/Papermill.ts +0 -185
  116. package/src/interactors/PlyGame.ts +0 -172
  117. package/src/interactors/PlyTokenLock.ts +0 -47
  118. package/src/interactors/Pulp.ts +0 -39
  119. package/src/interactors/Referral.ts +0 -214
  120. package/src/interactors/Staking.ts +0 -152
  121. package/src/interactors/index.ts +0 -9
  122. package/src/interactors/misc.ts +0 -431
  123. package/src/types/index.ts +0 -222
@@ -1,178 +0,0 @@
1
- import {
2
- AuErc20,
3
- AuriAirdrop,
4
- AuriFairLaunch,
5
- AuriInternalLens,
6
- AuriLens,
7
- AuriOracle,
8
- Comptroller,
9
- IERC20,
10
- PULP,
11
- PlyGame,
12
- PlyTokenLock,
13
- ReferralDirectoryV2,
14
- } from '@aurigami/contracts/typechain';
15
- import { Contract } from 'ethers';
16
- import * as abis from '../abis';
17
- import { networkAddresses } from '../consts/constants';
18
- import { assert } from '../helpers/helpers';
19
- import { AuTokenWithUnderlying } from '../types';
20
- import { BlockchainEntityRead } from './BlockchainEntity';
21
-
22
- export class AuriEnv extends BlockchainEntityRead {
23
- private _comptroller: Comptroller | null = null;
24
- private _auriFairLaunch: AuriFairLaunch | null = null;
25
- private _auriLens: AuriLens | null = null;
26
- private _ply: IERC20 | null = null;
27
- private _pulp: PULP | null = null;
28
- private _auriInternalLens: AuriInternalLens | null = null;
29
- private _auriAirdrop: AuriAirdrop | null = null;
30
- private _referralDirectoryV2: ReferralDirectoryV2 | null = null;
31
- private _oracle: AuriOracle | null = null;
32
- private _plygame: PlyGame | null = null;
33
- private _plyTokenLock: PlyTokenLock | null = null;
34
-
35
- public getContract<CType extends Contract>(address: string, abi: any) {
36
- return new Contract(address, abi, this._networkConnection.provider) as CType;
37
- }
38
-
39
- public getAuTokenContracts(): AuTokenWithUnderlying[] {
40
- return networkAddresses.auTokens.map((auToken) => {
41
- const contract = this.getContract<AuTokenWithUnderlying>(
42
- auToken.address,
43
- abis.AuTokenABI.abi
44
- );
45
- contract.underlying = auToken.underlying;
46
- return contract;
47
- });
48
- }
49
-
50
- /**
51
- * Get AuErc20 contract by its address or underlying name.
52
- *
53
- * Either address or underlyingName must be provided
54
- *
55
- */
56
- public getAuErc20Contract({
57
- address,
58
- underlyingName,
59
- }: {
60
- address?: string;
61
- underlyingName?: string;
62
- }): AuErc20 {
63
- assert(
64
- address !== undefined || underlyingName !== undefined,
65
- 'Either address or underlyingName must be provided'
66
- );
67
-
68
- if (underlyingName) {
69
- address = networkAddresses.auTokens.find(
70
- (auToken) => auToken.name == 'au' + underlyingName
71
- )?.address;
72
- assert(address !== undefined, `AuToken with underlying ${underlyingName} not found`);
73
- }
74
-
75
- return this.getContract<AuErc20>(address!, abis.AuErc20ABI.abi);
76
- }
77
-
78
- get comptroller(): Comptroller {
79
- if (!this._comptroller) {
80
- this._comptroller = this.getContract<Comptroller>(
81
- networkAddresses.misc.COMPTROLLER,
82
- abis.ComptrollerABI.abi
83
- );
84
- }
85
- return this._comptroller;
86
- }
87
-
88
- get auriFairLaunch(): AuriFairLaunch {
89
- if (!this._auriFairLaunch) {
90
- this._auriFairLaunch = this.getContract<AuriFairLaunch>(
91
- networkAddresses.misc.AURIFAIRLAUNCH,
92
- abis.AuriFairLaunchABI.abi
93
- );
94
- }
95
- return this._auriFairLaunch;
96
- }
97
-
98
- get auriLens(): AuriLens {
99
- if (!this._auriLens) {
100
- this._auriLens = this.getContract<AuriLens>(
101
- networkAddresses.misc.AURILENS,
102
- abis.AuriLensABI.abi
103
- );
104
- }
105
- return this._auriLens;
106
- }
107
-
108
- get ply(): IERC20 {
109
- if (!this._ply) {
110
- this._ply = this.getContract<IERC20>(networkAddresses.tokens.PLY, abis.EIP20InterfaceABI.abi);
111
- }
112
- return this._ply;
113
- }
114
-
115
- get pulp(): PULP {
116
- if (!this._pulp) {
117
- this._pulp = this.getContract<PULP>(networkAddresses.tokens.PULP, abis.PulpABI.abi);
118
- }
119
- return this._pulp;
120
- }
121
-
122
- get auriInternalLens(): AuriInternalLens {
123
- if (!this._auriInternalLens) {
124
- this._auriInternalLens = this.getContract<AuriInternalLens>(
125
- networkAddresses.misc.AURI_INTERNAL_LENS,
126
- abis.AuriInternalLensABI.abi
127
- );
128
- }
129
- return this._auriInternalLens;
130
- }
131
-
132
- get auriAirdrop(): AuriAirdrop {
133
- if (!this._auriAirdrop) {
134
- this._auriAirdrop = this.getContract<AuriAirdrop>(
135
- networkAddresses.misc.AURI_AIRDROP,
136
- abis.AuriAirdropABI.abi
137
- );
138
- }
139
- return this._auriAirdrop;
140
- }
141
-
142
- get referralDirectoryV2(): ReferralDirectoryV2 {
143
- if (!this._referralDirectoryV2) {
144
- this._referralDirectoryV2 = this.getContract<ReferralDirectoryV2>(
145
- networkAddresses.misc.REFERRAL_V2,
146
- abis.ReferralDirectoryV2ABI.abi
147
- );
148
- }
149
- return this._referralDirectoryV2;
150
- }
151
-
152
- get oracle(): AuriOracle {
153
- if (!this._oracle) {
154
- this._oracle = this.getContract<AuriOracle>(
155
- networkAddresses.misc.PYTH_ORACLE,
156
- abis.AuriOracleABI.abi
157
- );
158
- }
159
- return this._oracle;
160
- }
161
-
162
- get plygame(): PlyGame {
163
- if (!this._plygame) {
164
- this._plygame = this.getContract<PlyGame>(networkAddresses.misc.PLYGAME, abis.PlyGameABI.abi);
165
- }
166
- return this._plygame;
167
- }
168
-
169
- get plyTokenLock(): PlyTokenLock {
170
- if (!this._plyTokenLock) {
171
- this._plyTokenLock = this.getContract<PlyTokenLock>(
172
- networkAddresses.misc.PLY_TOKEN_LOCK,
173
- abis.PlyTokenLockABI.abi
174
- );
175
- }
176
- return this._plyTokenLock;
177
- }
178
- }
@@ -1,4 +0,0 @@
1
- export * from './auriEnv';
2
- export * from './BlockchainEntity';
3
- export * from './token';
4
- export * from './tokenAmount';
@@ -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
- }
@@ -1,8 +0,0 @@
1
- import axios from 'axios';
2
-
3
- export async function queryGraphQL(query: string, graphqlUrl: string): Promise<any> {
4
- let resp = await axios.post(graphqlUrl, {
5
- query: query,
6
- });
7
- return resp.data.data!;
8
- }
@@ -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
- // }