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

@ -30,7 +30,7 @@ type Client struct {
ctx context.Context ctx context.Context
version string version string
Wif *wallet.WIF Wif *wallet.WIF
Balancer BalanceGetter Balancer BalanceGetter
} }
// ClientOptions defines options for the RPC client. // ClientOptions defines options for the RPC client.

View file

@ -11,7 +11,7 @@ import (
errs "github.com/pkg/errors" errs "github.com/pkg/errors"
) )
func (s NeoScanServer) getBalance(address string) ([]*Unspent, error) { func (s NeoScanServer) GetBalance(address string) ([]*Unspent, error) {
var ( var (
err error err error
req *http.Request req *http.Request
@ -21,7 +21,7 @@ func (s NeoScanServer) getBalance(address string) ([]*Unspent, error) {
balanceURL = s.URL + s.Path 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") 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) { func (s NeoScanServer) CalculateInputs(address string, assetIdUint util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
var ( var (
err error err error
num, i = uint16(0), uint16(0) num, i = uint16(0), uint16(0)
required = cost required = cost
selected = util.Fixed8(0) selected = util.Fixed8(0)
@ -64,7 +64,7 @@ func (s NeoScanServer) CalculateInputs(address string, assetIdUint util.Uint256,
assetUnspent Unspent assetUnspent Unspent
assetId = GlobalAssets[assetIdUint.String()] 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) return nil, util.Fixed8(0), errs.Wrapf(err, "Cannot get balance for address %v", address)
} }
filterSpecificAsset(assetId, us, &assetUnspent) filterSpecificAsset(assetId, us, &assetUnspent)

View file

@ -108,27 +108,38 @@ func (c *Client) SendRawTransaction(rawTX string) (*response, error) {
// SendToAddress sends an amount of specific asset to a given address. // SendToAddress sends an amount of specific asset to a given address.
// This call requires open wallet. (`Wif` key in client struct.) // 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. // 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 ( var (
err error err error
buf = &bytes.Buffer{} buf = &bytes.Buffer{}
rawTx *transaction.Transaction rawTx *transaction.Transaction
rawTxStr string rawTxStr string
txParams = ContractTxParams{ txParams = ContractTxParams{
assetId: asset, assetId: asset,
address: address, address: address,
value: amount, value: amount,
wif: *c.Wif, wif: *c.Wif,
balancer: c.Balancer, balancer: c.Balancer,
} }
resp *response
response = &SendToAddressResponse{}
) )
if rawTx, err = CreateRawContractTransaction(txParams); err != nil { 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 { 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()) 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) receiverOutput = transaction.NewOutput(assetID, amount, toAddressHash)
tx.AddOutput(receiverOutput) 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") return nil, errs.Wrap(err, "Failed to create invocation script")
} }
if witness.VerificationScript, err = wif.GetVerificationScript(); err != nil { if witness.VerificationScript, err = wif.GetVerificationScript(); err != nil {
@ -65,7 +65,7 @@ func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transac
return tx, nil return tx, nil
} }
func getInvocationScript(tx *transaction.Transaction, wif wallet.WIF) ([]byte, error) { func GetInvocationScript(tx *transaction.Transaction, wif wallet.WIF) ([]byte, error) {
const ( const (
pushbytes64 = 0x40 pushbytes64 = 0x40
) )

View file

@ -36,6 +36,7 @@ type (
// total: summarized asset amount from all the `inputs` // total: summarized asset amount from all the `inputs`
// error: error would be considered in the caller function // 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) 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 package rpc
import "github.com/CityOfZion/neo-go/pkg/core/transaction"
type InvokeScriptResponse struct { type InvokeScriptResponse struct {
responseHeader responseHeader
Error *Error `json:"error,omitempty"` Error *Error `json:"error,omitempty"`
@ -73,3 +75,48 @@ type response struct {
Error *Error `json:"error"` Error *Error `json:"error"`
Result interface{} `json:"result"` 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"`
}