Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/script #6

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,53 @@ CHILD_TEST_RPC=
# An Etherscan API key is needed for verifying contracts on Ethereum (testnets/mainnet),
# while a Polygonscan API key is needed for Polygon's chains.
ETHERSCAN_API_KEY=
POLYGONSCAN_API_KEY=
POLYGONSCAN_API_KEY=

# Script Environment Variables
# These are used in the scripts to interact with the contracts

# Decode:
# The address of the contract to decode
DECODE_CONTRACT_ADDRESS=
# The transaction hash of the function to decode
DECODE_TRANSACTION_HASH=
# The name of the function to decode
DECODE_FUNCTION_NAME=

# GetBlockData:
# The block number to get data from
GET_BLOCK_DATA_BLOCK_NUMBER=

# InRangeEvents:
# The address of the contract to get events from
IN_RANGE_EVENTS_CONTRACT_ADDRESS=
# The name of the contract to get events from
IN_RANGE_EVENTS_CONTRACT_NAME=
# The event name and parameters to get
IN_RANGE_EVENTS_EVENT=
# The address of the validator to get events for
IN_RANGE_EVENTS_VALIDATOR_ADDRESS=
# The start block to get events from
IN_RANGE_EVENTS_START_BLOCK=
# The end block to get events from (Should be <= 1000 + start block)
IN_RANGE_EVENTS_END_BLOCK=

# SumBlocksForAll:
# The number of blocks to check behind from the given block number
SUM_BLOCKS_FOR_ALL_CHECKED_BLOCKS=
# The address of the contract to get events from (RewardPool address)
SUM_BLOCKS_FOR_ALL_CONTRACT_ADDRESS=
# The name of the function to get events from (distributeRewardsFor)
SUM_BLOCKS_FOR_ALL_FUNCTION_NAME=

# SumBlocksForOne:
# The number to start checking blocks from
SUM_BLOCKS_FOR_ONE_BLOCK_NUMBER=
# The number of block to check to from the start block
SUM_BLOCKS_FOR_ONE_MAX_BLOCK_NUMBER=
# The address of the validator to get signed blocks for
SUM_BLOCKS_FOR_ONE_VALIDATOR_ADDRESS=
# The address of the contract to get events from (RewardPool address)
SUM_BLOCKS_FOR_ONE_CONTRACT_ADDRESS=
# The name of the function to get events from (distributeRewardsFor)
SUM_BLOCKS_FOR_ONE_FUNCTION_NAME=
2 changes: 1 addition & 1 deletion docs/RewardPool/RewardPool.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ Keeps the delegation pools
function delegationPositions(address, address) external view returns (uint256 duration, uint256 start, uint256 end, uint256 base, uint256 vestBonus, uint256 rsiBonus)
```

The vesting positions for every delegator.
The vesting positions for every delegator



Expand Down
24 changes: 24 additions & 0 deletions scripts/Decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Run: npx hardhat run scripts/Decode.ts --network childTest
import { decodeTransaction } from "./_helper";

// Input parameters for the script:
const CONTRACT_ADDRESS = process.env.DECODE_CONTRACT_ADDRESS;
const TRANSACTION_HASH = process.env.DECODE_TRANSACTION_HASH;
const FUNCTION_NAME = process.env.DECODE_FUNCTION_NAME;

async function decodeTransactionInput() {
if (!CONTRACT_ADDRESS || !TRANSACTION_HASH || !FUNCTION_NAME) {
console.error("Environment variables are not set.");
process.exitCode = 1;
return;
}
const decodedData = await decodeTransaction(CONTRACT_ADDRESS, TRANSACTION_HASH, FUNCTION_NAME);
console.log("___________Decoded Input Data___________");
console.log(decodedData);
}

// Run the script
decodeTransactionInput().catch((error) => {
console.error(error);
process.exitCode = 1;
});
27 changes: 27 additions & 0 deletions scripts/GetBlockData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Run: npx hardhat run scripts/GetBlockData.ts --network childTest
Vitomir2 marked this conversation as resolved.
Show resolved Hide resolved
import { getTransactionsByBlock } from "./_helper";

// Input parameters for the script:
const BLOCK_NUMBER = process.env.GET_BLOCK_DATA_BLOCK_NUMBER;

async function getBlockData() {
if (!BLOCK_NUMBER) {
console.error("The GET_BLOCK_DATA_BLOCK_NUMBER environment variable is not set.");
process.exitCode = 1;
}
const block = await getTransactionsByBlock(Number(BLOCK_NUMBER));
console.log(`
__________Block details__________

Date of the block:
${new Date(block.timestamp * 1000)}
_________________________________
`);
console.log(block);
}

// Run the script
getBlockData().catch((error) => {
console.error(error);
process.exitCode = 1;
});
21 changes: 21 additions & 0 deletions scripts/GetLastBlock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Run: npx hardhat run scripts/GetLastBlock.ts --network childTest
import { getCurrentBlock } from "./_helper";

async function getCurrentTimestamp() {
const block = await getCurrentBlock();
const blockNumber = block.number;
const currentTimestamp = block.timestamp;
console.log(`
_________________________________________

Block Number: ${blockNumber}
TimeStamp of the block: ${currentTimestamp}
_________________________________________
`);
}

// Run the script
getCurrentTimestamp().catch((error) => {
console.error(error);
process.exitCode = 1;
});
49 changes: 49 additions & 0 deletions scripts/InRangeEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Run: npx hardhat run scripts/InRangeEvents.ts --network childTest
import { getEventsByFilters } from "./_helper";

// Input parameters for the script:
const CONTRACT_ADDRESS = process.env.IN_RANGE_EVENTS_CONTRACT_ADDRESS;
const CONTRACT_NAME = process.env.IN_RANGE_EVENTS_CONTRACT_NAME;
const EVENT = process.env.IN_RANGE_EVENTS_EVENT;
const VALIDATOR_ADDRESS = process.env.IN_RANGE_EVENTS_VALIDATOR_ADDRESS;
const YOUR_START_BLOCK = process.env.IN_RANGE_EVENTS_START_BLOCK;
const YOUR_END_BLOCK = process.env.IN_RANGE_EVENTS_END_BLOCK;

async function getEventsByIndexedAddress(
contractAddress: string,
contractName: string,
event: string,
indexedAddress: string | null | undefined,
startBlock: number,
endBlock: number
) {
const events = await getEventsByFilters(contractAddress, contractName, event, indexedAddress, startBlock, endBlock);
console.log(
`
_________________________________

Events for ${event}
indexing ${indexedAddress}
from ${startBlock} to ${endBlock} block
_________________________________`
);
console.log(events);
}

// Run the script
if (CONTRACT_ADDRESS && CONTRACT_NAME && EVENT && YOUR_START_BLOCK && YOUR_END_BLOCK) {
getEventsByIndexedAddress(
CONTRACT_ADDRESS,
CONTRACT_NAME,
EVENT,
VALIDATOR_ADDRESS,
Number(YOUR_START_BLOCK),
Number(YOUR_END_BLOCK)
).catch((error) => {
console.error(error);
process.exitCode = 1;
});
} else {
console.error("Environment variables are not set.");
process.exitCode = 1;
}
78 changes: 78 additions & 0 deletions scripts/SumBlocksForAll.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Run: npx hardhat run scripts/SumBlocksForAll.ts --network childTest
import { getTransactionsByBlock, decodeTransaction, getCurrentBlock } from "./_helper";

// Input parameters for the script:
const CHECKED_BLOCKS = process.env.SUM_BLOCKS_FOR_ALL_CHECKED_BLOCKS;
const CONTRACT_ADDRESS = process.env.SUM_BLOCKS_FOR_ALL_CONTRACT_ADDRESS;
const FUNCTION_NAME = process.env.SUM_BLOCKS_FOR_ALL_FUNCTION_NAME;

// Get the last block that gave uptime
async function getLastBlockNumberReward() {
const currentBlock = await getCurrentBlock();
const currentBlockNumber = currentBlock.number;
const roundedValue = Math.floor(currentBlockNumber / 500) * 500;
return roundedValue;
}

// Get the signed blocks for validators from each block
async function getDataFromBlock(_blockNumber: number) {
const block = await getTransactionsByBlock(_blockNumber);
if (!CONTRACT_ADDRESS || !FUNCTION_NAME) {
console.error("Environment variables are not set.");
process.exitCode = 1;
return;
}
const decodedData = await decodeTransaction(CONTRACT_ADDRESS, block.transactions[1].hash, FUNCTION_NAME);
if (decodedData === undefined || decodedData.uptime === undefined) {
return 0;
}
return decodedData.uptime;
}

// Loop through the blocks and sum the signed blocks for each validator
async function sumValuesForAddresses(_blockNumber: number, _BlockBackToBeChecked: number) {
const sums: { [address: string]: number } = {};
const block = await getTransactionsByBlock(_blockNumber);
console.log(`
_____________________________________________________
Getting the sum of values for the last ${_BlockBackToBeChecked} blocks
Starting from block ${_blockNumber}
Date of block number:
${new Date(block.timestamp * 1000)}:
_____________________________________________________
`);

for (let i = _blockNumber; i > _blockNumber - _BlockBackToBeChecked; i -= 500) {
const blocks = await getDataFromBlock(i);
blocks.forEach((item: any) => {
const address = item[0];
const value = parseInt(item[1]);

if (sums[address]) {
sums[address] += value;
} else {
sums[address] = value;
}
});
console.log(`Looped: ${i}, block`);
}

console.log(sums);
return sums;
}

// Run the script
(async () => {
SamBorisov marked this conversation as resolved.
Show resolved Hide resolved
// Can Hardcode value for specific block number (make sure it is rounded to 500)
const latestBlockNumber = await getLastBlockNumberReward();
if (!CHECKED_BLOCKS) {
console.error("Environment variables are not set.");
process.exitCode = 1;
return;
}
const checkedBlocks = parseInt(CHECKED_BLOCKS);
sumValuesForAddresses(latestBlockNumber, checkedBlocks).catch((error) => {
console.error(error);
process.exitCode = 1;
});
})();
67 changes: 67 additions & 0 deletions scripts/SumBlocksForOne.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Run: npx hardhat run scripts/SumBlocksForOne.ts --network childTest
import { getTransactionsByBlock, decodeTransaction } from "./_helper";

// Input parameters for the script:
const BLOCK_NUMBER = process.env.SUM_BLOCKS_FOR_ONE_BLOCK_NUMBER;
const MAX_BLOCK_NUMBER = process.env.SUM_BLOCKS_FOR_ONE_MAX_BLOCK_NUMBER;
const VALIDATOR_ADDRESS = process.env.SUM_BLOCKS_FOR_ONE_VALIDATOR_ADDRESS;
const CONTRACT_ADDRESS = process.env.SUM_BLOCKS_FOR_ONE_CONTRACT_ADDRESS;
const FUNCTION_NAME = process.env.SUM_BLOCKS_FOR_ONE_FUNCTION_NAME;

// Get the signed blocks for validator from each the block
async function getDataFromBlock(_blockNumber: number) {
const block = await getTransactionsByBlock(_blockNumber);
if (!CONTRACT_ADDRESS || !FUNCTION_NAME || !VALIDATOR_ADDRESS) {
console.error("Environment variables are not set.");
process.exitCode = 1;
return;
}
const decodedData = await decodeTransaction(CONTRACT_ADDRESS, block.transactions[1].hash, FUNCTION_NAME);
if (decodedData === undefined || decodedData.uptime === undefined) {
return 0;
}
const signedBlocks = getAddressValue(decodedData.uptime, VALIDATOR_ADDRESS);
return signedBlocks;
}

// Helper function to get the number of signed blocks for a given address
function getAddressValue(data: any, address: any) {
for (const element of data) {
if (element[0] === address) {
return element[1];
}
}
return 0; // Add 0 if address didn't sign any blocks
}

// Get the sum of signed blocks for the validator
async function getSumBlocks() {
console.log(
`
_________________________________

Summing the signed blocks
for ${VALIDATOR_ADDRESS}
from ${BLOCK_NUMBER} to ${MAX_BLOCK_NUMBER} block
_________________________________`
);
let sum: number = 0;
if (!BLOCK_NUMBER || !MAX_BLOCK_NUMBER) {
console.error("Environment variables are not set.");
process.exitCode = 1;
return;
}
for (let i = Number(BLOCK_NUMBER); i < Number(MAX_BLOCK_NUMBER); i += 500) {
const blocks = await getDataFromBlock(i);
const blockToNum = parseInt(blocks);
sum += blockToNum;
console.log(`Signed blocks: ${blocks}`);
}
console.log(`Sum of signed blocks: ${sum}`);
}

// Run the script
getSumBlocks().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Loading
Loading