Пример #1
0
// NewEffectResource initializes a new resource from an EffectRecord
func NewEffectResource(op db.EffectRecord) (EffectResource, error) {
	result, err := op.Details()

	if err != nil {
		return nil, err
	}

	if result == nil {
		result = make(map[string]interface{})
	}

	result["_links"] = halgo.Links{}.
		Link("precedes", "/effects?cursor=%s&order=asc", op.PagingToken()).
		Link("succeeds", "/effects?cursor=%s&order=desc", op.PagingToken()).
		Link("operation", "/operations/%d", op.HistoryOperationID).
		Items
	result["paging_token"] = op.PagingToken()
	result["type_i"] = op.Type
	result["account"] = op.Account

	ts, ok := effectResourceTypeNames[op.Type]

	if ok {
		result["type"] = ts
	} else {
		//TODO: log a warning when we encounter this... it implies our code is out of date
		result["type"] = "unknown"
	}

	return result, nil
}
Пример #2
0
// NewTradeResource initializes a new resource from an EffectRecord
func NewTradeResource(r db.EffectRecord) (result TradeResource, err error) {

	if r.Type != db.EffectTrade {
		err = ErrInvalidTrade
		return
	}

	var details map[string]interface{}
	details, err = r.Details()

	if err != nil {
		return
	}

	seller, ok := details["seller"].(string)
	if !ok {
		err = ErrInvalidTrade
		return
	}

	result = TradeResource{
		Links: halgo.Links{}.
			Link("seller", "/accounts/%s", seller).
			Link("buyer", "/accounts/%s", r.Account).
			Link("order_book", "/order_book?TODO"),
		ID:                r.PagingToken(),
		PagingToken:       r.PagingToken(),
		Seller:            seller,
		SoldAssetType:     details["sold_asset_type"],
		SoldAssetCode:     details["sold_asset_code"],
		SoldAssetIssuer:   details["sold_asset_issuer"],
		Buyer:             r.Account,
		BoughtAssetType:   details["bought_asset_type"],
		BoughtAssetCode:   details["bought_asset_code"],
		BoughtAssetIssuer: details["bought_asset_issuer"],
	}

	return
}