1
- // SPDX-License-Identifier: GPL-3.0
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity ^ 0.8.0 ;
2
3
3
- pragma solidity ^ 0.8.9 ;
4
-
5
- import "./NilCurrencyBase.sol " ;
4
+ import "./Nil.sol " ;
6
5
7
6
/**
8
- * @title Wallet
9
- * @dev Basic Wallet contract which provides functional for calling another contracts and sending tokens.
10
- * It also supports multi-currency functionality providing methods for minting and sending currency.
11
- * NilCurrencyBase class implements functional for managing own currency(where `currencyId = address(this)`).
7
+ * @title NilCurrencyBase
8
+ * @dev Abstract contract that provides functionality for currency processing.
9
+ * Methods with "Internal" suffix are internal, which means that they can be called only from the derived contract
10
+ * itself. But there are default wrapper methods that provide the account owner access to internal methods.
11
+ * They are virtual, so the main contract can disable them by overriding them. Then only logic of the contract can use
12
+ * internal methods.
12
13
*/
13
- contract Wallet is NilCurrencyBase {
14
+ abstract contract NilCurrencyBase is NilBase {
15
+ uint totalSupply;
16
+ string tokenName;
17
+
18
+ /**
19
+ * @dev Returns the total supply of the currency.
20
+ * @return The total supply of the currency.
21
+ */
22
+ function getCurrencyTotalSupply () public view returns (uint ) {
23
+ return totalSupply;
24
+ }
14
25
15
- bytes pubkey;
26
+ /**
27
+ * @dev Returns the balance of the currency owned by this contract.
28
+ * @return The balance of the currency owned by this contract.
29
+ */
30
+ function getOwnCurrencyBalance () public view returns (uint256 ) {
31
+ return Nil.currencyBalance (address (this ), getCurrencyId ());
32
+ }
16
33
17
34
/**
18
- * @dev Fallback function to receive Ether.
35
+ * @dev Returns the unique identifier of the currency owned by this contract.
36
+ * @return The unique identifier of the currency owned by this contract.
19
37
*/
20
- receive () external payable {}
38
+ function getCurrencyId () public view returns (CurrencyId) {
39
+ return CurrencyId.wrap (address (this ));
40
+ }
21
41
22
42
/**
23
- * @dev Function to handle bounce messages .
24
- * @param err The error message .
43
+ * @dev Returns the name of the currency .
44
+ * @return The name of the currency .
25
45
*/
26
- function bounce (string calldata err ) external payable {}
46
+ function getCurrencyName () public view returns (string memory ) {
47
+ return tokenName;
48
+ }
27
49
28
50
/**
29
- * @dev Constructor to initialize the wallet with a public key .
30
- * @param _pubkey The public key to initialize the wallet with .
51
+ * @dev Set the name of the currency .
52
+ * @param name The name of the currency .
31
53
*/
32
- constructor ( bytes memory _pubkey ) payable {
33
- pubkey = _pubkey ;
54
+ function setCurrencyName ( string memory name ) onlyExternal virtual public {
55
+ tokenName = name ;
34
56
}
35
57
36
58
/**
37
- * @dev Sends raw message.
38
- * @param message The raw message to send.
59
+ * @dev Mints a specified amount of currency using external call.
60
+ * It is wrapper over `mintCurrencyInternal` method to provide access to the owner of the account.
61
+ * @param amount The amount of currency to mint.
39
62
*/
40
- function send ( bytes calldata message ) onlyExternal public {
41
- Nil. sendMessage (message );
63
+ function mintCurrency ( uint256 amount ) onlyExternal virtual public {
64
+ mintCurrencyInternal (amount );
42
65
}
43
66
44
67
/**
45
- * @dev Makes an asynchronous call.
46
- * @param dst The destination address.
47
- * @param refundTo The address where to send refund message.
48
- * @param bounceTo The address where to send bounce message.
49
- * @param feeCredit The amount of tokens available to pay all fees during message processing.
50
- * @param deploy Whether to deploy the contract.
51
- * @param tokens The multi-currency tokens to send.
52
- * @param value The value to send.
53
- * @param callData The call data of the called method.
68
+ * @dev Burns a specified amount of currency using external call.
69
+ * It is wrapper over `burnCurrencyInternal` method to provide access to the owner of the account.
70
+ * @param amount The amount of currency to burn.
54
71
*/
55
- function asyncCall (
56
- address dst ,
57
- address refundTo ,
58
- address bounceTo ,
59
- uint feeCredit ,
60
- bool deploy ,
61
- Nil.Token[] memory tokens ,
62
- uint value ,
63
- bytes calldata callData ) onlyExternal public {
64
- Nil.asyncCallWithTokens (dst, refundTo, bounceTo, feeCredit, Nil.FORWARD_NONE, deploy, value, tokens, callData);
72
+ function burnCurrency (uint256 amount ) onlyExternal virtual public {
73
+ burnCurrencyInternal (amount);
65
74
}
66
75
67
76
/**
68
- * @dev Makes a synchronous call, which is just a regular EVM call, without using messages.
69
- * @param dst The destination address.
70
- * @param feeCredit The amount of tokens available to pay all fees during message processing.
71
- * @param value The value to send.
72
- * @param call_data The call data of the called method.
77
+ * @dev Sends a specified amount of arbitrary currency to a given address.
78
+ * It is wrapper over `sendCurrencyInternal` method to provide access to the owner of the account.
79
+ * @param amount The amount of currency to mint.
73
80
*/
74
- function syncCall (address dst , uint feeCredit , uint value , bytes memory call_data ) onlyExternal public {
75
- (bool success ,) = dst.call {value: value, gas: feeCredit}(call_data);
76
- require (success, "Call failed " );
81
+ function sendCurrency (address to , CurrencyId currencyId , uint256 amount ) onlyExternal virtual public {
82
+ sendCurrencyInternal (to, currencyId, amount);
77
83
}
78
84
79
85
/**
80
- * @dev Verifies an external message.
81
- * @param hash The hash of the data.
82
- * @param signature The signature to verify.
83
- * @return True if the signature is valid, false otherwise.
86
+ * @dev Mints a specified amount of currency and increases the total supply.
87
+ * All minting should be carried out using this method.
88
+ * @param amount The amount of currency to mint.
84
89
*/
85
- function verifyExternal (uint256 hash , bytes calldata signature ) external view returns (bool ) {
86
- return Nil.validateSignature (pubkey, hash, signature);
90
+ function mintCurrencyInternal (uint256 amount ) internal {
91
+ bool success = __Precompile__ (Nil.MANAGE_CURRENCY).precompileManageCurrency (amount, true );
92
+ require (success, "Mint failed " );
93
+ totalSupply += amount;
87
94
}
88
- }
95
+
96
+ /**
97
+ * @dev Burns a specified amount of currency and decreases the total supply.
98
+ * All burning should be carried out using this method.
99
+ * @param amount The amount of currency to mint.
100
+ */
101
+ function burnCurrencyInternal (uint256 amount ) internal {
102
+ require (totalSupply >= amount, "Burn failed: not enough tokens " );
103
+ bool success = __Precompile__ (Nil.MANAGE_CURRENCY).precompileManageCurrency (amount, false );
104
+ require (success, "Burn failed " );
105
+ totalSupply -= amount;
106
+ }
107
+
108
+ /**
109
+ * @dev Sends a specified amount of arbitrary currency to a given address.
110
+ * @param to The address to send the currency to.
111
+ * @param currencyId ID of the currency to send.
112
+ * @param amount The amount of currency to send.
113
+ */
114
+ function sendCurrencyInternal (address to , CurrencyId currencyId , uint256 amount ) internal {
115
+ Nil.Token[] memory tokens_ = new Nil.Token [](1 );
116
+ tokens_[0 ] = Nil.Token (currencyId, amount);
117
+ Nil.asyncCallWithTokens (to, address (0 ), address (0 ), 0 , Nil.FORWARD_REMAINING, false , 0 , tokens_, "" );
118
+ }
119
+
120
+ /**
121
+ * @dev Returns the balance of the currency for a given address.
122
+ * @param account The address to check the balance for.
123
+ * @return The balance of the currency for the given address.
124
+ */
125
+ function getCurrencyBalanceOf (address account ) public view returns (uint256 ) {
126
+ return Nil.currencyBalance (account, getCurrencyId ());
127
+ }
128
+ }
129
+
0 commit comments