forked from TrueCloudLab/neoneo-go
parent
74f0019df2
commit
77296f6481
6 changed files with 74 additions and 15 deletions
|
@ -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
|
||||||
|
@ -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)
|
||||||
|
|
|
@ -108,7 +108,7 @@ 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{}
|
||||||
|
@ -121,14 +121,25 @@ func (c *Client) SendToAddress(asset util.Uint256, address string, amount util.F
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
)
|
)
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -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"`
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue