Swiftpack.co -  Jesulonimi21/Swift-Algorand-Sdk as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
Jesulonimi21/Swift-Algorand-Sdk
A Swift Sdk for interacting with the Algorand Blockchain
.package(url: "https://github.com/Jesulonimi21/Swift-Algorand-Sdk.git", from: "1.0.0")

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(assetId: 14077815).execute(){ response in
        if response.isSuccessful{
            print(response.data!.toJson()!)
        }else{
            print(response.errorDescription)
        }
    }

     indexerClient.searchForTransactions().txid(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(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

GitHub

link
Stars: 7
Last commit: 7 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Release Notes

Initial Version
7 weeks ago

This release comes with support for the following features:

  1. Node And Network Settings
  2. Accounts and Transactions
  3. Create and Fund Accounts
  4. Transactions
  5. Multisig Transaction
  6. ASA
  7. Atomic Transfer
  8. Algorand Smart Contract
  9. Rekey Transaction
  10. Indexer
  11. Compile Teal
  12. Dryrun Debug
  13. Stateful Smart Contracts

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API