Skip to content

Commit 8abd4d6

Browse files
author
Ho
committed
catching up with new master
1 parent 0b97fae commit 8abd4d6

File tree

6 files changed

+35
-32
lines changed

6 files changed

+35
-32
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ contract-deployed.env
1313
ganache/
1414

1515
# secrets
16-
/secrets.json
16+
/secrets.json
17+
accounts.json
18+
deployed.json

contracts/Events.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ pragma solidity ^0.8.0;
55
interface Events {
66
event NewToken(address submitter, address tokenAddr, uint16 tokenId);
77
event RegisterUser(address ethAddr, uint16 userId, bytes32 bjjPubkey);
8-
event Deposit(uint16 tokenId, bytes32 to, uint256 amount);
8+
event Deposit(uint16 tokenId, bytes32 to, uint128 amount);
99
}

contracts/FluiDex.sol

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pragma solidity ^0.8.0;
55
import "@openzeppelin/contracts/access/AccessControl.sol";
66
import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol";
77
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
8+
import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
89
import "@openzeppelin/contracts/access/Ownable.sol";
910
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
1011
import "hardhat/console.sol"; // for debugging
@@ -79,9 +80,10 @@ contract FluiDexDemo is
7980
/**
8081
* @notice request to add a new ERC20 token
8182
* @param tokenAddr the ERC20 token address
83+
* @param prec specify the precise inside fluidex
8284
* @return the new ERC20 token tokenId
8385
*/
84-
function addToken(address tokenAddr)
86+
function addToken(address tokenAddr, uint8 prec)
8587
external
8688
override
8789
nonReentrant
@@ -94,12 +96,11 @@ contract FluiDexDemo is
9496

9597
uint16 tokenId = tokenNum;
9698
tokenIdToAddr[tokenId] = tokenAddr;
97-
tokenAddrToId[tokenAddr] = tokenId;
99+
tokenAddrToId[tokenAddr] = prec;
98100

99-
//TODO: should provide token's prec and check token's decimal
100-
tokenScales[tokenId] = 12;
101-
102-
emit NewToken(origin, tokenAddr, tokenId);
101+
uint8 tokenDecimal = IERC20Metadata(tokenAddr).decimals();
102+
require(tokenDecimal >= prec, "prec can not exceed token's decimal");
103+
tokenScales[tokenId] = tokenDecimal - prec;
103104
return tokenId;
104105
}
105106

@@ -111,27 +112,25 @@ contract FluiDexDemo is
111112
payable
112113
override
113114
onlyRole(DELEGATE_ROLE)
115+
returns (uint128 realAmount)
114116
{
115117
uint16 accountId = userBjjPubkeyToUserId[to];
116118
require(accountId != 0, "non-existed user");
117-
uint128 amount = Operations.scaleTokenValueToAmount(
119+
realAmount = Operations.scaleTokenValueToAmount(
118120
msg.value,
119121
tokenScales[0]
120122
);
121123

122124
Operations.Deposit memory op = Operations.Deposit({
123125
accountId: accountId,
124126
tokenId: 0,
125-
amount: amount
127+
amount: realAmount
126128
});
127129

128130
addPriorityRequest(
129131
Operations.OpType.Deposit,
130132
Operations.writeDepositPubdataForPriorityQueue(op)
131133
);
132-
133-
//TODO: correct the value to scaled amount?
134-
emit Deposit(ETH_ID, to, msg.value);
135134
}
136135

137136
/**
@@ -143,34 +142,31 @@ contract FluiDexDemo is
143142
nonReentrant
144143
tokenExist(token)
145144
onlyRole(DELEGATE_ROLE)
146-
returns (uint16 tokenId, uint256 realAmount)
145+
returns (uint16 tokenId, uint128 realAmount)
147146
{
148147
tokenId = tokenAddrToId[address(token)];
149148

150149
uint256 balanceBeforeDeposit = token.balanceOf(address(this));
151150
token.safeTransferFrom(msg.sender, address(this), amount);
152151
uint256 balanceAfterDeposit = token.balanceOf(address(this));
153-
uint256 realAmount = balanceAfterDeposit - balanceBeforeDeposit;
154152

155153
uint16 accountId = userBjjPubkeyToUserId[to];
156154
require(accountId != 0, "non-existed user");
157-
uint128 scaledAmount = Operations.scaleTokenValueToAmount(
158-
amount,
155+
realAmount = Operations.scaleTokenValueToAmount(
156+
balanceAfterDeposit - balanceBeforeDeposit,
159157
tokenScales[tokenId]
160158
);
161159

162160
Operations.Deposit memory op = Operations.Deposit({
163161
accountId: accountId,
164162
tokenId: tokenId,
165-
amount: scaledAmount
163+
amount: realAmount
166164
});
167165

168166
addPriorityRequest(
169167
Operations.OpType.Deposit,
170168
Operations.writeDepositPubdataForPriorityQueue(op)
171169
);
172-
173-
emit Deposit(tokenId, to, realAmount);
174170
}
175171

176172
/**
@@ -204,8 +200,6 @@ contract FluiDexDemo is
204200
Operations.OpType.Registry,
205201
Operations.writeRegistryPubdataForPriorityQueue(op)
206202
);
207-
208-
emit RegisterUser(ethAddr, userId, bjjPubkey);
209203
}
210204

211205
function getBlockStateByBlockId(uint256 _block_id)

contracts/FluiDexDelegate.sol

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,16 @@ contract FluiDexDelegate is
4747
/**
4848
* @notice request to add a new ERC20 token
4949
* @param tokenAddr the ERC20 token address
50+
* @param prec specify the precise inside fluidex
5051
* @return tokenId the new ERC20 token tokenId
5152
*/
52-
function addToken(address tokenAddr)
53+
function addToken(address tokenAddr, uint8 prec)
5354
external
5455
override
5556
onlyRole(TOKEN_ADMIN_ROLE)
5657
returns (uint16 tokenId)
5758
{
58-
tokenId = target.addToken(tokenAddr);
59+
tokenId = target.addToken(tokenAddr, prec);
5960
emit NewToken(msg.sender, tokenAddr, tokenId);
6061
}
6162

@@ -68,8 +69,8 @@ contract FluiDexDelegate is
6869
override
6970
orCreateUser(msg.sender, to)
7071
{
71-
target.depositETH{value: msg.value}(to);
72-
emit Deposit(ETH_ID, to, msg.value);
72+
uint128 finalAmount = target.depositETH{value: msg.value}(to);
73+
emit Deposit(ETH_ID, to, finalAmount);
7374
}
7475

7576
/**
@@ -87,8 +88,9 @@ contract FluiDexDelegate is
8788
uint256 realAmount = balanceAfterDeposit - balanceBeforeDeposit;
8889
token.safeIncreaseAllowance(address(target), realAmount);
8990

90-
(uint16 tokenId, uint256 finalAmount) = target.depositERC20(
91+
(uint16 tokenId, uint128 finalAmount) = target.depositERC20(
9192
token,
93+
to,
9294
realAmount
9395
);
9496
emit Deposit(tokenId, to, finalAmount);

contracts/IFluiDex.sol

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@ interface IFluiDex {
1919
/**
2020
* @notice request to add a new ERC20 token
2121
* @param tokenAddr the ERC20 token address
22+
* @param prec specify the precise inside fluidex
2223
* @return the new ERC20 token tokenId
2324
*/
24-
function addToken(address tokenAddr) external returns (uint16);
25+
function addToken(address tokenAddr, uint8 prec) external returns (uint16);
2526

2627
/**
2728
* @param to the L2 address (bjjPubkey) of the deposit target.
2829
*/
29-
function depositETH(bytes32 to) external payable;
30+
function depositETH(bytes32 to)
31+
external
32+
payable
33+
returns (uint128 realAmount);
3034

3135
/**
3236
* @param amount the deposit amount.
3337
*/
34-
function depositERC20(IERC20 token, uint256 amount)
38+
function depositERC20(IERC20 token, bytes32 to, uint256 amount)
3539
external
36-
returns (uint16 tokenId, uint256 realAmount);
40+
returns (uint16 tokenId, uint128 realAmount);
3741

3842
function getBlockStateByBlockId(uint256 _block_id)
3943
external

contracts/IFluiDexDelegate.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ interface IFluiDexDelegate {
88
/**
99
* @notice request to add a new ERC20 token
1010
* @param tokenAddr the ERC20 token address
11+
* @param prec specify the precise inside fluidex
1112
* @return the new ERC20 token tokenId
1213
*/
13-
function addToken(address tokenAddr) external returns (uint16);
14+
function addToken(address tokenAddr, uint8 prec) external returns (uint16);
1415

1516
/**
1617
* @param to the L2 address (bjjPubkey) of the deposit target.

0 commit comments

Comments
 (0)