Skip to content

Commit efd37e5

Browse files
authored
(mostly) format cleanup (#56)
1 parent b7a0d00 commit efd37e5

27 files changed

+2657
-2717
lines changed

.gitignore

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,5 @@ out/
77
/broadcast/*/31337/
88
/broadcast/**/dry-run/
99

10-
# Docs
11-
docs/
12-
1310
# Dotenv file
1411
.env

README.md

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ It is strongly recommended that you run the forge test suite of this SDK with yo
4040

4141
This SDK comes with its own IERC20 interface. Given that projects tend to combine different SDKs, there's often this annoying issue of clashes of IERC20 interfaces, even though the are effectively the same. We handle this issue by importing `IERC20/IERC20.sol` which allows remapping the `IERC20/` prefix to whatever directory contains `IERC20.sol` in your project, thus providing an override mechanism that should allow dealing with this problem seamlessly until forge allows remapping of individual files.
4242

43+
## Components
44+
45+
For additional documentation of components, see the docs directory.
46+
4347
## Philosophy/Creeds
4448

4549
In This House We Believe:
@@ -49,42 +53,8 @@ In This House We Believe:
4953
* git gud
5054
* shut up and suffer
5155

52-
## WormholeRelayer
53-
54-
### Introduction
55-
56-
The WormholeRelayer (also sometimes referred to as the automatic or generic relayer) allows integrators to leverage external parties known as delivery providers, to relay messages emitted on a given source chain to the intended target chain.
57-
58-
This frees integrators, who are building a cross-chain app, from the cumbersome and painful task of having to run relaying infrastructure themselves (and thus e.g. dealing with the headache of having to acquire gas tokens for the target chain).
59-
60-
Messages include, but aren't limited to: Wormhole attestations (VAAs), Circle attestations (CCTP)
61-
62-
Delivery providers provide a quote for the cost of a delivery on the source chain and also take payment there. This means the process is not fully trustless (delivery providers can take payment and then fail to perform the delivery), however the state of the respective chains always makes it clear whether a delivery provider has done their duty for a given delivery and delivery providers can't maliciously manipulate the content of a delivery.
63-
64-
### Example Usage
65-
66-
[HelloWormhole - Simple cross-chain message sending application](https://github.com/wormhole-foundation/hello-wormhole)
67-
68-
[HelloToken - Simple cross-chain token sending application](https://github.com/wormhole-foundation/hello-token)
69-
70-
[HelloUSDC - Simple cross-chain USDC sending application using CCTP](https://github.com/wormhole-foundation/hello-usdc)
71-
72-
### SDK Summary
73-
74-
- Includes interfaces to interact with contracts in the Wormhole ecosystem ([src/interfaces](https://github.com/wormhole-foundation/wormhole-solidity-sdk/tree/main/src/interfaces))
75-
- Includes the base class ‘Base’ with helpers for common actions that will typically need to be done within ‘receiveWormholeMessages’:
76-
- [`onlyWormholeRelayer()`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/Base.sol#L9): Checking that msg.sender is the wormhole relayer contract
77-
Sometimes, Cross-chain applications may be set up such that there is one ‘spoke’ contract on every chain, which sends messages to the ‘hub’ contract. If so, we’d ideally only want to allow messages to be sent from these spoke contracts. Included are helpers for this:
78-
79-
- [`setRegisteredSender(uint16 sourceChain, bytes32 sourceAddress)`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/Base.sol#L45): Setting the specified sender for ‘sourceChain’ to be ‘sourceAddress’
80-
- [`isRegisteredSender(uint16 sourceChain, bytes32 sourceAddress)`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/Base.sol#L30): Checking that the sender who requested the delivery is the registered address for that chain
81-
82-
Look at test/Counter.t.sol for an example usage of Base
83-
84-
- Included are also:
85-
- The ‘[TokenSender](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/TokenBase.sol#L24)’ and ‘[TokenReceiver](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/TokenBase.sol#L158)’ base classes with helpers for smart contracts that wish to send and receive tokens using Wormhole’s TokenBridge. See ‘[HelloToken](https://github.com/wormhole-foundation/hello-token)’ for example usage.
86-
- The ‘[CCTPSender](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/CCTPBase.sol#L59)’ and ‘[CCTPReceiver](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/CCTPBase.sol#L177)’ base classes with helpers for smart contracts that wish to send and receive both tokens using Wormhole’s TokenBridge as well as USDC using CCTP. See ‘[HelloUSDC](https://github.com/wormhole-foundation/hello-usdc)’ for example usage.
87-
- Or a combination of both - [CCTPAndTokenBase](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/CCTPAndTokenBase.sol).
88-
- Helpers for setting up a local forge testing environment. See ‘[HelloWormhole](https://github.com/wormhole-foundation/hello-wormhole)’ for example usage.
56+
## Notable Solidity Repos
8957

90-
**Note: This code is meant to be used as starter / reference code. Feel free to modify for use in your contracts, and also make sure to audit any code used from here as part of your contracts before deploying to mainnet.**
58+
* [OpenZeppelin](https://github.com/OpenZeppelin/openzeppelin-contracts)
59+
* [Solmate](https://github.com/transmissions11/solmate) / [Solady](https://github.com/Vectorized/solady)
60+
* [Uniswap Permit2](https://github.com/Uniswap/permit2) + [explanation](https://github.com/dragonfly-xyz/useful-solidity-patterns/tree/main/patterns/permit2)

docs/Optimization.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Compiler Optimization
2+
3+
List of ways to avoid short-comings of the current optimizer which lead to suboptimal byte code
4+
5+
## for loop array length checking
6+
7+
```
8+
function iterate(uint[] memory myArray) {
9+
uint len = myArray.length;
10+
for (uint i; i < len; ++i) { /*...*/}
11+
}
12+
```
13+
is more efficient than
14+
```
15+
function iterate(uint[] memory myArray) {
16+
for (uint i; i < myArray.length; ++i) { /*...*/}
17+
}
18+
```
19+
even if it is trivial for the optimizer to check that `myArray`'s length can't possibly change as part of the loop.
20+
21+
If `myArray` uses `calldata` instead of `memory`, both versions produce the same bytecode.
22+
23+
## prefer `< MAX + 1` over `<= MAX` for const comparison
24+
25+
Given that the EVM only supports `LT` and `GT` but not `LTE` or `GTE`, solc implements `x<=y` as `!(x>y)`. However, given a constant `MAX`, since solc resolves `MAX + 1` at compile time, `< MAX + 1` saves one `ISZERO` opcode.
File renamed without changes.

docs/WormholeRelayer.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# WormholeRelayer
2+
3+
The WormholeRelayer (also sometimes referred to as the automatic or generic relayer) allows integrators to leverage external parties known as delivery providers, to relay messages emitted on a given source chain to the intended target chain.
4+
5+
This frees integrators, who are building a cross-chain app, from the cumbersome and painful task of having to run relaying infrastructure themselves (and thus e.g. dealing with the headache of having to acquire gas tokens for the target chain).
6+
7+
Messages include, but aren't limited to: Wormhole attestations (VAAs), Circle attestations (CCTP)
8+
9+
Delivery providers provide a quote for the cost of a delivery on the source chain and also take payment there. This means the process is not fully trustless (delivery providers can take payment and then fail to perform the delivery), however the state of the respective chains always makes it clear whether a delivery provider has done their duty for a given delivery and delivery providers can't maliciously manipulate the content of a delivery.
10+
11+
## Example Usage
12+
13+
[HelloWormhole - Simple cross-chain message sending application](https://github.com/wormhole-foundation/hello-wormhole)
14+
15+
[HelloToken - Simple cross-chain token sending application](https://github.com/wormhole-foundation/hello-token)
16+
17+
[HelloUSDC - Simple cross-chain USDC sending application using CCTP](https://github.com/wormhole-foundation/hello-usdc)
18+
19+
## SDK Summary
20+
21+
- Includes interfaces to interact with contracts in the Wormhole ecosystem ([src/interfaces](https://github.com/wormhole-foundation/wormhole-solidity-sdk/tree/main/src/interfaces))
22+
- Includes the base class ‘Base’ with helpers for common actions that will typically need to be done within ‘receiveWormholeMessages’:
23+
- [`onlyWormholeRelayer()`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/Base.sol#L9): Checking that msg.sender is the wormhole relayer contract
24+
Sometimes, Cross-chain applications may be set up such that there is one ‘spoke’ contract on every chain, which sends messages to the ‘hub’ contract. If so, we’d ideally only want to allow messages to be sent from these spoke contracts. Included are helpers for this:
25+
26+
- [`setRegisteredSender(uint16 sourceChain, bytes32 sourceAddress)`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/Base.sol#L45): Setting the specified sender for ‘sourceChain’ to be ‘sourceAddress’
27+
- [`isRegisteredSender(uint16 sourceChain, bytes32 sourceAddress)`](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/Base.sol#L30): Checking that the sender who requested the delivery is the registered address for that chain
28+
29+
Look at test/Counter.t.sol for an example usage of Base
30+
31+
- Included are also:
32+
- The ‘[TokenSender](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/TokenBase.sol#L24)’ and ‘[TokenReceiver](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/TokenBase.sol#L158)’ base classes with helpers for smart contracts that wish to send and receive tokens using Wormhole’s TokenBridge. See ‘[HelloToken](https://github.com/wormhole-foundation/hello-token)’ for example usage.
33+
- The ‘[CCTPSender](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/CCTPBase.sol#L59)’ and ‘[CCTPReceiver](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/CCTPBase.sol#L177)’ base classes with helpers for smart contracts that wish to send and receive both tokens using Wormhole’s TokenBridge as well as USDC using CCTP. See ‘[HelloUSDC](https://github.com/wormhole-foundation/hello-usdc)’ for example usage.
34+
- Or a combination of both - [CCTPAndTokenBase](https://github.com/wormhole-foundation/wormhole-solidity-sdk/blob/main/src/WormholeRelayer/CCTPAndTokenBase.sol).
35+
- Helpers for setting up a local forge testing environment. See ‘[HelloWormhole](https://github.com/wormhole-foundation/hello-wormhole)’ for example usage.
36+
37+
**Note: This code is meant to be used as starter / reference code. Feel free to modify for use in your contracts, and also make sure to audit any code used from here as part of your contracts before deploying to mainnet.**

src/Utils.sol

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ pragma solidity ^0.8.19;
55
error NotAnEvmAddress(bytes32);
66

77
function toUniversalAddress(address addr) pure returns (bytes32 universalAddr) {
8-
universalAddr = bytes32(uint256(uint160(addr)));
8+
universalAddr = bytes32(uint256(uint160(addr)));
99
}
1010

1111
function fromUniversalAddress(bytes32 universalAddr) pure returns (address addr) {
12-
if (bytes12(universalAddr) != 0)
13-
revert NotAnEvmAddress(universalAddr);
12+
if (bytes12(universalAddr) != 0)
13+
revert NotAnEvmAddress(universalAddr);
1414

15-
assembly ("memory-safe") {
16-
addr := universalAddr
17-
}
15+
assembly ("memory-safe") {
16+
addr := universalAddr
17+
}
1818
}

src/WormholeRelayer/Base.sol

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,49 +7,49 @@ import "wormhole-sdk/interfaces/IWormhole.sol";
77
import "wormhole-sdk/Utils.sol";
88

99
abstract contract Base {
10-
IWormholeRelayer public immutable wormholeRelayer;
11-
IWormhole public immutable wormhole;
10+
IWormholeRelayer public immutable wormholeRelayer;
11+
IWormhole public immutable wormhole;
1212

13-
address registrationOwner;
14-
mapping(uint16 => bytes32) registeredSenders;
13+
address registrationOwner;
14+
mapping(uint16 => bytes32) registeredSenders;
1515

16-
constructor(address _wormholeRelayer, address _wormhole) {
17-
wormholeRelayer = IWormholeRelayer(_wormholeRelayer);
18-
wormhole = IWormhole(_wormhole);
19-
registrationOwner = msg.sender;
20-
}
16+
constructor(address _wormholeRelayer, address _wormhole) {
17+
wormholeRelayer = IWormholeRelayer(_wormholeRelayer);
18+
wormhole = IWormhole(_wormhole);
19+
registrationOwner = msg.sender;
20+
}
2121

22-
modifier onlyWormholeRelayer() {
23-
require(
24-
msg.sender == address(wormholeRelayer),
25-
"Msg.sender is not Wormhole Relayer"
26-
);
27-
_;
28-
}
22+
modifier onlyWormholeRelayer() {
23+
require(
24+
msg.sender == address(wormholeRelayer),
25+
"Msg.sender is not Wormhole Relayer"
26+
);
27+
_;
28+
}
2929

30-
modifier isRegisteredSender(uint16 sourceChain, bytes32 sourceAddress) {
31-
require(
32-
registeredSenders[sourceChain] == sourceAddress,
33-
"Not registered sender"
34-
);
35-
_;
36-
}
30+
modifier isRegisteredSender(uint16 sourceChain, bytes32 sourceAddress) {
31+
require(
32+
registeredSenders[sourceChain] == sourceAddress,
33+
"Not registered sender"
34+
);
35+
_;
36+
}
3737

38-
/**
39-
* Sets the registered address for 'sourceChain' to 'sourceAddress'
40-
* So that for messages from 'sourceChain', only ones from 'sourceAddress' are valid
41-
*
42-
* Assumes only one sender per chain is valid
43-
* Sender is the address that called 'send' on the Wormhole Relayer contract on the source chain)
44-
*/
45-
function setRegisteredSender(
46-
uint16 sourceChain,
47-
bytes32 sourceAddress
48-
) public {
49-
require(
50-
msg.sender == registrationOwner,
51-
"Not allowed to set registered sender"
52-
);
53-
registeredSenders[sourceChain] = sourceAddress;
54-
}
38+
/**
39+
* Sets the registered address for 'sourceChain' to 'sourceAddress'
40+
* So that for messages from 'sourceChain', only ones from 'sourceAddress' are valid
41+
*
42+
* Assumes only one sender per chain is valid
43+
* Sender is the address that called 'send' on the Wormhole Relayer contract on the source chain)
44+
*/
45+
function setRegisteredSender(
46+
uint16 sourceChain,
47+
bytes32 sourceAddress
48+
) public {
49+
require(
50+
msg.sender == registrationOwner,
51+
"Not allowed to set registered sender"
52+
);
53+
registeredSenders[sourceChain] = sourceAddress;
54+
}
5555
}

0 commit comments

Comments
 (0)