TypeScript Methods for Token Conversion
Blockchain App Builder automatically generates methods that you can use to convert fungible tokens that use the Token Taxonomy Framework standard.
The token conversion methods include the concept of the exchange pool. The exchange pool account is funded by other token accounts. When you mint tokens, you can specify that a percentage of the minted tokens are transferred to the exchange pool account.
Token Conversion Process
A typical flow for converting tokens follows these steps:- Call the
initializeExchangePoolUsermethod to initialize the exchange pool user. - Call the
createExchangePoolAccountsmethod to create exchange pool accounts. Create an exchange pool account for every type of fungible token that you want to convert from or convert to. - Call the
addConversionRatemethod to set the conversion rate for each pair of tokens that you want to convert between. - Fund the exchange pool token accounts in one of the following ways:
- Transfer tokens to the exchange pool token accounts using the standard transfer methods.
- Call the
mintWithFundingExchangePoolTokenmethod when minting tokens, which can transfer a percentage of minted tokens to an exchange pool account.
- Call the
tokenConversionmethod to convert between two fungible tokens. A single user can convert tokens between two of their token accounts, or a pair of users can directly convert tokens from one account to another. - The exchange pool user can view the exchange pool account balances and account
transactions.
- Call the
getAccountmethod to view the balances of each of the exchange pool token accounts. - Call the
getAccountTransactionHistoryandgetAccountTransactionHistoryWithFiltersmethods to view account transactions for each of the exchange pool token accounts.
- Call the
Automatically Generated Token Conversion Methods
Blockchain App Builder automatically generates methods to convert between
different types of fungible tokens. Controller methods must have a
@Validator(...params) decorator to be invokable.
-
initializeExchangePoolUser - This method initializes the exchange pool user, which is a
one-time activity. This method can be called only by a
Token Adminof the chaincode. -
createExchangePoolAccounts - This method creates exchange pool token accounts for a given
array of token IDs. This method can be called only by a
Token Adminof the chaincode. -
addConversionRate - This method adds a conversion rate for a pair of tokens. The
token conversion rate can be specified up to eight decimal places. This
method can be called only by a
Token Adminof the chaincode. -
getConversionRate - This method gets the current conversion rate for a pair of
tokens. This method can be called by the
Token Adminof the chaincode, anyOrg Admin, and by any token account owner. -
updateConversionRate - This method updates the current conversion rate for a pair of
tokens. The token conversion rate can be specified up to eight decimal
places. This method can be called only by a
Token Adminof the chaincode. -
mintWithFundingExchangePool - This method mints tokens in the caller's account based on the specified token ID and quantity. A percentage of tokens from the minted quantity is then transferred to the exchange pool token account.
-
tokenConversion - This method converts tokens from the caller's account to the
account specified by the
to_token_id,to_org_idandto_user_idvalues. This method can be called by theToken Adminof the chaincode and by any token account owner. An exchange pool user cannot call this method. -
getConversionHistory - This method returns the token conversion history for a specified
token account. This method can be called by the
Token Adminof the chaincode, anOrg Adminof the specified organization, or by the token account owner. -
getConversionRateHistory - This method returns the token conversion rate history for a pair
of tokens. This method can be called by the
Token Adminof the chaincode, anyOrg Admin, and by any token account owner. -
getExchangePoolUser - This method returns the
org_idanduser_idvalues for the exchange pool user. This method can be called only by aToken Adminof the chaincode.
Token Conversion SDK Methods
-
initializeExchangePoolUser - This method initializes the exchange pool user, which is a
one-time activity. This method can be called only by a
Token Adminof the chaincode. -
createExchangePoolAccounts - This method creates exchange pool token accounts for a given
array of token IDs. This method can be called only by a
Token Adminof the chaincode. -
addConversionToken - This method adds tokens with a new conversion rate for a
specified token. The token conversion rate can be specified up to eight
decimal places. This method can be called only by a
Token Adminof the chaincode. -
get - This method gets the current conversion rate for a pair of
tokens. This method can be called by the
Token Adminof the chaincode and by any token account owner. -
updateTokenConversionRate - This method updates the current conversion rate for a pair of
tokens. The token conversion rate can be specified up to eight decimal
places. This method can be called only by a
Token Adminof the chaincode. -
mintWithFundingExchangePool - This method mints tokens in the caller's account based on the specified token ID and quantity. A percentage of tokens from the minted quantity is then transferred to the exchange pool token account.
-
tokenConversion - This method converts tokens from the caller's account to the
account specified by the
to_token_id,to_org_id, andto_user_idvalues. This method can be called by theToken Adminof the chaincode and by any token account owner. An exchange pool user cannot call this method. -
getTokenConversionHistory - This method returns the token conversion history for a specified
token account. This method can be called by the
Token Adminof the chaincode, anOrg Adminof the specified organization, and by the token account owner. -
history - This method returns the token conversion rate history for a pair
of tokens. This method can be called by the
Token Adminof the chaincode, anyOrg Admin, and by any token account owner. -
getExchangePoolUser - This method returns the
OrgIdandUserIdvalues for the exchange pool user. This method can be called only by aToken Adminof the chaincode.