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

Comments and cleanup #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 35 additions & 38 deletions QRC20X.sol
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,21 @@ contract QRC20 {
return true;
}
/**
* This function sends tokens to an address on another chain by creating an external transaction (ETX).

* This function uses opETX which constructs an external transaction and adds it to the block.

* The ETX will make its way over to the destination and automatically execute when the given base fee is correct.

* `to` must be an address on a different chain. The chain of a given address is determined by the first byte of the address.

* gasLimit, minerTip and basefee are for executing the transaction on the destination chain. Choose these carefully.

* The base fee and miner tip are in Wei and may not be the same as they are on your current chain.

* If the base fee or miner tip are too low, the ETX will wait in the destination chain until they are high enough to be added in a block.

* You must send a value with the function call equal to the following amount: (baseFee + minerTip) * gasLimit
*/
* @dev This function sends tokens to an address on another chain by creating an external transaction (ETX). It uses opETX
* which constructs an external transaction and adds it to the block. The ETX will make its way over to the destination and
* automatically execute when the given base fee is correct. The `to` must be an address on a different chain. The chain of a
* given address is determined by the first byte of the address. gasLimit, minerTip and basefee are for executing the
* transaction on the destination chain. Choose these carefully. The base fee and miner tip are in Wei and may not be the
* same as they are on your current chain. If the base fee or miner tip are too low, the ETX will wait in the destination
* chain until they are high enough to be added in a block. You must send a value with the function call equal to the
* following amount: (baseFee + minerTip) * gasLimit
*
* @param to The address of the recipient on the destination chain
* @param amount The amount of tokens to send
* @param gasLimit The amount of gas for execution
* @param minerTip The tip paid to the miner for execution
* @param baseFee The base fee
*/
function crossChainTransfer(address to, uint256 amount, uint256 gasLimit, uint256 minerTip, uint256 baseFee) public payable {
bool isInternal;
assembly {
Expand Down Expand Up @@ -487,28 +486,12 @@ contract QRC20 {
) internal {}

/**
* This function allows the deployer to add an external address for the token contract on a different chain.
* @dev This function allows the deployer to add external addresses for the token contract on different chains.
* Note that the deployer can only add one address per chain and this address cannot be changed afterwards.
* Be very careful when adding an address here.
*/
function AddApprovedAddress(uint8 chain, address addr) public {
bool isInternal;
assembly {
isInternal := isaddrinternal(addr)
}
require(!isInternal, "Address is not external");
require(msg.sender == _deployer, "Sender is not deployer");
require(chain < 9, "Max 9 zones");
require(ApprovedAddresses[chain] == address(0), "The approved address for this zone already exists");
ApprovedAddresses[chain] = addr;
}

/**
* This function allows the deployer to add external addresses for the token contract on different chains.
* Note that the deployer can only add one address per chain and this address cannot be changed afterwards.
* In comparison to AddApprovedAddress, this function allows the address(es) to be internal so that the same
* approved list can be used for every instance of the contract on each chain.
* Be very careful when adding addresses here.
*
* @param chain uint8 array of the chain indexes (i.e. cyprus 1 = 0, cyprus 2 = 1)
* @param addr array of the addresses to add as approved
*/
function AddApprovedAddresses(uint8[] calldata chain, address[] calldata addr) external {
require(msg.sender == _deployer, "Sender is not deployer");
Expand All @@ -520,7 +503,11 @@ contract QRC20 {
}
}

// This function uses the stored prefix list to determine an address's location based on its first byte.
/**
* @dev This function uses the stored prefix list to determine an address's location based on its first byte.
*
* @param addr address to check location of
*/
function getAddressLocation(address addr) public view returns (uint8) {
uint8 prefix = uint8(toBytes(addr)[0]);
if (ValidPrefixes[prefix]) {
Expand All @@ -529,10 +516,20 @@ contract QRC20 {
revert("Invalid Location");
}

/**
* @dev This function uses `abi.encodePacked` to encode the address into a bytes format.
*
* @param a The address to be converted to bytes.
*/
function toBytes(address a) public pure returns (bytes memory) {
return abi.encodePacked(a);
}

/**
* @notice Converts an unsigned integer to its decimal string representation.
*
* @param _i The unsigned integer to convert to a string.
*/
function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
if (_i == 0) {
return "0";
Expand All @@ -554,4 +551,4 @@ contract QRC20 {
}
return string(bstr);
}
}
}
62 changes: 34 additions & 28 deletions QRC721X.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,19 @@ contract QRC721 is IERC721Errors {
// Token symbol
string private _symbol;

// Address of the contract deployer
address private _deployer;

// Base URI for token metadata
string public baseURI;

// List of external token contracts that can send tokens to users on this chain
address[12] public ApprovedAddresses;

// Mapping of address prefix to chain location
mapping(uint8 => uint8) public PrefixToLocation;

// Mapping of valid address prefixes
mapping(uint8 => bool) public ValidPrefixes;

mapping(uint256 /*tokenId*/ => address) private _owners;
Expand Down Expand Up @@ -134,9 +141,10 @@ contract QRC721 is IERC721Errors {
/**
* @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
*/
constructor() {
_name = "Quai Cross-Chain NFT";
_symbol = "QXC";
constructor(string memory name_, string memory symbol_, string memory baseURI_) {
_name = name_;
_symbol = symbol_;
baseURI = baseURI_;
_deployer = msg.sender;
PrefixToLocation[0] = 0; // zone 0-0 // cyprus1
PrefixToLocation[1] = 1; // zone 0-1 // cyprus2
Expand Down Expand Up @@ -211,7 +219,7 @@ contract QRC721 is IERC721Errors {
* by default, can be overridden in child contracts.
*/
function _baseURI() internal pure returns (string memory) {
return "https://qu.ai/nft/";
return _baseURI;
}

/**
Expand Down Expand Up @@ -633,28 +641,12 @@ contract QRC721 is IERC721Errors {
}

/**
* This function allows the deployer to add an external address for the token contract on a different chain.
* Note that the deployer can only add one address per chain and this address cannot be changed afterwards.
* Be very careful when adding an address here.
*/
function AddApprovedAddress(uint8 chain, address addr) public {
bool isInternal;
assembly {
isInternal := isaddrinternal(addr)
}
require(!isInternal, "Address is not external");
require(msg.sender == _deployer, "Sender is not deployer");
require(chain < 9, "Max 9 zones");
require(ApprovedAddresses[chain] == address(0), "The approved address for this zone already exists");
ApprovedAddresses[chain] = addr;
}

/**
* This function allows the deployer to add external addresses for the token contract on different chains.
* Note that the deployer can only add one address per chain and this address cannot be changed afterwards.
* In comparison to AddApprovedAddress, this function allows the address(es) to be internal so that the same
* approved list can be used for every instance of the contract on each chain.
* @dev This function allows the deployer to add external addresses for the token contract on different chains.
* Note that the deployer can only add one address per shard and this address cannot be changed afterwards.
* Be very careful when adding addresses here.
*
* @param chain uint8 array of the chain indexes (i.e. cyprus 1 = 0, cyprus 2 = 1)
* @param addr array of the addresses to add as approved
*/

function AddApprovedAddresses(uint8[] calldata chain, address[] calldata addr) external {
Expand All @@ -674,7 +666,11 @@ contract QRC721 is IERC721Errors {

}

// This function uses the stored prefix list to determine an address's location based on its first byte.
/**
* @dev This function uses the stored prefix list to determine an address's location based on its first byte.
*
* @param addr address to check location of
*/
function getAddressLocation(address addr) public view returns (uint8) {
uint8 prefix = uint8(toBytes(addr)[0]);
if (ValidPrefixes[prefix]) {
Expand All @@ -683,11 +679,19 @@ contract QRC721 is IERC721Errors {
revert("Invalid Location");
}

/**
* @dev This function uses `abi.encodePacked` to encode the address into a bytes format.
*
* @param a The address to be converted to bytes.
*/
function toBytes(address a) public pure returns (bytes memory) {
return abi.encodePacked(a);
}

/**
* @dev Converts a `uint256` to its ASCII `string` decimal representation.
* @notice Converts an unsigned integer to its decimal string representation.
*
* @param value The unsigned integer to convert to a string.
*/
function toString(uint256 value) internal pure returns (string memory) {
unchecked {
Expand All @@ -713,6 +717,8 @@ contract QRC721 is IERC721Errors {
/**
* @dev Return the log in base 10 of a positive value rounded towards zero.
* Returns 0 if given 0.
*
* @param value The unsigned integer perform the log on
*/
function log10(uint256 value) internal pure returns (uint256) {
uint256 result = 0;
Expand Down Expand Up @@ -747,4 +753,4 @@ contract QRC721 is IERC721Errors {
}
return result;
}
}
}