contracts/scripts/update-membership-price.cjs
2026-02-18 20:48:41 -08:00

83 lines
2.7 KiB
JavaScript

const hre = require("hardhat");
function requiredEnv(name) {
const value = process.env[name];
if (!value || !value.trim()) {
throw new Error(`Missing required env: ${name}`);
}
return value.trim();
}
function requireAddress(name, value, { allowZero = false } = {}) {
if (!hre.ethers.utils.isAddress(value)) {
throw new Error(`Invalid address for ${name}: ${value}`);
}
if (!allowZero && value === hre.ethers.constants.AddressZero) {
throw new Error(`${name} cannot be zero address`);
}
return value;
}
function parseUint(name, value) {
try {
const parsed = hre.ethers.BigNumber.from(value);
if (parsed.lt(0)) {
throw new Error("must be >= 0");
}
return parsed;
} catch (err) {
throw new Error(`Invalid uint for ${name}: ${value} (${err.message})`);
}
}
async function main() {
const membershipContract = requireAddress(
"MEMBERSHIP_CONTRACT_ADDRESS",
requiredEnv("MEMBERSHIP_CONTRACT_ADDRESS"),
);
const targetCurrency = requireAddress(
"MINT_CURRENCY_ADDRESS",
(process.env.MINT_CURRENCY_ADDRESS || hre.ethers.constants.AddressZero).trim(),
{ allowZero: true },
);
const targetAmount = parseUint("MINT_AMOUNT_ATOMIC", requiredEnv("MINT_AMOUNT_ATOMIC"));
const [deployer] = await hre.ethers.getSigners();
console.log("deployer:", deployer.address);
console.log("network:", hre.network.name);
console.log("membership_contract:", membershipContract);
console.log("target_mint_currency:", targetCurrency);
console.log("target_mint_amount_atomic:", targetAmount.toString());
const abi = [
"function currentMintPrice() view returns (address currency, uint256 amountAtomic)",
"function updateMintPrice(address currency, uint256 amountAtomic)",
];
const contract = new hre.ethers.Contract(membershipContract, abi, deployer);
const [currentCurrency, currentAmount] = await contract.currentMintPrice();
console.log("current_mint_currency:", currentCurrency);
console.log("current_mint_amount_atomic:", currentAmount.toString());
const sameCurrency = currentCurrency.toLowerCase() === targetCurrency.toLowerCase();
const sameAmount = currentAmount.eq(targetAmount);
if (sameCurrency && sameAmount) {
console.log("status: no_change_required");
return;
}
const tx = await contract.updateMintPrice(targetCurrency, targetAmount);
console.log("submitted_tx:", tx.hash);
await tx.wait(1);
console.log("mined_tx:", tx.hash);
const [nextCurrency, nextAmount] = await contract.currentMintPrice();
console.log("updated_mint_currency:", nextCurrency);
console.log("updated_mint_amount_atomic:", nextAmount.toString());
}
main().catch((err) => {
console.error(err);
process.exitCode = 1;
});