Skip to content

Commit

Permalink
Merge pull request #531 from stellar/offers-liabilities
Browse files Browse the repository at this point in the history
Add offer liabilities
  • Loading branch information
bartekn authored Aug 2, 2018
2 parents 5cfcd4a + c60f888 commit 0e29fa7
Show file tree
Hide file tree
Showing 11 changed files with 5,893 additions and 5,753 deletions.
8 changes: 5 additions & 3 deletions protocols/horizon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type Account struct {
}

// GetNativeBalance returns the native balance of the account
func (a Account) GetNativeBalance() (string, error){
func (a Account) GetNativeBalance() (string, error) {
for _, balance := range a.Balances {
if balance.Asset.Type == "native" {
return balance.Balance, nil
Expand Down Expand Up @@ -123,8 +123,10 @@ func (res AssetStat) PagingToken() string {

// Balance represents an account's holdings for a single currency type
type Balance struct {
Balance string `json:"balance"`
Limit string `json:"limit,omitempty"`
Balance string `json:"balance"`
Limit string `json:"limit,omitempty"`
BuyingLiabilities string `json:"buying_liabilities"`
SellingLiabilities string `json:"selling_liabilities"`
base.Asset
}

Expand Down
9 changes: 6 additions & 3 deletions services/horizon/internal/actions_account.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package horizon

import (
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/render/sse"
"github.com/stellar/go/services/horizon/internal/resourceadapter"
"github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/support/render/hal"
)

Expand Down Expand Up @@ -55,8 +55,11 @@ func (action *AccountShowAction) loadParams() {
}

func (action *AccountShowAction) loadRecord() {
app := AppFromContext(action.R.Context())
protocolVersion := app.protocolVersion

action.Err = action.CoreQ().
AccountByAddress(&action.CoreRecord, action.Address)
AccountByAddress(&action.CoreRecord, action.Address, protocolVersion)
if action.Err != nil {
return
}
Expand All @@ -74,7 +77,7 @@ func (action *AccountShowAction) loadRecord() {
}

action.Err = action.CoreQ().
TrustlinesByAddress(&action.CoreTrustlines, action.Address)
TrustlinesByAddress(&action.CoreTrustlines, action.Address, protocolVersion)
if action.Err != nil {
return
}
Expand Down
4 changes: 4 additions & 0 deletions services/horizon/internal/actions_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ func (action *PathIndexAction) loadQuery() {
}

func (action *PathIndexAction) loadSourceAssets() {
app := AppFromContext(action.R.Context())
protocolVersion := app.protocolVersion

action.Err = action.CoreQ().AssetsForAddress(
&action.Query.SourceAssets,
action.GetAddress("source_account"),
protocolVersion,
)
}

Expand Down
27 changes: 25 additions & 2 deletions services/horizon/internal/db2/core/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ func (ac Account) IsAuthRevocable() bool {
}

// AccountByAddress loads a row from `accounts`, by address
func (q *Q) AccountByAddress(dest interface{}, addy string) error {
sql := selectAccount.Limit(1).Where("accountid = ?", addy)
func (q *Q) AccountByAddress(dest interface{}, addy string, protocolVersion int32) error {
var selectQuery sq.SelectBuilder

if protocolVersion >= 10 {
selectQuery = selectAccount
} else {
selectQuery = selectAccountPreV10
}

sql := selectQuery.Limit(1).Where("accountid = ?", addy)

return q.Get(dest, sql)
}
Expand Down Expand Up @@ -68,4 +76,19 @@ var selectAccount = sq.Select(
"a.homedomain",
"a.thresholds",
"a.flags",
// Liabilities can be NULL so can error without `coalesce`:
// `Invalid value for xdr.Int64`
"coalesce(a.buyingliabilities, 0) as buyingliabilities",
"coalesce(a.sellingliabilities, 0) as sellingliabilities",
).From("accounts a")

var selectAccountPreV10 = sq.Select(
"a.accountid",
"a.balance",
"a.seqnum",
"a.numsubentries",
"a.inflationdest",
"a.homedomain",
"a.thresholds",
"a.flags",
).From("accounts a")
34 changes: 19 additions & 15 deletions services/horizon/internal/db2/core/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (

// Account is a row of data from the `accounts` table
type Account struct {
Accountid string
Balance xdr.Int64
Seqnum string
Numsubentries int32
Inflationdest null.String
HomeDomain null.String
Thresholds xdr.Thresholds
Flags xdr.AccountFlags
Accountid string
Balance xdr.Int64
Seqnum string
Numsubentries int32
Inflationdest null.String
HomeDomain null.String
Thresholds xdr.Thresholds
Flags xdr.AccountFlags
BuyingLiabilities xdr.Int64 `db:"buyingliabilities"`
SellingLiabilities xdr.Int64 `db:"sellingliabilities"`
}

// AccountData is a row of data from the `accountdata` table
Expand Down Expand Up @@ -117,13 +119,15 @@ type TransactionFee struct {

// Trustline is a row of data from the `trustlines` table from stellar-core
type Trustline struct {
Accountid string
Assettype xdr.AssetType
Issuer string
Assetcode string
Tlimit xdr.Int64
Balance xdr.Int64
Flags int32
Accountid string
Assettype xdr.AssetType
Issuer string
Assetcode string
Tlimit xdr.Int64
Balance xdr.Int64
Flags int32
BuyingLiabilities xdr.Int64 `db:"buyingliabilities"`
SellingLiabilities xdr.Int64 `db:"sellingliabilities"`
}

// AssetFromDB produces an xdr.Asset by combining the constituent type, code and
Expand Down
31 changes: 27 additions & 4 deletions services/horizon/internal/db2/core/trustline.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (

// AssetsForAddress loads `dest` as `[]xdr.Asset` with every asset the account
// at `addy` can hold.
func (q *Q) AssetsForAddress(dest interface{}, addy string) error {
func (q *Q) AssetsForAddress(dest interface{}, addy string, protocolVersion int32) error {
var tls []Trustline

err := q.TrustlinesByAddress(&tls, addy)
err := q.TrustlinesByAddress(&tls, addy, protocolVersion)
if err != nil {
return err
}
Expand All @@ -38,8 +38,16 @@ func (q *Q) AssetsForAddress(dest interface{}, addy string) error {
}

// TrustlinesByAddress loads all trustlines for `addy`
func (q *Q) TrustlinesByAddress(dest interface{}, addy string) error {
sql := selectTrustline.Where("accountid = ?", addy)
func (q *Q) TrustlinesByAddress(dest interface{}, addy string, protocolVersion int32) error {
var selectQuery sq.SelectBuilder

if protocolVersion >= 10 {
selectQuery = selectTrustline
} else {
selectQuery = selectTrustlinePreV10
}

sql := selectQuery.Where("accountid = ?", addy)
return q.Select(dest, sql)
}

Expand Down Expand Up @@ -71,5 +79,20 @@ var selectTrustline = sq.Select(
"tl.tlimit",
"tl.balance",
"tl.flags",
// Liabilities can be NULL so can error without `coalesce`:
// `Invalid value for xdr.Int64`
"coalesce(tl.buyingliabilities, 0) as buyingliabilities",
"coalesce(tl.sellingliabilities, 0) as sellingliabilities",
).From("trustlines tl")

var selectTrustlinePreV10 = sq.Select(
"tl.accountid",
"tl.assettype",
"tl.issuer",
"tl.assetcode",
"tl.tlimit",
"tl.balance",
"tl.flags",
).From("trustlines tl")

var selectBalances = sq.Select("COUNT(*)", "COALESCE(SUM(balance), 0) as sum").From("trustlines")
3 changes: 2 additions & 1 deletion services/horizon/internal/ingest/asset_stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ func statTrustlinesInfo(coreQ *core.Q, assetType xdr.AssetType, assetCode string
// statAccountInfo fetches all the stats from the accounts table
func statAccountInfo(coreQ *core.Q, accountID string) (int8, string, error) {
var account core.Account
err := coreQ.AccountByAddress(&account, accountID)
// We don't need liabilities data here so let's use the old V9 query
err := coreQ.AccountByAddress(&account, accountID, 9)
if err != nil {
return -1, "", errors.Wrap(err, "coreQ.AccountByAddress error")
}
Expand Down
5 changes: 2 additions & 3 deletions services/horizon/internal/resourceadapter/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"
"fmt"

. "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/httpx"
. "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/support/render/hal"
)

Expand Down Expand Up @@ -41,7 +41,7 @@ func PopulateAccount(
}

// add native balance
err = PopulateNativeBalance(&dest.Balances[len(dest.Balances)-1], ca.Balance)
err = PopulateNativeBalance(&dest.Balances[len(dest.Balances)-1], ca.Balance, ca.BuyingLiabilities, ca.SellingLiabilities)
if err != nil {
return
}
Expand Down Expand Up @@ -73,4 +73,3 @@ func PopulateAccount(
dest.Links.Data.PopulateTemplated()
return
}

8 changes: 6 additions & 2 deletions services/horizon/internal/resourceadapter/balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"context"

"github.com/stellar/go/amount"
. "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/assets"
"github.com/stellar/go/services/horizon/internal/db2/core"
"github.com/stellar/go/xdr"
. "github.com/stellar/go/protocols/horizon"
)

func PopulateBalance(ctx context.Context, dest *Balance, row core.Trustline) (err error) {
Expand All @@ -17,19 +17,23 @@ func PopulateBalance(ctx context.Context, dest *Balance, row core.Trustline) (er
}

dest.Balance = amount.String(row.Balance)
dest.BuyingLiabilities = amount.String(row.BuyingLiabilities)
dest.SellingLiabilities = amount.String(row.SellingLiabilities)
dest.Limit = amount.String(row.Tlimit)
dest.Issuer = row.Issuer
dest.Code = row.Assetcode
return
}

func PopulateNativeBalance(dest *Balance, stroops xdr.Int64) (err error) {
func PopulateNativeBalance(dest *Balance, stroops, buyingLiabilities, sellingLiabilities xdr.Int64) (err error) {
dest.Type, err = assets.String(xdr.AssetTypeAssetTypeNative)
if err != nil {
return
}

dest.Balance = amount.String(stroops)
dest.BuyingLiabilities = amount.String(buyingLiabilities)
dest.SellingLiabilities = amount.String(sellingLiabilities)
dest.Limit = ""
dest.Issuer = ""
dest.Code = ""
Expand Down
34 changes: 31 additions & 3 deletions xdr/Stellar-ledger-entries.x
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ typedef opaque Thresholds[4];
typedef string string32<32>;
typedef string string64<64>;
typedef int64 SequenceNumber;
typedef opaque DataValue<64>;
typedef opaque DataValue<64>;

enum AssetType
{
Expand Down Expand Up @@ -50,6 +50,12 @@ struct Price
int32 d; // denominator
};

struct Liabilities
{
int64 buying;
int64 selling;
};

// the 'Thresholds' type is packed uint8_t values
// defined by these indexes
enum ThresholdIndexes
Expand Down Expand Up @@ -123,6 +129,18 @@ struct AccountEntry
{
case 0:
void;
case 1:
struct
{
Liabilities liabilities;

union switch (int v)
{
case 0:
void;
}
ext;
} v1;
}
ext;
};
Expand All @@ -139,7 +157,6 @@ enum TrustLineFlags
AUTHORIZED_FLAG = 1
};


// mask for all trustline flags
const MASK_TRUSTLINE_FLAGS = 1;

Expand All @@ -158,6 +175,18 @@ struct TrustLineEntry
{
case 0:
void;
case 1:
struct
{
Liabilities liabilities;

union switch (int v)
{
case 0:
void;
}
ext;
} v1;
}
ext;
};
Expand Down Expand Up @@ -221,7 +250,6 @@ struct DataEntry
ext;
};


struct LedgerEntry
{
uint32 lastModifiedLedgerSeq; // ledger the LedgerEntry was last changed
Expand Down
Loading

0 comments on commit 0e29fa7

Please sign in to comment.