@aurigami/sdk 1.4.1 → 1.4.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.4.1",
2
+ "version": "1.4.2",
3
3
  "license": "MIT",
4
4
  "main": "dist/index.js",
5
5
  "typings": "dist/index.d.ts",
@@ -6,7 +6,7 @@ import { BigNumber as BN, Contract, utils } from "ethers";
6
6
  import { TransferEventQuery } from "./transfer-event-query";
7
7
  import { AIRDROP_START_TIMESTAMP, AIRDROP_KEEP_THRESHOLD, AIRDROP_END_TIMESTAMP, networkAddresses } from "./constants";
8
8
  import AuriAirdropABI from "@aurigami/contracts/artifacts/contracts/AuriAirdrop.sol/AuriAirdrop.json"
9
- import { getBlockBeforeTimestamp, getCurrentTimestamp, getDecimal } from "./helpers";
9
+ import { getBlockBeforeTimestamp, getCurrentTimestamp, getDecimal, isSameAddress } from "./helpers";
10
10
  import AIRDROP_AUTOKEN_INFO from "./airdrop_misc/auTokensInfo.json"
11
11
  import AIRDROP_WHITELIST_ADDRESSES from "./airdrop_misc/whitelist.json"
12
12
  import { AuriAirdrop } from "@aurigami/contracts/typechain";
@@ -44,13 +44,11 @@ class AirdropSettings {
44
44
  settings.airdropStartBlock = await getBlockBeforeTimestamp(
45
45
  provider,
46
46
  settings.airdropStartTimestamp,
47
- 63317969, //block number at the time I was writing this line, it's sure to be before the start of the airdrop
48
47
  );
49
48
 
50
49
  settings.airdropEndBlock = await getBlockBeforeTimestamp(
51
50
  provider,
52
51
  settings.airdropEndTimestamp,
53
- settings.airdropStartBlock,
54
52
  );
55
53
 
56
54
  this._instance = settings;
@@ -129,10 +127,10 @@ class AirdropAuTokenKeepTracker extends BlockchainEntityRead {
129
127
  );
130
128
 
131
129
  for (let { from, to, amount, blockNumber } of events) {
132
- if (from == address) {
130
+ if (isSameAddress(from, address)) {
133
131
  balance = balance.sub(amount);
134
132
  }
135
- if (to == address) {
133
+ if (isSameAddress(to, address)) {
136
134
  balance = balance.add(amount);
137
135
  }
138
136
 
@@ -0,0 +1,15 @@
1
+ import fetch from 'node-fetch';
2
+
3
+ const AURORA_API_BASE_URL = 'https://api.aurorascan.dev/api';
4
+
5
+ export async function getQuery(
6
+ module: string, action: string, params: Record<string, any> = {},
7
+ ): Promise<any> {
8
+ params = {...params, 'module': module, 'action': action};
9
+
10
+ let resp = await fetch(AURORA_API_BASE_URL + '?' + new URLSearchParams(params), {
11
+ headers: { 'Content-Type': 'application/json' },
12
+ method: 'GET'
13
+ });
14
+ return (await resp.json()).result!;
15
+ }
package/src/helpers.ts CHANGED
@@ -3,6 +3,7 @@ import { EIP20Interface, IERC20 } from "@aurigami/contracts/typechain";
3
3
  import { Provider } from "@ethersproject/abstract-provider";
4
4
  import BigNumber from "bignumber.js";
5
5
  import { BigNumber as BN, Contract, providers, utils } from "ethers";
6
+ import { getQuery as getAuroraAPIQuery } from "./aurora-api-helpers";
6
7
  import { decimalRecords } from "./decimals";
7
8
  import { Address, NetworkConnection } from "./types";
8
9
 
@@ -34,18 +35,6 @@ export function getDecimal(address: Address): number {
34
35
  return decimalRecords[address.toLowerCase()];
35
36
  }
36
37
 
37
- /**
38
- * Get the decimals of a token via EIP20Interface
39
- */
40
- export async function getDecimalsViaContract(address: Address, provider: Provider): Promise<number> {
41
- let result = getDecimal(address);
42
- if (result) {
43
- return result;
44
- }
45
- let token: EIP20Interface = new Contract(address, EIP20InterfaceABI.abi, provider) as EIP20Interface;
46
- return token.decimals();
47
- }
48
-
49
38
  export function calcLMRewardApr(rewardsValue: BigNumber, totalStakeValue: BigNumber, frequencyPerYear: number): BigNumber {
50
39
  if (totalStakeValue.lte(0)) { // avoid division by zero when there is no stake
51
40
  return new BigNumber('999999999');
@@ -60,47 +49,20 @@ export async function getBlockTimestamp(provider: Provider, blockNumber: number)
60
49
 
61
50
  /**
62
51
  * Get the block number of the last block that has a timestamp before the given timestamp
52
+ * @param timestamp input timestamp
53
+ * @returns return the last block that its timestamp <= the give timestamp
54
+ * @note Check this API for more information: https://aurorascan.dev/apis#blocks
63
55
  *
64
- * Note: For optimized reason, this function is not guaranteed to return the correct block number,
65
- * it may differ from the actual block number at most 500 blocks.
56
+ * This function is much faster than using binary search.
66
57
  */
67
- export async function getBlockBeforeTimestamp(provider: Provider, timestamp: number, startBlock?: number): Promise<number> {
68
- // stop searching if the right - left < blockThreshold
69
- const blockThreshold = 50 * 10; //50 is the average block in 1 minutes.
70
- // Using K-search + promise all to speed up the search
71
- const K = 8;
72
-
73
- let leftBlock: number = (startBlock || 1);
74
- let rightBlock = await provider.getBlockNumber();
75
-
76
- if ((await getBlockTimestamp(provider, rightBlock)) < timestamp) {
77
- return rightBlock;
78
- }
79
-
80
- while (rightBlock - leftBlock >= blockThreshold) {
81
- let size = Math.floor((rightBlock - leftBlock + 1) / K);
82
- let promises = [];
83
-
84
- for (let i = 1; i < K; i++) {
85
- promises.push(getBlockTimestamp(provider, leftBlock + i * size));
86
- }
87
-
88
- let blockTimestamps = await Promise.all(promises);
89
- let nextLeftBlock = leftBlock;
90
-
91
- for (let i = 1; i < K; i++) {
92
- if (blockTimestamps[i - 1] <= timestamp) {
93
- nextLeftBlock = leftBlock + i * size;
94
- }
95
- }
96
-
97
- for (let i = K - 1; i >= 1; i--) {
98
- if (blockTimestamps[i - 1] > timestamp) {
99
- rightBlock = leftBlock + i * size - 1;
100
- }
101
- }
102
-
103
- leftBlock = nextLeftBlock;
104
- }
105
- return leftBlock;
58
+ export async function getBlockBeforeTimestamp(provider: Provider, timestamp: number) {
59
+ let result = await getAuroraAPIQuery(
60
+ 'block',
61
+ 'getblocknobytime',
62
+ {'timestamp': timestamp, 'closest': 'before'}
63
+ );
64
+
65
+ // In case the API return `Block timestamp too far in the future`
66
+ // use the last block number
67
+ return parseInt(result) || await provider.getBlockNumber();
106
68
  }