Exemple #1
0
        public void StaticCallContractOnLocalNode()
        {
            Account account = nativeClient.GetAccount(baseKeyPair.PublicKey);
            ulong   nonce   = account.Nonce + 1;

            // Compile the call contract
            Calldata calldata = nativeClient.EncodeCalldata(logger, TestConstants.TestContractSourceCode, TestConstants.TestContractFunction, TestConstants.TestContractFunctionParams);
            List <Dictionary <AccountParameter, object> > dict = new List <Dictionary <AccountParameter, object> >();

            dict.Add(new Dictionary <AccountParameter, object> {
                { AccountParameter.PUBLIC_KEY, baseKeyPair.PublicKey }
            });
            dict.Add(new Dictionary <AccountParameter, object> {
                { AccountParameter.PUBLIC_KEY, baseKeyPair.PublicKey }
            });

            DryRunResults results = nativeClient.DryRunTransactionsAsync(dict, null, new List <UnsignedTx> {
                CreateUnsignedContractCallTx(nonce, calldata.CallData, null), CreateUnsignedContractCallTx(nonce + 1, calldata.CallData, null)
            }).TimeoutAsync(TestConstants.NUM_TRIALS_DEFAULT).RunAndUnwrap();

            logger.LogInformation(JsonConvert.SerializeObject(results));
            foreach (DryRunResult result in results.Results)
            {
                Assert.AreEqual("ok", result.Result);
            }
        }
Exemple #2
0
        public void CallContractAfterDryRunOnLocalNode()
        {
            Account account = nativeClient.GetAccount(baseKeyPair.PublicKey);
            ulong   nonce   = account.Nonce + 1;

            // Compile the call contract
            Calldata calldata = nativeClient.EncodeCalldata(logger, TestConstants.TestContractSourceCode, TestConstants.TestContractFunction, TestConstants.TestContractFunctionParams);
            List <Dictionary <AccountParameter, object> > dict = new List <Dictionary <AccountParameter, object> >();

            dict.Add(new Dictionary <AccountParameter, object> {
                { AccountParameter.PUBLIC_KEY, baseKeyPair.PublicKey }
            });
            DryRunResults results = nativeClient.PerformDryRunTransactions(logger, dict, null, new List <UnsignedTx> {
                CreateUnsignedContractCallTx(nonce, calldata.CallData, null)
            });

            logger.LogInformation("callContractAfterDryRunOnLocalNode: " + JsonConvert.SerializeObject(results));
            foreach (DryRunResult result in results.Results)
            {
                Assert.AreEqual("ok", result.Result);
                var contractAfterDryRunTx = nativeClient.CreateContractCallTransaction(Constants.BaseConstants.ABI_VERSION, calldata.CallData, localDeployedContractId, result.CallObj.GasUsed, result.CallObj.GasPrice, nonce, baseKeyPair.PublicKey, 0);

                UnsignedTx unsignedTxNative = contractAfterDryRunTx.CreateUnsignedTransaction();
                Tx         signedTxNative   = nativeClient.SignTransaction(unsignedTxNative, baseKeyPair.PrivateKey);

                // post the signed contract call tx
                PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTxNative);
                Assert.AreEqual(postTxResponse.TXHash, Encoding.ComputeTxHash(signedTxNative.TX));
                logger.LogInformation("CreateContractTx hash: " + postTxResponse.TXHash);

                // get the tx info object to resolve the result
                TxInfoObject txInfoObject = nativeClient.WaitForTxInfoObject(logger, postTxResponse.TXHash);

                // decode the result to json
                JToken json = nativeClient.DecodeCallResult(TestConstants.TestContractSourceCode, TestConstants.TestContractFunction, txInfoObject.CallInfo.ReturnType, txInfoObject.CallInfo.ReturnValue, CompileOptsBackend.Fate);


                Assert.AreEqual(TestConstants.TestContractFunctionParam, json.Value <string>());
            }
        }
        public void CallPayAndSplitMethod()
        {
            Account    account = nativeClient.GetAccount(baseKeyPair.PublicKey);
            BigInteger balanceRecipient1;
            BigInteger balanceRecipient2;
            BigInteger balanceRecipient3;

            try
            {
                balanceRecipient1 = nativeClient.GetAccount(initialReceiver1.PublicKey).Balance;
                balanceRecipient2 = nativeClient.GetAccount(initialReceiver2.PublicKey).Balance;
                balanceRecipient3 = nativeClient.GetAccount(initialReceiver3.PublicKey).Balance;
                // if one of the accounts wasn't active we get an error and know that the
                // accounts
                // don't have any balance
            }
            catch (Exception)
            {
                balanceRecipient1 = 0;
                balanceRecipient2 = 0;
                balanceRecipient3 = 0;
            }

            ulong    nonce        = account.Nonce + 1;
            decimal  paymentValue = "1".ToAettos(Unit.AE);
            Calldata calldata     = nativeClient.EncodeCalldata(logger, paymentSplitterSource, "payAndSplit", null);

            logger.LogInformation("Contract ID: " + localDeployedContractId);
            List <Dictionary <AccountParameter, object> > accounts = new List <Dictionary <AccountParameter, object> >();
            Dictionary <AccountParameter, object>         ac       = new Dictionary <AccountParameter, object>()
            {
                { AccountParameter.PUBLIC_KEY, baseKeyPair.PublicKey }
            };

            accounts.Add(ac);
            DryRunResults dryRunResults = nativeClient.PerformDryRunTransactions(logger, accounts, null, new List <UnsignedTx>()
            {
                nativeClient.CreateUnsignedContractCallTx(logger, baseKeyPair.PublicKey, nonce, calldata.CallData, null, localDeployedContractId, new BigInteger(paymentValue))
            });

            logger.LogInformation("callContractAfterDryRunOnLocalNode: " + JsonConvert.SerializeObject(dryRunResults));
            Assert.AreEqual(1, dryRunResults.Results.Count);
            DryRunResult dryRunResult = dryRunResults.Results.First();

            Assert.AreEqual("ok", dryRunResult.Result);
            ContractCallTransaction contractAfterDryRunTx = nativeClient.CreateContractCallTransaction(Constants.BaseConstants.ABI_VERSION, calldata.CallData, localDeployedContractId, dryRunResult.CallObj.GasUsed, dryRunResult.CallObj.GasPrice, nonce, baseKeyPair.PublicKey, 0);

            contractAfterDryRunTx.Model.Amount = new BigInteger(paymentValue);

            UnsignedTx unsignedTxNative = contractAfterDryRunTx.CreateUnsignedTransaction();
            Tx         signedTxNative   = nativeClient.SignTransaction(unsignedTxNative, baseKeyPair.PrivateKey);

            // post the signed contract call tx
            PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTxNative);

            Assert.AreEqual(postTxResponse.TXHash, Utils.Encoding.ComputeTxHash(signedTxNative.TX));
            logger.LogInformation("CreateContractTx hash: " + postTxResponse.TXHash);

            // we wait until the tx is available and the payment should have been splitted
            TxInfoObject txInfoObject = nativeClient.WaitForTxInfoObject(logger, postTxResponse.TXHash);

            logger.LogInformation("PayAndSplit transaction - hash " + postTxResponse.TXHash + " - " + txInfoObject);
            if ("revert".Equals(txInfoObject.CallInfo.ReturnType))
            {
                Assert.Fail("transaction reverted: " + nativeClient.DecodeCalldata(logger, txInfoObject.CallInfo.ReturnValue, "string"));
            }

            Assert.AreEqual(balanceRecipient1 + new BigInteger(paymentValue * 0.4m), nativeClient.GetAccount(initialReceiver1.PublicKey).Balance);
            Assert.AreEqual(balanceRecipient2 + new BigInteger(paymentValue * 0.4m), nativeClient.GetAccount(initialReceiver2.PublicKey).Balance);
            Assert.AreEqual(balanceRecipient3 + new BigInteger(paymentValue * 0.2m), nativeClient.GetAccount(initialReceiver3.PublicKey).Balance);
        }