# How we did it - technical

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](https://blog.muun.com/rebalancing-in-the-lightning-network/)). 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, at our [GitHub repository](https://github.com/adexnetwork/adex-validator).

The AdEx Platform 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](https://github.com/AdExNetwork/adex-protocol-eth/blob/master/contracts/Identity.sol).

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

1. A channel is open by invoking `channelOpen` on the [core contract](https://github.com/AdExNetwork/adex-protocol-eth/blob/master/contracts/AdExCore.sol#L33); this transfers DAI from the user's wallet and locks them up; see [an example](https://etherscan.io/tx/0x0613554997e43d330bd1cbfd363fc3829afaeb7f38c4033350d13184e3969960)
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](https://github.com/AdExNetwork/adex-protocol-eth/blob/master/contracts/AdExCore.sol#L66) 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](https://etherscan.io/tx/0x89e434c7e0ec0a83b134be94c3c9e01ec99a7d39fe98036ae84c1bf307ffd075). 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](https://github.com/adexnetwork/adex-protocol).
