@@ -5,6 +5,7 @@ pragma solidity ^0.8.0;
55import "@openzeppelin/contracts/access/AccessControl.sol " ;
66import {ReentrancyGuard} from "@openzeppelin/contracts/security/ReentrancyGuard.sol " ;
77import "@openzeppelin/contracts/token/ERC20/IERC20.sol " ;
8+ import "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol " ;
89import "@openzeppelin/contracts/access/Ownable.sol " ;
910import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol " ;
1011import "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 )
0 commit comments