forked from stellar/go
/
trade.go
67 lines (61 loc) · 1.79 KB
/
trade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package resourceadapter
import (
"context"
"fmt"
"github.com/stellar/go/amount"
. "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/httpx"
"github.com/stellar/go/support/render/hal"
)
// Populate fills out the details of a trade using a row from the history_trades
// table.
func PopulateTrade(
ctx context.Context,
dest *Trade,
row history.Trade,
) {
dest.ID = row.PagingToken()
dest.PT = row.PagingToken()
dest.OfferID = fmt.Sprintf("%d", row.OfferID)
dest.BaseOfferID = ""
if row.BaseOfferID != nil {
dest.BaseOfferID = fmt.Sprintf("%d", *row.BaseOfferID)
}
dest.BaseAccount = row.BaseAccount
dest.BaseAssetType = row.BaseAssetType
dest.BaseAssetCode = row.BaseAssetCode
dest.BaseAssetIssuer = row.BaseAssetIssuer
dest.BaseAmount = amount.String(row.BaseAmount)
dest.CounterOfferID = ""
if row.CounterOfferID != nil {
dest.CounterOfferID = fmt.Sprintf("%d", *row.CounterOfferID)
}
dest.CounterAccount = row.CounterAccount
dest.CounterAssetType = row.CounterAssetType
dest.CounterAssetCode = row.CounterAssetCode
dest.CounterAssetIssuer = row.CounterAssetIssuer
dest.CounterAmount = amount.String(row.CounterAmount)
dest.LedgerCloseTime = row.LedgerCloseTime
dest.BaseIsSeller = row.BaseIsSeller
if row.HasPrice() {
dest.Price = &Price{
N: int32(row.PriceN.Int64),
D: int32(row.PriceD.Int64),
}
}
populateTradeLinks(ctx, dest, row.HistoryOperationID)
}
func populateTradeLinks(
ctx context.Context,
dest *Trade,
opid int64,
) {
lb := hal.LinkBuilder{httpx.BaseURL(ctx)}
dest.Links.Base = lb.Link("/accounts", dest.BaseAccount)
dest.Links.Counter = lb.Link("/accounts", dest.CounterAccount)
dest.Links.Operation = lb.Link(
"/operations",
fmt.Sprintf("%d", opid),
)
}