|
| 1 | +# CCIPSender |
| 2 | + |
| 3 | +> This project is a proof-of-concept to demonstration interactions with the CCIP bridge. |
| 4 | +> |
| 5 | +> It's not designed to be production code, it's not security-audited, use at your own risk. |
| 6 | +
|
| 7 | +## Overview |
| 8 | + |
| 9 | +A sender contract serves as an entrypoint between your end user and the router contract from Chainlink. This contract is under your control and can be used to offer more possibility of customization. |
| 10 | + |
| 11 | +The sender contract will be the main entrypoint for your user to bridge tokens. It will contain all the logic to transfer tokens and send messages. |
| 12 | + |
| 13 | +More information is available in the [CCIP Chainlink website](https://docs.chain.link/ccip) and in the library CCIP in github [smartcontractkit/ccip](https://github.com/smartcontractkit/ccip) |
| 14 | + |
| 15 | +The main reference is the [CCIP masterclass](https://andrej-rakic.gitbook.io/chainlink-ccip/ccip-masterclass/exercise-1-transfer-tokens) |
| 16 | + |
| 17 | +## Configuration and usage |
| 18 | + |
| 19 | +To allow bidirectional transactions, you must deploy the sender contract on each blockchain. |
| 20 | + |
| 21 | +Let's look at the example of a bridge between Polygon and Avalanche. |
| 22 | + |
| 23 | +After deployment of the contracts, the first step is to establish the link between Polygon and Avalanche. |
| 24 | + |
| 25 | +### Allow list chains |
| 26 | + |
| 27 | +We will allow Avalanche to be a destination chain. We call the function `setAllowlistChain()` to do this. |
| 28 | + |
| 29 | +```solidity |
| 30 | +function setAllowlistChain(uint64 _chainSelector, bool allowedSourceChain, bool allowedDestinationChain) |
| 31 | +``` |
| 32 | + |
| 33 | + Example: |
| 34 | + |
| 35 | +```solidity |
| 36 | +setAllowlistChain(6433500567565415381, false, true) |
| 37 | +``` |
| 38 | + |
| 39 | +And Polygon too |
| 40 | + |
| 41 | +```solidity |
| 42 | +setAllowlistChain(4051577828743386545, false, true) |
| 43 | +``` |
| 44 | + |
| 45 | +The configuration of the source chain is only useful if we use a receiver contract, but this is not the case for the moment. |
| 46 | + |
| 47 | + |
| 48 | +### Activate fee tokens |
| 49 | + |
| 50 | +We will now set the fee payment for our two senders contracts. |
| 51 | + |
| 52 | +For Polygon, we will authorize to pay the fees in MATIC, the Polygon PoS native token |
| 53 | + |
| 54 | +For Avalanche, we will authorize to pay the fees in AVAX, the Avalanche native token. |
| 55 | + |
| 56 | +The selected id for native token is zero, the function has to be called in the two blockchains |
| 57 | + |
| 58 | +```solidity |
| 59 | +changeStatusFeePaymentMethod(0, true); |
| 60 | +``` |
| 61 | + |
| 62 | +For non-native token, we use another function `setFeePaymentMethod`: |
| 63 | + |
| 64 | +```solidity |
| 65 | +function setFeePaymentMethod(IERC20 tokenAddress_, string calldata label_) |
| 66 | +``` |
| 67 | + |
| 68 | +### Authorize user to use the contracts |
| 69 | + |
| 70 | +Our function `transferTokens` from our sender contract is protected by an access control. Only authorized users can transfer tokens. You have to grant the corresponding role to your brider's users. |
| 71 | + |
| 72 | +```solidity |
| 73 | +grantRole(bytes32 role, address account) |
| 74 | +``` |
| 75 | + |
| 76 | +Example: |
| 77 | + |
| 78 | +```solidity |
| 79 | +grantRole(`b0f04d2e44f4b417ab78712b52802767b073e39f75dba9f6e3a31125b053f026`, <Sender address>) |
| 80 | +``` |
| 81 | + |
| 82 | +### Fund the sender contract |
| 83 | + |
| 84 | +It is important to fund our sender contract to pay the fees. |
| 85 | + |
| 86 | +For that, we will call the function ` depositNativeTokens()` with our account holding native tokens. |
| 87 | + |
| 88 | +### Approve the sender contract |
| 89 | + |
| 90 | +With our bridge user, we authorize the bridge to transfer our tokens in our name. |
| 91 | + |
| 92 | +This is done with the classic ERC-20 `approve` function. |
| 93 | + |
| 94 | + |
| 95 | +## Module |
| 96 | + |
| 97 | +We divided the code into several components called modules. Each module is responsible to perform specific task. |
| 98 | + |
| 99 | +| **Group** | **Contract name** | **Description** | |
| 100 | +| :------------ | :------------------- | :----------------------------------------------------------- | |
| 101 | +| | CCIPSender | Our main contract to deploy | |
| 102 | +| | CCIPBaseSender | Our base contract providing the public transfer functions | |
| 103 | +| Security | | | |
| 104 | +| | AuthorizationModule | Manage the access control | |
| 105 | +| Wrappers | | | |
| 106 | +| | CCIPSenderBuild | Build a CCIP message | |
| 107 | +| | CCIPContractBalance | Withdraw native and fee tokens from the contracts | |
| 108 | +| Configuration | | | |
| 109 | +| | CCIPAllowlistedChain | Set and define the blockchain supported by the sender contract. | |
| 110 | +| | CCIPRouterManage | Store the router contracts address and associated functions | |
| 111 | +| | CCIPSenderPayment | Compute fee required to transmit the transfer message | |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | +## Documentation |
| 116 | + |
| 117 | +Here a summary of the main documentation |
| 118 | + |
| 119 | +| Document | Link/Files | |
| 120 | +| ----------------------- | -------------------------------------- | |
| 121 | +| Technical documentation | [doc/technical](./doc/technical.md) | |
| 122 | +| Toolchain | [doc/TOOLCHAIN.md](./doc/TOOLCHAIN.md) | |
| 123 | +| Surya report | [doc/schem/surya](./doc/schema/surya) | |
| 124 | + |
| 125 | +## Deployment |
| 126 | + |
| 127 | +The main contract is `CCIPSender`. This contract has to be deployed on each source chain where you want perform transfers. |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | +## Usage |
| 132 | + |
| 133 | +*Explain how it works.* |
| 134 | + |
| 135 | + |
| 136 | +## Toolchain installation |
| 137 | + |
| 138 | +The contracts are developed and tested with [Foundry](https://book.getfoundry.sh), a smart contract development toolchain. |
| 139 | + |
| 140 | +To install the Foundry suite, please refer to the official instructions in the [Foundry book](https://book.getfoundry.sh/getting-started/installation). |
| 141 | + |
| 142 | +## Initialization |
| 143 | + |
| 144 | +You must first initialize the submodules, with |
| 145 | + |
| 146 | +``` |
| 147 | +forge install |
| 148 | +``` |
| 149 | + |
| 150 | +See also the command's [documentation](https://book.getfoundry.sh/reference/forge/forge-install). |
| 151 | + |
| 152 | +Later you can update all the submodules with: |
| 153 | + |
| 154 | +``` |
| 155 | +forge update |
| 156 | +``` |
| 157 | + |
| 158 | +See also the command's [documentation](https://book.getfoundry.sh/reference/forge/forge-update). |
| 159 | + |
| 160 | + |
| 161 | + |
| 162 | +## Compilation |
| 163 | + |
| 164 | +The official documentation is available in the Foundry [website](https://book.getfoundry.sh/reference/forge/build-commands) |
| 165 | + |
| 166 | +``` |
| 167 | + forge build --contracts src/deplyoment/CCIPSender.sol |
| 168 | +``` |
| 169 | + |
| 170 | +## Testing |
| 171 | + |
| 172 | +You can run the tests with |
| 173 | + |
| 174 | +``` |
| 175 | +forge test |
| 176 | +``` |
| 177 | + |
| 178 | +To run a specific test, use |
| 179 | + |
| 180 | +``` |
| 181 | +forge test --match-contract <contract name> --match-test <function name> |
| 182 | +``` |
| 183 | + |
| 184 | +See also the test framework's [official documentation](https://book.getfoundry.sh/forge/tests), and that of the [test commands](https://book.getfoundry.sh/reference/forge/test-commands). |
| 185 | + |
| 186 | +### Coverage |
| 187 | + |
| 188 | +* Perform a code coverage |
| 189 | + |
| 190 | +``` |
| 191 | +forge coverage |
| 192 | +``` |
| 193 | + |
| 194 | +* Generate LCOV report |
| 195 | + |
| 196 | +``` |
| 197 | +forge coverage --report lcov |
| 198 | +``` |
| 199 | + |
| 200 | +- Generate `index.html` |
| 201 | + |
| 202 | +```bash |
| 203 | +forge coverage --report lcov && genhtml lcov.info --branch-coverage --output-dir coverage |
| 204 | +``` |
| 205 | + |
| 206 | +See [Solidity Coverage in VS Code with Foundry](https://mirror.xyz/devanon.eth/RrDvKPnlD-pmpuW7hQeR5wWdVjklrpOgPCOA-PJkWFU) & [Foundry forge coverage](https://www.rareskills.io/post/foundry-forge-coverage) |
| 207 | + |
| 208 | +## Intellectual property |
| 209 | + |
| 210 | +The original code is copyright (c) Taurus 2024, and is released under [MIT license](LICENSE). |
0 commit comments