Skip to content

Commit

Permalink
reTryWithdrawWithExistingTree
Browse files Browse the repository at this point in the history
  • Loading branch information
sanbir committed Mar 1, 2024
1 parent ea51512 commit 75dcf39
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 23 deletions.
19 changes: 19 additions & 0 deletions 98-re-try-withdraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import "dotenv/config"
import {logger} from "./scripts/helpers/logger";
import {reTryWithdrawWithExistingTree} from "./scripts/reTryWithdrawWithExistingTree";

async function main() {
logger.info('98-re-try-withdraw started')

await reTryWithdrawWithExistingTree()

logger.info('98-re-try-withdraw finished')
}

// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"05-merkle-tree": "ts-node 05-merkle-tree.ts",
"06-report-root-to-oracle-contract.ts": "ts-node 06-report-root-to-oracle-contract.ts",
"07-withdraw": "ts-node 07-withdraw.ts",
"98-re-try-withdraw": "ts-node 98-re-try-withdraw.ts",
"99-get-fee-distributors-with-balance-ssv": "ts-node 99-get-fee-distributors-with-balance-ssv.ts",
"start": "ts-node index.ts"
}
Expand Down
6 changes: 5 additions & 1 deletion scripts/deployFeeDistributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
} from "./helpers/getFeeDistributorFactoryContract";
import {FeeDistributorIdentityParams} from "./models/FeeDistributorIdentityParams";
import {ContractTransaction} from "ethers";
import {getNonce, incrementNonce} from "./helpers/nonce";

export async function deployFeeDistributor(params: FeeDistributorIdentityParams) {
const factory = getFeeDistributorFactoryContractSigned()
Expand All @@ -13,9 +14,12 @@ export async function deployFeeDistributor(params: FeeDistributorIdentityParams)
params.referrerConfig, {
gasLimit: 300000,
maxFeePerGas: process.env.MAX_FEE_PER_GAS,
maxPriorityFeePerGas: process.env.MAX_PIORITY_FEE_PER_GAS
maxPriorityFeePerGas: process.env.MAX_PIORITY_FEE_PER_GAS,
nonce: getNonce()
}
)

incrementNonce()

return tx.hash
}
34 changes: 34 additions & 0 deletions scripts/helpers/nonce.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {ethers} from "ethers";
import {logger} from "./logger";

let nonce = 0

export async function setInitialNonce() {
logger.info('setInitialNonce started')

if (!process.env.RPC_URL) {
throw new Error("No RPC_URL in ENV")
}
if (!process.env.PRIVATE_KEY) {
throw new Error("No PRIVATE_KEY in ENV")
}

const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL)

// @ts-ignore
let wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider)

nonce = await provider.getTransactionCount(wallet.address)

logger.info('nonce = ' + nonce)

logger.info('setInitialNonce finished')
}

export function incrementNonce() {
nonce++
}

export function getNonce() {
return nonce
}
5 changes: 4 additions & 1 deletion scripts/makeOracleReport.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {getOracleContract} from "./helpers/getOracleContract";
import {getNonce, incrementNonce} from "./helpers/nonce";

export async function makeOracleReport(root: string) {
const oracle = getOracleContract()

await oracle.report(root)
await oracle.report(root, {nonce: getNonce()})

incrementNonce()
}
28 changes: 11 additions & 17 deletions scripts/reTryWithdrawWithExistingTree.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import {getDatedJsonFilePath} from "./helpers/getDatedJsonFilePath";
import {StandardMerkleTree} from "@openzeppelin/merkle-tree";
import fs from "fs";
import {withdrawAll} from "./withdrawAll";
import {getAllBalances} from "./getAllBalances";
import {getBalancesDiff} from "./getBalancesDiff";
import {logger} from "./helpers/logger";
import {setInitialNonce} from "./helpers/nonce";
import * as path from "path";

export async function reTryWithdrawWithExistingTree() {
logger.info('reTryWithdrawWithExistingTree started')

const merkleTreeFilePath = getDatedJsonFilePath('merkle-tree')
await setInitialNonce()

console.log(path.dirname(process.argv[1]))

// @ts-ignore
const fds = JSON.parse(fs.readFileSync(path.dirname(process.argv[1]) + '/reports/fee-distributors-with-legacy-already-split-amounts2024-03-01T07:43:48.600Z.json'))

const merkleTreeFilePath = './reports/merkle-tree2024-03-01T07:43:48.600Z.json'

// @ts-ignore
const tree = StandardMerkleTree.load(JSON.parse(fs.readFileSync(merkleTreeFilePath)));
Expand All @@ -19,19 +25,7 @@ export async function reTryWithdrawWithExistingTree() {

logger.info(feeDistributorsAddresses.length + ' feeDistributorsAddresses found in the tree file')

await withdrawAll(feeDistributorsAddresses, tree)

const balancesAfter = await getAllBalances(feeDistributorsAddresses, 'balances-after')

const balancesBeforeFilePath = getDatedJsonFilePath('balances-before')
// @ts-ignore
const balancesBefore = JSON.parse(fs.readFileSync(balancesBeforeFilePath))

const balancesDiff = await getBalancesDiff(balancesBefore, balancesAfter)
const balancesDiffPath = getDatedJsonFilePath('balances-diff')
logger.info('Saving balances diff to ' + balancesDiffPath)
fs.writeFileSync(balancesDiffPath, JSON.stringify(balancesDiff))
logger.info('Balances diff saved')
await withdrawAll(fds, tree)

logger.info('reTryWithdrawWithExistingTree finished')
}
3 changes: 3 additions & 0 deletions scripts/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ import {buildMerkleTreeForFeeDistributorAddress} from "./helpers/buildMerkleTree
import {logger} from "./helpers/logger";
import {makeOracleReport} from "./makeOracleReport";
import {withdrawAll} from "./withdrawAll";
import {setInitialNonce} from "./helpers/nonce";

export async function withdraw() {
try {
await setInitialNonce()

const fds = await getFeeDistributorsWithUpdatedAmountsFromAlreadySplitClRewards()

const rewardData = fds.map(fd => {
Expand Down
6 changes: 3 additions & 3 deletions scripts/withdrawAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {withdrawTx} from "./withdrawTx";
import {getUse4337} from "./helpers/getUse4337";
import {getDatedJsonFilePath} from "./helpers/getDatedJsonFilePath";
import fs from "fs";
import {FeeDistributorWithAmountForPeriod} from "./models/FeeDistributorWithAmountForPeriod";
import {getIsContract} from "./helpers/getIsContract";
import {deployFeeDistributor} from "./deployFeeDistributor";
import {FeeDistributorWithAmount} from "./models/FeeDistributorWithAmount";

export async function withdrawAll(feeDistributors: FeeDistributorWithAmountForPeriod[], tree: StandardMerkleTree<any[]>) {
export async function withdrawAll(feeDistributors: FeeDistributorWithAmount[], tree: StandardMerkleTree<any[]>) {
logger.info('withdrawAll started')

if (!process.env.MIN_BALANCE_TO_WITHDRAW_IN_GWEI) {
Expand All @@ -20,7 +20,7 @@ export async function withdrawAll(feeDistributors: FeeDistributorWithAmountForPe

for (const fd of feeDistributors) {
try {
const isDeployed = getIsContract(fd.feeDistributor)
const isDeployed = await getIsContract(fd.feeDistributor)

if (!isDeployed) {
if (!fd.identityParams) {
Expand Down
6 changes: 5 additions & 1 deletion scripts/withdrawTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {obtainProof} from "./helpers/obtainProof";
import {logger} from "./helpers/logger";
import {getFeeDistributorContractSigned} from "./helpers/getFeeDistributorContract";
import {ContractTransaction} from "ethers";
import {getNonce, incrementNonce} from "./helpers/nonce";

export async function withdrawTx(feeDistributorAddress: string, tree: StandardMerkleTree<any[]>) {
logger.info('withdrawTx started for: ' + feeDistributorAddress)
Expand All @@ -22,9 +23,12 @@ export async function withdrawTx(feeDistributorAddress: string, tree: StandardMe
const tx: ContractTransaction = await feeDistributor.withdraw(proof, amountInGwei, {
gasLimit: 200000,
maxFeePerGas: process.env.MAX_FEE_PER_GAS,
maxPriorityFeePerGas: process.env.MAX_PIORITY_FEE_PER_GAS
maxPriorityFeePerGas: process.env.MAX_PIORITY_FEE_PER_GAS,
nonce: getNonce()
})

incrementNonce()

logger.info('withdrawTx finished for: ' + feeDistributorAddress + '. Hash: ' + tx.hash)

return tx.hash
Expand Down

0 comments on commit 75dcf39

Please sign in to comment.