Skip to content

Instantly share code, notes, and snippets.

@xtools-at
Created February 10, 2022 14:00
Show Gist options
  • Save xtools-at/4a1e7d4afd1d2460db8b75d7e3df705a to your computer and use it in GitHub Desktop.
Save xtools-at/4a1e7d4afd1d2460db8b75d7e3df705a to your computer and use it in GitHub Desktop.
PolyWars RPC test
const Web3 = require('web3'); // web3@1.7.0
const startBlock = 901990;
const rpcMaxBlocksServer = 1000;
const rpcUrl = 'https://pangolin-rpc.darwinia.network';
const waitBatchRpcCalls = 60; // ms
// contract adresses and abis
const contracts = [
{
// game
address: '0xE6969775d08171e24165aad38859D3bB96954515',
type: 'Battle',
abi: [{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "attacker",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "target",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "winner",
"type": "address"
},
{
"indexed": false,
"internalType": "uint256",
"name": "xpEarned",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "ressLooted",
"type": "uint256"
}
],
"name": "Battle",
"type": "event"
}],
}, {
// market
address: '0x236bdA4589e44e6850f5aC6a74BfCa398a86c6c0',
type: 'ListingStatus',
abi: [{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "listingId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint8",
"name": "status",
"type": "uint8"
},
{
"indexed": true,
"internalType": "address",
"name": "seller",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "price",
"type": "uint256"
},
{
"indexed": true,
"internalType": "address",
"name": "buyer",
"type": "address"
}
],
"name": "ListingStatus",
"type": "event"
}],
}, {
// nft
address: '0xDA7A19b9bF91AD9DccF60DD20B2D4996ac67f847',
type: 'Transfer',
abi: [{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "from",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "to",
"type": "address"
},
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
}
],
"name": "Transfer",
"type": "event"
}],
},
];
const wait = (ms) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve();
}, ms);
});
};
// events
const getEventOptions = (filterObj, fromBlock, toBlock, noToBlock) => {
let toBlockObj = {};
if (!noToBlock) {
toBlockObj = {
toBlock: toBlock != null ? toBlock : 'latest',
};
}
return {
filter: filterObj || {},
fromBlock: fromBlock != null ? fromBlock : startBlock,
...toBlockObj,
};
};
const getEvents = (contract, type, filterObj, fromBlock, toBlock) => {
return contract.getPastEvents(type, getEventOptions(filterObj, fromBlock, toBlock));
};
// web3
const getWeb3 = () => {
return new Web3(new Web3.providers.HttpProvider(rpcUrl), {
keepalive: true,
headers: [{ name: 'Access-Control-Allow-Origin', value: '*' }],
withCredentials: false,
timeout: 30 * 1000, // s * ms, default: 10s
});
};
const web3Http = getWeb3();
const getContract = (contractConfig) => {
let contract;
try {
contract = new web3Http.eth.Contract(contractConfig.abi, contractConfig.address);
} catch (e) {
console.error(e);
}
return contract;
};
// main method
const getInitialEvents = async (contractConfig) => {
const eventType = contractConfig.type;
const currentBlock = await web3Http.eth.getBlockNumber();
console.log(`Starting to fetch ${eventType} events, current block:`, currentBlock);
const initialBlocks = currentBlock - startBlock;
if (initialBlocks > rpcMaxBlocksServer) {
// batch requests
let events = [];
const contract = getContract(contractConfig);
for (let i = 0; i < initialBlocks; i += rpcMaxBlocksServer) {
try {
const fromBlock = i + startBlock;
const toBlock = i + rpcMaxBlocksServer > initialBlocks ? 'latest' : fromBlock + rpcMaxBlocksServer - 1;
const batchEvents = await getEvents(
contract, eventType, null,
fromBlock, toBlock,
).catch((e) => {
console.error(`Error fetching initial ${eventType} events`, e);
return;
});
console.log('finished', i + rpcMaxBlocksServer, 'blocks, from/to block:', fromBlock, toBlock);
if (events == null) return null;
await wait(waitBatchRpcCalls);
events = events.concat(batchEvents);
} catch (e) {
console.error(`Error fetching initial ${eventType} events`, e);
return;
}
}
return events;
}
};
// start lookup
(async function() {
console.log(`start time: ${new Date().toUTCString()}`)
await getInitialEvents(contracts[0]);
console.log(`time after finishing first event class: ${new Date().toUTCString()}`)
await wait(200);
await getInitialEvents(contracts[1]);
console.log(`time after finishing 2nd event class: ${new Date().toUTCString()}`)
await wait(200);
await getInitialEvents(contracts[2]);
console.log(`time after finishing 3rd event class: ${new Date().toUTCString()}`)
console.log('Done');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment