forked from stellar/go
/
trade_effect.go
58 lines (50 loc) · 1.36 KB
/
trade_effect.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
package resource
import (
"errors"
"fmt"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/httpx"
"github.com/stellar/go/services/horizon/internal/render/hal"
"golang.org/x/net/context"
)
// PopulateFromEffect fills out the details of a trade resource from a
// history.Effect row.
func (res *TradeEffect) PopulateFromEffect(
ctx context.Context,
row history.Effect,
ledger history.Ledger,
) (err error) {
if row.Type != history.EffectTrade {
err = errors.New("invalid effect; not a trade")
return
}
if row.LedgerSequence() != ledger.Sequence {
err = errors.New("invalid ledger; different sequence than trade")
return
}
row.UnmarshalDetails(res)
res.ID = row.PagingToken()
res.PT = row.PagingToken()
res.Buyer = row.Account
res.LedgerCloseTime = ledger.ClosedAt
res.populateLinks(ctx, res.Seller, res.Buyer, row.HistoryOperationID)
return
}
// PagingToken implementation for hal.Pageable
func (res TradeEffect) PagingToken() string {
return res.PT
}
func (res *TradeEffect) populateLinks(
ctx context.Context,
seller string,
buyer string,
opid int64,
) {
lb := hal.LinkBuilder{Base: httpx.BaseURL(ctx)}
res.Links.Seller = lb.Link("/accounts", res.Seller)
res.Links.Buyer = lb.Link("/accounts", res.Buyer)
res.Links.Operation = lb.Link(
"/operations",
fmt.Sprintf("%d", opid),
)
}