diff --git a/EIP2535-Diamonds-Reference-Implementation/contracts/Diamond.sol b/EIP2535-Diamonds-Reference-Implementation/contracts/Diamond.sol index d663b8d..f0c4388 100644 --- a/EIP2535-Diamonds-Reference-Implementation/contracts/Diamond.sol +++ b/EIP2535-Diamonds-Reference-Implementation/contracts/Diamond.sol @@ -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); @@ -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); @@ -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; + } } diff --git a/backend/contracts/FacetRegistry.sol b/backend/contracts/FacetRegistry.sol index 046b4e7..e1437f1 100644 --- a/backend/contracts/FacetRegistry.sol +++ b/backend/contracts/FacetRegistry.sol @@ -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 { diff --git a/backend/scripts/deploy.ts b/backend/scripts/deploy.ts index 10f4607..a95e218 100644 --- a/backend/scripts/deploy.ts +++ b/backend/scripts/deploy.ts @@ -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 { +export async function deployKimberlite(facetRegistry: FacetRegistry): Promise { console.log('Deploying facets') // The `facetCuts` variable is the FacetCut[] that contains the functions to add during diamond deployment @@ -51,7 +51,7 @@ async function deployKimberlite(facetRegistry: FacetRegistry): Promise { +export async function deployRegistry(): Promise { const Registry = await ethers.getContractFactory("FacetRegistry") console.log('Deploying FacetRegistry') const registry = await Registry.deploy() @@ -77,6 +77,3 @@ if (require.main === module) { }) } - -exports.deployKimberlite = deployKimberlite -exports.deployRegistry = deployRegistry \ No newline at end of file diff --git a/backend/scripts/extract.ts b/backend/scripts/extract.ts index 0415073..4208d3a 100644 --- a/backend/scripts/extract.ts +++ b/backend/scripts/extract.ts @@ -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) + }) + +} + + diff --git a/backend/scripts/facets/deploy.ts b/backend/scripts/facets/deploy.ts index 12f4dde..4170978 100644 --- a/backend/scripts/facets/deploy.ts +++ b/backend/scripts/facets/deploy.ts @@ -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; } @@ -25,11 +28,24 @@ 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 { // 1. Deploy contract with ethers and hardhat - + const facetRegistry = await getFacetRegistry() const System = await ethers.getContractFactory(systemName) const system = await System.deploy() @@ -37,8 +53,8 @@ async function deploySystem(systemName: string): Promise { 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, @@ -57,10 +73,10 @@ async function deploySystem(systemName: string): Promise { - + // 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 @@ -81,7 +97,7 @@ async function deploySystem(systemName: string): Promise { 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); @@ -97,7 +113,15 @@ async function deploySystem(systemName: string): Promise { //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 }