-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update contracts to 2024.10.01 release
- Loading branch information
Showing
2 changed files
with
98 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,88 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.0; | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
import "./Nil.sol"; | ||
pragma solidity ^0.8.9; | ||
|
||
import "./NilCurrencyBase.sol"; | ||
|
||
/** | ||
* @title NilCurrencyBase | ||
* @dev Abstract contract that provides functionality for currency processing. | ||
* Methods with "Internal" suffix are internal, which means that they can be called only from the derived contract | ||
* itself. But there are default wrapper methods that provide the account owner access to internal methods. | ||
* They are virtual, so the main contract can disable them by overriding them. Then only logic of the contract can use | ||
* internal methods. | ||
* @title Wallet | ||
* @dev Basic Wallet contract which provides functional for calling another contracts and sending tokens. | ||
* It also supports multi-currency functionality providing methods for minting and sending currency. | ||
* NilCurrencyBase class implements functional for managing own currency(where `currencyId = address(this)`). | ||
*/ | ||
abstract contract NilCurrencyBase is NilBase { | ||
uint totalSupply; | ||
string tokenName; | ||
|
||
/** | ||
* @dev Returns the total supply of the currency. | ||
* @return The total supply of the currency. | ||
*/ | ||
function getCurrencyTotalSupply() public view returns(uint) { | ||
return totalSupply; | ||
} | ||
|
||
/** | ||
* @dev Returns the balance of the currency owned by this contract. | ||
* @return The balance of the currency owned by this contract. | ||
*/ | ||
function getOwnCurrencyBalance() public view returns(uint256) { | ||
return Nil.currencyBalance(address(this), getCurrencyId()); | ||
} | ||
contract Wallet is NilCurrencyBase { | ||
|
||
/** | ||
* @dev Returns the unique identifier of the currency owned by this contract. | ||
* @return The unique identifier of the currency owned by this contract. | ||
*/ | ||
function getCurrencyId() public view returns(uint256) { | ||
return uint256(uint160(address(this))); | ||
} | ||
bytes pubkey; | ||
|
||
/** | ||
* @dev Returns the name of the currency. | ||
* @return The name of the currency. | ||
* @dev Fallback function to receive Ether. | ||
*/ | ||
function getCurrencyName() public view returns(string memory) { | ||
return tokenName; | ||
} | ||
receive() external payable {} | ||
|
||
/** | ||
* @dev Set the name of the currency. | ||
* @param name The name of the currency. | ||
* @dev Function to handle bounce messages. | ||
* @param err The error message. | ||
*/ | ||
function setCurrencyName(string memory name) onlyExternal virtual public { | ||
tokenName = name; | ||
} | ||
function bounce(string calldata err) external payable {} | ||
|
||
/** | ||
* @dev Mints a specified amount of currency using external call. | ||
* It is wrapper over `mintCurrencyInternal` method to provide access to the owner of the account. | ||
* @param amount The amount of currency to mint. | ||
* @dev Constructor to initialize the wallet with a public key. | ||
* @param _pubkey The public key to initialize the wallet with. | ||
*/ | ||
function mintCurrency(uint256 amount) onlyExternal virtual public { | ||
mintCurrencyInternal(amount); | ||
constructor(bytes memory _pubkey) payable { | ||
pubkey = _pubkey; | ||
} | ||
|
||
/** | ||
* @dev Sends a specified amount of arbitrary currency to a given address. | ||
* It is wrapper over `sendCurrencyInternal` method to provide access to the owner of the account. | ||
* @param amount The amount of currency to mint. | ||
* @dev Sends raw message. | ||
* @param message The raw message to send. | ||
*/ | ||
function sendCurrency(address to, uint256 currencyId, uint256 amount) onlyExternal virtual public { | ||
sendCurrencyInternal(to, currencyId, amount); | ||
function send(bytes calldata message) onlyExternal public { | ||
Nil.sendMessage(message); | ||
} | ||
|
||
/** | ||
* @dev Mints a specified amount of currency and increases the total supply. | ||
* All minting should be carried out using this method. | ||
* @param amount The amount of currency to mint. | ||
* @dev Makes an asynchronous call. | ||
* @param dst The destination address. | ||
* @param refundTo The address where to send refund message. | ||
* @param bounceTo The address where to send bounce message. | ||
* @param feeCredit The amount of tokens available to pay all fees during message processing. | ||
* @param deploy Whether to deploy the contract. | ||
* @param tokens The multi-currency tokens to send. | ||
* @param value The value to send. | ||
* @param callData The call data of the called method. | ||
*/ | ||
function mintCurrencyInternal(uint256 amount) internal { | ||
bool success = __Precompile__(Nil.MINT_CURRENCY).precompileMintCurrency(amount); | ||
require(success, "Mint failed"); | ||
totalSupply += amount; | ||
function asyncCall( | ||
address dst, | ||
address refundTo, | ||
address bounceTo, | ||
uint feeCredit, | ||
bool deploy, | ||
Nil.Token[] memory tokens, | ||
uint value, | ||
bytes calldata callData) onlyExternal public { | ||
Nil.asyncCallWithTokens(dst, refundTo, bounceTo, feeCredit, Nil.FORWARD_NONE, deploy, value, tokens, callData); | ||
} | ||
|
||
/** | ||
* @dev Sends a specified amount of arbitrary currency to a given address. | ||
* @param to The address to send the currency to. | ||
* @param currencyId ID of the currency to send. | ||
* @param amount The amount of currency to send. | ||
* @dev Makes a synchronous call, which is just a regular EVM call, without using messages. | ||
* @param dst The destination address. | ||
* @param feeCredit The amount of tokens available to pay all fees during message processing. | ||
* @param value The value to send. | ||
* @param call_data The call data of the called method. | ||
*/ | ||
function sendCurrencyInternal(address to, uint256 currencyId, uint256 amount) internal { | ||
Nil.Token[] memory tokens_ = new Nil.Token[](1); | ||
tokens_[0] = Nil.Token(currencyId, amount); | ||
Nil.asyncCall(to, address(0), address(0), 0, Nil.FORWARD_REMAINING, false, 0, tokens_, ""); | ||
function syncCall(address dst, uint feeCredit, uint value, bytes memory call_data) onlyExternal public { | ||
(bool success,) = dst.call{value: value, gas: feeCredit}(call_data); | ||
require(success, "Call failed"); | ||
} | ||
|
||
/** | ||
* @dev Returns the balance of the currency for a given address. | ||
* @param account The address to check the balance for. | ||
* @return The balance of the currency for the given address. | ||
* @dev Verifies an external message. | ||
* @param hash The hash of the data. | ||
* @param signature The signature to verify. | ||
* @return True if the signature is valid, false otherwise. | ||
*/ | ||
function getCurrencyBalanceOf(address account) public view returns(uint256) { | ||
return Nil.currencyBalance(account, getCurrencyId()); | ||
function verifyExternal(uint256 hash, bytes calldata signature) external view returns (bool) { | ||
return Nil.validateSignature(pubkey, hash, signature); | ||
} | ||
} |