github.com/klaytn/klaytn@v1.12.1/api/api_public_cypress.go (about) 1 // Copyright 2019 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 package api 18 19 import ( 20 "context" 21 "errors" 22 "strings" 23 24 "github.com/klaytn/klaytn/accounts/abi" 25 "github.com/klaytn/klaytn/common" 26 "github.com/klaytn/klaytn/networks/rpc" 27 ) 28 29 // cypressCreditABI is the input ABI used to generate the binding from. 30 const cypressCreditABI = "[{\"constant\":true,\"inputs\":[],\"name\":\"getPhoto\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"getNames\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"pure\",\"type\":\"function\"}]" 31 32 // CypressCredit contract is stored in the address zero. 33 var cypressCreditContractAddress = common.HexToAddress("0x0000000000000000000000000000000000000000") 34 35 var errNoCypressCreditContract = errors.New("no cypress credit contract") 36 37 type CreditOutput struct { 38 Photo string `json:"photo"` 39 Names string `json:"names"` 40 } 41 42 // callCypressCreditGetFunc executes funcName in CypressCreditContract and returns the output. 43 func (s *PublicBlockChainAPI) callCypressCreditGetFunc(ctx context.Context, parsed *abi.ABI, funcName string) (*string, error) { 44 abiGet, err := parsed.Pack(funcName) 45 if err != nil { 46 return nil, err 47 } 48 49 args := CallArgs{ 50 To: &cypressCreditContractAddress, 51 Data: abiGet, 52 } 53 ret, err := s.Call(ctx, args, rpc.NewBlockNumberOrHashWithNumber(rpc.LatestBlockNumber)) 54 if err != nil { 55 return nil, err 56 } 57 58 output := new(string) 59 err = parsed.UnpackIntoInterface(output, funcName, ret) 60 if err != nil { 61 return nil, err 62 } 63 64 return output, nil 65 } 66 67 // GetCypressCredit calls getPhoto and getNames in the CypressCredit contract 68 // and returns all the results as a struct. 69 func (s *PublicBlockChainAPI) GetCypressCredit(ctx context.Context) (*CreditOutput, error) { 70 answer, err := s.IsContractAccount(ctx, cypressCreditContractAddress, rpc.NewBlockNumberOrHashWithNumber(rpc.LatestBlockNumber)) 71 if err != nil { 72 return nil, err 73 } 74 if !answer { 75 return nil, errNoCypressCreditContract 76 } 77 78 parsed, err := abi.JSON(strings.NewReader(cypressCreditABI)) 79 if err != nil { 80 return nil, err 81 } 82 83 photo, err := s.callCypressCreditGetFunc(ctx, &parsed, "getPhoto") 84 if err != nil { 85 return nil, err 86 } 87 88 names, err := s.callCypressCreditGetFunc(ctx, &parsed, "getNames") 89 if err != nil { 90 return nil, err 91 } 92 93 output := &CreditOutput{ 94 Photo: *photo, 95 Names: *names, 96 } 97 98 return output, nil 99 }