@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/dist/helpers/graphql-helpers.d.ts +1 -0
- package/dist/helpers/helpers.d.ts +7 -0
- package/dist/helpers/index.d.ts +1 -0
- package/dist/sdk.cjs.development.js +209 -27
- 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 +207 -28
- package/dist/sdk.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/contracts/Referral.ts +5 -3
- package/src/helpers/graphql-helpers.ts +10 -0
- package/src/helpers/helpers.ts +56 -0
- package/src/helpers/index.ts +1 -0
package/package.json
CHANGED
|
@@ -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
|
|
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:
|
|
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
|
+
}
|
package/src/helpers/helpers.ts
CHANGED
|
@@ -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
|