Solana-blockchain client, written in pure swift.
- Key pairs generation
- Networking with POST methods for comunicating with solana-based networking system
- Create, sign transactions
- Socket communication
- Orca swap
- Serum DEX Swap
- RenVM (Support: Bitcoin)
To run the example project, clone the repo, and run pod install
from the Example directory first.
Demo wallet: p2p-wallet
- iOS 11 or later
- RxSwift
- RxAlamofire
- TweetNacl
- CryptoSwift
- Starscream
SolanaSwift is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SolanaSwift', :git => 'https://github.com/p2p-org/solana-swift.git', :branch => 'deprecated/1.3.8'
-
Every class or struct is defined within namespace
SolanaSDK
, for example:SolanaSDK.Account
,SolanaSDK.Error
. -
Import
import SolanaSwift
- Create an
AccountStorage
for saving account'skeyPairs
(public and private key), for example:KeychainAccountStorage
for saving intoKeychain
in production, orInMemoryAccountStorage
for temporarily saving into memory for testing. TheAccountStorage
must conform to protocolSolanaSDKAccountStorage
, which has 2 requirements: function for savingsave(_ account:) throws
and computed propertyaccount: SolanaSDK.Account?
for retrieving user's account.
Example:
import KeychainSwift
struct KeychainAccountStorage: SolanaSDKAccountStorage {
let tokenKey = <YOUR_KEY_TO_STORE_IN_KEYCHAIN>
func save(_ account: SolanaSDK.Account) throws {
let data = try JSONEncoder().encode(account)
keychain.set(data, forKey: tokenKey)
}
var account: SolanaSDK.Account? {
guard let data = keychain.getData(tokenKey) else {return nil}
return try? JSONDecoder().decode(SolanaSDK.Account.self, from: data)
}
}
struct InMemoryAccountStorage: SolanaSDKAccountStorage {
private var _account: SolanaSDK.Account?
func save(_ account: SolanaSDK.Account) throws {
_account = account
}
var account: SolanaSDK.Account? {
_account
}
}
- Creating an instance of
SolanaSDK
:
let solanaSDK = SolanaSDK(endpoint: <YOUR_API_ENDPOINT>, accountStorage: KeychainAccountStorage.shared) // endpoint example: https://api.mainnet-beta.solana.com
- Creating an account:
let mnemonic = Mnemonic()
let account = try SolanaSDK.Account(phrase: mnemonic.phrase, network: .mainnetBeta, derivablePath: .default)
try solanaSDK.accountStorage.save(account)
- Send pre-defined POST methods, which return a
RxSwift.Single
. List of predefined methods:
Example:
solanaSDK.getBalance(account: account, commitment: "recent")
.subscribe(onNext: {balance in
print(balance)
})
.disposed(by: disposeBag)
- Send token:
solanaSDK.sendNativeSOL(
to destination: String,
amount: UInt64,
isSimulation: Bool = false
)
.subscribe(onNext: {result in
print(result)
})
.disposed(by: disposeBag)
solanaSDK.sendSPLTokens(
mintAddress: String,
decimals: Decimals,
from fromPublicKey: String,
to destinationAddress: String,
amount: UInt64,
isSimulation: Bool = false
)
.subscribe(onNext: {result in
print(result)
})
.disposed(by: disposeBag)
- Send custom method, which was not defined by using method
request<T: Decodable>(method:, path:, bcMethod:, parameters:) -> Single<T>
Example:
(solanaSDK.request(method: .post, bcMethod: "aNewMethodThatReturnsAString", parameters: []) as Single<String>)
- Subscribe and observe socket events:
// accountNotifications
solanaSDK.subscribeAccountNotification(account: <ACCOUNT_PUBLIC_KEY>, isNative: <BOOL>) // isNative = true if you want to observe native solana account
solanaSDK.observeAccountNotifications() // return an Observable<(pubkey: String, lamports: Lamports)>
// signatureNotifications
solanaSDK.observeSignatureNotification(signature: <SIGNATURE>) // return an Completable
OrcaSwap has been moved to new library OrcaSwapSwift
RenVM has been moved to new library RenVMSwift
SerumSwap has been moved to new library SerumSwapSwift
- For supporting new methods, data types, edit
SolanaSDK+Methods
orSolanaSDK+Models
- For testing, run
Example
project and creating test usingRxBlocking
- Welcome to contribute, feel free to change and open a PR.
Chung Tran, chung.t@p2p.org
SolanaSwift is available under the MIT license. See the LICENSE file for more info.