智能合约的生命周期根据其运行机制可概括为协商、开发、部署、运维、学习和自毁六个阶段,其中开发阶段包括合约上链前的合约测试,学习阶段包括智能合约的运行反馈与合约更新等.智能合约的基础架构模型,模型自底向上由基础设施层、合约层、运维层、智能层、表现层和应用层组成,
基础设施层:封装了支持智能合约及其衍生应用实现的所有基础设施,包括分布式账本及其关键技术、开发环境和可信数据源等,这些基础设施的选择将在一定程度上影响智能合约的设计模式和合约属性.
event DeployMarketEvent();
event ChangeMarketStatusEvent(uint8 status);
event SetTokenInfoEvent(uint16 tokenCode,string symbol,address tokenAddr,uint64 scaleFactor,uint minDeposit);
event SetWithdrawAddrEvent(address trader,address withdrawAddr);
event DepositEvent(address trader,uint16 tokenCode,string symbol,uint64 amountE8,uint64 depositIndex);
event WithdrawEvent(address trader,uint16 tokenCode,string symbol,uint64 amountE8,uint64 lastOpIndex);
event TransferFeeEvent(uint16 tokenCode,uint64 amountE8,address toAddr);
//balanceE8
is the total balance after this deposit confirmation
event ConfirmDepositEvent(address trader,uint16 tokenCode,uint64 balanceE8);
//amountE8
is the post-fee initiated withdraw amount
//pendingWithdrawE8
is the total pending withdraw amount after this withdraw initiation
event InitiateWithdrawEvent(address trader,uint16 tokenCode,uint64 amountE8,uint64 pendingWithdrawE8);
event MatchOrdersEvent(address trader1,uint64 nonce1,address trader2,uint64 nonce2);
event HardCancelOrderEvent(address trader,uint64 nonce);
event SetFeeRatesEvent(uint16 makerFeeRateE4,uint16 takerFeeRateE4,uint16 withdrawFeeRateE4);
event SetFeeRebatePercentEvent(address trader,uint8 feeRebatePercent);
function Dex2(address admin_)public{
admin=admin_;
setTokenInfo(0/tokenCode/,"ETH",0/tokenAddr/,ETH_SCALE_FACTOR,0/minDeposit/);
emit DeployMarketEvent();
}
function()external{
revert();
}
//Change the market status of DEX.
function changeMarketStatus(uint8 status_)external{
if(msg.sender!=admin)revert();
if(marketStatus==CLOSED)revert();//closed is forever
marketStatus=status_;
emit ChangeMarketStatusEvent(status_);
}
//Each trader can specify a withdraw address(but cannot change it later).Once a trader's
//withdraw address is set,following withdrawals of this trader will go to the withdraw address
//instead of the trader's address.
function setWithdrawAddr(address withdrawAddr)external{
if(withdrawAddr==0)revert();
if(traders[msg.sender].withdrawAddr!=0)revert();//cannot change withdrawAddr once set
traders[msg.sender].withdrawAddr=withdrawAddr;
emit SetWithdrawAddrEvent(msg.sender,withdrawAddr);
}
//Deposit ETH from msg.sender for the given trader.
function depositEth(address traderAddr)external payable{
if(marketStatus!=ACTIVE)revert();
if(traderAddr==0)revert();
if(msg.value<tokens[0].minDeposit)revert();
if(msg.data.length!=4+32)revert();//length condition of param count
uint64 pendingAmountE8=uint64(msg.value/(ETH_SCALE_FACTOR/10**8));//msg.value is in Wei
if(pendingAmountE8==0)revert();
uint64 depositIndex=++lastDepositIndex;
setDeposits(depositIndex,traderAddr,0,pendingAmountE8);
emit DepositEvent(traderAddr,0,"ETH",pendingAmountE8,depositIndex);
}
//Deposit token(other than ETH)from msg.sender for a specified trader.