@aurigami/sdk 1.4.0 → 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.0",
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/constants.ts CHANGED
@@ -80,6 +80,7 @@ export const networkAddresses: NetworkAddresses = {
80
80
  USDC: '0xb12bfca5a55806aaf64e99521918a4bf0fc40802'.toLowerCase(),
81
81
  WNEAR: '0xC42C30aC6Cc15faC9bD938618BcaA1a1FaE8501d'.toLowerCase(),
82
82
  TRI: '0xFa94348467f64D5A457F75F8bc40495D33c65aBB'.toLowerCase(),
83
+ META: '0xc21ff01229e982d7c8b8691163b0a3cb8f357453'.toLocaleLowerCase(),
83
84
  AURPLY: dummyAddress.toLowerCase() // This is dummy
84
85
  }
85
86
  }
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
  }
@@ -17,7 +17,7 @@ export async function fetchPriceFromCoingeckoAPI(id: string): Promise<BigNumber>
17
17
  ).then((res: any) => {
18
18
  return res.data
19
19
  })
20
-
20
+
21
21
  return new BigNumber(price[id].usd)
22
22
  }
23
23
 
@@ -28,7 +28,10 @@ export async function fetchPriceFromCoingecko(id: Address): Promise<BigNumber> {
28
28
 
29
29
  case networkAddresses.tokens.TRI:
30
30
  return fetchPriceFromCoingeckoAPI('trisolaris');
31
-
31
+
32
+ case networkAddresses.tokens.META:
33
+ return fetchPriceFromCoingeckoAPI('meta-pool');
34
+
32
35
  default:
33
36
  throw Error(`Unknown token ${id} in fetchPriceFromCoingecko`);
34
37
  }
@@ -110,12 +113,13 @@ export async function fetchStNEARPrice(): Promise<BigNumber> {
110
113
  function shouldFetchFromCoingecko(address: string) {
111
114
  address = address.toLowerCase();
112
115
  return address == networkAddresses.tokens.AURORA ||
113
- address == networkAddresses.tokens.TRI;
116
+ address == networkAddresses.tokens.TRI ||
117
+ address == networkAddresses.tokens.META;
114
118
  }
115
119
 
116
120
  export async function fetchPrice(address: Address, provider: providers.Provider): Promise<BigNumber> {
117
121
  if (hardcodePLYPrice && isSameAddress(address, networkAddresses.tokens.PLY)) return new BigNumber(0);
118
-
122
+
119
123
  // if (isSameAddress(address, networkAddresses.tokens.stNEAR)) return fetchStNEARPrice();
120
124
  if (shouldFetchFromCoingecko(address)) return fetchPriceFromCoingecko(address);
121
125
 
@@ -124,7 +128,7 @@ export async function fetchPrice(address: Address, provider: providers.Provider)
124
128
  });
125
129
  if (matchingAuToken !== undefined) {
126
130
  return fetchPriceFromOracle(matchingAuToken.address, getDecimal(address), provider);
127
- } else if (isSameAddress(address, networkAddresses.tokens.AUPLYLP)) {
131
+ } else if (isSameAddress(address, networkAddresses.tokens.AURPLY)) {
128
132
  return fetchLPPrice(address, provider);
129
133
  } else {
130
134
  return fetchPriceFromCoingecko(address);
@@ -138,4 +142,4 @@ export async function fetchValuation(tokenAmount: TokenAmount, provider: provide
138
142
 
139
143
  export function calcValuation(tokenAmount: TokenAmount, price: BigNumber) {
140
144
  return new BigNumber(tokenAmount.formattedAmount()).multipliedBy(price);
141
- }
145
+ }