From 77296f6481ff7e38d3622ba789a60428c55cc5ee Mon Sep 17 00:00:00 2001 From: Anastasia Prasolova Date: Tue, 22 Jan 2019 15:14:40 +0300 Subject: [PATCH] Small fixes (#117) * small fixes * gofmt --- pkg/rpc/client.go | 2 +- pkg/rpc/neoScanBalanceGetter.go | 8 +++--- pkg/rpc/rpc.go | 27 +++++++++++++------ pkg/rpc/txBuilder.go | 4 +-- pkg/rpc/txTypes.go | 1 + pkg/rpc/types.go | 47 +++++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 15 deletions(-) diff --git a/pkg/rpc/client.go b/pkg/rpc/client.go index c9fc7d41f..3ed7c4335 100644 --- a/pkg/rpc/client.go +++ b/pkg/rpc/client.go @@ -30,7 +30,7 @@ type Client struct { ctx context.Context version string Wif *wallet.WIF - Balancer BalanceGetter + Balancer BalanceGetter } // ClientOptions defines options for the RPC client. diff --git a/pkg/rpc/neoScanBalanceGetter.go b/pkg/rpc/neoScanBalanceGetter.go index 0c991644a..381656444 100644 --- a/pkg/rpc/neoScanBalanceGetter.go +++ b/pkg/rpc/neoScanBalanceGetter.go @@ -11,7 +11,7 @@ import ( errs "github.com/pkg/errors" ) -func (s NeoScanServer) getBalance(address string) ([]*Unspent, error) { +func (s NeoScanServer) GetBalance(address string) ([]*Unspent, error) { var ( err error req *http.Request @@ -21,7 +21,7 @@ func (s NeoScanServer) getBalance(address string) ([]*Unspent, error) { balanceURL = s.URL + s.Path ) - if req, err = http.NewRequest(http.MethodGet, balanceURL + address, nil); err != nil { + if req, err = http.NewRequest(http.MethodGet, balanceURL+address, nil); err != nil { return nil, errs.Wrap(err, "Failed to compose HTTP request") } @@ -56,7 +56,7 @@ func filterSpecificAsset(asset string, balance []*Unspent, assetBalance *Unspent func (s NeoScanServer) CalculateInputs(address string, assetIdUint util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) { var ( - err error + err error num, i = uint16(0), uint16(0) required = cost selected = util.Fixed8(0) @@ -64,7 +64,7 @@ func (s NeoScanServer) CalculateInputs(address string, assetIdUint util.Uint256, assetUnspent Unspent assetId = GlobalAssets[assetIdUint.String()] ) - if us, err = s.getBalance(address); err != nil { + if us, err = s.GetBalance(address); err != nil { return nil, util.Fixed8(0), errs.Wrapf(err, "Cannot get balance for address %v", address) } filterSpecificAsset(assetId, us, &assetUnspent) diff --git a/pkg/rpc/rpc.go b/pkg/rpc/rpc.go index ea6a5b325..8b444f2c3 100644 --- a/pkg/rpc/rpc.go +++ b/pkg/rpc/rpc.go @@ -108,27 +108,38 @@ func (c *Client) SendRawTransaction(rawTX string) (*response, error) { // SendToAddress sends an amount of specific asset to a given address. // This call requires open wallet. (`Wif` key in client struct.) // If response.Result is `true` then transaction was formed correctly and was written in blockchain. -func (c *Client) SendToAddress(asset util.Uint256, address string, amount util.Fixed8) (*response, error) { +func (c *Client) SendToAddress(asset util.Uint256, address string, amount util.Fixed8) (*SendToAddressResponse, error) { var ( err error buf = &bytes.Buffer{} rawTx *transaction.Transaction rawTxStr string txParams = ContractTxParams{ - assetId: asset, - address: address, - value: amount, - wif: *c.Wif, + assetId: asset, + address: address, + value: amount, + wif: *c.Wif, balancer: c.Balancer, } + resp *response + response = &SendToAddressResponse{} ) if rawTx, err = CreateRawContractTransaction(txParams); err != nil { - return nil, errors.Wrap(err, "Failed to create raw transaction for `sendtoaddress`") + return nil, errors.Wrap(err, "failed to create raw transaction for `sendtoaddress`") } if err = rawTx.EncodeBinary(buf); err != nil { - return nil, errors.Wrap(err, "Failed to encode raw transaction to binary for `sendtoaddress`") + return nil, errors.Wrap(err, "failed to encode raw transaction to binary for `sendtoaddress`") } rawTxStr = hex.EncodeToString(buf.Bytes()) - return c.SendRawTransaction(rawTxStr) + if resp, err = c.SendRawTransaction(rawTxStr); err != nil { + return nil, errors.Wrap(err, "failed to send raw transaction") + } + response.Error = resp.Error + response.ID = resp.ID + response.JSONRPC = resp.JSONRPC + response.Result = &TxResponse{ + TxID: rawTx.Hash().String(), + } + return response, nil } diff --git a/pkg/rpc/txBuilder.go b/pkg/rpc/txBuilder.go index 9f93591f7..7d098f213 100644 --- a/pkg/rpc/txBuilder.go +++ b/pkg/rpc/txBuilder.go @@ -53,7 +53,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac receiverOutput = transaction.NewOutput(assetID, amount, toAddressHash) tx.AddOutput(receiverOutput) - if witness.InvocationScript, err = getInvocationScript(tx, wif); err != nil { + if witness.InvocationScript, err = GetInvocationScript(tx, wif); err != nil { return nil, errs.Wrap(err, "Failed to create invocation script") } if witness.VerificationScript, err = wif.GetVerificationScript(); err != nil { @@ -65,7 +65,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac return tx, nil } -func getInvocationScript(tx *transaction.Transaction, wif wallet.WIF) ([]byte, error) { +func GetInvocationScript(tx *transaction.Transaction, wif wallet.WIF) ([]byte, error) { const ( pushbytes64 = 0x40 ) diff --git a/pkg/rpc/txTypes.go b/pkg/rpc/txTypes.go index bbf0d7c22..740fc58cb 100644 --- a/pkg/rpc/txTypes.go +++ b/pkg/rpc/txTypes.go @@ -36,6 +36,7 @@ type ( // total: summarized asset amount from all the `inputs` // error: error would be considered in the caller function CalculateInputs(address string, assetId util.Uint256, amount util.Fixed8) (inputs []transaction.Input, total util.Fixed8, err error) + GetBalance(address string) ([]*Unspent, error) } ) diff --git a/pkg/rpc/types.go b/pkg/rpc/types.go index f59ad0fe0..797fc6348 100644 --- a/pkg/rpc/types.go +++ b/pkg/rpc/types.go @@ -1,5 +1,7 @@ package rpc +import "github.com/CityOfZion/neo-go/pkg/core/transaction" + type InvokeScriptResponse struct { responseHeader Error *Error `json:"error,omitempty"` @@ -73,3 +75,48 @@ type response struct { Error *Error `json:"error"` Result interface{} `json:"result"` } + +type SendToAddressResponse struct { + responseHeader + Error *Error `json:"error"` + Result *TxResponse +} + +// struct represents verbose output of `getrawtransaction` RPC call +type GetRawTxResponse struct { + responseHeader + Error *Error `json:"error"` + Result *RawTxResponse `json: "result"` +} + +type RawTxResponse struct { + TxResponse + BlockHash string `json: "blockhash"` + Confirmations uint `json: "confirmations"` + BlockTime uint `json: "blocktime"` +} + +type TxResponse struct { + TxID string `json: "txid"` + Size int `json: "size"` + Type string `json: "type"` // todo: convert to TransactionType + Version int `json: "version"` + Attributes []transaction.Attribute `json: "attributes"` + Vins []Vin `json: "vin"` + Vouts []Vout `json: "vout"` + SysFee int `json: "sys_fee"` + NetFee int `json: "net_fee"` + Scripts []transaction.Witness `json: "scripts"` +} + +type Vin struct { + TxId string `json: "txid"` + Vout int `json: "vout"` +} + +type Vout struct { + N int `json: "n"` + Asset string `json: "asset"` + Value int `json: "value"` + Address string `json: "address"` +}