Skip to content

thorchain/thorchain-ios

Repository files navigation

ThorchainFramework

Build Version Platforms License

ThorchainFramework is a native Swift package that can be added to any project which requires client side Thorchain network requests and calculations.

The framework is designed to work with the Multichain Thorchain network to assist clients in creating transactions with memo's for Swaps and Staking. Functions are also available to perform Swap/Stake/Slip/Fee calculations for display to users using the latest information from Thorchain node(s) Midgard service. The entire Midgard API is available as native Swift functions

The framework also safely queries multiple Thorchain nodes for the latest inbound vault addresses which are cached in memory for 15 minutes. For paranoid level security, you should run your own Thorchain node which can be given to the framework to query in addition to other known nodes.

Dependencies

BigInt package is pulled via https://github.com/attaswift/BigInt

Installation

Swift Package

Use Xcode package manager Add Package Dependency and use the Github URL.

Cocoapods

Edit Podfile

target 'MyApp' do
    use_frameworks!
    pod 'ThorchainFramework'
end

Usage

import ThorchainFramework
import BigInt

To perform a Swap, Thorchain creates a high level API that performs all network requests and returns transaction and swap calculation info on the main thread via a callback:

let thorchain = Thorchain(withChain: .testnet)
thorchain.performSwap(fromAsset: .ETH,
                      toAsset: .RuneNative,
                      destinationAddress: "tthor1abcdef...",
                      fromAssetAmount: 0.1) { (swapData) in
    
    if let txParams = swapData?.0, let swapCalculations = swapData?.1 {
        // Success - Current transaction estimates (display to user):
        print(swapCalculations.slip)
        print(swapCalculations.fee)
        print(swapCalculations.output)
        
        // If user chooses to perform transaction, use the
        // following information to perform your transaction:
        switch txParams {
        case .regularSwap(let regularTxData):
            print(regularTxData.amount)
            print(regularTxData.memo)
            print(regularTxData.recipient)
        case .routedSwap(let routedTxData):
            print(routedTxData.routerContractAddress) //use .deposit()
            print(routedTxData.payableVaultAddress)
            print(routedTxData.assetAddress)
            print(routedTxData.amount)
            print(routedTxData.memo)
        case .runeNativeDeposit(let runeNativeDeposit):
            print(runeNativeDeposit.memo)
            print(runeNativeDeposit.amount)
        }
    }
}

Alternatively you can use the lower level functions directly:

let memo = Thorchain.getSwapMemo(asset: .BTC, destinationAddress: "btc12345", limit: 1234)
// "SWAP:BTC.BTC:btc12345:1234"
let assetInput = AssetAmount(1).baseAmount
let assetPool = Thorchain.PoolData(assetBalance: AssetAmount(110).baseAmount, runeBalance: AssetAmount(100).baseAmount)
var slip : Decimal = Thorchain.getSwapSlip(inputAmount: assetInput, pool: assetPool, toRune: true)
// 0.00900901

For Midgard data, ThorchainFramework provides network requests:

let thorchain = Thorchain(withChain: .testnet)
thorchain.getMidgardPools { (pools) in
    if let pools = pools {
        // Success
        for pool : MidgardPool in pools {
            print(pool.asset)
            print(pool.assetDepth)
            print(pool.runeDepth)
            print(pool.assetPrice)
            print(pool.assetPriceUSD)
            print(pool.poolAPY)
            print(pool.volume24h)
            print(pool.status)
            print(pool.units)
        }
    }
}

Safety

Memory Safety: Thorchain framework will hold a strong reference to itself for the duration of network requests or until they timeout (10 seconds). If your local reference to Thorchain goes out of scope, the request will continue and call the completion handler then deallocate. Thread Safety: Thorchain framework functions are all non-blocking and should be called from the Main Thread. Your completion handlers will be called on the Main Thread.

Decimals

Thorchain (the network and this framework) uses 1e8 decimals internally for all assets. This means for a 1.0 ETH transaction, you would interact with the ThorchainFramework with AssetAmount(1.0) or BaseAmount(100_000_000) (not 1e18). When the framework outputs an AssetAmount for you to perform in a live blockchain transaction, you should use the correct number of decimals the real chain requires (e.g. 1e18 for ETH) for your real world transaction.

For very large (or precise) values, do not use float or integer literals in initialisers as the compiler will truncate to Double/Int precision. Instead, you should initialise large values with a String e.g. Decimal(string: "0.0909090909090909090909") or BigInt("4554557182994857123112")

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published