Codius is a standard that uses Interledger to purchase computational resources. You can imagine it like cloud hosting but because it doesn’t give the uploader the ability to modify their code, you can also use it to run decentralized smart contracts. If you want to learn more, I recommend you read the blog post for the release.

In this blog post, I’ll explain how to write Codius contracts that send and receive payments.

How do Payments in Codius Work?

If you’ve used Solidity, you’ll know that sending and receiving payments are built-in functions on Ethereum. But Codius uses containers instead of a special-purpose language. Codius doesn’t have a built-in ledger or token either. So how do we send payments?

The answer is that all Codius contracts have moneyd built-in. You don’t need to add anything in order to enable this functionality; your contract can just connect to localhost:7768 and use Interledger.

You can’t conjure money out of thin air, though. You need to receive money over Interledger before you can spend any.

The contract that we’ll upload in this tutorial will receive money and then send it out to a receiver that you specify.

Prerequisites

This is a fairly advanced tutorial. If you’re new to Codius and/or Interledger, it’s worth taking your time to go through some of the earlier tutorials on the Interledger Blog. “Using Moneyd to Join the ILP Testnet” is a good place to start.

You need Node.js v8.10.0 or higher. If you don’t have Node.js, use NVM to install it.

If you’re not familiar with SPSP, I recommend you read the tutorial on it.

If you’ve never uploaded to Codius, read the tutorial on it.

You’ll need to have Moneyd running on the live network

Because this tutorial uses the live Interledger network, you’ll need cryptocurrency funds. Our current tutorials all use XRP.

If you don’t have XRP and don’t want to buy any, stay tuned for future tutorials that describe how to join the network with Ethereum and Bitcoin.

Run an SPSP Server

The contract that we upload is going to receive money and then send it to another destination. In order to have somewhere to send, you’ll have to run an SPSP server. The SPSP server connects to Moneyd and accepts Interledger packets.

npm install -g ilp-spsp-server

ilp-spsp-server --subdomain MY_SUBDOMAIN

This will grab you a domain via localtunnel.me. Your output will look something like this:

> ilp-spsp-server --subdomain example

connecting...

created receiver...

listening on 39635

public at: https://example.localtunnel.me

payment pointer is: $example.localtunnel.me

Keep that process running and copy the payment pointer, including the $ . You’ll use it in the next step.

Create a Codius Manifest

In order to upload to Codius, you need a manifest that describes what to run. It includes a list of containers, as well as environment variable definitions.

The program we’re going to run works in two parts:

An SPSP server that receives payments (GitHub Repo). It runs an HTTP server that responds to queries with your Interledger address. It also connects to Moneyd inside of the contract environment and accepts Interledger payments.

It runs an HTTP server that responds to queries with your Interledger address. It also connects to Moneyd inside of the contract environment and accepts Interledger payments. An SPSP client that sends payments (Github Repo). It connects to Moneyd, and repeatedly tries to send payments for 10 units. Of course, this is a trivial use of Interledger. You can do anything with your money, even upload another Codius contract.

We use two files to tell Codius how to run our program: codius.json and codiusvars.json .

In a new folder, create a file called codius.json and put the following text into it:

Then create a file called codiusvars.json , in the same directory, and put the following JSON data into it:

Our contract’s name is codius-spsp .

is . Our contract publicly exposes port 8080 from the container port space (this is the port that our SPSP server listens on).

from the container port space (this is the port that our SPSP server listens on). Our contract runs an SPSP server from sharafian/ilp-spsp-server on docker hub. The SPSP server listens on Interledger and accepts payments.

on docker hub. The SPSP server listens on Interledger and accepts payments. Our contract also runs an SPSP “spender” from sharafian/ilp-spsp-spender on docker hub. The SPSP spender continuously sends micropayments to a destination specified by PAYMENT_POINTER in the environment.

on docker hub. The SPSP spender continuously sends micropayments to a destination specified by in the environment. We define PAYMENT_POINTER to be $sharafian.com .

As the contract works right now, this contract will send all its earnings to $sharafian.com , my payment pointer.

To make sure the money goes to you, replace $sharafian.com with the payment pointer you copied down in the previous step.

Upload To Codius

If you’ve followed our tutorial on uploading to codius, you’ll already have the codius command. Otherwise, run:

npm install -g codius

You may get an EACCESS error if Node.js is not properly configured. Just install NVM and try again.

Once the codius module is done installing, you’re ready to upload. In the same directory where you put your codius.json and your codiusvars.json , run:

codius upload

Enter y when it asks you to confirm your upload. If it’s successful, it will end with something resembling the following output:

✔ 1 Successful Uploads o Manifest Hash: gycv4aiexmaizeet7qotrp6coxyfd6dncnhpa5sd66k4nb5vinbq ✔ Codius State File: default.codiusstate.json Updated

If you weren’t successful, don’t worry. The codius network is still new, so it’s possible that either you or your host had a misconfiguration. The price to host this contract for 10 minutes is only 0.002200 XRP, or 0.1 cents at the current price of XRP. Just give the upload another try, and it will choose a new host.

Once your contract is uploaded, you can send an SPSP payment to the url field of that result. If this doesn’t work, you can try to re-upload the contract to a new host.

> ilp-spsp send --receiver 'https://g...codius.fungkwokpan.art/' --amount 100

paying 100 to "https://g...codius.fungkwokpan.art/"...

sent!

After you’ve paid the contract, you’ll start to see the money coming back from your contract.

In your SPSP server logs, you’ll start to see packets of 10 units coming in (if you’re in a different currency, the amounts will be different).

> ilp-spsp-server --subdomain example

connecting...

created receiver...

listening on 46421

public at: https://example.localtunnel.me

payment pointer is: $example.localtunnel.me

got packet for 10 units

got packet for 10 units

got packet for 10 units

got packet for 10 units

got packet for 10 units

got packet for 10 units

got packet for 10 units

got packet for 10 units

got packet for 10 units

got packet for 10 units

Ideas and Next Steps

Now that you’ve seen how Codius contracts can access Interledger, you can start writing your own applications. Here’s a few ideas for what you can do:

Modify the ILP SPSP Spender to send an amount other than 10. You can clone the repo and use docker to publish your changed versions to docker hub.

Point the SPSP spender at $spsp.siren.sh/RIPPLE_ADDRESS/DESTINATION_TAG to send to any XRP address (even if there’s not any Interledger receiver there). Make sure that you send at least 1000 drops (a drop is one millionth of an XRP) to trigger a settlement.

to send to any XRP address (even if there’s not any Interledger receiver there). Make sure that you send at least 1000 drops (a drop is one millionth of an XRP) to trigger a settlement. Use the ILP Fetch library in your contract to make a paid API call. (If you’re feeling ambitious you can try using the Codius API to make a contract that uploads another contract. Just try not to create Skynet).

If you have any questions, ideas, or suggestions for future tutorials, join the Interledger Gitter and the Codius Gitter!