Пример #1
0
func (this *Account) Populate(ctx context.Context, row db.AccountRecord) (err error) {
	this.ID = row.Accountid
	this.PT = row.PagingToken()
	this.Address = row.Accountid
	this.Sequence = row.Seqnum
	this.SubentryCount = row.Numsubentries
	this.InflationDestination = row.Inflationdest.String
	this.HomeDomain = row.HomeDomain.String

	this.Flags.Populate(ctx, row)
	this.Thresholds.Populate(ctx, row)

	// populate balances
	this.Balances = make([]Balance, len(row.Trustlines)+1)
	for i, tl := range row.Trustlines {
		err = this.Balances[i].Populate(ctx, tl)
		if err != nil {
			return
		}
	}

	// add native balance
	err = this.Balances[len(this.Balances)-1].PopulateNative(row.Balance)
	if err != nil {
		return
	}

	// populate signers
	this.Signers = make([]Signer, len(row.Signers)+1)
	for i, s := range row.Signers {
		this.Signers[i].Populate(ctx, s)
	}

	this.Signers[len(this.Signers)-1].PopulateMaster(row)

	lb := hal.LinkBuilder{httpx.BaseURL(ctx)}
	self := fmt.Sprintf("/accounts/%s", row.Address)
	this.Links.Self = lb.Link(self)
	this.Links.Transactions = lb.PagedLink(self, "transactions")
	this.Links.Operations = lb.PagedLink(self, "operations")
	this.Links.Payments = lb.PagedLink(self, "payments")
	this.Links.Effects = lb.PagedLink(self, "effects")
	this.Links.Offers = lb.PagedLink(self, "Offers")

	return
}
Пример #2
0
// NewAccountRsource creates a new AccountResource from a provided db.CoreAccountRecord and
// a provided db.AccountRecord.
//
// panics if the two records are not for the same logical account.
func NewAccountResource(ac db.AccountRecord) AccountResource {

	address := ac.Address
	self := fmt.Sprintf("/accounts/%s", address)

	balances := make([]BalanceResource, len(ac.Trustlines)+1)

	for i, tl := range ac.Trustlines {
		balance := BalanceResource{
			Balance: AmountToString(tl.Balance),
			Limit:   AmountToString(tl.Tlimit),
			Issuer:  tl.Issuer,
			Code:    tl.Assetcode,
		}

		switch tl.Assettype {
		case int32(xdr.AssetTypeAssetTypeCreditAlphanum4):
			balance.Type = "credit_alphanum4"
		case int32(xdr.AssetTypeAssetTypeCreditAlphanum12):
			balance.Type = "credit_alphanum12"
		}

		balances[i] = balance
	}

	// add native balance
	balances[len(ac.Trustlines)] = BalanceResource{Type: "native", Balance: AmountToString(ac.Balance)}

	// thresholds
	var thresholds ThresholdsResource
	xdrThresholds, err := ac.DecodeThresholds()
	if err == nil {
		thresholds = ThresholdsResource{
			LowThreshold:  xdrThresholds[1],
			MedThreshold:  xdrThresholds[2],
			HighThreshold: xdrThresholds[3],
		}
	}

	// signers
	signers := make([]SignerResource, len(ac.Signers)+1)

	for i, s := range ac.Signers {
		signers[i] = SignerResource{Address: s.Publickey, Weight: s.Weight}
	}

	signers[len(ac.Signers)] = SignerResource{Address: ac.Address, Weight: int32(xdrThresholds[0])}

	// flags
	flags := FlagsResource{
		AuthRequired:  ac.IsAuthRequired(),
		AuthRevocable: ac.IsAuthRevocable(),
	}

	return AccountResource{
		Links: halgo.Links{}.
			Self(self).
			Link("transactions", "%s/transactions%s", self, hal.StandardPagingOptions).
			Link("operations", "%s/operations%s", self, hal.StandardPagingOptions).
			Link("effects", "%s/effects%s", self, hal.StandardPagingOptions).
			Link("offers", "%s/offers%s", self, hal.StandardPagingOptions),
		ID:                   address,
		PagingToken:          ac.PagingToken(),
		Address:              address,
		Sequence:             ac.Seqnum,
		SubentryCount:        ac.Numsubentries,
		InflationDestination: ac.Inflationdest,
		HomeDomain:           ac.HomeDomain,
		Thresholds:           thresholds,
		Flags:                flags,
		Balances:             balances,
		Signers:              signers,
	}
}