@aurigami/sdk 1.7.0 → 1.7.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.
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.7.0",
2
+ "version": "1.7.2",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -70,4 +70,4 @@
70
70
  "jest": {
71
71
  "testURL": "https://app.aurigami.finance"
72
72
  }
73
- }
73
+ }
@@ -1,6 +1,7 @@
1
1
  import { ReferralDirectory } from '@aurigami/contracts/typechain';
2
2
  import { TransactionResponse } from '@ethersproject/abstract-provider';
3
3
  import { Contract, ethers } from 'ethers';
4
+ import { getBlocksTimestamp } from '..';
4
5
  import * as abis from '../abis';
5
6
  import * as consts from '../consts';
6
7
  import { BlockchainEntity, BlockchainEntityRead } from '../entities';
@@ -62,10 +63,11 @@ export class ReferralRead extends BlockchainEntityRead {
62
63
  */
63
64
  public async getReferralsList(userAddr: Address): Promise<{ user: string; timestamp: number }[]> {
64
65
  const result = await AuriAPI.getPaginatedApi('/referral/list', { address: userAddr });
65
-
66
- const items = result.items.map((item) => ({
66
+ const blocks = result.items.map((item) => item.blockNumber!);
67
+ const blocksTimestamp = await getBlocksTimestamp(this._networkConnection.provider, blocks);
68
+ const items = result.items.map((item, i) => ({
67
69
  user: item.user!,
68
- timestamp: item.blockNumber!, // @TODO: change this to timestamp
70
+ timestamp: blocksTimestamp[i],
69
71
  }));
70
72
  return items;
71
73
  }
@@ -0,0 +1,10 @@
1
+ import axios from 'axios';
2
+
3
+ const GRAPHQL_URL = 'https://explorer.mainnet.aurora.dev/graphiql';
4
+
5
+ export async function queryGraphQL(query: string): Promise<any> {
6
+ let resp = await axios.post(GRAPHQL_URL, {
7
+ query: query,
8
+ });
9
+ return resp.data.data!;
10
+ }
@@ -5,6 +5,7 @@ import { decimalRecords } from '../consts/decimals';
5
5
  import { symbolRecords } from '../consts/symbols';
6
6
  import { Address } from '../types';
7
7
  import { getQuery as getAuroraAPIQuery } from './aurora-api-helpers';
8
+ import { queryGraphQL } from './graphql-helpers';
8
9
 
9
10
  export function assert(condition: boolean, message: string = ''): void {
10
11
  if (!condition) {
@@ -69,6 +70,61 @@ export async function getBlockTimestamp(provider: Provider, blockNumber: number)
69
70
  return block.timestamp;
70
71
  }
71
72
 
73
+ export async function getBlocksTimestampViaRPC(
74
+ provider: Provider,
75
+ blocksNumber: number[]
76
+ ): Promise<number[]> {
77
+ // Promise all 200 blocks at a time, using too many blocks at once will cause the RPC call timeout.
78
+ const CHUNK_SIZE = 200;
79
+ let results: number[] = [];
80
+ for (let i = 0; i < blocksNumber.length; i += CHUNK_SIZE) {
81
+ let slice = blocksNumber.slice(i, i + CHUNK_SIZE);
82
+ let promises = slice.map(async (blockNumber) => getBlockTimestamp(provider, blockNumber));
83
+ results = results.concat(await Promise.all(promises));
84
+ }
85
+ return results;
86
+ }
87
+
88
+ /**
89
+ * Get timestamp of multiple blocks.
90
+ * @note the getBlockTimestamp function is too slow because of the RPC call, even with Promise.all.
91
+ * So I use the GraphQL endpoint of Aurora instead. You can check it here: https://explorer.mainnet.aurora.dev/graphiql
92
+ */
93
+ export async function getBlocksTimestamp(
94
+ provider: Provider,
95
+ blockNumbers: number[]
96
+ ): Promise<number[]> {
97
+ const CHUNK_SIZE = 100; // graphql query limit
98
+
99
+ let promises = [];
100
+ let lines: string[] = blockNumbers.map((x) => `b${x}: block(number: ${x}) { timestamp }`);
101
+
102
+ for (let i = 0; i < lines.length; i += CHUNK_SIZE) {
103
+ let query = `{ ${lines.slice(i, i + CHUNK_SIZE).join('\n')} }`;
104
+ promises.push(queryGraphQL(query));
105
+ }
106
+
107
+ // merge all the json objects
108
+ let results = (await Promise.all(promises)).reduce((acc, cur) => {
109
+ return { ...acc, ...cur };
110
+ });
111
+
112
+ let missingBlocks = blockNumbers.filter((blockNumber) => !results[`b${blockNumber}`]);
113
+ if (missingBlocks.length > 0) {
114
+ devLog(`These blocks are missing from Aurora GraphQL endpoint: ${missingBlocks.join(', ')}`);
115
+ }
116
+
117
+ let missingBlocksTimestamps = await getBlocksTimestampViaRPC(provider, missingBlocks);
118
+
119
+ missingBlocksTimestamps.forEach((timestamp, index) => {
120
+ results[`b${missingBlocks[index]}`] = { timestamp: new Date(timestamp * 1000).toISOString() };
121
+ });
122
+
123
+ return blockNumbers.map((blockNumber) =>
124
+ Math.trunc(new Date(results[`b${blockNumber}`]!.timestamp).getTime() / 1000)
125
+ );
126
+ }
127
+
72
128
  /**
73
129
  * Get the block number of the last block that has a timestamp before the given timestamp
74
130
  * @param timestamp input timestamp
@@ -1,5 +1,6 @@
1
1
  export * from './aurigami-api';
2
2
  export * from './aurora-api-helpers';
3
+ export * from './graphql-helpers';
3
4
  export * from './helpers';
4
5
  export * from './priceFetcher';
5
6
  export * from './transfer-event-query';