1.合约调用
参数说明
contractName:合约名称
method:合约方法
txId:交易ID格式要求:长度为64字节,字符在a-z0-9可为空,若为空字符串,将自动生成txId
kvs:合约参数
timeout:超时时间,单位:s,若传入-1,将使用默认超时时间:10s
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(https://git.chainmaker.org.cn/chainmaker/sdk-go/-/blob/master/examples/user_contract_evm_balance/main.go)
5.更多示例和用法
更多示例和用法,请参看单元测试用例
功能单测代码用户合约sdk_user_contract_test.go系统合约sdk_system_contract_test.go链配置sdk_chain_config_test.go证书管理sdk_cert_manage_test.go消息订阅sdk_subscribe_test.go