コード例 #1
0
        public ILocalExecutionResult CallContractMethodLocally(
            string methodName,
            string contractAddress,
            decimal amount,
            string[] parameters = null,
            ulong gasLimit      = SmartContractFormatLogic.GasLimitMaximum / 2, // half of maximum
            ulong gasPrice      = SmartContractMempoolValidator.MinGasPrice,
            string sender       = null)
        {
            var request = new LocalCallContractRequest
            {
                Amount          = amount.ToString(CultureInfo.InvariantCulture),
                ContractAddress = contractAddress,
                GasLimit        = gasLimit,
                GasPrice        = gasPrice,
                MethodName      = methodName,
                Parameters      = parameters,
                Sender          = sender ?? this.MinerAddress.Address
            };
            JsonResult response = (JsonResult)this.smartContractsController.LocalCallSmartContractTransaction(request);

            return((ILocalExecutionResult)response.Value);
        }
コード例 #2
0
        public void SendAndReceiveLocalSmartContractPropertyCallTransactionsUsingController()
        {
            using (SmartContractNodeBuilder builder = SmartContractNodeBuilder.Create(this))
            {
                CoreNode scSender   = builder.CreateSmartContractPowNode().WithWallet().Start();
                CoreNode scReceiver = builder.CreateSmartContractPowNode().WithWallet().Start();

                int maturity = (int)scReceiver.FullNode.Network.Consensus.CoinbaseMaturity;

                HdAddress addr = TestHelper.MineBlocks(scSender, maturity + 5).AddressUsed;

                int spendable = GetSpendableBlocks(maturity + 5, maturity);
                var total     = scSender.FullNode.WalletManager().GetSpendableTransactionsInWallet(WalletName).Sum(s => s.Transaction.Amount);
                Assert.Equal(Money.COIN * spendable * 50, total);

                SmartContractsController      senderSmartContractsController = scSender.FullNode.NodeService <SmartContractsController>();
                SmartContractWalletController senderWalletController         = scSender.FullNode.NodeService <SmartContractWalletController>();
                ContractCompilationResult     compilationResult = ContractCompiler.CompileFile("SmartContracts/StorageDemo.cs");
                Assert.True(compilationResult.Success);

                Gas gasLimit = (Gas)(SmartContractFormatRule.GasLimitMaximum / 2);

                var buildRequest = new BuildCreateContractTransactionRequest
                {
                    AccountName  = AccountName,
                    GasLimit     = gasLimit,
                    GasPrice     = SmartContractMempoolValidator.MinGasPrice,
                    ContractCode = compilationResult.Compilation.ToHexString(),
                    FeeAmount    = "0.001",
                    Password     = Password,
                    WalletName   = WalletName,
                    Sender       = addr.Address
                };

                JsonResult result   = (JsonResult)senderSmartContractsController.BuildCreateSmartContractTransaction(buildRequest);
                var        response = (BuildCreateContractTransactionResponse)result.Value;
                TestHelper.Connect(scSender, scReceiver);

                SmartContractSharedSteps.SendTransaction(scSender, scReceiver, senderWalletController, response.Hex);
                TestHelper.MineBlocks(scReceiver, 2);
                TestHelper.WaitLoop(() => TestHelper.AreNodesSynced(scReceiver, scSender));

                // Make a call request where the MethodName is the name of a property
                var callRequest = new LocalCallContractRequest
                {
                    GasLimit        = gasLimit,
                    GasPrice        = SmartContractMempoolValidator.MinGasPrice,
                    Amount          = "0",
                    MethodName      = "Counter",
                    ContractAddress = response.NewContractAddress,
                    Sender          = addr.Address
                };

                result = (JsonResult)senderSmartContractsController.LocalCallSmartContractTransaction(callRequest);
                var callResponse = (ILocalExecutionResult)result.Value;

                // Check that the locally executed transaction returns the correct results
                Assert.Equal(12345, callResponse.Return);
                Assert.False(callResponse.Revert);
                Assert.True(callResponse.GasConsumed > 0);
                Assert.Null(callResponse.ErrorMessage);
                Assert.NotNull(callResponse.InternalTransfers);

                TestHelper.MineBlocks(scReceiver, 2);
                TestHelper.WaitLoop(() => TestHelper.AreNodesSynced(scReceiver, scSender));
            }
        }