2018-12-21 09:32:18 +00:00
|
|
|
package rpc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
|
|
|
"sort"
|
|
|
|
|
2019-11-19 17:16:15 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core"
|
2018-12-21 09:32:18 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/core/transaction"
|
2019-11-15 19:04:10 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/rpc/wrappers"
|
2018-12-21 09:32:18 +00:00
|
|
|
"github.com/CityOfZion/neo-go/pkg/util"
|
|
|
|
errs "github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// GetBalance performs a request to get balance for the address specified.
|
2019-01-22 12:14:40 +00:00
|
|
|
func (s NeoScanServer) GetBalance(address string) ([]*Unspent, error) {
|
2018-12-21 09:32:18 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
req *http.Request
|
|
|
|
res *http.Response
|
|
|
|
balance NeoScanBalance
|
|
|
|
client = http.Client{}
|
|
|
|
balanceURL = s.URL + s.Path
|
|
|
|
)
|
|
|
|
|
2019-01-22 12:14:40 +00:00
|
|
|
if req, err = http.NewRequest(http.MethodGet, balanceURL+address, nil); err != nil {
|
2018-12-21 09:32:18 +00:00
|
|
|
return nil, errs.Wrap(err, "Failed to compose HTTP request")
|
|
|
|
}
|
|
|
|
|
|
|
|
if res, err = client.Do(req); err != nil {
|
|
|
|
return nil, errs.Wrap(err, "Failed to perform HTTP request")
|
|
|
|
}
|
|
|
|
|
2019-01-25 11:20:35 +00:00
|
|
|
defer res.Body.Close()
|
2018-12-21 09:32:18 +00:00
|
|
|
|
|
|
|
if err = json.NewDecoder(res.Body).Decode(&balance); err != nil {
|
|
|
|
return nil, errs.Wrap(err, "Failed to decode HTTP response")
|
|
|
|
}
|
|
|
|
return balance.Balance, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterSpecificAsset(asset string, balance []*Unspent, assetBalance *Unspent) {
|
|
|
|
for _, us := range balance {
|
|
|
|
if us.Asset == asset {
|
|
|
|
assetBalance.Unspent = us.Unspent
|
|
|
|
assetBalance.Asset = us.Asset
|
|
|
|
assetBalance.Amount = us.Amount
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-03 14:51:37 +00:00
|
|
|
// CalculateInputs creates input transactions for the specified amount of given asset belonging to specified address.
|
2019-09-03 15:11:13 +00:00
|
|
|
func (s NeoScanServer) CalculateInputs(address string, assetIDUint util.Uint256, cost util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
|
2018-12-21 09:32:18 +00:00
|
|
|
var (
|
2019-01-22 12:14:40 +00:00
|
|
|
err error
|
2018-12-21 09:32:18 +00:00
|
|
|
us []*Unspent
|
|
|
|
assetUnspent Unspent
|
2019-11-15 19:04:10 +00:00
|
|
|
assetID = wrappers.GlobalAssets[assetIDUint.ReverseString()]
|
2018-12-21 09:32:18 +00:00
|
|
|
)
|
2019-01-22 12:14:40 +00:00
|
|
|
if us, err = s.GetBalance(address); err != nil {
|
2018-12-21 09:32:18 +00:00
|
|
|
return nil, util.Fixed8(0), errs.Wrapf(err, "Cannot get balance for address %v", address)
|
|
|
|
}
|
2019-09-03 15:11:13 +00:00
|
|
|
filterSpecificAsset(assetID, us, &assetUnspent)
|
2019-11-19 17:16:15 +00:00
|
|
|
return unspentsToInputs(assetUnspent.Unspent, cost)
|
|
|
|
}
|
|
|
|
|
|
|
|
// unspentsToInputs uses UnspentBalances to create a slice of inputs for a new
|
|
|
|
// transcation containing the required amount of asset.
|
|
|
|
func unspentsToInputs(utxos core.UnspentBalances, required util.Fixed8) ([]transaction.Input, util.Fixed8, error) {
|
|
|
|
var (
|
|
|
|
num, i uint16
|
|
|
|
selected = util.Fixed8(0)
|
|
|
|
)
|
|
|
|
sort.Sort(utxos)
|
2018-12-21 09:32:18 +00:00
|
|
|
|
2019-11-19 17:16:15 +00:00
|
|
|
for _, us := range utxos {
|
2018-12-21 09:32:18 +00:00
|
|
|
if selected >= required {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
selected += us.Value
|
|
|
|
num++
|
|
|
|
}
|
|
|
|
if selected < required {
|
2019-01-25 11:20:35 +00:00
|
|
|
return nil, util.Fixed8(0), errors.New("cannot compose inputs for transaction; check sender balance")
|
2018-12-21 09:32:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inputs := make([]transaction.Input, 0, num)
|
|
|
|
for i = 0; i < num; i++ {
|
|
|
|
inputs = append(inputs, transaction.Input{
|
2019-11-19 17:16:15 +00:00
|
|
|
PrevHash: utxos[i].Tx,
|
|
|
|
PrevIndex: utxos[i].Index,
|
2018-12-21 09:32:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return inputs, selected, nil
|
|
|
|
}
|