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

22 facet deploy #31

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
23 changes: 22 additions & 1 deletion EIP2535-Diamonds-Reference-Implementation/contracts/Diamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { LibDiamond } from "./libraries/LibDiamond.sol";
import { IDiamondCut } from "./interfaces/IDiamondCut.sol";
import { IDiamondLoupe } from "./interfaces/IDiamondLoupe.sol";
import { IERC173 } from "./interfaces/IERC173.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Receiver.sol";

// When no function exists for function called
error FunctionNotFound(bytes4 _functionSelector);
Expand All @@ -25,7 +26,7 @@ struct DiamondArgs {
bytes initCalldata;
}

contract Diamond {
contract Diamond is ERC1155Receiver {

constructor(IDiamondCut.FacetCut[] memory _diamondCut, DiamondArgs memory _args) payable {
LibDiamond.setContractOwner(_args.owner);
Expand Down Expand Up @@ -68,4 +69,24 @@ contract Diamond {
}

receive() external payable {}

function onERC1155Received(
address operator,
address from,
uint256 id,
uint256 value,
bytes calldata data
) external returns (bytes4) {
return this.onERC1155Received.selector;
}

function onERC1155BatchReceived(
address operator,
address from,
uint256[] calldata ids,
uint256[] calldata values,
bytes calldata data
) external returns (bytes4) {
return this.onERC1155BatchReceived.selector;
}
}
1 change: 1 addition & 0 deletions backend/contracts/FacetRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Receiver.sol";
import "./interfaces/IRegistry.sol";

contract FacetRegistry is ERC1155, IRegistry {
Expand Down
9 changes: 3 additions & 6 deletions backend/scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import hre, { ethers } from "hardhat";
import { ethers } from "hardhat";
import { getSelectors, FacetCutAction } from 'diamond-1-hardhat/scripts/libraries/diamond.js';
import { FacetRegistry, Kimberlite } from "../typechain-types";
import { verify } from "./verify";

async function deployKimberlite(facetRegistry: FacetRegistry): Promise<Kimberlite> {
export async function deployKimberlite(facetRegistry: FacetRegistry): Promise<Kimberlite> {

console.log('Deploying facets')
// The `facetCuts` variable is the FacetCut[] that contains the functions to add during diamond deployment
Expand Down Expand Up @@ -51,7 +51,7 @@ async function deployKimberlite(facetRegistry: FacetRegistry): Promise<Kimberlit
return kimberlite
}

async function deployRegistry(): Promise<FacetRegistry> {
export async function deployRegistry(): Promise<FacetRegistry> {
const Registry = await ethers.getContractFactory("FacetRegistry")
console.log('Deploying FacetRegistry')
const registry = await Registry.deploy()
Expand All @@ -77,6 +77,3 @@ if (require.main === module) {
})

}

exports.deployKimberlite = deployKimberlite
exports.deployRegistry = deployRegistry
41 changes: 35 additions & 6 deletions backend/scripts/extract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
import { ethers } from "hardhat";
async function extract(kimberliteAddress :string) {
import hre, { ethers } from "hardhat";
import { deployKimberlite, deployRegistry } from "./deploy";

console.log('Extract')
export async function getKimberlite() {
if (hre.network.name == "hardhat") {
const facetRegistry = await deployRegistry()
return deployKimberlite(facetRegistry)
}
const factory = await ethers.getContractFactory("Kimberlite")
const contract = await factory.attach(kimberliteAddress)
await contract.extractDiamond("https://arweave.net/5S1NrCPfLJX7FgRx78kV8nDJCnwFkWRKSc18pDdD4Sw")
if (hre.network.name == "polygon") {
return await factory.attach("0xf78b989D3cF27EFc309887501a749fE2aEAAA277")
}
if (hre.network.name == "mantle_testnet") {
return await factory.attach("0x05c7df69BA4Be0F6483F8778606E7253Bc2254A4")
}
throw "Unknown network"
}

export async function extract() {
console.log('Extract')
const kimberlite = await getKimberlite();
const tx = await kimberlite.extractDiamond("https://arweave.net/5S1NrCPfLJX7FgRx78kV8nDJCnwFkWRKSc18pDdD4Sw")
const receipt = await tx.wait()
const diamondAddress = receipt.events![3].args!.diamond
console.log("Extracted diamond", diamondAddress);
return diamondAddress;
}

extract("0x306DD94AdEc5D88383065C237Ed1958687Be4daf")
if (require.main === module) {
extract()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})

}


40 changes: 32 additions & 8 deletions backend/scripts/facets/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ import fs from 'fs/promises';
import path from 'path';
import Arweave from 'arweave';
import { verify, getEtherscanEndpoint, getEtherscanEndpointOrPolygon } from "../verify";
import { deployRegistry } from "../deploy"
//import jwk_data from './../../cache/arweave-keyfile.json'
import hre from "hardhat"
import { extract } from "../extract";

const groupName = "Characters"
const systemName = "TrivialCharactersSystem"
const storageKey = "character.storage"
const storageContents = `struct AliveState {
const storageContents = `struct AliveState {
mapping(uint256 => bool) alive;
}

Expand All @@ -25,20 +28,33 @@ const storageContents = `struct AliveState {
return state.alive[id];
}`

async function getFacetRegistry() {
if (hre.network.name == "hardhat") {
return deployRegistry()
}
const factory = await ethers.getContractFactory("FacetRegistry")
if (hre.network.name == "polygon") {
return await factory.attach("0xE056F64f06e8D0F7Cd2a9501138D3aFa0eF3FF32")
}
if (hre.network.name == "mantle_testnet") {
return await factory.attach("0x56E427509b7dca569E8F20BE2F30B0206AB6289b")
}
throw "Unknown network"
}

async function deploySystem(systemName: string): Promise<BaseContract> {
// 1. Deploy contract with ethers and hardhat


const facetRegistry = await getFacetRegistry()

const System = await ethers.getContractFactory(systemName)
const system = await System.deploy()
console.log(`${systemName} deployed to ${system.address}, ${Object.keys(system)}`);
await verify(system.address, [])

const etherScanEndpoint = await getEtherscanEndpointOrPolygon()
const explorer_url = `${etherScanEndpoint.urls.browserURL}/address/${system.address}#code`
const explorer_url = `${etherScanEndpoint.urls.browserURL}/address/${system.address}#code`

// 2. the resulting metadata of deployed contract is saved locally
let resultJson = {
address: system.address,
Expand All @@ -57,10 +73,10 @@ async function deploySystem(systemName: string): Promise<BaseContract> {




// 3. the resulting metadata of deployed contract is saved to public decentralized storage
// here we've got arweave

// NB! This is an example of saving meta to decentralized storage.
// To make this demo working, we've put a real jwk file into project
// Please, make your own arweave wallet and jwk file
Expand All @@ -81,7 +97,7 @@ async function deploySystem(systemName: string): Promise<BaseContract> {


let transactionA = await arweave.createTransaction({
data: JSON.stringify(resultJson, null, 4)
data: JSON.stringify(resultJson, null, 4)
}, jwk_data);

await arweave.transactions.sign(transactionA, jwk_data);
Expand All @@ -97,7 +113,15 @@ async function deploySystem(systemName: string): Promise<BaseContract> {
//TODO get public url of result
console.log(`https://viewblock.io/arweave/tx/${transactionA.id}`);
//TODO await uploaded


//Replace with diamond address

const diamondAddress = await extract();
console.log('dddd', diamondAddress);
const tx = await facetRegistry.mintFacet(system.address, diamondAddress)
const receipt = await tx.wait()
console.log(receipt)

return system
}

Expand Down