Small fixes (#117)

* small fixes

* gofmt
This commit is contained in:
Anastasia Prasolova 2019-01-22 15:14:40 +03:00 committed by Anthony De Meulemeester
parent 74f0019df2
commit 77296f6481
6 changed files with 74 additions and 15 deletions

View file

@ -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")
}
@ -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)

View file

@ -108,7 +108,7 @@ 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{}
@ -121,14 +121,25 @@ func (c *Client) SendToAddress(asset util.Uint256, address string, amount util.F
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
}

View file

@ -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
)

View file

@ -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)
}
)

View file

@ -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"`
}