コード例 #1
0
        public void CallContractOnLocalNodeTest()
        {
            Account account = nativeClient.GetAccount(baseKeyPair.PublicKey);
            ulong   nonce   = account.Nonce + 1;

            Calldata callData = nativeClient.EncodeCalldata(logger, TestConstants.TestContractSourceCode, TestConstants.TestContractFunction, TestConstants.TestContractFunctionParams);

            UnsignedTx unsignedTxNative = CreateUnsignedContractCallTx(nonce, callData.CallData, null);
            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>());
        }
コード例 #2
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);
            }
        }
コード例 #3
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>());
            }
        }
コード例 #4
0
        public void DeployPaymentSplitter()
        {
            ByteCode byteCode = nativeClient.Compile(paymentSplitterSource, null, null);
            Calldata calldata = nativeClient.EncodeCalldata(logger, paymentSplitterSource, "init", new List <string> {
                GenerateMapParam(initialWeights)
            });

            logger.LogInformation("contract bytecode: " + byteCode.Bytecode);
            logger.LogInformation("contract calldata: " + calldata.CallData);
            Account account = nativeClient.GetAccount(baseKeyPair.PublicKey);
            string  ownerId = baseKeyPair.PublicKey;

            ushort     abiVersion = Constants.BaseConstants.ABI_VERSION;
            ushort     vmVersion  = Constants.BaseConstants.VM_VERSION;
            BigInteger amount     = 0;
            BigInteger deposit    = 0;
            ulong      ttl        = 0;
            ulong      gas        = 4800000;
            BigInteger gasPrice   = Constants.BaseConstants.MINIMAL_GAS_PRICE;
            ulong      nonce      = account.Nonce + 1;

            ContractCreateTransaction contractTx = nativeClient.CreateContractCreateTransaction(abiVersion, amount, calldata.CallData, byteCode.Bytecode, deposit, gas, gasPrice, nonce, ownerId, ttl, vmVersion);

            UnsignedTx unsignedTx = contractTx.CreateUnsignedTransaction();

            logger.LogInformation("Unsigned Tx - hash - dryRun: " + unsignedTx.TX);
            Tx signedTxNative = nativeClient.SignTransaction(unsignedTx, baseKeyPair.PrivateKey);

            logger.LogInformation("CreateContractTx hash (native signed): " + signedTxNative);

            PostTxResponse postTxResponse = nativeClient.PostTx(logger, signedTxNative);
            TxInfoObject   txInfoObject   = nativeClient.WaitForTxInfoObject(logger, postTxResponse.TXHash);

            localDeployedContractId = txInfoObject.CallInfo.ContractId;
            logger.LogInformation("Deployed contract - hash " + postTxResponse.TXHash + " - " + txInfoObject);
            if ("revert".Equals(txInfoObject.CallInfo.ReturnType))
            {
                Assert.Fail("transaction reverted: " + nativeClient.DecodeCalldata(logger, txInfoObject.CallInfo.ReturnValue, "string"));
            }
        }
コード例 #5
0
        public void TestEncodeCalldata()
        {
            Calldata callData = nativeClient.EncodeCallData(TestConstants.TestContractSourceCode, "init");

            Assert.AreEqual(TestConstants.TestContractCallData, callData.CallData);
        }
コード例 #6
0
        public void TestCompileContractCall()
        {
            Calldata calldata = nativeClient.EncodeCallData(TestConstants.TestContractSourceCode, TestConstants.TestContractFunction, TestConstants.TestContractFunctionParams);

            Assert.AreEqual(TestConstants.EncodedServiceCall, calldata.CallData);
        }
コード例 #7
0
        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);
        }