Skip to content

KevinVitale/Web3Kit

Repository files navigation

Platforms

  • macOS, v10.15+;
  • iOS, v13+;
  • Linux

Adding Web3Kit as a Dependency

To use the Web3Kit library in a Swift Package Manager (SPM) project, add the following line to the dependencies in you Package.swift file:

.package(url: "https://github.com/KevinVitale/Web3Kit", from: "0.0.2"),

The Web3Kit library is under active development, and while attempts are made to maintain source-stability, this is not guaranteed between minor versions. You may specify .upToNextMinor(from:), instead of from(_:), if you need to be at a specific version.

Usage

A Web3Kit clients can work with any node source, such as:

  • a local Ganache node (even one running inside a Docker container);
  • a local network Geth node;
  • an Infura node!

All this makes testing on macOS, iOS, or Linux really simple.

Example: Requests

An architectural goal for Web3Kit is to be small, yet flexible and expressive.

For any request you want to make, call (request:parameters:) with a string literal:

// Create a client
let web3 = Web3Client("127.0.0.1:8545")

// Calls 'web3_sha3'
let result = try await web3(request: "sha3", parameters: ["0xdeadbeef"]).result
print(result ?? "")

For convenience, most requests have statically defined equivalents. For the above example:

// Create a client
let web3 = Web3Client("127.0.0.1:8545")

// Calls 'web3_sha3'
let result = try await web3(request: .sha(data: "0xdeadbeef")).result
print(result ?? "")

Example: Checking balances

This example checks the balances of all the accounts on the node. It also demonstrates how my micro-framework, Wei can be used to convert String values:

// Create a client
let web3 = Web3Client("127.0.0.1:8545")

// Use `.eth' to invoke domain-specific API requests
let balance = web3.eth(request: "getBalance", parameters: ["0x0000000000000000000000000000000000000000"]).result ?? ""

// Print the amount in ETHER
print(balance(as: .wei).to(.ether))

Related Projects

Web3Kit is part of a suite of Ethereum tools and frameworks built for those who ❤️ Swift.