小明今天繼續完成的是用戶取消下單的功能:
function cancelOrder(string symbolName, bool isSellOrder, uint priceInWei, uint offerKey) public {
uint8 symbolNameIndex = getSymbolIndexOrThrow(symbolName);
if (isSellOrder) {
require(tokens[symbolNameIndex].sellBook[priceInWei].offers[offerKey].who == msg.sender);
uint tokensAmount = tokens[symbolNameIndex].sellBook[priceInWei].offers[offerKey].amount;
require(tokenBalanceForAddress[msg.sender][symbolNameIndex] + tokensAmount >= tokenBalanceForAddress[msg.sender][symbolNameIndex]);
tokenBalanceForAddress[msg.sender][symbolNameIndex] += tokensAmount;
tokens[symbolNameIndex].sellBook[priceInWei].offers[offerKey].amount = 0;
emit SellOrderCanceled(symbolNameIndex, priceInWei, offerKey);
}
else {
require(tokens[symbolNameIndex].buyBook[priceInWei].offers[offerKey].who == msg.sender);
uint etherToRefund = tokens[symbolNameIndex].buyBook[priceInWei].offers[offerKey].amount * priceInWei;
require(balanceEthForAddress[msg.sender] + etherToRefund >= balanceEthForAddress[msg.sender]);
balanceEthForAddress[msg.sender] += etherToRefund;
tokens[symbolNameIndex].buyBook[priceInWei].offers[offerKey].amount = 0;
emit BuyOrderCanceled(symbolNameIndex, priceInWei, offerKey);
}
}
本文同時發佈於作者部落格:https://www.bdetw.com/blog
想找區塊鏈人才或想學習區塊鏈知識的夥伴。歡迎參加社群小聚,一起來輕鬆交流、互相學習成長、認識新朋友、發現新機會!
台北區塊鏈社群
https://bitlly.co/Q4dIK
BDE 區塊鏈學院 - 提供專業的區塊鏈培訓與顧問服務。
https://bitlly.co/mbDwX