Coder Social home page Coder Social logo

thorchain-thorchain-ios's Introduction

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")

thorchain-thorchain-ios's People

Contributors

hildisviniottar avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.