mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-29 13:41:47 +00:00
parent
74f0019df2
commit
77296f6481
6 changed files with 74 additions and 15 deletions
|
@ -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.
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
)
|
||||
|
||||
|
|
|
@ -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"`
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue