W3Connect is a Swift library for interacting with the blockchain & smart contract ecosystem using the Web3 interface. By connecting to a network or node, you can to compose, send transactions and read values from smart contracts without the need of writing your own implementations of the protocols.
W3Connect aims to make it as simple and easy to follow and understand. The class structure follows the same pattern as the official Web3 library but renames and organizes the classes, variables, and methods for ease of use, efficiency, and understanding.
W3Connect supports all networks that are compatible with the Ethereum web3.js
library.
To use the library, add import W3Connect
in your swift files.
import W3Connect
Define a new connection with a Provider
:
let w3 = Blockchain(connectTo: "https://mainnet.infura.io/<your_infura_id>")
With W3Connect
you can use a local or remote node as the Provider
to communicate with the blockchain network allowing for fetching address balances, signing of transactions, reading contracts and calling of contracts and their respective functions.
All standard Web3 web3_
methods are accessible using the base Blockchain
struct.
struct Blockchain {
let network: Network
let node: Node
...
}
Network
: Provides information about the current connected network and related data. Renamed fromnet_
.
Node
: Allows for interacting with the blockchain and it's smart contracts. Renamed frometh_
.
Creates new message call transaction or a contract creation for signed transactions.
Parameters
Transaction
: The signed transaction
Returns
NetworkData
, 32 Bytes - The transaction hash, or the zero hash if the transaction is not yet available
To send some ETH you first need to get the current transaction count of the sender (nonce), create the transaction, sign it and then send it.
let privateKey = try! EthereumPrivateKey(hexPrivateKey: "0xa26da69ed1df3ba4bb2a231d506b711eace012f1bd2571dfbfff9650b03375af")
firstly {
web3.eth.getTransactionCount(address: privateKey.address, block: .latest)
}.then { nonce in
let tx = try EthereumTransaction(
nonce: nonce,
gasPrice: EthereumQuantity(quantity: 21.gwei),
gas: 21000,
to: EthereumAddress(hex: "0xC0866A1a0ed41e1aa75c932cA3c55fad847fd90D", eip55: true),
value: EthereumQuantity(quantity: 1.eth)
)
return try tx.sign(with: privateKey, chainId: 1).promise
}.then { tx in
web3.eth.sendRawTransaction(transaction: tx)
}.done { hash in
print(hash)
}.catch { error in
print(error)
}
firstly {
web3.eth.getBlockTransactionCountByNumber(block: .block(5397389))
}.done { count in
print(count) // 88
}.catch { error in
print(error)
}
StaticContract
A protocol that provides predefined functions and events to interact with the contract
ERC20Contract
: ERC-20 Standard for TokensERC721Contract
: ERC-721 Standard for NFT
DynamicContract
A parsed JSON-RPC object representation that dynamically generates a contract at run-time.
There are several options to create contract abi interfaces:
- Use the generic ERC20 or ERC721 static
Contract
- Creating your own
Contract
interface to make contract calls - Parse the JSON ABI representation of the dynamic
Contract
, just like in web3.js
By creating your own interfaces, you can interact with any smart contract!
To add W3Connect to a Swift Package Manager based project, add the following to your Package.swift
files dependencies
array.
.package(name: "W3Connect", url: "https://github.com/rkohl/W3Connect", .upToNextMajor(from: "1.1.0")),
W3Connect is available under the MIT license. See the LICENSE file for more info.