Skip to content

Commit

Permalink
feat: core contract
Browse files Browse the repository at this point in the history
  • Loading branch information
Delioos committed Oct 1, 2024
1 parent bc3ab49 commit 2ec10e9
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@ out/
/broadcast/*/31337/
/broadcast/**/dry-run/

# openzeppelin
lib/

# Docs
docs/

# Dotenv file
.env

# javascript files
**/*.js
12 changes: 12 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
[submodule "lib/forge-std"]
path = lib/forge-std
url = https://github.com/foundry-rs/forge-std
[submodule "lib/openzeppelin-contracts"]
path = lib/openzeppelin-contracts
url = https://github.com/OpenZeppelin/openzeppelin-contracts
[submodule "lib/chainlink"]
path = lib/chainlink
url = https://github.com/smartcontractkit/chainlink
[submodule "lib/chainlink-ccip"]
path = lib/chainlink-ccip
url = https://github.com/smartcontractkit/chainlink-ccip
[submodule "lib/ccip"]
path = lib/ccip
url = https://github.com/smartcontractkit/ccip
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
TODO: provide architecture and a quick project overview

`ERC1155Core.sol` contains the main logic.
It's based on the (openzeppellin) ERC1155 non fungible fractionalized contract.
To enable cross chain interoperability, we need to add burn and mint. A transfer from a chain 'A' to a chain 'B' consists of burning the supply in the chain 'A' and minting it on the chain 'B' ensuring "uniqueness" of our data on all chains.

kudos to (@andrej)[https://x.com/andrej_dev] for the chainlink bootcamp <3
12 changes: 9 additions & 3 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
src = 'src'
out = 'out'
libs = ['lib']
solc = '0.8.24'
cache = true
force = true
optimizer = true
optimizer_runs = 200
evm_version = 'cancun'

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions lib/ccip
Submodule ccip added at 71799d
1 change: 1 addition & 0 deletions lib/chainlink
Submodule chainlink added at 72a7d2
1 change: 1 addition & 0 deletions lib/chainlink-ccip
Submodule chainlink-ccip added at 424892
1 change: 1 addition & 0 deletions lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at b72e3d
2 changes: 2 additions & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@openzeppelin/=lib/openzeppelin-contracts/
@chainlink/=lib/chainlink/
85 changes: 85 additions & 0 deletions src/ERC1155Core.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;

import {ERC1155Supply, ERC1155} from "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";
import {OwnerIsCreator} from "@chainlink/contracts/src/v0.8/shared/access/OwnerIsCreator.sol";

/**
* THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY.
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/
contract ERC1155Core is ERC1155Supply, OwnerIsCreator {
address internal s_issuer;

// Optional mapping for token URIs
mapping(uint256 tokenId => string) private _tokenURIs;

event SetIssuer(address indexed issuer);

error ERC1155Core_CallerIsNotIssuerOrItself(address msgSender);

modifier onlyIssuerOrItself() {
if (msg.sender != address(this) && msg.sender != s_issuer) {
revert ERC1155Core_CallerIsNotIssuerOrItself(msg.sender);
}
_;
}

// Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
constructor(string memory uri_) ERC1155(uri_) {}

function setIssuer(address _issuer) external onlyOwner {
s_issuer = _issuer;

emit SetIssuer(_issuer);
}

function mint(address _to, uint256 _id, uint256 _amount, bytes memory _data, string memory _tokenUri)
public
onlyIssuerOrItself
{
_mint(_to, _id, _amount, _data);
_tokenURIs[_id] = _tokenUri;
}

function mintBatch(
address _to,
uint256[] memory _ids,
uint256[] memory _amounts,
bytes memory _data,
string[] memory _tokenUris
) public onlyIssuerOrItself {
_mintBatch(_to, _ids, _amounts, _data);
for (uint256 i = 0; i < _ids.length; ++i) {
_tokenURIs[_ids[i]] = _tokenUris[i];
}
}

function burn(address account, uint256 id, uint256 amount) public onlyIssuerOrItself {
if (account != _msgSender() && !isApprovedForAll(account, _msgSender())) {
revert ERC1155MissingApprovalForAll(_msgSender(), account);
}

_burn(account, id, amount);
}

function burnBatch(address account, uint256[] memory ids, uint256[] memory amounts) public onlyIssuerOrItself {
if (account != _msgSender() && !isApprovedForAll(account, _msgSender())) {
revert ERC1155MissingApprovalForAll(_msgSender(), account);
}

_burnBatch(account, ids, amounts);
}

function uri(uint256 tokenId) public view override returns (string memory) {
string memory tokenURI = _tokenURIs[tokenId];

return bytes(tokenURI).length > 0 ? tokenURI : super.uri(tokenId);
}

function _setURI(uint256 tokenId, string memory tokenURI) internal {
_tokenURIs[tokenId] = tokenURI;
emit URI(uri(tokenId), tokenId);
}
}

0 comments on commit 2ec10e9

Please sign in to comment.