Skip to content

Instantly share code, notes, and snippets.

@Ivshti

Ivshti/how.md Secret

Last active September 6, 2019 15:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ivshti/29552cdf20eda21191f7c795fd46de2a to your computer and use it in GitHub Desktop.
Save Ivshti/29552cdf20eda21191f7c795fd46de2a to your computer and use it in GitHub Desktop.

How we built the largest payment channel network on Ethereum

With over 9 million transactions and over $13,000 in DAI locked on chain, AdEx Network is currently the largest payment channel network on Ethereum.

What is AdEx?

AdEx Network develops a solution for digital advertising that improves transparency, reduces ad fraud and protects user privacy. It has no tracking, reveals no data about the user, and enables payments for each impression through payment channels on Ethereum.

How did we end up building currently the largest payment channel network on Ethereum?

AdEx Network has always been a solution for digital advertising, so we didn’t start the project with payment channels in mind, but we quickly realized we needed such a solution. There is a large number of interactions between advertisers and publishers on the platform - impressions are reported and per-impression payments are carried out, and this is why scalability is crucial.

For AdEx, using payment channels has a few important benefits:

  • Firstly, it allows us to minimize the number of on-chain transactions, and that’s a huge UX improvement. In AdEx, the only on-chain transactions that you need are opening a campaign (as an advertiser) and withdrawing your earnings (as a publisher). Users do not need to learn what payment channels are or how they work, they only need to deposit/withdraw funds to AdEx.
  • Secondly, because making payments does not incur any extra fees, we can use micropayments for each individual impression (or any other type of payable event). This means publishers have a guarantee they can withdraw their revenue at any given time, unlike traditional systems where you can only withdraw your earnings for set, usually long periods, and/or only if you reach a certain threshold.
  • But most importantly, since the payments are per impression, this eliminates any possibility that an advertiser would cheat a publisher out of their earnings. This also makes reporting completely transparent, because you know about every impression and how much was paid for it, in real time.

We decided to call our own payment channels technology OUTPACE - Off-chain Unidirectional Trustless PAyment ChannEls. The two most important features of OUTPACE are that it’s strictly unidirectional, which simplifies the architecture, because it does not require exit games/watchtowers, and It allows multiple parties to be paid from a single channel (one-to-many).

The latter is very useful for AdEx, since advertisers on the network usually will pay many publishers, using one-to-many payment channel.

For more technical details on how OUTPACE works, you may want to read the article we wrote when we first introduced the technology 8 months ago. Now let’s talk some numbers.

More than 9 million impressions in less than two months

At the end of June, we decided we were ready to release the latest version of our platform after testing it in closed beta. First, we started rolling out our ads via AdEx Network on our other product - the media center Stremio, serving more than 100,000 daily ad impressions. We quickly added a few other publishers - two of them being among the most visited websites in our home country. We quickly added supply of 8 of the most popular display ad positions and at the moment we have a total of 11 publishers on the platform.

In less than two months, website visitors generated more than 9 million impressions on AdEx Network. This means more than 9 million payment transactions, all settled on Ethereum. We just surpassed Connext by the amount of funds locked on chain with 13,000 DAI currently locked up on-chain, so it is safe to say that at the moment AdEx Network is the largest payment channel network on Ethereum by locked up funds, and by number of transactions.

You can check our stats in real time on our Explorer. Our payment channels are so efficient, that hundreds of thousands of impression payments get settled in one Ethereum transaction, costing a mere 0.10$ in gas fees.

What’s next?

9 million is a good number to start with in the context of blockchain transactions - but for ad impressions, it is modest. We are just starting to test AdEx Network and by the end of the year we are aimed at onboarding few hundred advertisers and publishers, so we believe numbers will rise significantly and they might become even more impressive pretty soon.

Technical details

In AdEx, each advertising campaign is an OUTPACE channel. The channel works by exchanging messages between it's two validators. The two validators are equivalent to channel participants in Lightning/Raiden (see Alice and Bob in this example). Those messages can be used while the channel is active, by any earner in the channel, to withdraw their funds on-chain.

Currently, the AdEx Network has two validators, which we've named Tom and Jerry. Anyone can run a new validator, or just explore the codebase. Check out the GitHub repository.

The AdEx Platform (the front-end) interacts with Ethereum for depositing funds, opening campaigns, withdrawing earnings from campaigns (called "channel sweeping") and withdrawing funds. To facilitate paying transaction fees in DAI, as well as scheduling transactions and multi-device authentication, we use a "smart" wallet, described in our Identity contract.

Let's walk you through the full process, with examples:

  1. A channel is open by invoking channelOpen on the core contract; this transfers DAI from the user's wallet and locks them up; see an example
  2. The Platform will automatically submit metadata about the channel to the validators, which will ensure it does indeed exist on-chain, and will start receiving events (impressions, clicks) for it
  3. The Market will detect that the validators have started processing the channel, and it's state will change from Ready to Active in the Market and the Explorer
  4. The AdView will start showing ads from the campaign this channel is tied to
  5. The AdView will send impression/click events to the validators, who will update the balances of earners (publishers) by exchanging NewState(balances, signature) and ApproveState(signature) messages between themselves
  6. Now a publisher who's earned from this channel may withdraw, by calling channelWithdraw and submitting the last signatures from the validators, along with a merkle proof of their balance in the balances tree; they may withdraw from multiple channels in one transaction ("sweeping"), thanks to our Identity contract, as you can see here. This particular transaction withdrew value incurred from ~159k micropayments, from 4 channels, and cost only $0.16 in gas fees. Keep in mind, the gas costs do not increase with larger amounts/more transactions.

In the Platform, step 6 is automated: the users see the total funds they have, no matter if on-chain or on channels. If you try to spend all your funds at once, the Platform will automatically "sweep" all channels first, making all your funds spendable on-chain.

For more details on the inner workings of the protocol, you can read our protocol docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment