Skip to content

Commit

Permalink
add missing files on bbgo contract folder
Browse files Browse the repository at this point in the history
  • Loading branch information
zenix authored and zenixls2 committed Jan 26, 2022
1 parent a29198f commit 3717148
Show file tree
Hide file tree
Showing 22 changed files with 33,071 additions and 16 deletions.
17 changes: 17 additions & 0 deletions contracts/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
parser: 'babel-eslint',
extends: 'standard',
env: {
node: true,
es6: true,
mocha: true
},
rules: {
'space-before-function-paren': ['error', 'never']
},
globals: {
contract: true,
web3: true,
assert: true
}
}
8 changes: 8 additions & 0 deletions contracts/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "solhint:recommended",
"plugins": [],
"rules": {
"compiler-version": ["error", ">=0.6.6"],
"reason-string": ["warn", {"maxLength": 64}]
}
}
46 changes: 45 additions & 1 deletion contracts/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,51 @@
# BBG Contracts
------------

### 1. Before Start

truffle migrate --network polygon
Create and modify the following files in this directory, the secret key inside the files are dummy ones from truffle dev server:
- development-secret.json
- polygon-secret.json
- bsc-secret.json

### 2. Prepare the dependencies

```bash
npm i
# if you want to develope in localhost, try to run npm run devserver separately
# ex: npm run devserver
# it will give you a set of secrets and account addresses
```

### 3. Deploy

Migrate:
```bash
npm run migrate:dev
# npm run migrate:polygon
# npm run migrate:polygon-test
# npm run migrate:bsc
# npm run migrate:bsc-test
```

Lint:
```bash
npm run lint
# # fix solidity issue
# npm run lint:sol:fix
# # fix js issue
# npm run lint:js:fix
```

Test:
```bash
npm run test
```

```bash
truffle run verify ChildMintableERC20 --network polygon
```

```bash
truffle run verify ChildMintableERC20@0x3Afe98235d680e8d7A52e1458a59D60f45F935C0 --network polygon
```
4 changes: 4 additions & 0 deletions contracts/bsc-secret.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"privateKey": "3899a918953e01bfe218116cdfeccbed579e26275c4a89abcbc70d2cb9e9bbb8",
"etherScanApiKey": ""
}
1 change: 1 addition & 0 deletions contracts/contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;


contract Migrations {
address public owner = msg.sender;
uint public last_completed_migration;
Expand Down
79 changes: 79 additions & 0 deletions contracts/contracts/child/ChildToken/ChildMintableERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
pragma solidity 0.6.6;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {AccessControlMixin} from "../../common/AccessControlMixin.sol";
import {IChildToken} from "./IChildToken.sol";
import {NativeMetaTransaction} from "../../common/NativeMetaTransaction.sol";
import {ContextMixin} from "../../common/ContextMixin.sol";


contract ChildMintableERC20 is
ERC20,
IChildToken,
AccessControlMixin,
NativeMetaTransaction,
ContextMixin
{
bytes32 public constant DEPOSITOR_ROLE = keccak256("DEPOSITOR_ROLE");

constructor(
string memory name_,
string memory symbol_,
uint8 decimals_,
address childChainManager
) public ERC20(name_, symbol_) {
_setupContractId("ChildMintableERC20");
_setupDecimals(decimals_);
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender());
_setupRole(DEPOSITOR_ROLE, childChainManager);
_initializeEIP712(name_);
}

// This is to support Native meta transactions
// never use msg.sender directly, use _msgSender() instead
function _msgSender()
internal
override
view
returns (address payable sender)
{
return ContextMixin.msgSender();
}

/**
* @notice called when token is deposited on root chain
* @dev Should be callable only by ChildChainManager
* Should handle deposit by minting the required amount for user
* Make sure minting is done only by this function
* @param user user address for whom deposit is being done
* @param depositData abi encoded amount
*/
function deposit(address user, bytes calldata depositData)
external
override
only(DEPOSITOR_ROLE)
{
uint256 amount = abi.decode(depositData, (uint256));
_mint(user, amount);
}

/**
* @notice called when user wants to withdraw tokens back to root chain
* @dev Should burn user's tokens. This transaction will be verified when exiting on root chain
* @param amount amount of tokens to withdraw
*/
function withdraw(uint256 amount) external {
_burn(_msgSender(), amount);
}

/**
* @notice Example function to handle minting tokens on matic chain
* @dev Minting can be done as per requirement,
* This implementation allows only admin to mint tokens but it can be changed as per requirement
* @param user user for whom tokens are being minted
* @param amount amount of token to mint
*/
function mint(address user, uint256 amount) public only(DEFAULT_ADMIN_ROLE) {
_mint(user, amount);
}
}
5 changes: 5 additions & 0 deletions contracts/contracts/child/ChildToken/IChildToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pragma solidity 0.6.6;

interface IChildToken {
function deposit(address user, bytes calldata depositData) external;
}
19 changes: 19 additions & 0 deletions contracts/contracts/common/AccessControlMixin.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
pragma solidity 0.6.6;

import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";


contract AccessControlMixin is AccessControl {
string private _revertMsg;
function _setupContractId(string memory contractId) internal {
_revertMsg = string(abi.encodePacked(contractId, ": INSUFFICIENT_PERMISSIONS"));
}

modifier only(bytes32 role) {
require(
hasRole(role, _msgSender()),
_revertMsg
);
_;
}
}
25 changes: 25 additions & 0 deletions contracts/contracts/common/ContextMixin.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
pragma solidity 0.6.6;

abstract contract ContextMixin {
function msgSender()
internal
view
returns (address payable sender)
{
if (msg.sender == address(this)) {
bytes memory array = msg.data;
uint256 index = msg.data.length;
/* solhint-disable no-inline-assembly */
assembly {
// Load the 32 bytes word from memory with the address on the lower 20 bytes, and mask those.
sender := and(
mload(add(array, index)),
0xffffffffffffffffffffffffffffffffffffffff
)
}
} else {
sender = msg.sender;
}
return sender;
}
}
77 changes: 77 additions & 0 deletions contracts/contracts/common/EIP712Base.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
pragma solidity 0.6.6;

import {Initializable} from "./Initializable.sol";


contract EIP712Base is Initializable {
struct EIP712Domain {
string name;
string version;
address verifyingContract;
bytes32 salt;
}

string constant public ERC712_VERSION = "1";

bytes32 internal constant EIP712_DOMAIN_TYPEHASH = keccak256(
bytes(
"EIP712Domain(string name,string version,address verifyingContract,bytes32 salt)"
)
);
bytes32 internal domainSeperator;

// supposed to be called once while initializing.
// one of the contractsa that inherits this contract follows proxy pattern
// so it is not possible to do this in a constructor
function _initializeEIP712(
string memory name
)
internal
initializer
{
_setDomainSeperator(name);
}

function _setDomainSeperator(string memory name) internal {
domainSeperator = keccak256(
abi.encode(
EIP712_DOMAIN_TYPEHASH,
keccak256(bytes(name)),
keccak256(bytes(ERC712_VERSION)),
address(this),
bytes32(getChainId())
)
);
}

function getDomainSeperator() public view returns (bytes32) {
return domainSeperator;
}

function getChainId() public pure returns (uint256) {
uint256 id;
/* solhint-disable no-inline-assembly */
assembly {
id := chainid()
}
return id;
}

/**
* Accept message hash and returns hash message in EIP712 compatible form
* So that it can be used to recover signer from signature signed using EIP712 formatted data
* https://eips.ethereum.org/EIPS/eip-712
* "\\x19" makes the encoding deterministic
* "\\x01" is the version byte to make it compatible to EIP-191
*/
function toTypedMessageHash(bytes32 messageHash)
internal
view
returns (bytes32)
{
return
keccak256(
abi.encodePacked("\x19\x01", getDomainSeperator(), messageHash)
);
}
}
12 changes: 12 additions & 0 deletions contracts/contracts/common/Initializable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pragma solidity 0.6.6;


contract Initializable {
bool public inited = false;

modifier initializer() {
require(!inited, "already inited");
_;
inited = true;
}
}
Loading

0 comments on commit 3717148

Please sign in to comment.