深入分析defi/nft/lp/dapp/pi質押挖礦分紅系統開發詳細方案及邏輯原始碼

丸子qy發表於2023-05-10

  

  1.合約呼叫

  

  引數說明

  

  contractName:合約名稱

  

  method:合約方法

  

  txId:交易ID格式要求:長度為64位元組,字元在a-z0-9可為空,若為空字串,將自動生成txId

  

  kvs:合約引數

  

  timeout:超時時間,單位:s,若傳入-1,將使用預設超時時間:10s。創作及開發:wwqqyy420

  

  withSyncResult:是否同步獲取交易執行結果當為true時,若成功呼叫,common.TxResponse.ContractResult.Result為common.TransactionInfo當為false時,若成功呼叫,common.TxResponse.ContractResult為空,可以透過common.TxResponse.TxId查詢交易結果

  

  limit:transaction limitation,執行交易時的資源消耗上限,設為nil則不設定上限

  

  2.部署wasm合約

  

  下文,將演示透過sdk部署wasm合約,

  

  sdk_user_contract_claim_test.go

  

  func testUserContractClaimCreate(ttesting.T,clientChainClient,admin1,admin2,admin3,admin4*ChainClient,withSyncResult bool,isIgnoreSameContract bool){resp,err:=createUserContract(client,admin1,admin2,admin3,admin4,claimContractName,claimVersion,claimByteCodePath,common.RuntimeType_WASMER,

  

  []common.KeyValuePair{},withSyncResult)if!isIgnoreSameContract{require.Nil(t,err)}fmt.Printf("CREATE claim contract resp:%+vn",resp)}func createUserContract(clientChainClient,admin1,admin2,admin3,admin4ChainClient,contractName,version,byteCodePath string,runtime common.RuntimeType,kvs[]common.KeyValuePair,withSyncResult bool)(*common.TxResponse,error){payloadBytes,err:=

  

  client.CreateContractCreatePayload(contractName,version,byteCodePath,runtime,kvs)if err!=nil{return nil,err}//各組織Admin許可權使用者簽名signedPayloadBytes1,err:=admin1.SignContractManagePayload(payloadBytes)if err!=nil{return nil,err}signedPayloadBytes2,err:=admin2.SignContractManagePayload(payloadBytes)if err!=nil{return nil,err}signedPayloadBytes3,err:=

  

  admin3.SignContractManagePayload(payloadBytes)if err!=nil{return nil,err}signedPayloadBytes4,err:=admin4.SignContractManagePayload(payloadBytes)if err!=nil{return nil,err}//收集併合並簽名mergeSignedPayloadBytes,err:=client.MergeContractManageSignedPayload([][]byte{signedPayloadBytes1,signedPayloadBytes2,signedPayloadBytes3,signedPayloadBytes4})if err!=nil{return nil,err}//傳送建立合約請求resp,err:=

  

  client.SendContractManageRequest(mergeSignedPayloadBytes,createContractTimeout,withSyncResult)if err!=nil{return nil,err}err=checkProposalRequestResp(resp,true)if err!=nil{return nil,err}return resp,nil

  

  3.呼叫wasm合約

  

  下文,將演示透過sdk呼叫wasm合約,

  

  sdk_user_contract_claim_test.go

  

  func testUserContractClaimInvoke(clientChainClient,method string,withSyncResult bool)(string,error){curTime:=fmt.Sprintf("%d",CurrentTimeMillisSeconds())fileHash:=uuid.GetUUID()params:=map[string]string{"time":curTime,"file_hash":fileHash,"file_name":fmt.Sprintf("file_%s",curTime),}err:=invokeUserContract(client,claimContractName,method,"",params,withSyncResult)if err!=nil{return"",err}return fileHash,nil}func invokeUserContract(clientChainClient,contractName,method,txId string,params map[string]string,withSyncResult bool)error{resp,err:=client.InvokeContract(contractName,method,txId,params,-1,withSyncResult)if err!=nil{return err}if resp.Code!=common.TxStatusCode_SUCCESS{return fmt.Errorf("invoke contract failed,[code:%d]/[msg:%s]n",resp.Code,resp.Message)}if!withSyncResult{fmt.Printf("invoke contract success,resp:[code:%d]/[msg:%s]/[txId:%s]n",resp.Code,resp.Message,resp.ContractResult.Result)}else{fmt.Printf("invoke contract success,resp:[code:%d]/[msg:%s]/[contractResult:%s]n",resp.Code,resp.Message,resp.ContractResult)}return nil}

  

  4.建立及呼叫evm合約

  

  sdk-go/examples/user_contract_evm_balance/main.go()


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70029396/viewspace-2951148/,如需轉載,請註明出處,否則將追究法律責任。

相關文章