Stablecoin
Karura Dollar (kUSD) stablecoin protocol is a dynamic decentralized system that allows users to autonomously generate kUSD stablecoin (stabilized against the US Dollar) using crypto assets in excess of value as collateral.
To interact with Karura from Javascript you can use @polkadot/api along with @acala-network/api. You can learn more about @polkadot/api [here]. (https://polkadot.js.org/docs/api).
We do also provide a Stablecoin SDK which provides more some automation around stablecoins.

Source Code of Karura Stablecoin

Read-Only Functions (State queries)

These functions only read information from the chain, and thus don't require signing transactions with a private key. Read more about state queries here: State queries docs

Get Vault for specific Account for given Collateral Type

Returns amount of collateral and amount of minted stablecoin as debit for specific collateral type and account.
Note :warning: debit reflects the only amount of minted kUSD. The amount of debt is higher as it includes accumulated interest. To calculate the total amount to payback you need to use debitExchangeRate parameter (the example for fetching debitExchangeRate is shown below).
1
positions(currencyId: CurrencyId, accountId: AccountId):
2
Promise<{collateral: number, debit: number}>
Copied!
Arguments
Name
Type
currencyId
CurrencyId
collateral currency Id
accountId
AccountId
account to fetch vaults for, can be passed as string
Example:
1
const result = await api.query.loans.positions(
2
{ TOKEN: "KSM" },
3
"<ACCOUNT>"
4
);
5
console.log(result.toHuman());
Copied!

Full code snippet:

Get total amount of collateral and borrowed kUSD for Collateral Type

Returns total amount of collateral and amount of borrowed stablecoin as debit for specific collateral type.
1
totalPositions(currencyId: CurrencyId):
2
Promise<{collateral: number, debit: number}>
Copied!
Arguments
Name
Type
currencyId
CurrencyId
identificator for currency used as collateral
Example:
1
const result = await api.query.loans.positions(
2
{ TOKEN: "KSM" },
3
"<ACCOUNT>"
4
);
5
console.log(result.toHuman());
Copied!

Full code snippet:

Get Risk Parameters for given Collateral type.

Each accepted by Karura Collateral Type can have different risk parameters. These values are controlled by Karura Governance.
1
collateralParams(currencyId: CurrencyId):
2
Promise<{
3
maximumTotalDebitValue: number,
4
interestRatePerSec: number,
5
liquidationRatio: number,
6
liquidationPenalty: number,
7
requiredCollateralRatio: number,
8
9
}>
Copied!
Arguments
Name
Type
currencyId
CurrencyId
identificator for currency used as collateral
Return values
Name
Type
maximumTotalDebitValue
Number
maximum amount of KUSD that can be borrowed for one vault
interestRatePerSec
Percent
the percentage of borrowed kUSD to be paid each next block. This amount should be added to globalInterestRatePerSec to calculate the debt
liquidationRatio
Percent
collateral ratio (collateral value / debt value) reaching which the vault gets liquidated
liquidationPenalty
Percent
penalty that is charged from the vault if the vault gets liquidated
requiredCollateralRatio
Percent
Minimum collateral ratio till which user can borrow kUSD
Example:
1
const result = await api.query.loans.totalPositions(
2
{ TOKEN: "KSM" }
3
);
4
console.log(result.toHuman());
Copied!

Full code snippet:

Get Debt Exchange Rate for given collateral type

This parameter is used to calculate the actual debt. The amount of minted kUSD should be multiplied by this parameter. As the Interest rate is accumulated depends on block number, it makes sense to fetch this parameter for a certain block.
1
debitExchangeRate(currencyId: CurrencyId):
2
Promise<number}>
Copied!
Arguments
Name
Type
currencyId
CurrencyId
identificator for currency used as collateral
Example:
1
const result = await api.query.cdpEngine.debitExchangeRate.at(
2
'<BLOCK_HASH'
3
{ TOKEN: "KSM" }
4
);
5
console.log(result.toHuman());
Copied!

Full code snippet:

State-Changing Functions

These transactions write data on-chain and require a private key to sign the transaction. To perform run test code snippets ensure that you have SEED_PHRASE environment variable defined in your .env file.

Create and manage the Vault

All operations: creating a vault, adding/removing collateral, borrowing, paying back kUSD can be done using a single method: honzon.adjustLoan
1
adjustLoan(currency_id: CurrencyId, collateral_adjustment: Number, debit_adjustment: Number): Extrinsic
Copied!
Arguments
Name
Type
currencyId
CurrencyId
collateral CurrencyId
collateral_adjustment
Signed Amount
positive means to deposit collateral currency into Vault, negative means withdraw collateral currency from the Vault
debit_adjustment
Signed Amount
positive means to mint some amount of stablecoin to the caller; negative means that caller will pay back stablecoin to the Vault
Example
1
const currencyId = { TOKEN: "KSM" };
2
const collateralAdjustment = <DESIRED_ADJUSTMENT>;
3
const debitAdjustment = <DESIRED_ADJUSTMENT>;
4
5
const extrinsic = api.tx.honzon.adjustLoan(
6
currencyId,
7
collateralAdjustment,
8
debitAdjustment
9
);
10
const hash = await extrinsic.signAndSend(signer);
11
console.log('hash', hash.toHuman());
Copied!
Note :warning: the supply amount should be denormalised with KAR decimals for this example

Full code snippet:

Grants permission for transferring loan

Sets permission to transfer caller's loan to another Account (to).
1
authorize(curencyId: CurrencyId, to: AccountId): Extrinsic
Copied!
Returns Extrinsic type that should be signed with a private key.
Arguments
Name
Type
currencyId
CurrencyId
collateral CurrencyId
to
AccountId
sets permission to transfer loan for this accountId
Example:
1
const accountId = "<ACCOUNT_ID>";
2
const extrinsic = api.tx.honzon.authorize(
3
{ TOKEN: "KSM" },
4
accountId
5
);
6
const hash = await extrinsic.signAndSend(signer);
7
console.log("hash", hash.toHuman());
Copied!

Full code snippet:

Removes permission for transferring loan

Removes permission to transfer caller's loan to another Account (to). This method can be used to decline previously given permission.
1
unauthorize(curencyId: CurrencyId, to: AccountId): Extrinsic
Copied!
Returns Extrinsic type that should be signed with a private key.
Arguments
Name
Type
currencyId
CurrencyId
collateral CurrencyId
to
AccountId
removes permission to transfer loan for this accountId
Example:
1
const accountId = "<ACCOUNT_ID>";
2
const extrinsic = api.tx.honzon.unauthorize(
3
{ TOKEN: "KSM" },
4
accountId
5
);
6
const hash = await extrinsic.signAndSend(signer);
7
console.log("hash", hash.toHuman());
Copied!

Removes permissions for ALL accounts to transfer the vault

Removes permission to transfer caller's vault to ALL accounts for all Collateral types. This method can be used to decline previously given permission.
1
unauthorizeAll(): Extrinsic
Copied!
Returns Extrinsic type that should be signed with a private key.
Example:
1
const extrinsic = api.tx.honzon.unauthorizeAll();
2
const hash = await extrinsic.signAndSend(signer);
3
console.log("hash", hash.toHuman());
Copied!

Transfering Vault to another account

Transfers Vault to the caller's account if it has permissions (if authorize was called previously by from account).
1
transferLoanFrom(currency_id, from): Extrinsic
Copied!
Returns Extrinsic type that should be signed with a private key.
Arguments
Name
Type
currencyId
CurrencyId
collateral CurrencyId
from
AccountId
transfers collateral from this account to the caller
Example:
1
const fromAccountId = "<ACCOUNT_ID>";
2
const extrinsic = api.tx.honzon.transferLoanFrom(
3
{ TOKEN: "KSM" },
4
fromAccountId
5
);
6
const hash = await extrinsic.signAndSend(signer);
7
console.log("hash", hash.toHuman());
Copied!

Closing caller's Vault by swapping collateral in DeX

This action can be done with adjustLoan, but there is a shortcut created for this purpose which is applied only to safe vaults (where the collateral ratio is above liquidation level) and where the debt amount is positive.
This method closes the caller's Vault by selling a sufficient amount of collateral on Karura Dex.
1
closeLoanHasDebitByDex(
2
currency_id: CurrencyId,
3
max_collateral_amount: number,
4
maybe_path?: CurrencyId[]
5
): Extrinsic
Copied!
Returns Extrinsic type that should be signed with a private key.
Arguments
Name
Type
currencyId
CurrencyId
collateral CurrencyId
max_collateral_amount
number
the maximum collateral that's allowed to be swapped in DeX to pay back the Vault
maybe_path
CurrencyId[]
swap path that can be used for swapping collateral for kUSD in DeX
Example:
1
const extrinsic = api.tx.honzon.closeLoanHasDebitByDex(
2
{ TOKEN: "KSM" },
3
// large number, allows swapping almost any amount
4
1 * 10 ** 30,
5
[{ TOKEN: "KSM" }, { TOKEN: "KUSD" }]
6
);
7
const hash = await extrinsic.signAndSend(signer);
8
console.log("hash", hash.toHuman());
Copied!

Full code snippet:

Last modified 1mo ago