Skip to content

Commit 4b03bd2

Browse files
committed
refactor: add extraData to UTXO model
Signed-off-by: MASDXI <sirawitt42@gmail.com>
1 parent cab5fc8 commit 4b03bd2

File tree

5 files changed

+78
-114
lines changed

5 files changed

+78
-114
lines changed

contracts/abstracts/UTXOToken.sol

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,15 @@ abstract contract UTXOToken is ERC20, IUTXOERC20 {
3939
* @param tokenId The identifier of the token transaction.
4040
* @param value The amount of tokens to transfer.
4141
* @param signature The signature associated with the transaction.
42+
* @param extraData The extra data for transaction output.
4243
*/
4344
function _transfer(
4445
address from,
4546
address to,
4647
bytes32 tokenId,
4748
uint256 value,
48-
bytes memory signature
49+
bytes memory signature,
50+
bytes32 extraData
4951
) internal virtual {
5052
uint256 txvalue = _UTXO.transactionValue(tokenId);
5153
if (txvalue < value) {
@@ -58,7 +60,8 @@ abstract contract UTXOToken is ERC20, IUTXOERC20 {
5860
UnspentTransactionOutput.TransactionOutput(value, to),
5961
tokenId,
6062
UnspentTransactionOutput.calculateTransactionHash(from, _UTXO.transactionCount(from)),
61-
from
63+
from,
64+
extraData
6265
);
6366
}
6467
_update(from, to, value);
@@ -68,13 +71,15 @@ abstract contract UTXOToken is ERC20, IUTXOERC20 {
6871
* @dev Internal function to mint tokens and create a transaction for the minted tokens.
6972
* @param account The address that will receive the minted tokens.
7073
* @param value The amount of tokens to mint and transfer.
74+
* @param extraData The extra data for transaction output.
7175
*/
72-
function _mintTransaction(address account, uint256 value) internal {
76+
function _mintTransaction(address account, uint256 value, bytes32 extraData) internal {
7377
_UTXO.createTransaction(
7478
UnspentTransactionOutput.TransactionOutput(value, account),
7579
bytes32(0),
7680
UnspentTransactionOutput.calculateTransactionHash(address(0), _UTXO.transactionCount(address(0))),
77-
address(0)
81+
address(0),
82+
extraData
7883
);
7984
_mint(account, value);
8085
}
@@ -84,8 +89,9 @@ abstract contract UTXOToken is ERC20, IUTXOERC20 {
8489
* @param account The address from which tokens will be burned.
8590
* @param tokenId The identifier of the token transaction to be burned.
8691
* @param value The amount of tokens to burn.
92+
* @param extraData The extra data for transaction output.
8793
*/
88-
function _burnTransaction(address account, bytes32 tokenId, uint256 value) internal {
94+
function _burnTransaction(address account, bytes32 tokenId, uint256 value, bytes32 extraData) internal {
8995
if (value == _UTXO.transactionValue(tokenId)) {
9096
_UTXO.consumeTransaction(tokenId, account);
9197
} else {
@@ -94,7 +100,8 @@ abstract contract UTXOToken is ERC20, IUTXOERC20 {
94100
UnspentTransactionOutput.TransactionOutput(value, account),
95101
tokenId,
96102
UnspentTransactionOutput.calculateTransactionHash(account, _UTXO.transactionCount(account)),
97-
account
103+
account,
104+
extraData
98105
);
99106
}
100107
_burn(account, value);
@@ -145,6 +152,15 @@ abstract contract UTXOToken is ERC20, IUTXOERC20 {
145152
return _UTXO.transactionOwner(tokenId);
146153
}
147154

155+
/**
156+
* @dev Function to fetch the extra data of a UTXO transaction identified by its token ID.
157+
* @param tokenId The identifier of the UTXO transaction.
158+
* @return The extra data of the UTXO associated with the specified token ID.
159+
*/
160+
function transactionExtraData(bytes32 tokenId) public view returns (bytes32) {
161+
return _UTXO.transactionExtraData(tokenId);
162+
}
163+
148164
/**
149165
* @dev Function to checks whether a UTXO transaction has been spent, identified by its token ID.
150166
* @param tokenId The identifier of the UTXO transaction.
@@ -170,7 +186,7 @@ abstract contract UTXOToken is ERC20, IUTXOERC20 {
170186
uint256 value,
171187
bytes memory signature
172188
) public virtual override returns (bool) {
173-
_transfer(msg.sender, to, tokenId, value, signature);
189+
_transfer(msg.sender, to, tokenId, value, signature, bytes32(""));
174190
return true;
175191
}
176192

@@ -192,7 +208,7 @@ abstract contract UTXOToken is ERC20, IUTXOERC20 {
192208
bytes memory signature
193209
) public virtual override returns (bool) {
194210
_spendAllowance(from, msg.sender, value);
195-
_transfer(from, to, tokenId, value, signature);
211+
_transfer(from, to, tokenId, value, signature, bytes32(""));
196212
return true;
197213
}
198214
}

contracts/libraries/UTXO.sol

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ library UnspentTransactionOutput {
235235
return self.transactions[id].owner;
236236
}
237237

238+
/**
239+
* @notice Retrieves the owner of a transaction in the UTXO.
240+
* @param self The UTXO storage.
241+
* @param id The identifier of the transaction.
242+
* @return The extraData of the transaction.
243+
*/
244+
function transactionExtraData(UTXO storage self, bytes32 id) internal view returns (bytes32) {
245+
return self.transactions[id].extraData;
246+
}
247+
238248
/**
239249
* @notice Retrieves the number of transactions associated with an account in the UTXO.
240250
* @param self The UTXO storage.

contracts/mocks/MockForest.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,25 @@ contract MockForest is ForestToken, FreezeAddress, FreezeBalance, FreezeToken {
2222

2323
// TODO
2424
// modifier checkFrozenLevel(bytes32 tokenId) {
25-
// check equal
25+
// check root equal check equal
2626
// _;
2727
// }
2828

2929
// TODO
3030
// modifier checkFrozenBeforeLevel(bytes32 tokenId)
31-
// check less than
31+
// check root equal and check less than
3232
// _;
3333
// }
3434

3535
// TODO
3636
// modifier checkFrozenAfterLevel(bytes32 tokenId)
37-
// check greater than
37+
// check root equal check greater than
3838
// _;
3939
// }
4040

4141
// TODO
4242
// modifier checkFrozenInBetweenLevel(bytes32 tokenId)
43-
// check greater than 'x' and less than 'y'
43+
// check root equal check greater than 'x' and less than 'y'
4444
// _;
4545
// }
4646

contracts/mocks/MockUtxo.sol

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,63 @@ import "../abstracts/extensions/FreezeToken.sol";
99
contract MockUtxo is UTXOToken, FreezeAddress, FreezeBalance, FreezeToken {
1010
constructor(string memory name_, string memory symbol_) UTXOToken(name_, symbol_) {}
1111

12-
function _transfer(
12+
function transfer(
13+
address to,
14+
bytes32 tokenId,
15+
uint256 value,
16+
bytes memory signature
17+
)
18+
public
19+
virtual
20+
override
21+
checkFrozenBalance(msg.sender, balanceOf(msg.sender))
22+
checkFrozenAddress(msg.sender, to)
23+
checkFrozenToken(tokenId)
24+
returns (bool)
25+
{
26+
bytes32 extraData = transactionExtraData(tokenId);
27+
if (extraData == bytes32("")) {
28+
extraData = tokenId;
29+
}
30+
_transfer(msg.sender, to, tokenId, value, signature, extraData);
31+
32+
return true;
33+
}
34+
35+
function transferFrom(
1336
address from,
1437
address to,
1538
bytes32 tokenId,
1639
uint256 value,
1740
bytes memory signature
1841
)
19-
internal
42+
public
2043
virtual
2144
override
2245
checkFrozenBalance(from, balanceOf(from))
2346
checkFrozenAddress(from, to)
2447
checkFrozenToken(tokenId)
48+
returns (bool)
2549
{
26-
super._transfer(from, to, tokenId, value, signature);
50+
_spendAllowance(from, msg.sender, value);
51+
bytes32 extraData = transactionExtraData(tokenId);
52+
if (extraData == bytes32("")) {
53+
extraData = tokenId;
54+
}
55+
_transfer(msg.sender, to, tokenId, value, signature, extraData);
56+
57+
return true;
2758
}
2859

2960
function mint(address account, uint256 value) public {
30-
_mintTransaction(account, value);
61+
_mintTransaction(account, value, bytes32(""));
3162
}
3263

3364
function burn(address account, bytes32 tokenId, uint256 value) public {
34-
_burnTransaction(account, tokenId, value);
65+
bytes32 extraData = transactionExtraData(tokenId);
66+
if (extraData == bytes32("")) {
67+
extraData = tokenId;
68+
}
69+
_burnTransaction(account, tokenId, value, extraData);
3570
}
3671
}

test/contracts/UTXO/eUTXO.test.js

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)