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

Add withdraw subtask to account task #48

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
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
57 changes: 57 additions & 0 deletions tasks/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,63 @@ ACCOUNT_SCOPE.task("create", "Create CMAccount")
}
});

ACCOUNT_SCOPE.task("withdraw", "Withdraw funds from CMAccount")
.addOptionalParam("privateKey", "Private key to use, default: CMACCOUNT_PK env variable", process.env.CMACCOUNT_PK)
.addOptionalParam(
"cmAccount",
"CMAccount address, default: CMACCOUNT_ADDRESS env variable",
process.env.CMACCOUNT_ADDRESS,
)
.addParam("recipient", "Recipient address")
.addParam("amount", "Amount to withdraw")
VjeraTurk marked this conversation as resolved.
Show resolved Hide resolved
.addOptionalParam("unit", "Unit of amount (CAM/nCAM/aCAM)", "aCAM")
.setAction(async (taskArgs, hre) => {
const cmAccount = await getCMAccount(taskArgs.cmAccount);
console.log("CMAccount:", taskArgs.cmAccount);

try {
// Validate unit
const validUnits = ["cam", "ncam", "acam"];
if (!validUnits.includes(taskArgs.unit.toLowerCase())) {
throw new Error(`Invalid unit. Must be one of: ${validUnits.join(", ")}`);
}

// Convert amount to wei based on unit
let amountInWei;
let hrUnit;
const unit = taskArgs.unit.toLowerCase();

switch (unit) {
case "cam":
amountInWei = ethers.parseEther(taskArgs.amount.toString());
hrUnit = "CAM";
break;
case "ncam":
amountInWei = ethers.parseUnits(taskArgs.amount.toString(), "gwei");
hrUnit = "nCAM";
break;
case "acam":
amountInWei = taskArgs.amount.toString();
hrUnit = "aCAM";
break;
}

console.log("Running on", hre.network.name);
console.log("💸 Withdrawing funds...");
console.log("From :", taskArgs.cmAccount);
console.log("To :", taskArgs.recipient);
console.log("Amount :", `${taskArgs.amount} ${hrUnit}`);
console.log("Amount (aCAM):", amountInWei.toString());

const signer = new ethers.Wallet(taskArgs.privateKey, ethers.provider);
const tx = await cmAccount.connect(signer).withdraw(taskArgs.recipient, amountInWei);
const receipt = await tx.wait();
console.log("Tx:", receipt.hash);
} catch (error) {
handleTransactionError(error, cmAccount);
}
});

ACCOUNT_SCOPE.task("bot:add", "Add bot to the CMAccount")
.addOptionalParam("privateKey", "Private key to use, default: CMACCOUNT_PK env variable", process.env.CMACCOUNT_PK)
.addOptionalParam(
Expand Down