@@ -14,7 +14,7 @@ contract MainChainGateway is OracleManagerContract {
1414
1515 event TRXReceived (address from , uint64 value , uint256 nonce );
1616 event TRC10Received (address from , uint64 tokenId , uint64 tokenValue , uint256 nonce );
17- event TRC20Received (address from , address contractAddress , uint64 value , uint256 nonce );
17+ event TRC20Received (address from , address contractAddress , uint256 value , uint256 nonce );
1818 event TRC721Received (address from , address contractAddress , uint256 uid , uint256 nonce );
1919 event TRC20Mapping (address contractAddress , uint256 nonce );
2020 event TRC721Mapping (address contractAddress , uint256 nonce );
@@ -41,6 +41,9 @@ contract MainChainGateway is OracleManagerContract {
4141 MappingMsg[] userMappingList;
4242 uint256 uint64Max = 18446744073709551615 ;
4343
44+ address payable public refGatewayAddress;
45+ uint256 public nonceBaseValue = 100000000000000000000 ; // 1*(10**20)
46+
4447 struct DepositMsg {
4548 address user;
4649 uint64 value;
@@ -128,9 +131,30 @@ contract MainChainGateway is OracleManagerContract {
128131
129132 // Approve and Deposit function for 2-step deposits
130133 // Requires first to have called `approve` on the specified TRC20 contract
134+ // version2 depositTRC20 function with uint256 as input value
135+ function depositTRC20 (address contractAddress , uint256 value ) payable
136+ public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256 ) {
137+ require (isMapAddrInGateways (contractAddress), "not an allowed token " );
138+ require (value >= depositMinTrc20, "value must be >= depositMinTrc20 " );
139+ require (msg .value >= depositFee, "msg.value need >= depositFee " );
140+ if (msg .value > depositFee) {
141+ msg .sender .transfer (msg .value - depositFee);
142+ }
143+ bonus += depositFee;
144+
145+ if (! TRC20 (contractAddress).transferFrom (msg .sender , address (this ), value)) {
146+ revert ("TRC20 transferFrom error " );
147+ }
148+ userDepositList.push (DepositMsg (msg .sender , 0 , 2 , contractAddress, 0 , 0 , value));
149+ emit TRC20Received (msg .sender , contractAddress, value, nonceBaseValue + userDepositList.length - 1 );
150+ return nonceBaseValue + userDepositList.length - 1 ;
151+
152+ }
153+
154+ // version1 depositTRC20 function with uint64 as input value
131155 function depositTRC20 (address contractAddress , uint64 value ) payable
132156 public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256 ) {
133- require (mainToSideContractMap[ contractAddress] == 1 , "not an allowed token " );
157+ require (isMapAddrInGateways ( contractAddress) , "not an allowed token " );
134158 require (value >= depositMinTrc20, "value must be >= depositMinTrc20 " );
135159 require (msg .value >= depositFee, "msg.value need >= depositFee " );
136160 if (msg .value > depositFee) {
@@ -141,27 +165,37 @@ contract MainChainGateway is OracleManagerContract {
141165 if (! TRC20 (contractAddress).transferFrom (msg .sender , address (this ), value)) {
142166 revert ("TRC20 transferFrom error " );
143167 }
144- userDepositList.push (DepositMsg (msg .sender , value , 2 , contractAddress, 0 , 0 , 0 ));
145- emit TRC20Received (msg .sender , contractAddress, value, userDepositList.length - 1 );
146- return userDepositList.length - 1 ;
168+ userDepositList.push (DepositMsg (msg .sender , 0 , 2 , contractAddress, 0 , 0 , value ));
169+ emit TRC20Received (msg .sender , contractAddress, value, nonceBaseValue + userDepositList.length - 1 );
170+ return nonceBaseValue + userDepositList.length - 1 ;
147171
148172 }
149173
150174 function getDepositMsg (uint256 nonce ) view public returns (address , uint256 , uint256 , address , uint256 , uint256 , uint256 ){
151- DepositMsg memory _depositMsg = userDepositList[nonce];
152- return (_depositMsg.user, uint256 (_depositMsg.value), uint256 (_depositMsg._type), _depositMsg.mainChainAddress,
153- uint256 (_depositMsg.tokenId), uint256 (_depositMsg.status), uint256 (_depositMsg.uId));
154-
175+ if (nonce >= nonceBaseValue) {
176+ DepositMsg memory _depositMsg = userDepositList[nonce - nonceBaseValue];
177+ return (_depositMsg.user, uint256 (_depositMsg.value), uint256 (_depositMsg._type), _depositMsg.mainChainAddress,
178+ uint256 (_depositMsg.tokenId), uint256 (_depositMsg.status), uint256 (_depositMsg.uId));
179+ }
180+ else {
181+ return MainChainGateway (refGatewayAddress).getDepositMsg (nonce);
182+ }
155183 }
156184
157185 function getMappingMsg (uint256 nonce ) view public returns (address , uint256 , uint256 ){
158- MappingMsg memory _mappingMsg = userMappingList[nonce];
159- return (_mappingMsg.mainChainAddress, uint256 (_mappingMsg._type), uint256 (_mappingMsg.status));
186+ if (nonce >= nonceBaseValue) {
187+ MappingMsg memory _mappingMsg = userMappingList[nonce - nonceBaseValue];
188+ return (_mappingMsg.mainChainAddress, uint256 (_mappingMsg._type), uint256 (_mappingMsg.status));
189+ }
190+ else {
191+ return MainChainGateway (refGatewayAddress).getMappingMsg (nonce);
192+ }
193+
160194 }
161195
162196 function depositTRC721 (address contractAddress , uint256 uid ) payable
163197 public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256 ) {
164- require (mainToSideContractMap[ contractAddress] == 1 , "not an allowed token " );
198+ require (isMapAddrInGateways ( contractAddress) , "not an allowed token " );
165199 require (msg .value >= depositFee, "msg.value need >= depositFee " );
166200 if (msg .value > depositFee) {
167201 msg .sender .transfer (msg .value - depositFee);
@@ -170,8 +204,8 @@ contract MainChainGateway is OracleManagerContract {
170204
171205 TRC721 (contractAddress).transferFrom (msg .sender , address (this ), uid);
172206 userDepositList.push (DepositMsg (msg .sender , 0 , 3 , contractAddress, 0 , 0 , uid));
173- emit TRC721Received (msg .sender , contractAddress, uid, userDepositList.length - 1 );
174- return userDepositList.length - 1 ;
207+ emit TRC721Received (msg .sender , contractAddress, uid, nonceBaseValue + userDepositList.length - 1 );
208+ return nonceBaseValue + userDepositList.length - 1 ;
175209 }
176210
177211 function depositTRX () payable public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256 ) {
@@ -181,8 +215,8 @@ contract MainChainGateway is OracleManagerContract {
181215 require (value >= depositMinTrx && value <= uint64Max, "must between depositMinTrx and uint64Max " );
182216
183217 userDepositList.push (DepositMsg (msg .sender , uint64 (value), 0 , address (0 ), 0 , 0 , 0 ));
184- emit TRXReceived (msg .sender , uint64 (value), userDepositList.length - 1 );
185- return userDepositList.length - 1 ;
218+ emit TRXReceived (msg .sender , uint64 (value), nonceBaseValue + userDepositList.length - 1 );
219+ return nonceBaseValue + userDepositList.length - 1 ;
186220 }
187221
188222 function depositTRC10 (uint64 tokenId , uint64 tokenValue ) payable public checkForTrc10 (tokenId, tokenValue)
@@ -196,12 +230,14 @@ contract MainChainGateway is OracleManagerContract {
196230 require (uint256 (tokenId) <= uint64Max, "tokenId must <= uint64Max " );
197231 require (tokenValue <= uint64Max, "tokenValue must <= uint64Max " );
198232 userDepositList.push (DepositMsg (msg .sender , tokenValue, 1 , address (0 ), tokenId, 0 , 0 ));
199- emit TRC10Received (msg .sender , tokenId, tokenValue, userDepositList.length - 1 );
200- return userDepositList.length - 1 ;
233+ emit TRC10Received (msg .sender , tokenId, tokenValue, nonceBaseValue + userDepositList.length - 1 );
234+ return nonceBaseValue + userDepositList.length - 1 ;
201235 }
202236
203237 function () payable external goDelegateCall onlyNotStop onlyNotPause {
204- revert ("not allow function fallback " );
238+ if (msg .sender != refGatewayAddress) {
239+ revert ("not allow function fallback " );
240+ }
205241 }
206242
207243 function mappingTRC20 (bytes memory txId ) payable public goDelegateCall onlyNotStop onlyNotPause isHuman returns (uint256 ) {
@@ -212,14 +248,14 @@ contract MainChainGateway is OracleManagerContract {
212248 bonus += mappingFee;
213249 address trc20Address = calcContractAddress (txId, msg .sender );
214250 require (trc20Address != sunTokenAddress, "mainChainAddress == sunTokenAddress " );
215- require (mainToSideContractMap[trc20Address] != 1 , "trc20Address mapped " );
251+ require (! isMapAddrInGateways (trc20Address) , "trc20Address mapped " );
216252 uint256 size;
217253 assembly {size := extcodesize (trc20Address)}
218254 require (size > 0 );
219255 userMappingList.push (MappingMsg (trc20Address, DataModel.TokenKind.TRC20, DataModel.Status.SUCCESS));
220256 mainToSideContractMap[trc20Address] = 1 ;
221- emit TRC20Mapping (trc20Address, userMappingList.length - 1 );
222- return userMappingList.length - 1 ;
257+ emit TRC20Mapping (trc20Address, nonceBaseValue + userMappingList.length - 1 );
258+ return nonceBaseValue + userMappingList.length - 1 ;
223259 }
224260
225261 // 2. deployDAppTRC721AndMapping
@@ -231,32 +267,33 @@ contract MainChainGateway is OracleManagerContract {
231267 bonus += mappingFee;
232268 address trc721Address = calcContractAddress (txId, msg .sender );
233269 require (trc721Address != sunTokenAddress, "mainChainAddress == sunTokenAddress " );
234- require (mainToSideContractMap[trc721Address] != 1 , "trc721Address mapped " );
270+ require (! isMapAddrInGateways (trc721Address) , "trc721Address mapped " );
235271 uint256 size;
236272 assembly {size := extcodesize (trc721Address)}
237273 require (size > 0 );
238274 userMappingList.push (MappingMsg (trc721Address, DataModel.TokenKind.TRC721, DataModel.Status.SUCCESS));
239275 mainToSideContractMap[trc721Address] = 1 ;
240- emit TRC721Mapping (trc721Address, userMappingList.length - 1 );
241- return userMappingList.length - 1 ;
276+ emit TRC721Mapping (trc721Address, nonceBaseValue + userMappingList.length - 1 );
277+ return nonceBaseValue + userMappingList.length - 1 ;
242278 }
243279
244280 function retryDeposit (uint256 nonce ) payable public goDelegateCall onlyNotStop onlyNotPause isHuman {
245281 require (msg .value >= retryFee, "msg.value need >= retryFee " );
282+ require (nonce >= nonceBaseValue, "nonce should not < nonceBaseValue " );
283+ require (nonce < nonceBaseValue + userDepositList.length , "nonce >= nonceBaseValue + userDepositList.length " );
246284 if (msg .value > retryFee) {
247285 msg .sender .transfer (msg .value - retryFee);
248286 }
249287 bonus += retryFee;
250- require (nonce < userDepositList.length , "nonce >= userDepositList.length " );
251- DepositMsg storage depositMsg = userDepositList[nonce];
288+ DepositMsg storage depositMsg = userDepositList[nonce - nonceBaseValue];
252289 // TRX, // 0
253290 // TRC10, // 1
254291 // TRC20, // 2
255292 // TRC721, // 3
256293 if (depositMsg._type == 0 ) {
257294 emit TRXReceived (depositMsg.user, depositMsg.value, nonce);
258295 } else if (depositMsg._type == 2 ) {
259- emit TRC20Received (depositMsg.user, depositMsg.mainChainAddress, depositMsg.value , nonce);
296+ emit TRC20Received (depositMsg.user, depositMsg.mainChainAddress, depositMsg.uId , nonce);
260297 } else if (depositMsg._type == 3 ) {
261298 emit TRC721Received (depositMsg.user, depositMsg.mainChainAddress, depositMsg.uId, nonce);
262299 } else {
@@ -266,12 +303,13 @@ contract MainChainGateway is OracleManagerContract {
266303
267304 function retryMapping (uint256 nonce ) payable public goDelegateCall onlyNotStop onlyNotPause isHuman {
268305 require (msg .value >= retryFee, "msg.value need >= retryFee " );
306+ require (nonce >= nonceBaseValue, "nonce should not < nonceBaseValue " );
307+ require (nonce < nonceBaseValue + userMappingList.length , "nonce >= nonceBaseValue + userMappingList.length " );
269308 if (msg .value > retryFee) {
270309 msg .sender .transfer (msg .value - retryFee);
271310 }
272311 bonus += retryFee;
273- require (nonce < userMappingList.length , "nonce >= userMappingList.length " );
274- MappingMsg storage mappingMsg = userMappingList[nonce];
312+ MappingMsg storage mappingMsg = userMappingList[nonce - nonceBaseValue];
275313 require (mappingMsg.status == DataModel.Status.SUCCESS, "mappingMsg.status != SUCCESS " );
276314
277315 if (mappingMsg._type == DataModel.TokenKind.TRC20) {
@@ -313,7 +351,17 @@ contract MainChainGateway is OracleManagerContract {
313351
314352 // Returns all the TRC20
315353 function getTRC20 (address contractAddress ) external view returns (uint256 ) {
316- return TRC20 (contractAddress).balanceOf (contractAddress);
354+ return TRC20 (contractAddress).balanceOf (address (this ));
355+ }
356+
357+ function getBonus () external view returns (uint256 ) {
358+ // TODO:
359+ // Changed in next version to make it recursively as:
360+ // MainChainGateway(refGatewayAddress).getBonus() + bonus
361+ if (refGatewayAddress == address (0 ))
362+ return bonus;
363+ else
364+ return MainChainGateway (refGatewayAddress).bonus () + bonus;
317365 }
318366
319367 // Returns TRC721 token by uid
@@ -353,4 +401,23 @@ contract MainChainGateway is OracleManagerContract {
353401 depositMinTrc20 = minValue;
354402 }
355403
404+ function setRefGatewayAddress (address payable ref ) public goDelegateCall onlyOwner {
405+ refGatewayAddress = ref;
406+ }
407+
408+ function isMapAddrInGateways (address trcAddress ) public goDelegateCall returns (bool ){
409+ if (mainToSideContractMap[trcAddress] == 1 ) {
410+ return true ;
411+ }
412+ if (refGatewayAddress == address (0 )) {
413+ return false ;
414+ }
415+ // TODO: In next version we should use gatewayPeers[i].isMapAddrInGateways(trcAddress) here
416+ if (MainChainGateway (refGatewayAddress).mainToSideContractMap (trcAddress) == 1 ) {
417+ mainToSideContractMap[trcAddress] = 1 ;
418+ return true ;
419+ }
420+ return false ;
421+ }
422+
356423}
0 commit comments