日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实现一个可管理、增发、兑换、冻结等高级功能的代币

發(fā)布時間:2025/4/16 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实现一个可管理、增发、兑换、冻结等高级功能的代币 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文首發(fā)于深入淺出區(qū)塊鏈社區(qū)
原文鏈接:實現一個可管理、增發(fā)、兌換、凍結等高級功能的代幣

本文主要介紹代幣高級功能的實現: 代幣管理、代幣增發(fā)、代幣兌換、資產凍結、Gas自動補充。

寫在前面

在上一篇:一步步教你創(chuàng)建自己的數字貨幣(代幣)進行ICO中我們實現一個最基本功能的代幣,本文將在上一遍文章的基礎上,講解如果添加更多的高級功能。

實現代幣的管理者

雖然區(qū)塊鏈是去中心化的,但是實現對代幣(合約)的管理,也在許多應用中有需求,為了對代幣進行管理,首先需要給合約添加一個管理者。

我們來看看如果實現,先創(chuàng)建一個owned合約。

contract owned {address public owner;function owned() {owner = msg.sender;}modifier onlyOwner {require(msg.sender == owner);_;}// 實現所有權轉移function transferOwnership(address newOwner) onlyOwner {owner = newOwner;}}

這個合約重要的是加入了一個函數修改器(Function Modifiers)onlyOwner,函數修改器是一個合約屬性,可以被繼承,還能被重寫。它用于在函數執(zhí)行前檢查某種前置條件。
關于函數修改器可進一步閱讀Solidity 教程系列10 - 完全理解函數修改器

如果熟悉Python的同學,會發(fā)現函數修改器的作用和Python的裝飾器很相似。

然后讓代幣合約繼承owned以擁有onlyOwner修改器,代碼如下:

contract MyToken is owned {function MyToken(uint256 initialSupply,string tokenName,uint8 decimalUnits,string tokenSymbol,address centralMinter) {if(centralMinter != 0 ) owner = centralMinter;} }

代幣增發(fā)

實現代幣增發(fā),代幣增發(fā)就如同央行印鈔票一樣,想必很多人都需要這樣的功能。

給合約添加以下的方法:

function mintToken(address target, uint256 mintedAmount) onlyOwner {balanceOf[target] += mintedAmount;totalSupply += mintedAmount;Transfer(0, owner, mintedAmount);Transfer(owner, target, mintedAmount);}

注意onlyOwner修改器添加在函數末尾,這表示只有ower才能調用這用函數。
他的功能很簡單,就是給指定的賬戶增加代幣,同時增加總供應量。

資產凍結

有時為了監(jiān)管的需要,需要實現凍結某些賬戶,凍結后,其資產仍在賬戶,但是不允許交易,之道解除凍結。
給合約添加以下的變量和方法(可以添加到合約的任何地方,但是建議把mapping加到和其他mapping一起,event也是如此):

mapping (address => bool) public frozenAccount;event FrozenFunds(address target, bool frozen);function freezeAccount(address target, bool freeze) onlyOwner {frozenAccount[target] = freeze;FrozenFunds(target, freeze);}

單單以上的代碼還無法凍結,需要把他加入到transfer函數中才能真正生效,因此修改transfer函數

function transfer(address _to, uint256 _value) {require(!frozenAccount[msg.sender]);... }

這樣在轉賬前,對發(fā)起交易的賬號做一次檢查,只有不是被凍結的賬號才能轉賬。

代幣買賣(兌換)

可以自己的貨幣中實現代幣與其他數字貨幣(ether 或其他tokens)的兌換機制。有了這個功能,我們的合約就可以在一買一賣中賺利潤了。

先來設置下買賣價格

uint256 public sellPrice;uint256 public buyPrice;function setPrices(uint256 newSellPrice, uint256 newBuyPrice) onlyOwner {sellPrice = newSellPrice;buyPrice = newBuyPrice;}

setPrices()添加了onlyOwner修改器,注意買賣的價格單位是wei(最小的貨幣單位: 1 eth = 1000000000000000000 wei)

添加來添加買賣函數:

function buy() payable returns (uint amount){amount = msg.value / buyPrice; // calculates the amountrequire(balanceOf[this] >= amount); // checks if it has enough to sellbalanceOf[msg.sender] += amount; // adds the amount to buyer's balancebalanceOf[this] -= amount; // subtracts amount from seller's balanceTransfer(this, msg.sender, amount); // execute an event reflecting the changereturn amount; // ends function and returns}function sell(uint amount) returns (uint revenue){require(balanceOf[msg.sender] >= amount); // checks if the sender has enough to sellbalanceOf[this] += amount; // adds the amount to owner's balancebalanceOf[msg.sender] -= amount; // subtracts the amount from seller's balancerevenue = amount * sellPrice;msg.sender.transfer(revenue); // sends ether to the seller: it's important to do this last to prevent recursion attacksTransfer(msg.sender, this, amount); // executes an event reflecting on the changereturn revenue; // ends function and returns}

加入了買賣功能后,要求我們在創(chuàng)建合約時發(fā)送足夠的以太幣,以便合約有能力回購市面上的代幣,否則合約將破產,用戶沒法先合約賣代幣。

實現Gas的自動補充

以太坊中的交易時需要gas(支付給礦工的費用,費用以ether來支付)。而如果用戶沒有以太幣,只有代幣的情況(或者我們想向用戶隱藏以太坊的細節(jié)),就需要自動補充gas的功能。這個功能將使我們代幣更加好用。

自動補充的邏輯是這樣了,在執(zhí)行交易之前,我們判斷用戶的余額(用來支付礦工的費用),如果用戶的余額非常少(低于某個閾值時)可能影響到交易進行,合約自動售出一部分代幣來補充余額,以幫助用戶順利完成交易。

先來設定余額閾值:

uint minBalanceForAccounts;function setMinBalance(uint minimumBalanceInFinney) onlyOwner {minBalanceForAccounts = minimumBalanceInFinney * 1 finney;}

finney 是貨幣單位 1 finney = 0.001eth
然后交易中加入對用戶的余額的判斷。

function transfer(address _to, uint256 _value) {...if(msg.sender.balance < minBalanceForAccounts)sell((minBalanceForAccounts - msg.sender.balance) / sellPrice);if(_to.balance<minBalanceForAccounts) // 可選,讓接受者也補充余額,以便接受者使用代幣。_to.send(sell((minBalanceForAccounts - _to.balance) / sellPrice));}

代碼部署

項目的完整的部署方法參考上一篇,不同的是創(chuàng)建合約時需要預存余額,如圖:

專欄已經有多篇文章介紹Remix Solidity IDE的使用,這里就不一一截圖演示了,請大家自己測試驗證。

參考文檔

  • Create your own crypto-currency with ethereum

深入淺出區(qū)塊鏈 - 系統(tǒng)學習區(qū)塊鏈,打造最好的區(qū)塊鏈技術博客。

轉載于:https://blog.51cto.com/13457438/2117194

《新程序員》:云原生和全面數字化實踐50位技術專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的实现一个可管理、增发、兑换、冻结等高级功能的代币的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。