Coding Guide

A coding overview to xchainjs.

General

The foundation of xchainjs is defined in the xchain-util package

  • Address: a crypto address as a string.
  • BaseAmount: a bigNumber in 1e8 format. E.g. 1 BTC = 100,000,000 in BaseAmount
  • AssetAmount: a BaseAmount*10^8. E.g. 1 BTC = 1 in Asset Amount.
  • Asset: Asset details {Chain, symbol, ticker, isSynth}

Info

All Assets must conform to the Asset Notation

assetFromString() is used to quickly create assets and will assign chain and synth.

  • CryptoAmount: is a class that has:
 baseAmount: BaseAmount
 readonly asset: Asset

All crypto should use the CryptoAmount object with the understanding they are in BaseAmount format. An example to switch between them:

// Define 1 BTC as CryptoAmount
oneBtc = new CryptoAmount(
  assetToBase(assetAmount(1)),
  assetFromStringEx(`BTC.BTC`),
);
// Print 1 BTC in BaseAmount
console.log(oneBtc.amount().toNumber().toFixed()); // 100000000
// Print 1 BTC in Asset Amount
console.log(oneBtc.AssetAmount().amount().toNumber().toFixed()); // 1

Query

Major data types for the thorchain-query package.

EstimateSwapParams

SwapInput

The input Type for estimateSwap. This is designed to be created by interfaces and passed into EstimateSwap. Also see Swap Memo for more information.

VariableData TypeComments
inputCryptoAmountInbound asset and amount
destinationAssetAssetOutbound asset
destinationAddressStringOutbound asset address
slipLimitBigNumberOptional: Used to set LIM
affiliateFeePercentnumberOptional: 0-0.1 allowed
affiliateAddressAddressOptional: THOR address
interfaceIDstringOptional: Assigned interface ID

SwapEstimate

The internal return type is used within estimateSwap after the calculation is done.

VariableData TypeComments
totalFeesTotalFeesAll fees for swap
slipPercentageBigNumberActual slip of the swap
netOutputCryptoAmountInput - totalFees
waitTimeSecondsnumberEstimated time for the swap
canSwapbooleanFalse if there is an issue
errorsstring arrayContains info if canSwap is false

TxDetails

Return type of estimateSwap. This is designed to be used by interfaces to give them all the information they need to display to the user.

VariableData TypeComments
txEstimateSwapEstimateSwap details
memostringConstructed memo THORChain will understand
expiryDateTimeWhen the SwapEstimate information will no longer be valid
toAddressstringCurrent Asgard Vault address from inbound_address

Danger

Do not use toAddress after expiry as the Asgard vault rotates

AMM

Major data types for the thorchain-query package.

ExecuteSwap

Input Type for doSwap where a swap will be actually conducted. Based on EstimateSwapParams.

TxSubmitted

Variable
hashstringinbound Tx Hash
urlstringBlock exploer url
waitTimeSecondsnumberEstimated time for the swap