- 第 1 章 区块链
- 第 2 章 以太坊
- 第 3 章 以太坊私链入门
- 第 4 章 以太坊网络
- 第 5 章 geth v1.8.16 命令详解
- 第 6 章 Wallet
- 第 7 章 Token
- 第 8 章 智能合约语言 Solidity v0.5.0
- 第 9 章 Truffle v4.1.8 开发框架
- 第 10 章 web3.js - 1.0.0
- 第 11 章 web3j v3.4.0 - Jave Client
- 11.2. 启动以太坊
- 11.3. Maven pom.xml 文件
- 11.4. Java 与 Solidity 数据类型映射关系
- 11.5. 常量
- 11.6. 连接到服务器获取版本号
- 11.7. 获得以太坊状态信息
- 11.8. 单位转换
- 11.9. 账号管理
- 11.10. Credentials
- 11.11. 交易
- 11.12. 钱包
- 11.13. 智能合约
- 11.14. ERC20合约
- 11.15. Infura
- 11.16. 助记词
- 11.17. 过滤器 (Filter)
- 11.18. Subscription
- 11.19. 解锁账号
- 11.20. IBAN (International Bank Account Number)
- 11.21. Springboot with Ethereum (web3j)
- 第 12 章 web3.py - A python interface for interacting with the Ethereum blockchain and ecosystem.
- 第 14 章 Ethereum Developer APIs
- 第 15 章 infura
- 第 16 章 以太坊案例
- 第 17 章 FAQ
- 17.3. Error: authentication needed: password or unlock
- 17.4. 新增节点后不生效
- 17.5. Unhandled rejection Error: Returned error: The method personal_unlockAccount does not exist/is not available
- 17.6. Error: exceeds block gas limit
- 17.7. Migrations.sol:11:3: Warning: Defining constructors as functions with the same name as the contract is deprecated. Use "constructor(…) { … }" instead.
- 17.8. Exception in thread "main" rx.exceptions.OnErrorNotImplementedException: Invalid response received: okhttp3.internal.http.RealResponseBody@6c25e6c4
- 17.9. 旧版本 Remix(browser-solidity) 本地安装
- 第 18 章 Hyperledger Fabric v2.0.0
- 第 19 章 Hyperledger Fabric 运维
- 第 20 章 Chaincode 链码(智能合约)
- 第 21 章 Hyperledger Fabric Client SDK for Node.js
- 第 22 章 fabric-sdk-java
- 第 24 章 已知 Hyperledger 落地案例
- 第 25 章 Fabric Command
- 第 26 章 Fabric FAQ
- 第 27 章 IPFS(InterPlanetary File System,星际文件系统)
- 第 28 章 IPFS 命令
- 第 29 章 IPFS WebUI
- 第 30 章 IPFS 集群配置
- 第 31 章 IPFS API
- 第 32 章 IPFS Faq
- 第 33 章 EOS
- 第 34 章 EOS 安装
- 第 35 章 CLEOS
- 第 36 章 智能合约开发
- 第 37 章 EOS Dapp 开发
- 第 38 章 FAQ
- 第 39 章 BaaS (Blockchain as a Service) 平台
- 第 40 章 BitCoin
- 第 41 章 其他区块链相关
- 附录 1. 附录
文章来源于网络收集而来,版权归原创者所有,如有侵权请及时联系!
21.2. Node.js 测试程序
21.2. Node.js 测试程序
package main /* -------------------------------------------------- Author: netkiller <netkiller@msn.com> Home: http://www.netkiller.cn Data: 2018-03-20 11:00 PM -------------------------------------------------- CORE_PEER_ADDRESS=peer:7051 CORE_CHAINCODE_ID_NAME=token3:1.0 chaincode/token/token3 peer chaincode install -n token3 -v 1.0 -p chaincodedev/chaincode/token peer chaincode instantiate -C myc -n token3 -v 1.0 -c '{"Args":[""]}' -P "OR ('Org1MSP.member','Org2MSP.member')" peer chaincode invoke -C myc -n token3 -c '{"function":"createAccount","Args":["coinbase"]}' peer chaincode invoke -C myc -n token3 -v 1.0 -c '{"function":"showAccount","Args":["coinbase"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"balanceAll","Args":["coinbase"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"initCurrency","Args":["Netkiller Token","NKC","1000000","coinbase"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"initCurrency","Args":["NEO Token","NEC","1000000","coinbase"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"setLock","Args":["true"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"setLock","Args":["false"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"mintToken","Args":["NKC","5000","coinbase"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"createAccount","Args":["netkiller"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"transferToken","Args":["coinbase","netkiller","NKC","100"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"balance","Args":["netkiller","NKC"]}' peer chaincode invoke -C myc -n token3 -c '{"function":"frozenAccount","Args":["netkiller","true"]}' -------------------------------------------------- */ import ( "encoding/json" "fmt" "strconv" "github.com/hyperledger/fabric/core/chaincode/shim" pb "github.com/hyperledger/fabric/protos/peer" ) type Msg struct{ Status bool `json:"Status"` Code int `json:"Code"` Message string `json:"Message"` } type Currency struct{ TokenName string `json:"TokenName"` TokenSymbol string `json:"TokenSymbol"` TotalSupply float64 `json:"TotalSupply"` } type Token struct { Lock bool `json:"Lock"` Currency map[string]Currency `json:"Currency"` } func (token *Token) transfer (_from *Account, _to *Account, _currency string, _value float64) []byte{ var rev []byte if (token.Lock){ msg := &Msg{Status: false, Code: 0, Message: "锁仓状态,停止一切转账活动"} rev, _ = json.Marshal(msg) return rev } if(_from.Frozen ) { msg := &Msg{Status: false, Code: 0, Message: "From 账号冻结"} rev, _ = json.Marshal(msg) return rev } if( _to.Frozen) { msg := &Msg{Status: false, Code: 0, Message: "To 账号冻结"} rev, _ = json.Marshal(msg) return rev } if(!token.isCurrency(_currency)){ msg := &Msg{Status: false, Code: 0, Message: "货币符号不存在"} rev, _ = json.Marshal(msg) return rev } if(_from.BalanceOf[_currency] >= _value){ _from.BalanceOf[_currency] -= _value; _to.BalanceOf[_currency] += _value; msg := &Msg{Status: true, Code: 0, Message: "转账成功"} rev, _ = json.Marshal(msg) return rev }else{ msg := &Msg{Status: false, Code: 0, Message: "余额不足"} rev, _ = json.Marshal(msg) return rev } } func (token *Token) initialSupply(_name string, _symbol string, _supply float64, _account *Account) []byte{ if _,ok := token.Currency[_symbol]; ok { msg := &Msg{Status: false, Code: 0, Message: "代币已经存在"} rev, _ := json.Marshal(msg) return rev } if _account.BalanceOf[_symbol] > 0 { msg := &Msg{Status: false, Code: 0, Message: "账号中存在代币"} rev, _ := json.Marshal(msg) return rev }else{ token.Currency[_symbol] = Currency{TokenName: _name, TokenSymbol: _symbol, TotalSupply: _supply} _account.BalanceOf[_symbol] = _supply msg := &Msg{Status: true, Code: 0, Message: "代币初始化成功"} rev, _ := json.Marshal(msg) return rev } } func (token *Token) mint(_currency string, _amount float64, _account *Account) []byte{ if(!token.isCurrency(_currency)){ msg := &Msg{Status: false, Code: 0, Message: "货币符号不存在"} rev, _ := json.Marshal(msg) return rev } cur := token.Currency[_currency] cur.TotalSupply += _amount; token.Currency[_currency] = cur _account.BalanceOf[_currency] += _amount; msg := &Msg{Status: true, Code: 0, Message: "代币增发成功"} rev, _ := json.Marshal(msg) return rev } func (token *Token) burn(_currency string, _amount float64, _account *Account) []byte{ if(!token.isCurrency(_currency)){ msg := &Msg{Status: false, Code: 0, Message: "货币符号不存在"} rev, _ := json.Marshal(msg) return rev } if(token.Currency[_currency].TotalSupply >= _amount){ cur := token.Currency[_currency] cur.TotalSupply -= _amount; token.Currency[_currency] = cur _account.BalanceOf[_currency] -= _amount; msg := &Msg{Status: false, Code: 0, Message: "代币回收成功"} rev, _ := json.Marshal(msg) return rev }else{ msg := &Msg{Status: false, Code: 0, Message: "代币回收失败,回收额度不足"} rev, _ := json.Marshal(msg) return rev } } func (token *Token) isCurrency(_currency string) bool { if _, ok := token.Currency[_currency]; ok { return true }else{ return false } } func (token *Token) setLock(_look bool) bool { token.Lock = _look return token.Lock } type Account struct { Name string `json:"Name"` Frozen bool `json:"Frozen"` BalanceOf map[string]float64 `json:"BalanceOf"` } func (account *Account) balance (_currency string) map[string]float64{ bal := map[string]float64{_currency:account.BalanceOf[_currency]} return bal } func (account *Account) balanceAll() map[string]float64{ return account.BalanceOf } // ----------- const TokenKey = "Token" // Define the Smart Contract structure type SmartContract struct { } func (s *SmartContract) Init(stub shim.ChaincodeStubInterface) pb.Response { token := &Token{Currency: map[string]Currency{}} tokenAsBytes, err := json.Marshal(token) err = stub.PutState(TokenKey, tokenAsBytes) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("Init Token %s \n", string(tokenAsBytes)) } return shim.Success(nil) } func (s *SmartContract) Query(stub shim.ChaincodeStubInterface) pb.Response { function, args := stub.GetFunctionAndParameters() if function == "balance" { return s.balance(stub, args) } else if function == "balanceAll" { return s.balanceAll(stub, args) } else if function == "showAccount" { return s.showAccount(stub, args) } return shim.Error("Invalid Smart Contract function name.") } func (s *SmartContract) Invoke(stub shim.ChaincodeStubInterface) pb.Response { // Retrieve the requested Smart Contract function and arguments function, args := stub.GetFunctionAndParameters() // Route to the appropriate handler function to interact with the ledger appropriately if function == "initLedger" { return s.initLedger(stub, args) } else if function == "createAccount" { return s.createAccount(stub, args) } else if function == "initCurrency" { return s.initCurrency(stub, args) } else if function == "setLock" { return s.setLock(stub, args) } else if function == "transferToken" { return s.transferToken(stub, args) } else if function == "frozenAccount" { return s.frozenAccount(stub, args) } else if function == "mintToken" { return s.mintToken(stub, args) } else if function == "balance" { return s.balance(stub, args) } else if function == "balanceAll" { return s.balanceAll(stub, args) } else if function == "showAccount" { return s.showAccount(stub, args) } else if function == "showToken" { return s.showToken(stub, args) } return shim.Error("Invalid Smart Contract function name.") } func (s *SmartContract) createAccount(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting 1") } key := args[0] name := args[0] existAsBytes,err := stub.GetState(key) fmt.Printf("GetState(%s) %s \n", key, string(existAsBytes)) if string(existAsBytes) != "" { fmt.Println("Failed to create account, Duplicate key.") return shim.Error("Failed to create account, Duplicate key.") } account := Account{ Name: name, Frozen: false, BalanceOf: map[string]float64{}} accountAsBytes, _ := json.Marshal(account) err = stub.PutState(key, accountAsBytes) if err != nil { return shim.Error(err.Error()) } fmt.Printf("createAccount %s \n", string(accountAsBytes)) return shim.Success(accountAsBytes) } func (s *SmartContract) initLedger(stub shim.ChaincodeStubInterface, args []string) pb.Response { return shim.Success(nil) } func (s *SmartContract) showToken(stub shim.ChaincodeStubInterface, args []string) pb.Response { tokenAsBytes,err := stub.GetState(TokenKey) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("GetState(%s)) %s \n", TokenKey, string(tokenAsBytes)) } return shim.Success(tokenAsBytes) } func (s *SmartContract) initCurrency(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 4 { return shim.Error("Incorrect number of arguments. Expecting 4") } _name := args[0] _symbol:= args[1] _supply,_:= strconv.ParseFloat(args[2], 64) _account := args[3] coinbaseAsBytes,err := stub.GetState(_account) if err != nil { return shim.Error(err.Error()) } fmt.Printf("Coinbase before %s \n", string(coinbaseAsBytes)) coinbase := &Account{} json.Unmarshal(coinbaseAsBytes, &coinbase) token := Token{} existAsBytes,err := stub.GetState(TokenKey) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("GetState(%s)) %s \n", TokenKey, string(existAsBytes)) } json.Unmarshal(existAsBytes, &token) result := token.initialSupply(_name,_symbol,_supply, coinbase) tokenAsBytes, _ := json.Marshal(token) err = stub.PutState(TokenKey, tokenAsBytes) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("Init Token %s \n", string(tokenAsBytes)) } coinbaseAsBytes, _ = json.Marshal(coinbase) err = stub.PutState(_account, coinbaseAsBytes) if err != nil { return shim.Error(err.Error()) } fmt.Printf("Coinbase after %s \n", string(coinbaseAsBytes)) return shim.Success(result) } func (s *SmartContract) transferToken(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 4 { return shim.Error("Incorrect number of arguments. Expecting 4") } _from := args[0] _to := args[1] _currency := args[2] _amount,_ := strconv.ParseFloat(args[3], 32) if(_amount <= 0){ return shim.Error("Incorrect number of amount") } fromAsBytes,err := stub.GetState(_from) if err != nil { return shim.Error(err.Error()) } fmt.Printf("fromAccount %s \n", string(fromAsBytes)) fromAccount := &Account{} json.Unmarshal(fromAsBytes, &fromAccount) toAsBytes,err := stub.GetState(_to) if err != nil { return shim.Error(err.Error()) } fmt.Printf("toAccount %s \n", string(toAsBytes)) toAccount := &Account{} json.Unmarshal(toAsBytes, &toAccount) tokenAsBytes,err := stub.GetState(TokenKey) if err != nil { return shim.Error(err.Error()) } fmt.Printf("Token %s \n", string(toAsBytes)) token := Token{Currency: map[string]Currency{}} json.Unmarshal(tokenAsBytes, &token) result := token.transfer(fromAccount, toAccount, _currency, _amount) fmt.Printf("Result %s \n", string(result)) fromAsBytes, err = json.Marshal(fromAccount) if err != nil { return shim.Error(err.Error()) } err = stub.PutState(_from, fromAsBytes) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("fromAccount %s \n", string(fromAsBytes)) } toAsBytes, err = json.Marshal(toAccount) if err != nil { return shim.Error(err.Error()) } err = stub.PutState(_to, toAsBytes) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("toAccount %s \n", string(toAsBytes)) } return shim.Success(result) } func (s *SmartContract) mintToken(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 3 { return shim.Error("Incorrect number of arguments. Expecting 3") } _currency := args[0] _amount,_ := strconv.ParseFloat(args[1], 32) _account := args[2] coinbaseAsBytes,err := stub.GetState(_account) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("Coinbase before %s \n", string(coinbaseAsBytes)) } coinbase := &Account{} json.Unmarshal(coinbaseAsBytes, &coinbase) tokenAsBytes,err := stub.GetState(TokenKey) if err != nil { return shim.Error(err.Error()) } fmt.Printf("Token before %s \n", string(tokenAsBytes)) token := Token{} json.Unmarshal(tokenAsBytes, &token) result := token.mint(_currency, _amount, coinbase) tokenAsBytes, err = json.Marshal(token) if err != nil { return shim.Error(err.Error()) } err = stub.PutState(TokenKey, tokenAsBytes) if err != nil { return shim.Error(err.Error()) } fmt.Printf("Token after %s \n", string(tokenAsBytes)) coinbaseAsBytes, _ = json.Marshal(coinbase) err = stub.PutState(_account, coinbaseAsBytes) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("Coinbase after %s \n", string(coinbaseAsBytes)) } fmt.Printf("mintToken %s \n", string(tokenAsBytes)) return shim.Success(result) } func (s *SmartContract) setLock(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting 2") } _look := args[0] tokenAsBytes,err := stub.GetState(TokenKey) if err != nil { return shim.Error(err.Error()) } // fmt.Printf("setLock - begin %s \n", string(tokenAsBytes)) token := Token{} json.Unmarshal(tokenAsBytes, &token) if(_look == "true"){ token.setLock(true) }else{ token.setLock(false) } tokenAsBytes, err = json.Marshal(token) if err != nil { return shim.Error(err.Error()) } err = stub.PutState(TokenKey, tokenAsBytes) if err != nil { return shim.Error(err.Error()) } fmt.Printf("setLock - end %s \n", string(tokenAsBytes)) return shim.Success(nil) } func (s *SmartContract) frozenAccount(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 2 { return shim.Error("Incorrect number of arguments. Expecting 2") } _account := args[0] _status := args[1] accountAsBytes,err := stub.GetState(_account) if err != nil { return shim.Error(err.Error()) } // fmt.Printf("setLock - begin %s \n", string(tokenAsBytes)) account := Account{} json.Unmarshal(accountAsBytes, &account) var status bool if(_status == "true"){ status = true; }else{ status = false } account.Frozen = status accountAsBytes, err = json.Marshal(account) if err != nil { return shim.Error(err.Error()) } err = stub.PutState(_account, accountAsBytes) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("frozenAccount - end %s \n", string(accountAsBytes)) } return shim.Success(nil) } func (s *SmartContract) showAccount(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting 1") } _account := args[0] accountAsBytes,err := stub.GetState(_account) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("Account balance %s \n", string(accountAsBytes)) } return shim.Success(accountAsBytes) } func (s *SmartContract) balance(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 2 { return shim.Error("Incorrect number of arguments. Expecting 1") } _account := args[0] _currency := args[1] accountAsBytes,err := stub.GetState(_account) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("Account balance %s \n", string(accountAsBytes)) } account := Account{} json.Unmarshal(accountAsBytes, &account) result := account.balance(_currency) resultAsBytes, _ := json.Marshal(result) fmt.Printf("%s balance is %s \n", _account, string(resultAsBytes)) return shim.Success(resultAsBytes) } func (s *SmartContract) balanceAll(stub shim.ChaincodeStubInterface, args []string) pb.Response { if len(args) != 1 { return shim.Error("Incorrect number of arguments. Expecting 1") } _account := args[0] accountAsBytes,err := stub.GetState(_account) if err != nil { return shim.Error(err.Error()) }else{ fmt.Printf("Account balance %s \n", string(accountAsBytes)) } account := Account{} json.Unmarshal(accountAsBytes, &account) result := account.balanceAll() resultAsBytes, _ := json.Marshal(result) fmt.Printf("%s balance is %s \n", _account, string(resultAsBytes)) return shim.Success(resultAsBytes) } // The main function is only relevant in unit test mode. Only included here for completeness. func main() { // Create a new Smart Contract err := shim.Start(new(SmartContract)) if err != nil { fmt.Printf("Error creating new Smart Contract: %s", err) } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论