feat: add foreign key constraints and metrics for blockchain node
This commit is contained in:
58
packages/solidity/aitbc-token/scripts/deploy.ts
Normal file
58
packages/solidity/aitbc-token/scripts/deploy.ts
Normal file
@ -0,0 +1,58 @@
|
||||
import { ethers } from "hardhat";
|
||||
import { AIToken__factory } from "../typechain-types";
|
||||
|
||||
function envOrDefault(name: string, fallback?: string): string | undefined {
|
||||
const value = process.env[name]?.trim();
|
||||
return value && value.length > 0 ? value : fallback;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const [deployer, coordinatorCandidate] = await ethers.getSigners();
|
||||
|
||||
console.log("Deploying AIToken using admin:", deployer.address);
|
||||
|
||||
const contractFactory: AIToken__factory = await ethers.getContractFactory("AIToken");
|
||||
const token = await contractFactory.deploy(deployer.address);
|
||||
await token.waitForDeployment();
|
||||
|
||||
const contractAddress = await token.getAddress();
|
||||
console.log("AIToken deployed to:", contractAddress);
|
||||
|
||||
const coordinatorRole = await token.COORDINATOR_ROLE();
|
||||
const attestorRole = await token.ATTESTOR_ROLE();
|
||||
|
||||
const coordinatorAddress = envOrDefault("COORDINATOR_ADDRESS", coordinatorCandidate.address);
|
||||
if (!coordinatorAddress) {
|
||||
throw new Error(
|
||||
"COORDINATOR_ADDRESS not provided and could not infer fallback signer address"
|
||||
);
|
||||
}
|
||||
|
||||
if (!(await token.hasRole(coordinatorRole, coordinatorAddress))) {
|
||||
console.log("Granting coordinator role to", coordinatorAddress);
|
||||
const tx = await token.grantRole(coordinatorRole, coordinatorAddress);
|
||||
await tx.wait();
|
||||
} else {
|
||||
console.log("Coordinator role already assigned to", coordinatorAddress);
|
||||
}
|
||||
|
||||
const attestorAddress = envOrDefault("ATTESTOR_ADDRESS");
|
||||
if (attestorAddress) {
|
||||
if (!(await token.hasRole(attestorRole, attestorAddress))) {
|
||||
console.log("Granting attestor role to", attestorAddress);
|
||||
const tx = await token.grantRole(attestorRole, attestorAddress);
|
||||
await tx.wait();
|
||||
} else {
|
||||
console.log("Attestor role already assigned to", attestorAddress);
|
||||
}
|
||||
} else {
|
||||
console.log("No ATTESTOR_ADDRESS provided; skipping attestor role grant.");
|
||||
}
|
||||
|
||||
console.log("Deployment complete. Export AITOKEN_ADDRESS=", contractAddress);
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
66
packages/solidity/aitbc-token/scripts/mintWithReceipt.ts
Normal file
66
packages/solidity/aitbc-token/scripts/mintWithReceipt.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import { ethers } from "hardhat";
|
||||
import { AIToken__factory } from "../typechain-types";
|
||||
|
||||
type HexString = `0x${string}`;
|
||||
|
||||
type EnvValue = string & {}
|
||||
|
||||
function requireEnv(name: string): EnvValue {
|
||||
const value = process.env[name]?.trim();
|
||||
if (!value) {
|
||||
throw new Error(`Missing required environment variable ${name}`);
|
||||
}
|
||||
return value as EnvValue;
|
||||
}
|
||||
|
||||
function parseUnits(value: string): bigint {
|
||||
try {
|
||||
if (value.startsWith("0x") || value.startsWith("0X")) {
|
||||
return BigInt(value);
|
||||
}
|
||||
return BigInt(value);
|
||||
} catch (error) {
|
||||
throw new Error(`UNITS must be a BigInt-compatible value, received ${value}`);
|
||||
}
|
||||
}
|
||||
|
||||
function assertHex(value: string, name: string): HexString {
|
||||
if (!value.startsWith("0x") && !value.startsWith("0X")) {
|
||||
throw new Error(`${name} must be 0x-prefixed`);
|
||||
}
|
||||
return value.toLowerCase() as HexString;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const contractAddress = assertHex(requireEnv("AITOKEN_ADDRESS"), "AITOKEN_ADDRESS");
|
||||
const providerAddress = requireEnv("PROVIDER_ADDRESS");
|
||||
const units = parseUnits(requireEnv("UNITS"));
|
||||
const receiptHash = assertHex(requireEnv("RECEIPT_HASH"), "RECEIPT_HASH");
|
||||
const signature = assertHex(requireEnv("ATTESTOR_SIGNATURE"), "ATTESTOR_SIGNATURE");
|
||||
|
||||
const coordinatorIndex = Number(process.env.COORDINATOR_SIGNER_INDEX ?? "1");
|
||||
const signers = await ethers.getSigners();
|
||||
const coordinator = signers[coordinatorIndex];
|
||||
if (!coordinator) {
|
||||
throw new Error(
|
||||
`COORDINATOR_SIGNER_INDEX=${coordinatorIndex} does not correspond to an available signer`
|
||||
);
|
||||
}
|
||||
|
||||
console.log("Using coordinator signer:", coordinator.address);
|
||||
console.log("Minting receipt for provider:", providerAddress);
|
||||
console.log("Units:", units.toString());
|
||||
|
||||
const token = AIToken__factory.connect(contractAddress, coordinator);
|
||||
const tx = await token.mintWithReceipt(providerAddress, units, receiptHash, signature);
|
||||
const receipt = await tx.wait();
|
||||
|
||||
console.log("Mint transaction hash:", receipt?.hash ?? tx.hash);
|
||||
const balance = await token.balanceOf(providerAddress);
|
||||
console.log("Provider balance:", balance.toString());
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error(error);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in New Issue
Block a user