8、智能合约部署为了部署智能合约,我们使用节点 url 和签名信息创建一个 TransactionSignedUnityRequest 。创建一个新的 EIP20Deployment 合约定义,我们设置构造函数参数并发送交易 。最后,我们创建 TransactionReceiptPollingRequest 来轮询交易收据并从交易收据中检索新部署的合约地址:
123456789101112131415161718192021222324252627282930313233343536
var url = "http://localhost:8545";var privateKey = "0xb5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";var account = "0x12890d2cce102216644c59daE5baed380d84830c";//initialising the transaction request sendervar transactionRequest = new TransactionSignedUnityRequest(url, privateKey, "YOURCHAINID");var deployContract = new EIP20Deployment(){InitialAmount = 10000,FromAddress = account,TokenName = "TST",TokenSymbol = "TST"};//deploy the contractyield return transactionRequest.SignAndSendDeploymentContractTransaction<EIP20DeploymentBase>(deployContract);if (transactionRequest.Exception != null){Debug.Log(transactionRequest.Exception.Message);yield break;}var transactionHash = transactionRequest.Result;Debug.Log("Deployment transaction hash:" + transactionHash);//create a poll to get the receipt when minedvar transactionReceiptPolling = new TransactionReceiptPollingRequest(url);//checking every 2 seconds for the receiptyield return transactionReceiptPolling.PollForReceipt(transactionHash, 2);var deploymentReceipt = transactionReceiptPolling.Result;Debug.Log("Deployment contract address:" + deploymentReceipt.ContractAddress);
9、查询智能合约要查询智能合约,我们需要创建一个提供 FunctionType 和 ReturnType 的新 QueryUnityRequest 。然后我们将执行查询,查询结果 Result 对象将为我们提供已经解码的合约的输出:1234567
//Query request using our acccount and the contracts address (no parameters needed and default values)var queryRequest = new QueryUnityRequest<BalanceOfFunction, BalanceOfFunctionOutput>(url, account);yield return queryRequest.Query(new BalanceOfFunction(){Owner = account}, deploymentReceipt.ContractAddress);//Getting the dto response already decodedvar dtoResult = queryRequest.Result;Debug.Log(dtoResult.Balance);
10、转账交易发送交易以便与与智能合约进行交互的步骤类似于部署 。我们首先创建一个 TransactionSignedUnityRequest 和包含任何参数的函数,一旦发送交易,我们会轮询确认交易成功的交易收据 。使用交易收据,我们可以解码该交易的任何日志/事件:
12345678910111213141516171819202122
var transactionTransferRequest = new TransactionSignedUnityRequest(url, privateKey, "YOURCHAINID");var newAddress = "0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe";var transactionMessage = new TransferFunction{FromAddress = account,To = newAddress,Value = http://www.zrodata.com/1000,};yield return transactionTransferRequest.SignAndSendTransaction(transactionMessage, deploymentReceipt.ContractAddress);var transactionTransferHash = transactionTransferRequest.Result;Debug.Log("Transfer txn hash:" + transactionHash);transactionReceiptPolling = new TransactionReceiptPollingRequest(url);yield return transactionReceiptPolling.PollForReceipt(transactionTransferHash, 2);var transferReceipt = transactionReceiptPolling.Result;var transferEvent = transferReceipt.DecodeAllEvents<TransferEventDTO>();Debug.Log("Transferd amount from event: " + transferEvent[0].Event.Value);
11、日志和事件为了检索智能合约的日志/事件,我们使用 EthGetLogsUnityRequest 和特定于我们事件的 FilterInput 。可以使用 EventDTO 扩展 GetEventABI() 创建 FilterInputs 。一旦我们生成了请求,我们就可以使用 Result.DecodeAllEvents 扩展方法解码所有匹配的事件 。12345
var getLogsRequest = new EthGetLogsUnityRequest(url);var eventTransfer = TransferEventDTO.GetEventABI();yield return getLogsRequest.SendRequest(eventTransfer.CreateFilterInput(deploymentReceipt.ContractAddress, account));var eventDecoded = getLogsRequest.Result.DecodeAllEvents<TransferEventDTO>();Debug.Log("Transferd amount from get logs event: " + eventDecoded[0].Event.Value);
原文链接:http://blog.hubwiz.com/2022/01/28/nethereum-unity-tutorial/推荐阅读
- 哪家月子中心好啊 坐月子哪家月子中心好
- 营业执照贷款能贷多少 营业执照贷款
- 房贷提前还款计算器在线 2021房贷提前还款计算器
- 多肉能放在卧室吗? 多肉放卧室可以吗
- 苹果和西红柿一起榨汁的功效 西红柿苹果可以一起吃吗?
- excel如何冻结多行多列 如何冻结多行多列
- c2驾照可以开多长的车子 C2驾照可以开多长的车?
- 平安银行房抵贷多久下款 平安银行房抵贷
- 西交利物浦大学学费标准 一年学费是多少
- 从剪辑手法 一个勺子亏了多少钱