@@ -63,17 +63,22 @@ abstract contract TokenBase is Base {
63
63
64
64
function getDecimals (address tokenAddress ) internal view returns (uint8 decimals ) {
65
65
// query decimals
66
- (,bytes memory queriedDecimals ) = address (tokenAddress).staticcall (abi.encodeWithSignature ("decimals() " ));
66
+ (, bytes memory queriedDecimals ) = address (tokenAddress).staticcall (abi.encodeWithSignature ("decimals() " ));
67
67
decimals = abi.decode (queriedDecimals, (uint8 ));
68
68
}
69
69
70
- function getTokenAddressOnThisChain (uint16 tokenHomeChain , bytes32 tokenHomeAddress ) internal view returns (address tokenAddressOnThisChain ) {
71
- return tokenHomeChain == wormhole.chainId () ? fromWormholeFormat (tokenHomeAddress) : tokenBridge.wrappedAsset (tokenHomeChain, tokenHomeAddress);
70
+ function getTokenAddressOnThisChain (uint16 tokenHomeChain , bytes32 tokenHomeAddress )
71
+ internal
72
+ view
73
+ returns (address tokenAddressOnThisChain )
74
+ {
75
+ return tokenHomeChain == wormhole.chainId ()
76
+ ? fromWormholeFormat (tokenHomeAddress)
77
+ : tokenBridge.wrappedAsset (tokenHomeChain, tokenHomeAddress);
72
78
}
73
79
}
74
80
75
81
abstract contract TokenSender is TokenBase {
76
-
77
82
/**
78
83
* transferTokens wraps common boilerplate for sending tokens to another chain using IWormholeRelayer
79
84
* - approves tokenBridge to spend 'amount' of 'token'
@@ -94,13 +99,13 @@ abstract contract TokenSender is TokenBase {
94
99
95
100
/**
96
101
* transferTokens wraps common boilerplate for sending tokens to another chain using IWormholeRelayer.
97
- * A payload can be included in the transfer vaa. By including a payload here instead of the deliveryVaa,
102
+ * A payload can be included in the transfer vaa. By including a payload here instead of the deliveryVaa,
98
103
* fewer trust assumptions are placed on the WormholeRelayer contract.
99
- *
104
+ *
100
105
* - approves tokenBridge to spend 'amount' of 'token'
101
- * - emits token transfer VAA
106
+ * - emits token transfer VAA
102
107
* - returns VAA key for inclusion in WormholeRelayer `additionalVaas` argument
103
- *
108
+ *
104
109
* Note: this function uses transferTokensWithPayload instead of transferTokens since the former requires that only the targetAddress
105
110
* can redeem transfers. Otherwise it's possible for another address to redeem the transfer before the targetContract is invoked by
106
111
* the offchain relayer and the target contract would have to be hardened against this.
@@ -137,12 +142,7 @@ abstract contract TokenSender is TokenBase {
137
142
vaaKeys[0 ] = transferTokens (token, amount, targetChain, targetAddress);
138
143
139
144
return wormholeRelayer.sendVaasToEvm {value: cost}(
140
- targetChain,
141
- targetAddress,
142
- payload,
143
- receiverValue,
144
- gasLimit,
145
- vaaKeys
145
+ targetChain, targetAddress, payload, receiverValue, gasLimit, vaaKeys
146
146
);
147
147
}
148
148
@@ -160,24 +160,18 @@ abstract contract TokenSender is TokenBase {
160
160
vaaKeys[0 ] = transferTokens (token, amount, targetChain, targetAddress);
161
161
162
162
wormholeRelayer.forwardVaasToEvm {value: forwardMsgValue}(
163
- targetChain,
164
- targetAddress,
165
- payload,
166
- receiverValue,
167
- gasLimit,
168
- vaaKeys
163
+ targetChain, targetAddress, payload, receiverValue, gasLimit, vaaKeys
169
164
);
170
165
}
171
166
}
172
167
173
168
abstract contract TokenReceiver is TokenBase {
174
-
175
169
struct TokenReceived {
176
170
bytes32 tokenHomeAddress;
177
171
uint16 tokenHomeChain;
178
172
address tokenAddress; // wrapped address if tokenHomeChain !== this chain, else tokenHomeAddress (in evm address format)
179
173
uint256 amount;
180
- uint256 amountNormalized; // if decimals > 8, normalized to 8 decimal places
174
+ uint256 amountNormalized; // if decimals > 8, normalized to 8 decimal places
181
175
}
182
176
183
177
function receiveWormholeMessages (
@@ -187,22 +181,26 @@ abstract contract TokenReceiver is TokenBase {
187
181
uint16 sourceChain ,
188
182
bytes32 deliveryHash
189
183
) external payable {
190
- TokenReceived[] memory receivedTokens =
191
- new TokenReceived [](additionalVaas.length );
184
+ TokenReceived[] memory receivedTokens = new TokenReceived [](additionalVaas.length );
192
185
193
186
for (uint256 i = 0 ; i < additionalVaas.length ; ++ i) {
194
187
IWormhole.VM memory parsed = wormhole.parseVM (additionalVaas[i]);
195
- require (parsed.emitterAddress == tokenBridge.bridgeContracts (parsed.emitterChainId), "Not a Token Bridge VAA " );
188
+ require (
189
+ parsed.emitterAddress == tokenBridge.bridgeContracts (parsed.emitterChainId), "Not a Token Bridge VAA "
190
+ );
196
191
ITokenBridge.TransferWithPayload memory transfer = tokenBridge.parseTransferWithPayload (parsed.payload);
197
- require (transfer.to == toWormholeFormat (address (this )) && transfer.toChain == wormhole.chainId (), "Token was not sent to this address " );
192
+ require (
193
+ transfer.to == toWormholeFormat (address (this )) && transfer.toChain == wormhole.chainId (),
194
+ "Token was not sent to this address "
195
+ );
198
196
199
197
tokenBridge.completeTransferWithPayload (additionalVaas[i]);
200
198
201
199
address thisChainTokenAddress = getTokenAddressOnThisChain (transfer.tokenChain, transfer.tokenAddress);
202
200
uint8 decimals = getDecimals (thisChainTokenAddress);
203
201
uint256 denormalizedAmount = transfer.amount;
204
- if (decimals > 8 ) denormalizedAmount *= uint256 (10 ) ** (decimals - 8 );
205
-
202
+ if (decimals > 8 ) denormalizedAmount *= uint256 (10 ) ** (decimals - 8 );
203
+
206
204
receivedTokens[i] = TokenReceived ({
207
205
tokenHomeAddress: transfer.tokenAddress,
208
206
tokenHomeChain: transfer.tokenChain,
@@ -211,8 +209,8 @@ abstract contract TokenReceiver is TokenBase {
211
209
amountNormalized: transfer.amount
212
210
});
213
211
}
214
-
215
- // call into overriden method
212
+
213
+ // call into overriden method
216
214
receivePayloadAndTokens (payload, receivedTokens, sourceAddress, sourceChain, deliveryHash);
217
215
}
218
216
@@ -223,5 +221,4 @@ abstract contract TokenReceiver is TokenBase {
223
221
uint16 sourceChain ,
224
222
bytes32 deliveryHash
225
223
) internal virtual {}
226
-
227
224
}
0 commit comments