Giter VIP home page Giter VIP logo

swift-algorand-sdk's Introduction

Swift Algorand SDK

This is a swift sdk that allows interaction with the algorand blockchain. It also supports interecting with the V2 indexer and Algo Apis

1 How To Install

In Xcode go to File > Swift Packages > Add Package Dependency and paste the git url of this package, make sure to choose the main branch and proceed

2 Quickstart

The swift algorand sdk currently only supports Algod and Indexer 2.0, the only thing you need to do to start using the swift-algorand-sdk after installing it in your project is to import it like below:

import swift_algorand_sdk

Let's try some code

var ALGOD_API_ADDR="ALGOD-API-ADDRESS";
var ALGOD_API_TOKEN="ALGOD-API-TOKEN";
var ALGOD_API_PORT="ALGOD-API-PORT"
var algodClient=AlgodClient(host: ALGOD_API_ADDR, port: ALGOD_API_PORT, token: ALGOD_API_TOKEN)
 algodClient.getStatus().execute(){nodeStatusResponse in
        if(nodeStatusResponse.isSuccessful){
            print(nodeStatusResponse.data!.lastRound)
        }else{
            print(nodeStatusResponse.errorDescription)
        }
    
    }

    algodClient.transactionParams().execute(){ paramResponse in
    if(paramResponse.isSuccessful){
        print(paramResponse.data!.lastRound)
    }else{
        print(paramResponse.errorDescription);
    }
}

Please do note that you can always change the APi key for the Header of either Algod or Indexer requests, for example, someone using purestake will simply add the line below to the line after initializing AlgodClient

algodClient.set(key: "X-API-Key")

Lets try a complete example of a payment transaction with purestake

var PURESTAKE_ALGOD_API_TESTNET_ADDRESS="https://testnet-algorand.api.purestake.io/ps2";
var PURESTAKE_API_KEY="YOUR-PURESTAKE-API-KEY";
var PURESTAKE_API_PORT="443";
var algodClient=AlgodClient(host: PURESTAKE_ALGOD_API_TESTNET_ADDRESS, port: PURESTAKE_API_PORT, token: PURESTAKE_API_KEY)

var mnemonic="cactus check vocal shuffle remember regret vanish spice problem property diesel success easily napkin deposit gesture forum bag talent mechanic reunion enroll buddy about attract"

 var account =  try Account(mnemonic)
    var senderAddress = account.getAddress()
    var receiverAddress = try! Address("FMBQKMGDE7LYNDHCSUPJBXNMMT3HC2TXMIFAJKGBYJQDZN4R3M554N4QTY")


        var trans =  algodClient.transactionParams().execute(){ paramResponse in
            if(!(paramResponse.isSuccessful)){
            print(paramResponse.errorDescription);
            return;
        }

    var tx = try! Transaction.paymentTransactionBuilder().setSender(senderAddress)
                .amount(10)
                .receiver(receiverAddress)
               .note("Swift Algo sdk is cool".bytes)
                .suggestedParams(params: paramResponse.data!)
                .build()

           
            var signedTransaction=account.signTransaction(tx: tx)
        
            var encodedTrans:[Int8]=CustomEncoder.encodeToMsgPack(signedTransaction)
           


            algodClient.rawTransaction().rawtxn(rawtaxn: encodedTrans).execute(){
               response in
                if(response.isSuccessful){
                    print(response.data!.txId)
                }else{
                    print(response.errorDescription)
                    print("Failed")
                }

            }
    }

you can further query the pending transaction by doing the below

algodClient.pendingTransactionInformation(txId: "PENDING-TRANSACTION-ID").execute(){ pendingTransactionResponse in
        if(pendingTransactionResponse.isSuccessful){
            print(pendingTransactionResponse.data!.confirmedRound)
        }else{
            print(pendingTransactionResponse.errorDescription!)
            print("Error")
        }
}

Quickstart For Indexer

The indexer allow's us to query the blockchain for data and information. There are 12 indexer methods that allow this. Let's try some code

var indexerClient=IndexerClient(host: "INDEXER_API_ADDRESS", port: "API_PORT", token: "API_KEY")
  indexerClient.lookUpAssetBalances(assetId:14077815).execute(){response in
        if response.isSuccessful{
                print("success")
            print(response.data!.toJson()!)
        }else{
            print(response.errorDescription)
        }
    }

indexerClient.lookUpBlocks(roundNumber: 12471917).execute(){response in

        if response.isSuccessful{
                print("success")
            print(response.data!.toJson()!)
        }else{
            print(response.errorDescription)
        }
    }

indexerClient.searchForAccounts(assetId: 14077815).execute(){ response in
        if response.isSuccessful{
            print(response.data!.toJson()!)
        }else{
            print(response.errorDescription)
        }
    }

     indexerClient.searchForTransactions(txid:"HPS2AQU26NNVTFIJVBYYZN2P2T73AONKWCS7HPT5JUQEQMXFHMJA").execute(){ response in
        if response.isSuccessful{
            print(response.data!.toJson()!)
        }else{
            print(response.errorDescription)
            print("failure")

        }
    }


 indexerClient.lookUpAssetById(id:14077815).execute(){response in

        if response.isSuccessful{
                print("success")
            print(response.data!..toJson()!)
        }else{
            print(response.errorDescription)
            print("Error");

        }
    }

      indexerClient.searchForAssets(assetId:14077815).execute(){ response in
        if response.isSuccessful{
            print(response.data!..toJson()!)
        }else{
            print(response.errorDescription)
            print("Error");
        }
    }

Please feel free to check out this IOS showcase that shows you how to do much more with the Swift Algorand SDK

swift-algorand-sdk's People

Contributors

jesulonimi21 avatar stefanomondino avatar kcarter787 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.