-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathweb3.ts
More file actions
177 lines (149 loc) · 5.42 KB
/
web3.ts
File metadata and controls
177 lines (149 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import md5 from 'js-md5';
// import _Web3 from 'web3';
// import HDWalletProvider from '@truffle/hdwallet-provider';
import CryptoJS from 'crypto-js';
// import ethersAddress from '@ethersproject/address';
// import { Contract } from '@ethersproject/contracts';
// import { Web3Provider } from '@ethersproject/providers';
// import { ethers } from 'ethers';
import { log, logError } from './log';
// const fetchPrice = async (id, vs = 'usd') => {
// const response = await fetch(`https://api.coingecko.com/api/v3/simple/price?ids=${id}&vs_currencies=${vs}`)
// return parseFloat((await response.json())[id][vs])
// }
// const fetchPrices = async () => {
// const response = await fetch('https://api.coingecko.com/api/v3/coins/list')
// prices = (await response.json())
// }
// export const Web3 = _Web3;
export const getRandomProvider = (HDWalletProvider, secret) => {
if (!process.env.PROVIDER_URI) console.error('env PROVIDER.URI not set');
return new HDWalletProvider(
secret.mnemonic,
process.env.PROVIDER_URI //networks[Math.floor(Math.random() * networks.length)]
);
};
export async function iterateBlocks(app, name, address, fromBlock, toBlock, event, processLog, updateConfig) {
if (!toBlock) return;
if (fromBlock === toBlock) return;
// event List(address indexed seller, address indexed buyer, uint256 tokenId, uint256 price);
// event Update(address indexed seller, address indexed buyer, uint256 tokenId, uint256 price);
// event Delist(address indexed seller, uint256 tokenId);
// event Buy(address indexed seller, address indexed buyer, uint256 tokenId, uint256 price);
// event Recover(address indexed user, address indexed seller, uint256 tokenId);
try {
const toBlock2 = fromBlock + 5000 < toBlock ? fromBlock + 5000 : toBlock;
const filter = {
address,
fromBlock,
toBlock: toBlock2,
topics: event.topics,
};
log(name, 'Iterating block', fromBlock, 'to', toBlock2, 'eventually', toBlock, 'for', event.topics);
const logs = await app.ethersProvider.bsc.getLogs(filter);
for (let i = 0; i < logs.length; i++) {
await processLog(logs[i], false);
}
// await wait(3 * 1000)
if (updateConfig && toBlock2 > 0) {
await updateConfig(toBlock2);
}
await iterateBlocks(app, name, address, toBlock2, toBlock, event, processLog, updateConfig);
} catch (e) {
logError('Iterate Blocks Error', e);
logError(name, address, fromBlock, toBlock, event);
// process.exit(1)
}
}
export const getAddress = (address) => {
const mainNetChainId = 56;
const chainId =
// @ts-ignore
typeof window !== 'undefined' && window?.location?.hostname === 'testnet.arken.gg'
? 97
: process.env.REACT_APP_CHAIN_ID;
return address[chainId] ? address[chainId] : address[mainNetChainId];
};
export function isValidRequest(web3, req) {
log('Verifying', req);
try {
// const hashedData = md5(JSON.stringify(req.signature.data));
return (
// hashedData === req.signature.hash &&
web3.eth.accounts.recover(req.signature.data, req.signature.hash).toLowerCase() ===
req.signature.address.toLowerCase()
);
} catch (e) {
logError(e);
return false;
}
}
export async function getSignedRequest(web3, secret, data) {
log('Signing', data);
try {
//const hashedData = md5(JSON.stringify(data));
return {
address: secret.address,
hash: (await web3.eth.accounts.sign(data, secret.key)).signature,
data,
};
} catch (e) {
logError(e);
return null;
}
}
// Get the topic signature hex
export function getTopicSignatureHex(ethers, topicSignature) {
return ethers.utils.id(topicSignature);
}
// Returns the checksummed address if the address is valid, otherwise returns false
export function isAddress(ethers, value) {
try {
return ethers.utils.getAddress(value);
} catch {
return false;
}
}
// Shorten the checksummed version of the input address to have 0x + 4 characters at start and end
export function shortenAddress(ethers, address, chars = 4) {
const parsed = isAddress(ethers, address);
if (!parsed) {
throw Error(`Invalid 'address' parameter '${address}'.`);
}
return `${parsed.substring(0, chars + 2)}...${parsed.substring(42 - chars)}`;
}
// account is optional
export function getContract(ethers, address, ABI, library, account) {
// pass in ethers
if (!isAddress(ethers, address) || address === ethers.constants.AddressZero) {
throw Error(`Invalid 'address' parameter '${address}'.`);
}
return new ethers.Contract(address, ABI, getProviderOrSigner(library, account));
}
// account is not optional
export function getSigner(library, account) {
return library.getSigner(account);
}
// account is optional
export function getProviderOrSigner(library, account) {
return account ? getSigner(library, account) : library;
}
export function encrypt(data, key) {
return CryptoJS.AES.encrypt(data, key).toString();
}
export function decrypt(data, key) {
return CryptoJS.AES.decrypt(data, key).toString(CryptoJS.enc.Utf8);
}
export function toBytes32(ethers, text) {
let data = ethers.utils.toUtf8Bytes(text);
if (data.length > 32) {
throw new Error('too long');
}
data = ethers.utils.zeroPad(data, 32);
return ethers.utils.hexlify(data);
}
export default function getLibrary(ethers, provider) {
const library = new ethers.providers.Web3Provider(provider); // pass in ethers.providers.Web3Provider
library.pollingInterval = 15000;
return library;
}