# ERC20 - ERC777 bridge
## Actors
- **Alice** and **Charlie** are 2 users of a DApp and want to exchange value using an ERC20 token.
- The **MainBridge** contract locks tokens on the mainnet.
- The **ForeignBridge** contract manages the minting and burning of the equivalent tokens on the **Foreign chain**
- A group of **validators** watches the ERC20 token events to trigger intra-chain
- The list of **validators** (addresses) are stored in the MainBridge and ForeignBridge contracts
- A **threshold** is set on how many signatures are required to execute the intra-chain transfers.
- The validators **sign-off** on intra-chain token deposits or withdrawals.
## Assumptions on trust
- Alice and Charlie **trust the majority** of the validators to act in their interest
- Alice and Charlie assume the **minimum threshold of validators is always online** to process their intra-chain conversions.
## Setup
An administrator ( currently the owner of the contracts ) adds an existing ERC20 token from the Main net in the ForeignBridge. This function creates a new (ERC777) token on the Foreign net and links the Mainnet address to the newly generated token.
The validators watch these events , and start watching Transfer() events on both chains. These watches are filtered on
- Transfers towards the MainBridge on the Main net
- Transfers towards the Foreignbridge on the Foreign net
## Scenario
1. Alice has some 'Sample ERC20' tokens on the mainnet and goes from Main-net to Foreign-net.
2. On the Foreign-net, she will send tokens to Charlie.
3. Charlie then redeems his received Foreign-net tokens for Main-net tokens.
```sequence
Title: Transfer to Foreign-net
Alice->Sample ERC20: 'Send 10 token to MainBridge`
Sample ERC20->MainBridge: transfers 10 token
Validator-->Sample ERC20: Sees Transfer() event to MainBridge
Validator->ForeignBridge: sign mint event
Note left of ForeignBridge: validator 2 signs too
Note left of ForeignBridge: validator 3 signs too
Note left of ForeignBridge: when sign threshold is reached
Validator->ForeignBridge: sign mint event
Note right of ForeignBridge: finalize mint request
ForeignBridge->Sample ERC777: mints 10 token for Alice
```
```sequence
Title: Usage on Foreign chain
Alice->Sample ERC777: 'Send 1 token to Charlie'
Sample ERC777->Charlie: transfers 1 token to Charlie
```
```sequence
Title: Redeem on Main-net
Charlie->Sample ERC777: 'Send 1 token to ForeignBridge'
Sample ERC777->ForeignBridge: transfers 1 token
Note over ForeignBridge: creates withdraw request
Validator-->Sample ERC777: Sees Transfer() event to ForeignBridge
Validator->ForeignBridge: sign Withdraw request
Note right of ForeignBridge: validator 2 signs
Note right of ForeignBridge: validator 3 signs
Note right of ForeignBridge: when sign threshold is reached
Validator->ForeignBridge: sign Withdraw request
Note right of ForeignBridge: finalize withdraw
ForeignBridge->Sample ERC777: Burn 1 token
Validator->MainBridge: withdraw(1,charlie)
MainBridge->Charlie: transfer 1 Sample ERC20 token
```
## notes
- after this flow , Alice still has 9 tokens locked in the MainBridge contract
- Alice can redeem her 9 tokens using the same flow as Charlie
# Token incentive for withdrawal
Alice needs the final withdrawal of her token executed on the main net - which costs gas.
Alice has 2 options:
- Alice executes the token withdrawal herself , using the validator signatures. She pays for the gas.
- Alice can incentivize someone else to do the transaction for her.
There is an incentivation scheme buit into the bridge , whereby Alice can offer a reward in tokens for anyone doing the final withdrawal transaction.
This is by offering a number of tokens as a reward to whoever executes the transaction.
```sequence
Title: Redeem on Main-net with token reward
Alice->ForeignBridge: 'Send 10 token to ForeignBridge'
Note over ForeignBridge: validation procedure takes place
Note over ForeignBridge: validator signatures are stored
Alice->ForeignBridge: 'I offer a reward of 1 SWT for doing my withdrawal'
Note over ForeignBridge: alices reward signatures is stored
ForeignBridge-->Charlie: charlie sees reward
Note over Charlie: Charlie executes withdrawal
Note over Charlie: ... using validators signatures
Note over Charlie: ... and alice's reward signature
Charlie->MainBridge: withdraw(10,alice)
Note over MainBridge: bridge sends 9 tokens to alice , 1 to charlie
```