@ethersphere/bee-factory
Version:
Local Ethereum Swarm development stack
120 lines • 6.97 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.deployContracts = deployContracts;
const path = __importStar(require("path"));
const fs = __importStar(require("fs"));
const ethers_1 = require("ethers");
const config_1 = require("../config");
function loadArtifact(name) {
// Artifacts are in src/contracts/artifacts/ at source time, but in
// dist/../src/contracts/artifacts/ after compilation. We probe both.
const candidates = [
path.join(__dirname, '..', 'contracts', 'artifacts', `${name}.json`),
path.join(__dirname, '..', '..', 'src', 'contracts', 'artifacts', `${name}.json`),
];
for (const p of candidates) {
if (fs.existsSync(p)) {
return JSON.parse(fs.readFileSync(p, 'utf8'));
}
}
throw new Error(`Artifact not found for contract: ${name}. Searched:\n ${candidates.join('\n ')}`);
}
async function deploy(signer, name, ...args) {
const artifact = loadArtifact(name);
const factory = new ethers_1.ethers.ContractFactory(artifact.abi, artifact.bytecode, signer);
const contract = await factory.deploy(...args);
const receipt = await contract.deploymentTransaction().wait();
return { contract, blockNumber: receipt.blockNumber };
}
async function deployContracts(provider) {
// NonceManager tracks nonces locally so back-to-back deploys don't all
// fetch the same stale nonce from the provider's block-polling cache.
const deployer = new ethers_1.ethers.NonceManager(new ethers_1.ethers.Wallet(config_1.DEPLOYER_KEY, provider));
const deployerAddress = await deployer.signer.getAddress();
// 1. BZZ Token — name, symbol, initialSupply (we mint per-node later)
const { contract: bzzToken } = await deploy(deployer, 'BzzToken', 'Bee', 'BZZ', 0n);
const bzzTokenAddress = await bzzToken.getAddress();
// 2. PostageStamp(bzzToken, minimumBucketDepth=16)
const { contract: postageStamp, blockNumber: postageStampStartBlock } = await deploy(deployer, 'PostageStamp', bzzTokenAddress, 16);
const postageStampAddress = await postageStamp.getAddress();
// 3. PriceOracle(postageStamp)
const { contract: priceOracle } = await deploy(deployer, 'PriceOracle', postageStampAddress);
const priceOracleAddress = await priceOracle.getAddress();
// 4. StakeRegistry(bzzToken, networkId, oracleContract)
const { contract: stakeRegistry } = await deploy(deployer, 'StakeRegistry', bzzTokenAddress, config_1.CHAIN_ID, priceOracleAddress);
const stakeRegistryAddress = await stakeRegistry.getAddress();
// 5. Redistribution(staking, postageContract, oracleContract)
const { contract: redistribution } = await deploy(deployer, 'Redistribution', stakeRegistryAddress, postageStampAddress, priceOracleAddress);
const redistributionAddress = await redistribution.getAddress();
// 6. SimpleSwapFactory(bzzToken)
const { contract: swapFactory } = await deploy(deployer, 'SimpleSwapFactory', bzzTokenAddress);
const swapFactoryAddress = await swapFactory.getAddress();
// 7. SwapPriceOracle — real price oracle for chequebook/swap accounting.
// price: PLUR per accounting unit; chequeValueDeduction: deducted from first cheque per peer.
const { contract: swapPriceOracle } = await deploy(deployer, 'SwapPriceOracle', 24000n, 0n);
const swapPriceOracleAddress = await swapPriceOracle.getAddress();
// ── Post-deployment role grants ──────────────────────────────────────────
// All contracts use OZ AccessControl. Role bytes are keccak256(name).
const PRICE_ORACLE_ROLE = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes('PRICE_ORACLE_ROLE'));
const REDISTRIBUTOR_ROLE = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes('REDISTRIBUTOR_ROLE'));
const PRICE_UPDATER_ROLE = ethers_1.ethers.keccak256(ethers_1.ethers.toUtf8Bytes('PRICE_UPDATER_ROLE'));
const grantAbi = ['function grantRole(bytes32 role, address account) external'];
const ps = new ethers_1.ethers.Contract(postageStampAddress, grantAbi, deployer);
const sr = new ethers_1.ethers.Contract(stakeRegistryAddress, grantAbi, deployer);
const po = new ethers_1.ethers.Contract(priceOracleAddress, grantAbi, deployer);
// PriceOracle → PRICE_ORACLE_ROLE on PostageStamp (so it can call setPrice)
await (await ps.grantRole(PRICE_ORACLE_ROLE, priceOracleAddress)).wait();
// Redistribution → REDISTRIBUTOR_ROLE on PostageStamp
await (await ps.grantRole(REDISTRIBUTOR_ROLE, redistributionAddress)).wait();
// Redistribution → REDISTRIBUTOR_ROLE on StakeRegistry
await (await sr.grantRole(REDISTRIBUTOR_ROLE, redistributionAddress)).wait();
// Deployer → PRICE_UPDATER_ROLE on PriceOracle so we can seed a price
await (await po.grantRole(PRICE_UPDATER_ROLE, deployerAddress)).wait();
// Seed an initial price so Bee nodes can read a non-zero price immediately
const priceOracleFull = new ethers_1.ethers.Contract(priceOracleAddress, ['function setPrice(uint32 _price) external returns (bool)'], deployer);
await (await priceOracleFull.setPrice(24000)).wait();
return {
bzzToken: bzzTokenAddress,
postageStamp: postageStampAddress,
postageStampStartBlock,
priceOracle: priceOracleAddress,
stakeRegistry: stakeRegistryAddress,
redistribution: redistributionAddress,
swapFactory: swapFactoryAddress,
swapPriceOracle: swapPriceOracleAddress,
};
}
//# sourceMappingURL=deploy.js.map