Exemplo n.º 1
0
        // Transfer given amount of UTXO resource to another address
        /// <summary>Transfer given amount of UTXO resource to another address</summary>
        /// <param name="bcname">the name of the blockchain</param>
        /// <param name="to">the receiver address</param>
        /// <param name="amount">the amount of UTXO resource</param>
        /// <param name="desc">addtional information attached to this transaction</param>
        /// <return>the response contains the Txid of this transaction</return>
        public Response Transfer(string bcname, string to, BigInteger amount, string desc)
        {
            var response = new Response()
            {
                Error = new XChainError()
                {
                    ErrorCode    = ErrorCode.Success,
                    ErrorMessage = "Success",
                }
            };

            if (string.IsNullOrEmpty(bcname) || string.IsNullOrEmpty(to))
            {
                return(MakeErrorResponse(ErrorCode.ParamError, null));
            }
            var utxo = SelectUTXO(bcname, this.Account.Address, this.Account.PublicKey, amount);

            if (utxo == null)
            {
                Console.WriteLine("Select utxo failed");
                return(MakeErrorResponse(ErrorCode.SelectUTXOError, null));
            }
            var tx = AssembleTx(utxo, this.Account, null, to, amount, null, desc);

            if (tx == null)
            {
                Console.WriteLine("AssembleTx failed");
                return(MakeErrorResponse(ErrorCode.UnknownError, null));
            }
            // post transaction
            var postRes = PostTx(bcname, tx);

            if (postRes == null || postRes.Header.Error != Pb.XChainErrorEnum.Success)
            {
                Console.WriteLine("InvokeContract: PostTx failed");
                return(MakeErrorResponse(ErrorCode.PostError, null));
            }
            response.Txid = XConvert.ByteArrayToHexString(tx.Txid).ToLower();
            return(response);
        }
Exemplo n.º 2
0
        // Invokes contract method with given args
        // TODO: multisig is not supported
        /// <summary>Invokes contract method with given args.
        ///     TODO: multisig is not supported</summary>
        /// <param name="bcname">the name of blockchain</param>
        /// <param name="contractName">the name of contract to invoke</param>
        /// <param name="method">the method name of contract to invoke</param>
        /// <param name="args">the arguments of contract</param>
        /// <param name="authRequire">add more address if multisig needed, otherwise keep null</param>
        /// <param name="contractType">the contract VM type, default to WASM</param>
        /// <param name="desc">description info, default to empty</param>
        public Response InvokeContract(string bcname, string contractName, string method,
                                       SortedDictionary <string, byte[]> args, List <string> authRequire = null, string desc = "",
                                       ContactVMType.Type contractType = ContactVMType.Type.WASM)
        {
            if (string.IsNullOrEmpty(bcname) || string.IsNullOrEmpty(method))
            {
                return(MakeErrorResponse(ErrorCode.ParamError, null));
            }
            // pre-execute contract
            var execRes = PreExecWithSelectUTXO(bcname, this.Account.Address,
                                                this.Account.PrivateKey, 0, contractName, method, args, contractType,
                                                this.Account.Address, authRequire);

            if (execRes == null)
            {
                Console.WriteLine("InvokeContract: PreExecWithSelectUTXO failed");
                return(MakeErrorResponse(ErrorCode.PostError, null));
            }
            // check contract response
            var contractResult = new Dictionary <string, string>();

            for (var i = 0; i < execRes.Response.Responses.Count; i++)
            {
                if (execRes.Response.Responses[i].Status >= 400)
                {
                    Console.WriteLine("Contract execute failed. res=" +
                                      JsonConvert.SerializeObject(execRes.Response.Responses[i]));
                    return(new Response
                    {
                        Error = new XChainError
                        {
                            ErrorCode = ErrorCode.Success,
                            ErrorMessage = "Success",
                        },
                    });
                }
                contractResult.Add(execRes.Response.Requests[i].ContractName + ":" + execRes.Response.Requests[i].MethodName,
                                   Encoding.ASCII.GetString(execRes.Response.Responses[i].Body.ToByteArray()));
            }

            // assemble transaction
            var tx = AssembleTx(execRes.UtxoOutput, this.Account, authRequire, "", 0, execRes.Response, desc);

            if (tx == null)
            {
                Console.WriteLine("InvokeContract: AssembleTx failed");
                return(MakeErrorResponse(ErrorCode.UnknownError, null));
            }

            // post transaction
            var postRes = PostTx(bcname, tx);

            if (postRes == null || postRes.Header.Error != Pb.XChainErrorEnum.Success)
            {
                Console.WriteLine("InvokeContract: PostTx failed");
                return(MakeErrorResponse(ErrorCode.PostError, null));
            }
            var res = new Response
            {
                Error = new XChainError
                {
                    ErrorCode = ErrorCode.Success,
                },
                Txid = XConvert.ByteArrayToHexString(tx.Txid),
                Data = contractResult,
            };

            return(res);
        }