Skip to content

Commit

Permalink
Feature/dev 1326 mono repo setup (#50)
Browse files Browse the repository at this point in the history
* fix(deps): update dependency api to v5.0.7 (#26)

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>

chore: update yarn.lock

feat(messaging): add messaging smart contracts

feat(deps): add missing hardhat dpes

chore(git): update git ignore

refactor(contract): rename ethereum folder

chore: update .gitignore

chore: update .gitignore

chore: update .gitignore

chore: hardhat config

feat(blockchain): deploy scripts

feat(contracts): create new default starknet nft

feat(contracts): hardhat config

fix(contracts): bridge messaging

fix(contracts): bridge messaging

chore: update hardhat config

* feat(hardhat): update & configure hardhat to deploy L1 & L2

* chore(deps): fix missing dependencies & versions

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
kwiss and renovate[bot] authored Feb 8, 2023
1 parent 7d34002 commit ca684a4
Show file tree
Hide file tree
Showing 23 changed files with 5,799 additions and 423 deletions.
6 changes: 6 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
PRIVATE_KEY=
ALCHEMY_KEY=
HOSTNAME_L1=
HOSTNAME_L2=
ETHERSCAN_API_KEY=
L2_NETWORK=
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ typechain
typechain-types
cache
artifacts
*.key

starknet-artifacts
33 changes: 33 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
node_modules

#Hardhat files
cache
artifacts

#Python artifacts
.venv*
__pycache__
.pytest_cache

#Starknet plugin
starknet-artifacts

#NPM lock file
package-lock.json

#Environment variables file
.env

#deployment artifacts folder
deployment

# Yarn error file
yarn-error.log

# Apple related file
.DS_Store

#IDEs
.idea

starknet-artifacts/**/*.json
1 change: 1 addition & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
31 changes: 31 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
FROM python:3.9

ENV HOSTNAME_L1 testnet-l1
ENV HOSTNAME_L2 testnet-l2

RUN apt update -y && apt upgrade -y && apt install curl git libssl-dev libgmp3-dev -y

# Copy folder
COPY . starklane
WORKDIR starklane

# Install Python dependencies
RUN rm -rf .venv && python -m venv .venv
RUN . .venv/bin/activate
RUN python -m pip install --upgrade pip && pip install poetry && poetry install

# Install Node 16
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - && \
apt-get install -y nodejs

# Install Yarn
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list && \
apt update -y && \
apt install yarn -y

# Install Node dependencies
RUN yarn

# Build Cairo files
RUN yarn build:l2
88 changes: 88 additions & 0 deletions apps/blockchain/contracts/ethereum/Bridge.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "./interfaces/IStarknetMessaging.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

interface NFTContract is IERC721 {
function name() external view returns (string memory);

function symbol() external view returns (string memory);

function tokenURI(uint256 tokenId) external view returns (string memory);
}

contract Bridge is Ownable {
IStarknetMessaging public starknetCore;
uint256 private CLAIM_SELECTOR;
uint256 private L2ContractAddress;

constructor(address starknetCore_) {
require(
starknetCore_ != address(0),
"Gateway/invalid-starknet-core-address"
);
starknetCore = IStarknetMessaging(starknetCore_);
}

function setClaimSelector(uint256 _claimSelector) external onlyOwner {
CLAIM_SELECTOR = _claimSelector;
}

function setL2ContractAddress(uint256 _L2ContractAddress)
external
onlyOwner
{
L2ContractAddress = _L2ContractAddress;
}

function strToUint(string memory text) public pure returns (uint256 res) {
bytes32 stringInBytes32 = bytes32(bytes(text));
uint256 strLen = bytes(text).length; // TODO: cannot be above 32
require(strLen <= 32, "String cannot be longer than 32");

uint256 shift = 256 - 8 * strLen;

uint256 stringInUint256;
assembly {
stringInUint256 := shr(shift, stringInBytes32)
}
return stringInUint256;
}

function deposit(address contractAddress, uint256 tokenId) public payable {
NFTContract tokenContract = NFTContract(contractAddress);

// optimistic transfer, should revert if no approved or not owner
tokenContract.transferFrom(msg.sender, address(this), tokenId);

string memory symbol = tokenContract.symbol();
string memory name = tokenContract.name();
string memory tokenUri = tokenContract.tokenURI(tokenId);

uint256[] memory payload = new uint256[](5);

payload[0] = uint256(uint160(contractAddress)); // l1_contract_address
payload[1] = uint256(uint160(msg.sender)); // to
payload[2] = tokenId; // token_id
payload[3] = strToUint(name);
payload[4] = strToUint(symbol);
payload[5] = strToUint(tokenUri);

starknetCore.sendMessageToL2{value: msg.value}(
L2ContractAddress,
CLAIM_SELECTOR,
payload
);
}

// TO REMOVE
function forceWithdraw(address l1_contract_address, uint256 tokenId)
public
onlyOwner
{
IERC721 tokenContract = NFTContract(l1_contract_address);
tokenContract.transferFrom(address(this), msg.sender, tokenId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.8.9;

import "./IStarknetMessagingEvents.sol";

interface IStarknetMessaging is IStarknetMessagingEvents {
/**
Sends a message to an L2 contract.
This function is payable, the payed amount is the message fee.
Returns the hash of the message and the nonce of the message.
*/
function sendMessageToL2(
uint256 toAddress,
uint256 selector,
uint256[] calldata payload
) external payable returns (bytes32, uint256);

/**
Consumes a message that was sent from an L2 contract.
Returns the hash of the message.
*/
function consumeMessageFromL2(
uint256 fromAddress,
uint256[] calldata payload
) external returns (bytes32);

/**
Starts the cancellation of an L1 to L2 message.
A message can be canceled messageCancellationDelay() seconds after this function is called.
Note: This function may only be called for a message that is currently pending and the caller
must be the sender of the that message.
*/
function startL1ToL2MessageCancellation(
uint256 toAddress,
uint256 selector,
uint256[] calldata payload,
uint256 nonce
) external returns (bytes32);

/**
Cancels an L1 to L2 message, this function should be called messageCancellationDelay() seconds
after the call to startL1ToL2MessageCancellation().
Note that the message fee is not refunded.
*/
function cancelL1ToL2Message(
uint256 toAddress,
uint256 selector,
uint256[] calldata payload,
uint256 nonce
) external returns (bytes32);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: Apache-2.0.
pragma solidity ^0.8.9;

interface IStarknetMessagingEvents {
// This event needs to be compatible with the one defined in Output.sol.
event LogMessageToL1(
uint256 indexed fromAddress,
address indexed toAddress,
uint256[] payload
);

// An event that is raised when a message is sent from L1 to L2.
event LogMessageToL2(
address indexed fromAddress,
uint256 indexed toAddress,
uint256 indexed selector,
uint256[] payload,
uint256 nonce,
uint256 fee
);

// An event that is raised when a message from L2 to L1 is consumed.
event ConsumedMessageToL1(
uint256 indexed fromAddress,
address indexed toAddress,
uint256[] payload
);

// An event that is raised when a message from L1 to L2 is consumed.
event ConsumedMessageToL2(
address indexed fromAddress,
uint256 indexed toAddress,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
);

// An event that is raised when a message from L1 to L2 Cancellation is started.
event MessageToL2CancellationStarted(
address indexed fromAddress,
uint256 indexed toAddress,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
);

// An event that is raised when a message from L1 to L2 is canceled.
event MessageToL2Canceled(
address indexed fromAddress,
uint256 indexed toAddress,
uint256 indexed selector,
uint256[] payload,
uint256 nonce
);
}
Loading

0 comments on commit ca684a4

Please sign in to comment.