// Populate fills out the details func (res *Transaction) Populate( ctx context.Context, row db.TransactionRecord, ) (err error) { res.ID = row.TransactionHash res.PT = row.PagingToken() res.Hash = row.TransactionHash res.Ledger = row.LedgerSequence res.LedgerCloseTime = row.LedgerCloseTime res.Account = row.Account res.AccountSequence = row.AccountSequence res.FeePaid = row.FeePaid res.OperationCount = row.OperationCount res.EnvelopeXdr = row.TxEnvelope res.ResultXdr = row.TxResult res.ResultMetaXdr = row.TxMeta res.MemoType = row.MemoType res.Memo = row.Memo.String res.Signatures = strings.Split(row.SignatureString, ",") res.ValidBefore = res.timeString(row.ValidBefore) res.ValidAfter = res.timeString(row.ValidAfter) lb := hal.LinkBuilder{httpx.BaseURL(ctx)} res.Links.Account = lb.Link("/accounts", res.Account) res.Links.Ledger = lb.Link("/ledgers", fmt.Sprintf("%d", res.Ledger)) res.Links.Operations = lb.PagedLink("/transactions", res.ID, "operations") res.Links.Effects = lb.PagedLink("/transactions", res.ID, "effects") res.Links.Self = lb.Link("/transactions", res.ID) res.Links.Succeeds = lb.Linkf("/transactions?order=desc&cursor=%s", res.PT) res.Links.Precedes = lb.Linkf("/transactions?order=asc&cursor=%s", res.PT) return }
// NewTransactionResource returns a new resource from a TransactionRecord func NewTransactionResource(tx db.TransactionRecord) TransactionResource { self := fmt.Sprintf("/transactions/%s", tx.TransactionHash) return TransactionResource{ Links: halgo.Links{}. Self(self). Link("account", "/accounts/%s", tx.Account). Link("ledger", "/ledgers/%d", tx.LedgerSequence). Link("operations", "%s/operations%s", self, hal.StandardPagingOptions). Link("effects", "%s/effects%s", self, hal.StandardPagingOptions). Link("precedes", "/transactions?cursor=%s&order=asc", tx.PagingToken()). Link("succeeds", "/transactions?cursor=%s&order=desc", tx.PagingToken()), ID: tx.TransactionHash, PagingToken: tx.PagingToken(), Hash: tx.TransactionHash, Ledger: tx.LedgerSequence, LedgerCloseTime: tx.LedgerCloseTime, Account: tx.Account, AccountSequence: tx.AccountSequence, MaxFee: tx.MaxFee, FeePaid: tx.FeePaid, OperationCount: tx.OperationCount, ResultCode: 0, //NOTE: if at some point a history_transaction row records the result code, use it ResultCodeString: "tx_success", EnvelopeXdr: tx.TxEnvelope.String, ResultXdr: tx.TxResult.String, ResultMetaXdr: tx.TxMeta.String, } }
// NewTransactionResource returns a new resource from a TransactionRecord func NewTransactionResource(tx db.TransactionRecord) TransactionResource { self := fmt.Sprintf("/transactions/%s", tx.TransactionHash) timeString := func(in sql.NullInt64) string { if !in.Valid { return "" } return time.Unix(in.Int64, 0).UTC().Format(time.RFC3339) } return TransactionResource{ Links: halgo.Links{}. Self(self). Link("account", "/accounts/%s", tx.Account). Link("ledger", "/ledgers/%d", tx.LedgerSequence). Link("operations", "%s/operations%s", self, hal.StandardPagingOptions). Link("effects", "%s/effects%s", self, hal.StandardPagingOptions). Link("precedes", "/transactions?cursor=%s&order=asc", tx.PagingToken()). Link("succeeds", "/transactions?cursor=%s&order=desc", tx.PagingToken()), ID: tx.TransactionHash, PagingToken: tx.PagingToken(), Hash: tx.TransactionHash, Ledger: tx.LedgerSequence, LedgerCloseTime: tx.LedgerCloseTime, Account: tx.Account, AccountSequence: tx.AccountSequence, MaxFee: tx.MaxFee, FeePaid: tx.FeePaid, OperationCount: tx.OperationCount, EnvelopeXdr: tx.TxEnvelope, ResultXdr: tx.TxResult, ResultMetaXdr: tx.TxMeta, MemoType: tx.MemoType, Memo: tx.Memo.String, Signatures: strings.Split(tx.SignatureString, ","), ValidBefore: timeString(tx.ValidBefore), ValidAfter: timeString(tx.ValidAfter), } }