Esempio n. 1
0
// GetOrderBookSummary loads a summary of an order book identified by a
// selling/buying pair. It is designed to drive an order book summary client
// interface (bid/ask spread, prices and volume, etc).
func (q *Q) GetOrderBookSummary(dest interface{}, selling xdr.Asset, buying xdr.Asset) error {
	var sql bytes.Buffer
	var oq orderbookQueryBuilder
	err := selling.Extract(&oq.SellingType, &oq.SellingCode, &oq.SellingIssuer)
	if err != nil {
		return err
	}
	err = buying.Extract(&oq.BuyingType, &oq.BuyingCode, &oq.BuyingIssuer)
	if err != nil {
		return err
	}

	oq.pushArg(20)

	err = orderbookQueryTemplate.Execute(&sql, &oq)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	err = q.SelectRaw(dest, sql.String(), oq.args...)
	if err != nil {
		return errors.Wrap(err, 1)
	}

	return nil
}
Esempio n. 2
0
// ConnectedAssets loads xdr.Asset records for the purposes of path
// finding.  Given the input asset type, a list of xdr.Assets is returned that
// each have some available trades for the input asset.
func (q *Q) ConnectedAssets(dest interface{}, selling xdr.Asset) error {

	assets, ok := dest.(*[]xdr.Asset)
	if !ok {
		return errors.New("dest is not *[]xdr.Asset")
	}

	var (
		t xdr.AssetType
		c string
		i string
	)

	err := selling.Extract(&t, &c, &i)
	if err != nil {
		return err
	}

	sql := sq.Select(
		"buyingassettype AS type",
		"coalesce(buyingassetcode, '') AS code",
		"coalesce(buyingissuer, '') AS issuer").
		From("offers").
		Where(sq.Eq{"sellingassettype": t}).
		GroupBy("buyingassettype", "buyingassetcode", "buyingissuer")

	if t != xdr.AssetTypeAssetTypeNative {
		sql = sql.Where(sq.Eq{"sellingassetcode": c, "sellingissuer": i})
	}

	var rows []struct {
		Type   xdr.AssetType
		Code   string
		Issuer string
	}

	err = q.Select(&rows, sql)

	if err != nil {
		return err
	}

	results := make([]xdr.Asset, len(rows))
	*assets = results

	for i, r := range rows {
		results[i], err = AssetFromDB(r.Type, r.Code, r.Issuer)
		if err != nil {
			return err
		}
	}

	return nil
}
Esempio n. 3
0
// Equals returns true if l and r are equivalent.
func Equals(l, r xdr.Asset) bool {
	var le, re struct {
		T xdr.AssetType
		C string
		I string
	}

	err := l.Extract(&le.T, &le.C, &le.I)
	if err != nil {
		panic(err)
	}

	err = r.Extract(&re.T, &re.C, &re.I)
	if err != nil {
		panic(err)
	}

	return le == re
}
Esempio n. 4
0
// assetDetails sets the details for `a` on `result` using keys with `prefix`
func (is *Session) assetDetails(result map[string]interface{}, a xdr.Asset, prefix string) error {
	var (
		t    string
		code string
		i    string
	)
	err := a.Extract(&t, &code, &i)
	if err != nil {
		return err
	}
	result[prefix+"asset_type"] = t

	if a.Type == xdr.AssetTypeAssetTypeNative {
		return nil
	}

	result[prefix+"asset_code"] = code
	result[prefix+"asset_issuer"] = i
	return nil
}
Esempio n. 5
0
// OfType filters the query to only effects of the given type.
func (q *EffectsQ) orderBookFilter(a xdr.Asset, prefix string) {
	var typ, code, iss string
	q.Err = a.Extract(&typ, &code, &iss)
	if q.Err != nil {
		return
	}

	if a.Type == xdr.AssetTypeAssetTypeNative {
		clause := fmt.Sprintf(`
				(heff.details->>'%sasset_type' = ?
		AND heff.details ?? '%sasset_code' = false
		AND heff.details ?? '%sasset_issuer' = false)`, prefix, prefix, prefix)
		q.sql = q.sql.Where(clause, typ)
		return
	}

	clause := fmt.Sprintf(`
		(heff.details->>'%sasset_type' = ?
	AND heff.details->>'%sasset_code' = ?
	AND heff.details->>'%sasset_issuer' = ?)`, prefix, prefix, prefix)
	q.sql = q.sql.Where(clause, typ, code, iss)
}
Esempio n. 6
0
func (this *Asset) Populate(ctx context.Context, asset xdr.Asset) error {
	return asset.Extract(&this.Type, &this.Code, &this.Issuer)
}
Esempio n. 7
0
			})

			It("passes a sanity test", func() {
				before, err := createAccount.StateBefore(masterAccount.LedgerKey(), 0)
				Expect(err).ToNot(HaveOccurred())
				Expect(before).ToNot(BeNil())
				after, err := createAccount.StateAfter(masterAccount.LedgerKey(), 0)
				Expect(err).ToNot(HaveOccurred())
				Expect(after).ToNot(BeNil())
				Expect(before.Data.MustAccount().Balance).To(BeNumerically(">", after.Data.MustAccount().Balance))
			})
		})

		Context("Trustlines", func() {
			var tlkey xdr.LedgerKey
			var line xdr.Asset
			BeforeEach(func() {
				line.SetCredit("USD", gatewayAccount)
				tlkey.SetTrustline(newAccount, line)
			})

			It("properly returns the state of a trustlines that gets removed", func() {
				before, err := removeTrustline.StateBefore(tlkey, 0)
				Expect(err).ToNot(HaveOccurred())
				Expect(before).ToNot(BeNil())

				tl := before.Data.MustTrustLine()
				Expect(tl.Limit).To(Equal(xdr.Int64(40000000000)))
			})

			It("properly returns the state of a trustlines that gets removed", func() {