Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
wangjj9219 committed Nov 2, 2023
1 parent 688bef4 commit ebd99df
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 31 deletions.
81 changes: 62 additions & 19 deletions contracts/token/MultiCurrency.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,111 @@ pragma solidity ^0.8.0;
/// @dev This contracts will interact with currencies pallet
library MultiCurrency {
/// @dev The MultiCurrency precompile address.
address private constant PRECOMPILE = address(0x0000000000000000000000000000000000000400);
address private constant PRECOMPILE =
address(0x0000000000000000000000000000000000000400);

function name() internal view returns (string memory) {
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(abi.encodeWithSignature("name()"));
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(
abi.encodeWithSignature("name()")
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

return abi.decode(returnData, (string));
}

function symbol() internal view returns (string memory) {
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(abi.encodeWithSignature("symbol()"));
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(
abi.encodeWithSignature("symbol()")
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

return abi.decode(returnData, (string));
}

function decimals() internal view returns (uint8) {
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(abi.encodeWithSignature("decimals()"));
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(
abi.encodeWithSignature("decimals()")
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

return abi.decode(returnData, (uint8));
}

function totalSupply() internal view returns (uint256) {
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(abi.encodeWithSignature("totalSupply()"));
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(
abi.encodeWithSignature("totalSupply()")
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

return abi.decode(returnData, (uint256));
}

function balanceOf(address account) internal view returns (uint256) {
(bool success, bytes memory returnData) =
PRECOMPILE.staticcall(abi.encodeWithSignature("balanceOf(address)", account));
(bool success, bytes memory returnData) = PRECOMPILE.staticcall(
abi.encodeWithSignature("balanceOf(address)", account)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}

return abi.decode(returnData, (uint256));
}

function transfer(address sender, address recipient, uint256 amount) internal {
(bool success, bytes memory returnData) =
PRECOMPILE.call(abi.encodeWithSignature("transfer(address,address,uint256)", sender, recipient, amount));
function transfer(
address sender,
address recipient,
uint256 amount
) internal {
(bool success, bytes memory returnData) = PRECOMPILE.call(
abi.encodeWithSignature(
"transfer(address,address,uint256)",
sender,
recipient,
amount
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}
}

function transferToAccountId(address sender, bytes32 recipient, uint256 amount) internal {
function transferToAccountId(
address sender,
bytes32 recipient,
uint256 amount
) internal {
(bool success, bytes memory returnData) = PRECOMPILE.call(
abi.encodeWithSignature("transferToAccountId(address,bytes32,uint256)", sender, recipient, amount)
abi.encodeWithSignature(
"transferToAccountId(address,bytes32,uint256)",
sender,
recipient,
amount
)
);
assembly {
if eq(success, 0) { revert(add(returnData, 0x20), returndatasize()) }
if eq(success, 0) {
revert(add(returnData, 0x20), returndatasize())
}
}
}
}
56 changes: 44 additions & 12 deletions contracts/token/Token.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ contract Token is IERC20 {
/// @param to The dest address, it cannot be the zero address.
/// @param amount The transfer amount.
/// @return Returns a boolean value indicating whether the operation succeeded.
function transfer(address to, uint256 amount) public override returns (bool) {
function transfer(
address to,
uint256 amount
) public override returns (bool) {
address owner = msg.sender;
_transfer(owner, to, amount);
return true;
Expand All @@ -73,7 +76,10 @@ contract Token is IERC20 {
/// @param spender The spender address.
/// @return Returns the remaining number of tokens.
/// The `spender` will be allowed to spend on behalf of `owner` through {transferFrom}. This is zero by default.
function allowance(address owner, address spender) public view override returns (uint256) {
function allowance(
address owner,
address spender
) public view override returns (uint256) {
return _allowances[owner][spender];
}

Expand All @@ -84,7 +90,10 @@ contract Token is IERC20 {
/// @param spender Approve the spender.
/// @param amount The approve amount.
/// @return Returns a boolean value indicating whether the operation succeeded.
function approve(address spender, uint256 amount) public override returns (bool) {
function approve(
address spender,
uint256 amount
) public override returns (bool) {
address owner = msg.sender;
_approve(owner, spender, amount);
return true;
Expand All @@ -98,7 +107,11 @@ contract Token is IERC20 {
/// @param to Transfer amount to the address. It cannot be the zero address.
/// @param amount The transfer amount.
/// @return Returns a boolean value indicating whether the operation succeeded.
function transferFrom(address from, address to, uint256 amount) public override returns (bool) {
function transferFrom(
address from,
address to,
uint256 amount
) public override returns (bool) {
address spender = msg.sender;
_spendAllowance(from, spender, amount);
_transfer(from, to, amount);
Expand All @@ -111,7 +124,10 @@ contract Token is IERC20 {
/// @param spender It cannot be the zero address.
/// @param addedValue The added value.
/// @return Returns a boolean value indicating whether the operation succeeded.
function increaseAllowance(address spender, uint256 addedValue) public returns (bool) {
function increaseAllowance(
address spender,
uint256 addedValue
) public returns (bool) {
address owner = msg.sender;
_approve(owner, spender, _allowances[owner][spender] + addedValue);
return true;
Expand All @@ -123,10 +139,16 @@ contract Token is IERC20 {
/// @param spender must have allowance for the caller of at least `subtractedValue`. It cannot be the zero address.
/// @param subtractedValue The subtracted value.
/// @return Returns a boolean value indicating whether the operation succeeded.
function decreaseAllowance(address spender, uint256 subtractedValue) public returns (bool) {
function decreaseAllowance(
address spender,
uint256 subtractedValue
) public returns (bool) {
address owner = msg.sender;
uint256 currentAllowance = _allowances[owner][spender];
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
require(
currentAllowance >= subtractedValue,
"ERC20: decreased allowance below zero"
);
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
Expand All @@ -139,11 +161,14 @@ contract Token is IERC20 {
/// @param dest The dest AccountId32 type account, it cannot be the zero AccountId32.
/// @param amount The transfer amount.
/// @return Returns a boolean value indicating whether the operation succeeded.
function transferToAccountId32(bytes32 dest, uint256 amount) public returns (bool) {
function transferToAccountId32(
bytes32 dest,
uint256 amount
) public returns (bool) {
address from = msg.sender;
require(from != address(0), "ERC20: transfer from the zero address");
require(
dest != 0x0000000000000000000000000000000000000000000000000000000000000000,
dest !=
0x0000000000000000000000000000000000000000000000000000000000000000,
"ERC20: transfer to the zero AccountId32"
);

Expand Down Expand Up @@ -186,10 +211,17 @@ contract Token is IERC20 {
/// Does not update the allowance amount in case of infinite allowance.
/// Revert if not enough allowance is available.
/// Might emit an {Approval} event.
function _spendAllowance(address owner, address spender, uint256 amount) internal {
function _spendAllowance(
address owner,
address spender,
uint256 amount
) internal {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
require(
currentAllowance >= amount,
"ERC20: insufficient allowance"
);
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
Expand Down

0 comments on commit ebd99df

Please sign in to comment.