# README

Token Transactions DB

The Token Transactions DB is a database of audited records. It is used to track the history of audited events. In particular, it is used to track payments, holdings, and transactions of any business party identified by a unique enrollment ID.

Getting Started

Each Token Transactions DB is bound to a wallet to be uniquely identifiable. To get the instance of the Token Transactions DB bound to a given wallet, use the following:

   ttxDB := ttxdb.Get(context, wallet)

Append Token Requests

A Token Request describes a set of token operations in a backend agnostic language. A Token Request can be assembled directly using the Token API or by using service packages like ttx.

Once a Token Request is assembled, it can be appended to the Token Transactions DB as follows:

	if err := ttxDB.Append(tokenRequest); err != nil {
		return errors.WithMessagef(err, "failed appending audit records for tx [%s]", tx.ID())
	}

The above code will extract the movement records and the transaction records from the Token Request and append them to the Token Transactions DB.

It is also possible to append just the transaction records corresponding to a given Token Request as follows:

	if err := ttxDB.AppendTransactionRecord(tokenRequest); err != nil {
		return errors.WithMessagef(err, "failed appending audit records for tx [%s]", tx.ID())
	}

Payments

To get a list of payments filtered by given criteria, one must first obtain a query executor like this:

    qe := ttxDB.NewQueryExecutor()
    defer aqe.Done() // Don't forget to close the query executor

Now, we are ready to perform payment queries. The following example shows how to retrieve the total amount of last 10 payments made by a given business party, identified by the corresponding enrollment ID, for a given token type.

    filter := qe.NewPaymentsFilter()
    filter, err = filter.ByEnrollmentId(eID).ByType(tokenType).Last(10).Execute()
    if err != nil {
        return errors.WithMessagef(err, "failed getting payments for enrollment id [%s] and token type [%s]", eID, tokenType)
    }
    sumLastPayments := filter.Sum()

Holdings

The following example shows how to retrieve the current holding of a given token type for a given business party. Recall that the current holding is equal to the difference between inbound and outbound transactions over the entire history.

    filter := qe.NewHoldingsFilter()
    filter, err = filter.ByEnrollmentId(eID).ByType(tokenType).Execute()
    if err != nil {
        return errors.WithMessagef(err, "failed getting holdings for enrollment id [%s] and token type [%s]", eID, tokenType)
    }
    holding := filter.Sum()

Transaction Records

The following example shows how to retrieve the total amount of transactions for a given business party,

	it, err := qe.Transactions(ttxdb.QueryTransactionsParams{From: p.From, To: p.To})
	if err != nil {
		return errors.WithMessagef(err, "failed getting transactions for enrollment id [%s]", eID)
	}
	defer it.Close()

	for {
		tx, err := it.Next()
		if err != nil {
			return errors.WithMessagef(err, "failed getting transactions for enrollment id [%s]", eID)
        }
		if tx == nil {
			break
		}
		fmt.Printf("Transaction: %s\n", tx.ID())
	}

# Packages

No description provided by the author
No description provided by the author

# Functions

Drivers returns a sorted list of the names of the registered drivers.
Get returns the DB for the given wallet.
NewManager creates a new DB manager.
Register makes a DB driver available by the provided name.

# Constants

Confirmed is the status of a transaction that has been confirmed by the ledger.
Deleted is the status of a transaction that has been deleted due to a failure to commit.
Issue is the action type for issuing tokens.
Pending is the status of a transaction that has been submitted to the ledger.
PersistenceTypeConfigKey is the key for the persistence type in the config.
Redeem is the action type for redeeming tokens.
Transfer is the action type for transferring tokens.
Unknown is the status of a transaction that is unknown.

# Structs

DB is a database that stores token transactions related information.
No description provided by the author
Manager handles the databases.
PaymentsFilter is a filter for payments.
QueryExecutor executors queries against the DB.
TransactionIterator is an iterator over transaction records.
ValidationRecordsIterator is an iterator over validation records.

# Interfaces

Wallet models a wallet.

# Type aliases

ActionType is the type of action performed by a transaction.
MovementRecord is a record of a movement of assets.
QueryTransactionsParams defines the parameters for querying movements.
QueryValidationRecordsParams defines the parameters for querying movements.
TransactionRecord is a more finer-grained version of a movement record.
TxStatus is the status of a transaction.
ValidationRecord is a more finer-grained version of a movement record.