mirror of
https://github.com/nspcc-dev/neo-go.git
synced 2024-11-27 03:58:06 +00:00
rpc: drop UTXO transfer support, remove Balancer
Nothing uses them now and they're irrelevant for Neo 3.
This commit is contained in:
parent
e358c70ecc
commit
337e65b696
4 changed files with 0 additions and 167 deletions
|
@ -46,13 +46,6 @@ type Client struct {
|
||||||
// All values are optional. If any duration is not specified
|
// All values are optional. If any duration is not specified
|
||||||
// a default of 4 seconds will be used.
|
// a default of 4 seconds will be used.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
// Balancer is an implementation of request.BalanceGetter interface,
|
|
||||||
// if not set then the default Client's implementation will be used, but
|
|
||||||
// it relies on server support for `getunspents` RPC call which is
|
|
||||||
// standard for neo-go, but only implemented as a plugin for C# node. So
|
|
||||||
// you can override it here to use NeoScanServer for example.
|
|
||||||
Balancer request.BalanceGetter
|
|
||||||
|
|
||||||
// Cert is a client-side certificate, it doesn't work at the moment along
|
// Cert is a client-side certificate, it doesn't work at the moment along
|
||||||
// with the other two options below.
|
// with the other two options below.
|
||||||
Cert string
|
Cert string
|
||||||
|
@ -108,9 +101,6 @@ func New(ctx context.Context, endpoint string, opts Options) (*Client, error) {
|
||||||
wifMu: new(sync.Mutex),
|
wifMu: new(sync.Mutex),
|
||||||
endpoint: url,
|
endpoint: url,
|
||||||
}
|
}
|
||||||
if opts.Balancer == nil {
|
|
||||||
opts.Balancer = cl
|
|
||||||
}
|
|
||||||
cl.opts = opts
|
cl.opts = opts
|
||||||
cl.requestF = cl.makeHTTPRequest
|
cl.requestF = cl.makeHTTPRequest
|
||||||
return cl, nil
|
return cl, nil
|
||||||
|
@ -141,27 +131,6 @@ func (c *Client) SetWIF(wif string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// CalculateInputs implements request.BalanceGetter interface and returns inputs
|
|
||||||
// array for the specified amount of given asset belonging to specified address.
|
|
||||||
// This implementation uses GetUnspents JSON-RPC call internally, so make sure
|
|
||||||
// your RPC server supports that.
|
|
||||||
func (c *Client) CalculateInputs(address string, asset util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
|
|
||||||
var utxos state.UnspentBalances
|
|
||||||
|
|
||||||
resp, err := c.GetUnspents(address)
|
|
||||||
if err != nil {
|
|
||||||
return nil, util.Fixed8(0), errors.Wrapf(err, "cannot get balance for address %v", address)
|
|
||||||
}
|
|
||||||
for _, ubi := range resp.Balance {
|
|
||||||
if asset.Equals(ubi.AssetHash) {
|
|
||||||
utxos = ubi.Unspents
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return unspentsToInputs(utxos, cost)
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Client) performRequest(method string, p request.RawParams, v interface{}) error {
|
func (c *Client) performRequest(method string, p request.RawParams, v interface{}) error {
|
||||||
var r = request.Raw{
|
var r = request.Raw{
|
||||||
JSONRPC: request.JSONRPCVersion,
|
JSONRPC: request.JSONRPCVersion,
|
||||||
|
|
|
@ -460,39 +460,6 @@ func (c *Client) SubmitBlock(b block.Block) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// TransferAsset 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) TransferAsset(asset util.Uint256, address string, amount util.Fixed8) (util.Uint256, error) {
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
rawTx *transaction.Transaction
|
|
||||||
txParams = request.ContractTxParams{
|
|
||||||
AssetID: asset,
|
|
||||||
Address: address,
|
|
||||||
Value: amount,
|
|
||||||
WIF: c.WIF(),
|
|
||||||
Balancer: c.opts.Balancer,
|
|
||||||
}
|
|
||||||
resp util.Uint256
|
|
||||||
)
|
|
||||||
|
|
||||||
if rawTx, err = request.CreateRawContractTransaction(txParams); err != nil {
|
|
||||||
return resp, errors.Wrap(err, "failed to create raw transaction")
|
|
||||||
}
|
|
||||||
|
|
||||||
validUntilBlock, err := c.CalculateValidUntilBlock()
|
|
||||||
if err != nil {
|
|
||||||
return resp, errors.Wrap(err, "failed to add validUntilBlock to raw transaction")
|
|
||||||
}
|
|
||||||
rawTx.ValidUntilBlock = validUntilBlock
|
|
||||||
|
|
||||||
if err = c.SendRawTransaction(rawTx); err != nil {
|
|
||||||
return resp, errors.Wrap(err, "failed to send raw transaction")
|
|
||||||
}
|
|
||||||
return rawTx.Hash(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SignAndPushInvocationTx signs and pushes given script as an invocation
|
// SignAndPushInvocationTx signs and pushes given script as an invocation
|
||||||
// transaction using given wif to sign it and spending the amount of gas
|
// transaction using given wif to sign it and spending the amount of gas
|
||||||
// specified. It returns a hash of the invocation transaction and an error.
|
// specified. It returns a hash of the invocation transaction and an error.
|
||||||
|
|
|
@ -5,77 +5,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/io"
|
"github.com/nspcc-dev/neo-go/pkg/io"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
|
||||||
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
||||||
errs "github.com/pkg/errors"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// CreateRawContractTransaction returns contract-type Transaction built from specified parameters.
|
|
||||||
func CreateRawContractTransaction(params ContractTxParams) (*transaction.Transaction, error) {
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
tx = transaction.NewContractTX()
|
|
||||||
toAddressHash, fromAddressHash util.Uint160
|
|
||||||
fromAddress string
|
|
||||||
receiverOutput *transaction.Output
|
|
||||||
|
|
||||||
wif, assetID, toAddress, amount, balancer = params.WIF, params.AssetID, params.Address, params.Value, params.Balancer
|
|
||||||
)
|
|
||||||
|
|
||||||
fromAddress = wif.PrivateKey.Address()
|
|
||||||
|
|
||||||
if fromAddressHash, err = address.StringToUint160(fromAddress); err != nil {
|
|
||||||
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", fromAddress)
|
|
||||||
}
|
|
||||||
|
|
||||||
if toAddressHash, err = address.StringToUint160(toAddress); err != nil {
|
|
||||||
return nil, errs.Wrapf(err, "Failed to take script hash from address: %v", toAddress)
|
|
||||||
}
|
|
||||||
tx.Sender = fromAddressHash
|
|
||||||
|
|
||||||
if err = AddInputsAndUnspentsToTx(tx, fromAddress, assetID, amount, balancer); err != nil {
|
|
||||||
return nil, errs.Wrap(err, "failed to add inputs and unspents to transaction")
|
|
||||||
}
|
|
||||||
receiverOutput = transaction.NewOutput(assetID, amount, toAddressHash)
|
|
||||||
tx.AddOutput(receiverOutput)
|
|
||||||
if acc, err := wallet.NewAccountFromWIF(wif.S); err != nil {
|
|
||||||
return nil, err
|
|
||||||
} else if err = acc.SignTx(tx); err != nil {
|
|
||||||
return nil, errs.Wrap(err, "failed to sign tx")
|
|
||||||
}
|
|
||||||
|
|
||||||
return tx, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddInputsAndUnspentsToTx adds inputs needed to transaction and one output
|
|
||||||
// with change.
|
|
||||||
func AddInputsAndUnspentsToTx(tx *transaction.Transaction, addr string, assetID util.Uint256, amount util.Fixed8, balancer BalanceGetter) error {
|
|
||||||
scriptHash, err := address.StringToUint160(addr)
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrapf(err, "failed to take script hash from address: %v", addr)
|
|
||||||
}
|
|
||||||
inputs, spent, err := balancer.CalculateInputs(addr, assetID, amount)
|
|
||||||
if err != nil {
|
|
||||||
return errs.Wrap(err, "failed to get inputs")
|
|
||||||
}
|
|
||||||
for _, input := range inputs {
|
|
||||||
tx.AddInput(&input)
|
|
||||||
}
|
|
||||||
|
|
||||||
if senderUnspent := spent - amount; senderUnspent > 0 {
|
|
||||||
senderOutput := transaction.NewOutput(assetID, senderUnspent, scriptHash)
|
|
||||||
tx.AddOutput(senderOutput)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// DetailsToSCProperties extract the fields needed from ContractDetails
|
// DetailsToSCProperties extract the fields needed from ContractDetails
|
||||||
// and converts them to smartcontract.PropertyState.
|
// and converts them to smartcontract.PropertyState.
|
||||||
func DetailsToSCProperties(contract *smartcontract.ContractDetails) smartcontract.PropertyState {
|
func DetailsToSCProperties(contract *smartcontract.ContractDetails) smartcontract.PropertyState {
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
package request
|
|
||||||
|
|
||||||
/*
|
|
||||||
Definition of types, interfaces and variables
|
|
||||||
required for raw transaction composing.
|
|
||||||
*/
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/core/transaction"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
||||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
// ContractTxParams contains parameters for tx to transfer assets.
|
|
||||||
// It includes (*Client).TransferAsset call params and utility data.
|
|
||||||
ContractTxParams struct {
|
|
||||||
AssetID util.Uint256
|
|
||||||
Address string
|
|
||||||
Value util.Fixed8
|
|
||||||
WIF keys.WIF // a WIF to send the transaction
|
|
||||||
// since there are many ways to provide unspents,
|
|
||||||
// transaction composer stays agnostic to that how
|
|
||||||
// unspents was got;
|
|
||||||
Balancer BalanceGetter
|
|
||||||
}
|
|
||||||
|
|
||||||
// BalanceGetter is an interface supporting CalculateInputs() method.
|
|
||||||
BalanceGetter interface {
|
|
||||||
// parameters
|
|
||||||
// address: base58-encoded address assets would be transferred from
|
|
||||||
// assetID: asset identifier
|
|
||||||
// amount: an asset amount to spend
|
|
||||||
// return values
|
|
||||||
// inputs: UTXO's for the preparing transaction
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
)
|
|
Loading…
Reference in a new issue