Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Hyperledger Fabic to realize Token token

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use Hyperledger Fabic to achieve Token tokens, the article introduces in great detail, has a certain reference value, interested friends must read it!

Hyperledger Fabic can also realize Token token 33.2.14.2. The integral is correct (token)

I found that with Taifang thinking, moving the tokens contract to hyperledger can also achieve the function of tokens, in addition to not being able to go on the exchange, basically meet the needs of our alternative points system, the following is I wrote such a contract to achieve a token transfer function similar to Ethernet Square in the super ledger.

Package mainimport ("bytes"encoding/json"fmt"strconv"github.com/hyperledger/fabric/core/chaincode/shim" sc "github.com/hyperledger/fabric/protos/peer") / / Define the SmartContract structuretype SmartContract struct {} type Token struct {Owner string `json: "Owner" `TotalSupply uint `json: "TotalSupply "`TokenName string `json:" TokenName "`TokenSymbol string `json:" TokenSymbol "`BalanceOf map [string] uint `json:" BalanceOf "`} func (token * Token) initialSupply () {token.BalanceOf [token.Owner] = token.TotalSupply } func (token * Token) transfer (_ from string, _ to string, _ value uint) {if (token.BalanceOf [_ from] > = _ value) {token.BalanceOf [_ from]-= _ value; token.BalanceOf [_ to] + = _ value }} func (token * Token) balance (_ from string) uint {return token.BalanceOf [_ from]} func (token * Token) burn (_ value uint) {if (token.BalanceOf [token.owner] > = _ value) {token.BalanceOf [token.Owner]-= _ value; token.TotalSupply-= _ value }} func (token * Token) burnFrom (_ from string, _ value uint) {if (token.BalanceOf [_ from] > = _ value) {token.BalanceOf [_ from]-= _ value; token.TotalSupply-= _ value;}} func (token * Token) mint (_ value uint) {token.BalanceOf [token.Owner] + = _ value; token.TotalSupply + = _ value } func (s * SmartContract) Init (stub shim.ChaincodeStubInterface) sc.Response {return shim.Success (nil)} func (s * SmartContract) initLedger (stub shim.ChaincodeStubInterface) sc.Response {token: = & Token {Owner: "netkiller", TotalSupply: 10000, TokenName: "tokens", TokenSymbol: "COIN" BalanceOf: map [string] uint {} token.initialSupply () tokenAsBytes, _: = json.Marshal (token) stub.PutState ("Token", tokenAsBytes) fmt.Println ("Added", tokenAsBytes) return shim.Success (nil)} func (s * SmartContract) transferToken (stub shim.ChaincodeStubInterface Args [] string) sc.Response {if len (args)! = 3 {return shim.Error ("Incorrect number of arguments. Expecting 2 ")} tokenAsBytes, _: = stub.GetState (args [0]) token: = Token {} json.Unmarshal (tokenAsBytes, & token) token.transfer (args [1], args [2], args [3]) tokenAsBytes, _ = json.Marshal (token) stub.PutState (args [0], tokenAsBytes) return shim.Success (nil)} func (s * SmartContract) balanceToken (stub shim.ChaincodeStubInterface Args [] string) sc.Response {if len (args)! = 1 {return shim.Error ("Incorrect number of arguments. Expecting 1 ")} tokenAsBytes, _: = stub.GetState (args [0]) token: = Token {} json.Unmarshal (tokenAsBytes, & token) amount: = token.balance (args [1]) return shim.Success (amount)} func (s * SmartContract) Invoke (stub shim.ChaincodeStubInterface) sc.Response {/ Retrieve the requested SmartContract function and arguments function Args: = stub.GetFunctionAndParameters () / / Route to the appropriate handler function to interact with the ledger appropriately if function = = "balanceToken" {return s.balanceToken (stub, args)} else if function = = "initLedger" {return s.initLedger (stub)} else if function = = "transferToken" {return s.transferToken (stub) Args)} return shim.Error ("Invalid Smart Contract function name.")} / / The main function is only relevant in unit test mode. Only included here for completeness.func main () {/ / Create a new SmartContract err: = shim.Start (new (SmartContract)) if err! = nil {fmt.Printf ("Error creating new SmartContract:% s", err)}}

Testing of contract codes

Func main () {token: = & Token {Owner: "netkiller", / / token Manager TotalSupply: 10000, / / Total amount of tokens issued TokenName: "Credit Company", / / token name TokenSymbol: "NEO" / / token symbol NEO BalanceOf: map [string] uint {}} token.initialSupply () / initialize token fmt.Println (token.balance ("netkiller")) / / query balance token.transfer ("netkiller", "neo", 100) / / transfer Here the account uses the user ID and does not use the hash value like the Ethernet Square wallet, because the hash value is not easy to remember. Fmt.Println (token.balance ("netkiller")) fmt.Println (token.balance ("neo"))}

We can set up many sets of such comparisons, such as fruit coins, vegetable coins, flow coins.

It is also not difficult to develop a small exchange, allowing users to trade these coins on the exchange.

The above is all the contents of the article "how to use Hyperledger Fabic to implement Token tokens". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report