Interact with the Ethereum Blockchain using Rivet and Swift

Introduction

By the time you finish this tutorial, you’ll be able to query the Ethereum blockchain using Swift Playgrounds, Web3.Swift, and your Rivet endpoint.

The Big Picture

  1. You’ll start by setting up a Rivet account if you haven’t already done so. If you have already done so and you are signed into Rivet, your key will appear here:

  2. Next, you’ll install all the necessary dependencies and configure them to use your Rivet endpoint.

  3. Finally, you’ll experiment with some Ethereum calls and see the results in a Swift Playground.

1. Setting up Rivet

My Rivet endpoint: https://YourApiKey.eth.rpc.rivet.cloud/

If you see a URL above that does not contain “YourApiKey”, then you already have a Rivet account set up and you’re logged in. Nice work! Go ahead and skip to step 2.

If you don’t see a URL with your unique key in it, no worries! This will just take a moment:

First, navigate to the Rivet signup page.

When you get there, enter a nickname for your account and a valid email address to which you have access. Select submit.

Web3-savvy?

You can also create an account and top it up with credits via Web3 using the Login with Web3 button on the signup page.

Not sure what this is? Not to worry, just disregard this box for now. Pretty soon, this will make perfect sense.

On the next view, you’ll verify your email address using a code we sent to it when you selected submit. Check your email for a message from Rivet, copy the code, and paste it in.

Got it? Good.

Finally, acknowledge our terms of use—and if you have a promo code, don’t forget to add that too. (If you do forget, contact us at support@rivet.cloud and we’ll hook you up retroactively if you can tell us what the code was supposed to be.

Good to go? Great! Go ahead and hit submit, and voila! Your endpoint URL is:

https://YourApiKey.eth.rpc.rivet.cloud/

Still need help?

If you don’t see it, make sure you’re signed in. If you still don’t see it, please contact us at support@rivet.cloud and we’ll get to the bottom of it. If you recall it, please include the nickname you gave us when you signed up in your message.

Congratulations, you’re 1/3 of the way through this tutorial!

2. The Setup

Swift Playgrounds are a standard feature in Xcode 6, but before you can use them with 3rd-party-creatd Swift packages, a bit of configuration is necessary.

Before you get started, make sure you’re updated to the latest version of Xcode (Search it in the Mac App Store—you’re up-to-date if it it doesn’t prompt you to ‘Update’), and download the Web3.Swift codebase from Github. (You can go ahead and unzip it now.)

Once that’s done, open Xcode, and from the file menu, hover over New, and select Workspace. Save it in a new folder on your desktop—nested in a new folder called hello-rivet—and name it…you guessed it…*hello-rivet*.

Then, back in the file menu, hover over New and select Playground. Select ‘Blank’, name it ‘hello-rivet’, and select ‘hello-rivet’ as its Group in the bottom of the menu. Save it.

Next, open your Finder and drag the extracted Swift.Web3 project folder into the workspace—above, not nested under—the hello-rivet Playground file.

Once Xcode is done importing the files, close and re-open the workspace. This will trigger the installation of dependencies.

Finally, in the top panel, select ‘Web3’ from the dropdown and select the ‘Build’ button. Once it completes, select and Build ‘Web3PromiseKit’ and ‘Web3ContractABI’.

Got it! Great! You’re almost ready—all that’s left is to configure your Rivet endpoint. Open your playground, clear out the default code, and add the following code:

import Web3
import Web3PromiseKit

Next, you’ll create a Web3 instance and connect it to your Rivet endpoint:

let web3 = Web3(rpcURL: "https://YourApiKey.eth.rpc.rivet.cloud/")

That’s all there is to it! Easy, right?

Ok, now let’s try some queries!

3. The Grand Finale

It’s time to start using Web3.Swift! Once properly configured, the web3 instance will allow you to interact with the Ethereum blockchain using Swift (without all the tedium of manually writing out all those asyncronous calls—thanks to the handy syntax provided by Web3PromiseKit).

Let’s give it a try by doing something simple, like getting the client version:

firstly {
     web3.clientVersion()
}.done { version in
     print(version)
}.catch { error in
     print("Error")
}

Your result should look something like this:

Geth/v1.9.18-stable/linux-amd64/go1.13.15

Now let’s try getting the latest gas price:

firstly {
     web3.eth.gasPrice()
}.done { price in
     print(price) // 88
}.catch { error in
     print(error)
}

The result (in Wei) should look something like:

34000000000

Got it? Great!

Now let’s try getting the current block number:

firstly {
     web3.eth.blockNumber()
}.done { number in
     print(number) // 88
}.catch { error in
     print(error)
}

The result should resemble:

11185529

Finally, let’s get the transaction count at that block:

firstly {
     web3.eth.getBlockTransactionCountByNumber(block: .block(11185529))
}.done { count in
     print(count)
}.catch { error in
     print(error)
}

If you used the same block in the sample above, did you get:

189

If you did, you’ve got the hang of this! (If you didn’t, make sure you didn’t get the count for a different block, and confirm that you’re using your mainnet endpoint!) Congratulations, you’re well on your way to making some Web3.Swift magic!

Conclusion

As you’ve seen in this tutorial, Web3.Swift can help you read block data, get the latest gas price, and get account balances—but it can do so much more. With it, you can build apps using Swift that can do everything from signing and sending transactions to deploying and interacting with smart contracts. Basically, anything you want to do with Ethereum in an iOS (or MacOS, iPadOS, WatchOS…or even tvOS) environment, you can do with Web3.Swift.

In future tutorials, we’ll expand from here to help you grow your creative repertoire. In the meantime, you should check out the resources available in the rest of this doc site, at the Web3.Swift Github, and at Ethereum.org or just start dreaming up what you want to learn how to BUIDL.

Congratulations!! You’re now an Ethereum BUIDLer—welcome to the community!!

—❤️Rivet