Home Coins TRON (TRX) SUN Network DAppChain Developer Documents: Contract

SUN Network DAppChain Developer Documents: Contract

1*jkXl5ElSvn80ezOrE3I kg
1*jkXl5ElSvn80ezOrE3I kg

3.1 Contract Sort

Contract is consist of three elements: MainChain gateway contract, SideChain gateway contract, Normal TRC20/TRC721 contract

3.2 MainChain gateway contract and SideChain gateway contract.

3.2.1 TRC20/TRC721 contract mapping:

//use mappingTRC20 for example
//Major-chain gateway contract methodology
operate mappingTRC20(bytes txId) public onlyNotStop onlyNotPause goDelegateCall payable returns (uint256) {
require(msg.worth >= mappingFee, "trc20MappingFee not sufficient");
if (msg.worth > 0) {
bonus += msg.worth;
tackle trc20Handle = calcContractAddress(txId, msg.sender);
require(trc20Handle != sunTokenAddress, "mainChainAddress == sunTokenAddress");
require(mainToSideContractMap[trc20Address] != 1, "trc20Handle mapped");
uint256 measurement;
meeting {measurement := extcodesize(trc20Handle)}
require(measurement > 0);
userMappingList.push(MappingMsg(trc20Handle, DataModel.TokenKind.TRC20, DataModel.Standing.SUCCESS));
mainToSideContractMap[trc20Address] = 1;
emit TRC20Mapping(trc20Handle, userMappingList.size - 1);
return userMappingList.size - 1;
//Aspect-chain gateway methodology
operate deployDAppTRC20AndMapping(tackle mainChainAddress, string identify, string image, uint8 decimals) inside returns (tackle r) {
tackle sideChainAddress = new DAppTRC20(tackle(this), identify, image, decimals);
mainToSideContractMap[mainChainAddress] = sideChainAddress;
sideToMainContractMap[sideChainAddress] = mainChainAddress;
emit DeployDAppTRC20AndMapping(mainChainAddress, sideChainAddress);
r = sideChainAddress;

TRC20/TRC721 contract proprietor has to place their contract deployment transaction id to MainChain gateway contract on main-chain. Gateway contract would confirm the existence of the transaction. A number of oracle will confirm and ensure the mapping operations and generate a corresponding TRC20/TRC721 contract on side-chain.

3.2.2 Asset deposit operation

//use depositTRX for example
//MainChain gateway contract methodology
operate depositTRX() payable public onlyNotStop onlyNotPause goDelegateCall returns (uint256) {
require(msg.worth > 0, "worth have to be > 0");
require(msg.worth >= depositMinTrx, "worth have to be >= depositMinTrx");
require(msg.worth <= uint64Max, "msg.worth should <= uint64Max");
userDepositList.push(DepositMsg(msg.sender, uint64(msg.worth), 0, tackle(0), 0, 0, 0));
emit TRXReceived(msg.sender, uint64(msg.worth), userDepositList.size - 1);
return userDepositList.size - 1;
//SideChain gateway contract methodology
operate depositTRX(tackle to, uint256 worth) inside {
emit DepositTRX(to, worth);

When a person deposit TRX/TRC10/TRC20/TRC721 property, the property will probably be locked in main-chain gateway. On side-chain , a number of oracle will verify deposit operation, and side-chain gateway will mint new asset to person side-chain account.

3.2.Three Asset withdraw operation

//use withdrawTRX for example
//side-chain gateway contract methodology
operate withdrawTRX() payable public onlyNotPause onlyNotStop goDelegateCall returns (uint256 r) {
require(msg.worth >= withdrawMinTrx + withdrawFee, "worth have to be >= withdrawMinTrx+withdrawFee");
if (msg.worth > 0) {
bonus += withdrawFee;
uint256 withdrawValue = msg.worth - withdrawFee;
require(withdrawValue > 0, "withdrawValue have to be > 0");
userWithdrawList.push(WithdrawMsg(msg.sender, tackle(0), 0, withdrawValue, DataModel.TokenKind.TRX, DataModel.Standing.SUCCESS));
// burn
emit WithdrawTRX(msg.sender, withdrawValue, userWithdrawList.size - 1);
r = userWithdrawList.size - 1;
//MainChain gateway contract methodology
operate withdrawTRX(tackle to, uint256 worth, uint256 nonce, bytes[] oracleSigns)
public onlyNotStop onlyOracle goDelegateCall {
require(oracleSigns.size <= numOracles, "withdraw TRX indicators num > oracles num");
bytes32 dataHash = keccak256(abi.encodePacked(to, worth, nonce));
bool needWithdraw = checkOracles(dataHash, nonce, oracleSigns);
if (needWithdraw) {
// guarantee it is not reentrant
emit TRXWithdraw(to, worth, nonce);

Totally different from deposit and mapping, withdraw operation is triggered on side-chain. Just like deposit operation, person can use gateway contract to set off withdraw operation for his or her TRX/TRC10/TRC20/TRC721 property. Asset on side-chain will probably be destroyed when it leaving side-chain.

3.Three Aspect-chain commonplace

TRC20/TRC721 solely Implement TRON primary interfaces. And add two mint capabilities within the contract. Additionally add two commonplace withdraw operate for TRC20/TRC721 WI.

//Features in commonplace TRC20 contract
operate mint(tackle to, uint256 worth) exterior onlyGateway {
require(to != tackle(0));
_totalSupply = _totalSupply.add(worth);
​ _balances[to] = _balances[to].add(worth);
​ emit Switch(tackle(0), to, worth);
​ }
operate withdrawal(uint256 worth) payable exterior returns (uint256 r) {
​ switch(gateway, worth);
​ r = ITRC20Receiver(gateway).onTRC20Obtained.worth(msg.worth)(msg.sender, worth);
​ }
//Features in commonplace TRC721 contract
operate mint(tackle to, uint256 tokenId) exterior onlyGateway {
​ require(to != tackle(0));
​ require(!_exists(tokenId));
_tokenOwner[tokenId] = to;
​ _ownedTokensCount[to] = _ownedTokensCount[to].add(1);
emit Switch(tackle(0), to, tokenId);
​ }
operate withdrawal(uint256 tokenId) payable exterior returns (uint256 r) {
​ switch(gateway, tokenId);
​ r = ITRC721Receiver(gateway).onTRC721Obtained.worth(msg.worth)(msg.sender, tokenId);
​ }

Read the original article here

Bitcoin (BTC) $ 8,773.42
Ethereum (ETH) $ 166.60
XRP (XRP) $ 0.233869
Bitcoin SV (BSV) $ 398.10
Bitcoin Cash (BCH) $ 349.19
Tether (USDT) $ 0.997640
Litecoin (LTC) $ 59.13
EOS (EOS) $ 3.85
Binance Coin (BNB) $ 17.04
Cardano (ADA) $ 0.041928