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

distributor script #18

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"author": "dzgoldman",
"license": "MIT",
"scripts": {
"distribute": "ts-node ./src-ts/cli/distributeRewards.ts",
"prepare": "forge install && cd lib/arbitrum-sdk && yarn",
"gen-recipients": "make install && hardhat run src-ts/getRecipientData.ts",
"test:e2e": "./test/e2e/test-e2e.bash"
Expand Down Expand Up @@ -37,4 +38,4 @@
"dotenv": "^16.3.1",
"yargs": "^17.7.2"
}
}
}
30 changes: 30 additions & 0 deletions src-ts/cli/distributeRewards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import dotenv from "dotenv";
import yargs from "yargs";
import { JsonRpcProvider } from "@ethersproject/providers";
import { Wallet } from "ethers";
import { distributeRewards } from "../lib";
dotenv.config();

const CHILD_CHAIN_PK = process.env.CHILD_CHAIN_PK;

if (!CHILD_CHAIN_PK) throw new Error("Need CHILD_CHAIN_PK");

const options = yargs(process.argv.slice(2))
.options({
rpcURL: { type: "string", demandOption: true },
rewardDistAddr: { type: "string", demandOption: true },
minBalanceEther: { type: "number", demandOption: false, default: 0 },
})
.parseSync() as {
rpcURL: string;
rewardDistAddr: string;
minBalanceEther: number;
};
DZGoldman marked this conversation as resolved.
Show resolved Hide resolved

(async () => {
await distributeRewards(
new Wallet(CHILD_CHAIN_PK, new JsonRpcProvider(options.rpcURL)),
options.rewardDistAddr,
options.minBalanceEther
);
})();
10 changes: 9 additions & 1 deletion src-ts/data/recipientAndWeightsData.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"updatedAt": "15:46:55 GMT-0500 (Eastern Standard Time)",
"updatedAt": "16:22:28 GMT-0500 (Eastern Standard Time)",
"data": [{
"chain": "Arbitum One",
"chainId": 42161,
"feeType": "L1 Base",
"address": "0xE6ec2174539a849f9f3ec973C66b333eD08C0c18",
"recipients": ["0x98e4dB7e07e584F89A2F6043E7b7C89DC27769eD"],
Expand All @@ -10,6 +11,7 @@
"recipientWeights": "0x1d460b64f7b8ba0be629afe9b4ae65333b379985d7ea823ff4c0b8c3b5102153"
}, {
"chain": "Arbitum One",
"chainId": 42161,
"feeType": "L1 Surplus",
"address": "0x2E041280627800801E90E9Ac83532fadb6cAd99A",
"recipients": ["0xbFc1FECa8B09A5c5D3EFfE7429eBE24b9c09EF58"],
Expand All @@ -18,6 +20,7 @@
"recipientWeights": "0x1d460b64f7b8ba0be629afe9b4ae65333b379985d7ea823ff4c0b8c3b5102153"
}, {
"chain": "Arbitum One",
"chainId": 42161,
"feeType": "L2 Base",
"address": "0xbF5041Fc07E1c866D15c749156657B8eEd0fb649",
"recipients": ["0xbFc1FECa8B09A5c5D3EFfE7429eBE24b9c09EF58"],
Expand All @@ -26,6 +29,7 @@
"recipientWeights": "0x1d460b64f7b8ba0be629afe9b4ae65333b379985d7ea823ff4c0b8c3b5102153"
}, {
"chain": "Arbitum One",
"chainId": 42161,
"feeType": "L2 Surplus",
"address": "0x32e7AF5A8151934F3787d0cD59EB6EDd0a736b1d",
"recipients": ["0xbFc1FECa8B09A5c5D3EFfE7429eBE24b9c09EF58"],
Expand All @@ -34,6 +38,7 @@
"recipientWeights": "0x1d460b64f7b8ba0be629afe9b4ae65333b379985d7ea823ff4c0b8c3b5102153"
}, {
"chain": "Arbitum Nova",
"chainId": 42170,
"feeType": "L1 Base",
"address": "0xc9722CfDDFbC6aF4E77023E8B5Bd87489EFEbf5F",
"recipients": ["0xD0749b3e537Ed52DE4e6a3Ae1eB6fc26059d0895"],
Expand All @@ -42,6 +47,7 @@
"recipientWeights": "0x1d460b64f7b8ba0be629afe9b4ae65333b379985d7ea823ff4c0b8c3b5102153"
}, {
"chain": "Arbitum Nova",
"chainId": 42170,
"feeType": "L1 Surplus",
"address": "0x509386DbF5C0BE6fd68Df97A05fdB375136c32De",
"recipients": ["0xf7951D92B0C345144506576eC13Ecf5103aC905a"],
Expand All @@ -50,6 +56,7 @@
"recipientWeights": "0x1d460b64f7b8ba0be629afe9b4ae65333b379985d7ea823ff4c0b8c3b5102153"
}, {
"chain": "Arbitum Nova",
"chainId": 42170,
"feeType": "L2 Base",
"address": "0x9fCB6F75D99029f28F6F4a1d277bae49c5CAC79f",
"recipients": ["0xf7951D92B0C345144506576eC13Ecf5103aC905a", "0xD0749b3e537Ed52DE4e6a3Ae1eB6fc26059d0895", "0x41C327d5fc9e29680CcD45e5E52446E0DB3DAdFd", "0x02C2599aa929e2509741b44F3a13029745aB1AB2", "0xA221f29236996BDEfA5C585acdD407Ec84D78447", "0x0fB1f1a31429F1A90a19Ab5486a6DFb384179641", "0xb814441ed86e98e8B83d31eEC095e4a5A36Fc3c2"],
Expand All @@ -58,6 +65,7 @@
"recipientWeights": "0x27c5930fa5bbbf9423b2d7e7555073eb37787a62eb8ffeac867b84726ed94af8"
}, {
"chain": "Arbitum Nova",
"chainId": 42170,
"feeType": "L2 Surplus",
"address": "0x3B68a689c929327224dBfCe31C1bf72Ffd2559Ce",
"recipients": ["0xf7951D92B0C345144506576eC13Ecf5103aC905a"],
Expand Down
1 change: 1 addition & 0 deletions src-ts/getRecipientData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const main = async () => {
}
recAndWeightsJsonData.push({
chain: chain.name,
chainId: chain.id,
feeType: feeType,
address,
...recAndWeightData,
Expand Down
74 changes: 63 additions & 11 deletions src-ts/lib.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
import { RewardDistributor__factory } from "../typechain-types";
import { RecipientsUpdatedEventObject } from "../typechain-types/src/RewardDistributor";

import { JsonRpcProvider } from "@ethersproject/providers";
import { rewardDistributors } from "./daoRewardDistributorContracts"
import { Provider } from "@ethersproject/providers";
import { Wallet, BigNumber, utils } from "ethers";
import { readFileSync } from "fs";

interface RecipientsAndWeights {
recipients: string[];
recipientGroup: string;
weights: number[];
recipientWeights: string;
}
export const getRecipientsAndWeights = async (
rewardDistAddress: string,
provider: JsonRpcProvider,
provider: Provider,
fromBlock = 0
) => {
): Promise<RecipientsAndWeights> => {
const distributor = RewardDistributor__factory.connect(
rewardDistAddress,
provider
Expand All @@ -21,16 +28,61 @@ export const getRecipientsAndWeights = async (
const latestLog = logs[logs.length - 1];
if (!latestLog) throw new Error("No updates found");

const eventObj = distributor.interface.parseLog(latestLog)
const eventObj = distributor.interface.parseLog(latestLog)
.args as unknown as RecipientsUpdatedEventObject;

return {
recipients: eventObj.recipients,
recipientGroup: eventObj.recipientGroup,
weights: eventObj.weights.map((w)=> w.toNumber()),
recipientWeights: eventObj.recipientWeights
}
return {
recipients: eventObj.recipients,
recipientGroup: eventObj.recipientGroup,
weights: eventObj.weights.map((w) => w.toNumber()),
recipientWeights: eventObj.recipientWeights,
};
};

export const distributeRewards = async (
connectedSigner: Wallet,
distributorAddr: string,
_minBalanceEther?: number
) => {
const chainId = await connectedSigner.getChainId();
const minBalanceWei = _minBalanceEther
? utils.parseEther(_minBalanceEther.toString())
: BigNumber.from(0);
const distributor = RewardDistributor__factory.connect(
distributorAddr,
connectedSigner
);
console.log(connectedSigner.address);

const bal = await connectedSigner.provider.getBalance(distributorAddr);
if (bal.lt(minBalanceWei)) {
console.log("Balance too low");
console.log("Min balance", minBalanceWei.toString());
console.log("Balance", bal.toString());
return;
}

const dataBuf = await readFileSync(
"./src-ts/data/recipientAndWeightsData.json"
).toString();
const data = JSON.parse(dataBuf).data;

let recAndWeights = data.find(
(datum: any) =>
datum.chainId === chainId &&
datum.address.toLowerCase() === distributorAddr.toLowerCase()
) as RecipientsAndWeights;

if (!recAndWeights) {
recAndWeights = await getRecipientsAndWeights(
distributorAddr,
connectedSigner.provider
);
}
const res = await distributor.distributeRewards(
recAndWeights.recipients,
recAndWeights.weights
);
const rec = await res.wait();
console.log("Rewards distributed", rec.transactionHash);
};
Loading