This repository was archived by the owner on May 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathCoreRelayer.sol
508 lines (455 loc) · 32.7 KB
/
CoreRelayer.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// SPDX-License-Identifier: Apache 2
pragma solidity ^0.8.0;
import "../interfaces/IWormholeRelayer.sol";
import "./CoreRelayerDelivery.sol";
import "./CoreRelayerStructs.sol";
contract CoreRelayer is CoreRelayerDelivery {
enum ErrorReason {
SEND_ERROR,
FORWARD_ERROR,
MULTICHAIN_FORWARD_ERROR,
OTHER_ERROR
}
function getRefundAddressBalance(bytes32 refundAddress) public view returns (uint256) {
address payable account = payable(address(uint160(uint256(refundAddress))));
return account.balance;
}
uint256 leftoverFunds = getRefundAddressBalance(refundAddress);
IWormhole wormhole = wormhole();
uint256 wormholeMessageFee = wormhole.messageFee();
function getNeededValue(IWormholeRelayer.MultichainSend memory sendContainer) public view returns (uint256) {
// uint256 wormholeMessageFee = wormhole().messageFee();
uint256 totalMaxTransactionFee = 0;
uint256 totalReceiverValue = 0;
// Calculate the total maximum transaction fee and receiver value for all Send requests
for (uint256 i = 0; i < sendContainer.requests.length; i++) {
totalMaxTransactionFee += sendContainer.requests[i].maxTransactionFee;
totalReceiverValue += sendContainer.requests[i].receiverValue;
}
// Calculate the total needed value for the MultichainSend
uint256 neededValue = wormholeMessageFee + totalMaxTransactionFee + totalReceiverValue;
return neededValue;
}
uint256 needed_Value = getNeededValue(sendContainer);
/**
* @notice This 'send' function emits a wormhole message that alerts the default wormhole relay provider to
* call the receiveWormholeMessage(bytes[] memory vaas, bytes[] memory additionalData) endpoint of the contract on chain 'targetChain' and address 'targetAddress'
* with the first argument being all of the wormhole message in the current transaction that have nonce 'nonce' (which have additionally been encoded and signed by the Guardian set to form 'VAAs'),
* (including the one emitted from this function, which can be ignored)
* (these messages will be ordered in the 'vaas' array in the order they were emitted in the source transaction)
* and with the second argument empty
*
*
* @param targetChain The chain that the vaas are delivered to, in Wormhole Chain ID format
* @param targetAddress The address (in Wormhole 32-byte format) on chain 'targetChain' of the contract to which the vaas are delivered.
* This contract must implement the IWormholeReceiver interface, which simply requires a 'receiveWormholeMessage(bytes[] memory vaas, bytes[] memory additionalData)' endpoint
* @param refundAddress The address (in Wormhole 32-byte format) on chain 'targetChain' to which any leftover funds (that weren't used for target chain gas or passed into targetAddress as value) should be sent
* @param maxTransactionFee The maximum amount (denominated in source chain (this chain) currency) that you wish to spend on funding gas for the target chain.
* If more gas is needed on the target chain than is paid for, there will be a Receiver Failure.
* Any unused value out of this fee will be refunded to 'refundAddress'
* If maxTransactionFee >= quoteGas(targetChain, gasLimit, getDefaultRelayProvider()), then as long as 'targetAddress''s receiveWormholeMessage function uses at most 'gasLimit' units of gas (and doesn't revert), the delivery will succeed
* @param receiverValue The amount (denominated in source chain currency) that will be converted to target chain currency and passed into the receiveWormholeMessage endpoint as value.
* If receiverValue >= quoteReceiverValue(targetChain, targetAmount, getDefaultRelayProvider()), then at least 'targetAmount' of targetChain currency will be passed into the 'receiveWormholeFunction' as value.
* @param nonce The messages to be relayed are all of the emitted wormhole messages in the current transaction that have nonce 'nonce'.
*
* This function must be called with a payment of at least maxTransactionFee + receiverValue + one wormhole message fee.
*
* @return sequence The sequence number for the emitted wormhole message, which contains encoded delivery instructions meant for the default wormhole relay provider.
* The relay provider will listen for these messages, and then execute the delivery as described.
*/
function send(
uint16 targetChain,
bytes32 targetAddress,
bytes32 refundAddress,
uint256 maxTransactionFee,
uint256 receiverValue,
uint32 nonce
) external payable returns (uint64 sequence) {
sequence = send(
IWormholeRelayer.Send(
targetChain, targetAddress, refundAddress, maxTransactionFee, receiverValue, getDefaultRelayParams()
),
nonce,
getDefaultRelayProvider()
);
if (!(maxTransactionFee >= quoteGas(targetChain, gasLimit, getDefaultRelayProvider()))){
revert(ErrorReason.SEND_ERROR); // send function error
}}
/**
* @notice This 'send' function emits a wormhole message that alerts a relay provider to
* call the receiveWormholeMessage(bytes[] memory vaas, bytes[] memory additionalData) endpoint of the contract on chain 'targetChain' and address 'targetAddress'
* with the first argument being all of the wormhole message in the current transaction that have nonce 'nonce' (which have additionally been encoded and signed by the Guardian set to form 'VAAs'),
* (including the one emitted from this function, which can be ignored)
* (these messages will be ordered in the 'vaas' array in the order they were emitted in the source transaction)
* and with the second argument empty
*
*
* @param request The Send request containing info about the targetChain, targetAddress, refundAddress, maxTransactionFee, receiverValue, and relayParameters
* @param nonce The messages to be relayed are all of the emitted wormhole messages in the current transaction that have nonce 'nonce'.
* @param relayProvider The address of (the relay provider you wish to deliver the messages)'s contract on this source chain. This must be a contract that implements IRelayProvider.
* If request.maxTransactionFee >= quoteGas(request.targetChain, gasLimit, relayProvider),
* then as long as 'request.targetAddress''s receiveWormholeMessage function uses at most 'gasLimit' units of gas (and doesn't revert), the delivery will succeed
* If request.receiverValue >= quoteReceiverValue(request.targetChain, targetAmount, relayProvider), then at least 'targetAmount' of targetChain currency will be passed into the 'receiveWormholeFunction' as value.
* To use the default relay provider, set this field to be getDefaultRelayProvider()
*
* This function must be called with a payment of at least request.maxTransactionFee + request.receiverValue + one wormhole message fee.
*
* @return sequence The sequence number for the emitted wormhole message, which contains encoded delivery instructions meant for your specified relay provider.
* The relay provider will listen for these messages, and then execute the delivery as described.
*/
function send(IWormholeRelayer.Send memory request, uint32 nonce, address relayProvider)
public
payable
returns (uint64 sequence)
{
// call multichainSend with one 'Send' in the requests array
sequence = multichainSend(multichainSendContainer(request, relayProvider), nonce);
}
/**
* @notice This 'forward' function can only be called in a IWormholeReceiver within the 'receiveWormholeMessages' function
* It's purpose is to use any leftover fee from the 'maxTransactionFee' of the current delivery to fund another delivery
*
* Specifically, suppose an integrator requested a Send (with parameters oldTargetChain, oldTargetAddress, etc)
* and sets quoteGas(oldTargetChain, gasLimit, oldRelayProvider) as 'maxTransactionFee' in a Send,
* but during the delivery on oldTargetChain, the call to oldTargetAddress's receiveWormholeMessages endpoint uses only x units of gas (where x < gasLimit).
*
* Normally, (gasLimit - x)/gasLimit * oldMaxTransactionFee, converted to target chain currency, would be refunded to 'oldRefundAddress'.
* However, if during execution of receiveWormholeMessage the integrator made a call to forward,
*
* We instead would use [(gasLimit - x)/gasLimit * oldMaxTransactionFee, converted to target chain currency] + (any additional funds passed into forward)
* to fund a new delivery (of wormhole messages emitted during execution of oldTargetAddress's receiveWormholeMessages) that is requested in the call to 'forward'.
*
* Specifically, this 'forward' function is only callable within a delivery (during receiveWormholeMessages) and indicates the in-progress delivery to use any leftover funds from the current delivery to fund a new delivery
* or equivalently, indicates the in-progress delivery to call the receiveWormholeMessage(bytes[] memory vaas, bytes[] memory additionalData) endpoint of the contract on chain 'targetChain' and address 'targetAddress'
* with the first argument being all of the wormhole message in the current transaction that have nonce 'nonce' (which have additionally been encoded and signed by the Guardian set to form 'VAAs'),
* (which will be all of the wormhole messages emitted during the execution of oldTargetAddress's receiveWormholeMessages in the order that they were emitted, as well as one wormhole message *that is always at the end* that can be ignored)
* and with the second argument empty
*
* @param targetChain The chain that the vaas are delivered to, in Wormhole Chain ID format
* @param targetAddress The address (in Wormhole 32-byte format) on chain 'targetChain' of the contract to which the vaas are delivered.
* This contract must implement the IWormholeReceiver interface, which simply requires a 'receiveWormholeMessage(bytes[] memory vaas, bytes[] memory additionalData)' endpoint
* @param refundAddress The address (in Wormhole 32-byte format) on chain 'targetChain' to which any leftover funds (that weren't used for target chain gas or passed into targetAddress as value) should be sent
* @param maxTransactionFee The maximum amount (denominated in source chain (this chain) currency) that you wish to spend on funding gas for the target chain.
* If more gas is needed on the target chain than is paid for, there will be a Receiver Failure.
* Any unused value out of this fee will be refunded to 'refundAddress'
* If maxTransactionFee >= quoteGas(targetChain, gasLimit, getDefaultRelayProvider()), then as long as 'targetAddress''s receiveWormholeMessage function uses at most 'gasLimit' units of gas (and doesn't revert), the delivery will succeed
* @param receiverValue The amount (denominated in source chain currency) that will be converted to target chain currency and passed into the receiveWormholeMessage endpoint as value.
* If receiverValue >= quoteReceiverValue(targetChain, targetAmount, getDefaultRelayProvider()), then at least 'targetAmount' of targetChain currency will be passed into the 'receiveWormholeFunction' as value.
* @param nonce The messages to be relayed are all of the emitted wormhole messages in the current transaction that have nonce 'nonce'.
*
* This forward will succeed if (leftover funds from the current delivery that would have been refunded) + (any extra msg.value passed into forward) is at least maxTransactionFee + receiverValue + one wormhole message fee.
*/
function forward(
uint16 targetChain,
bytes32 targetAddress,
bytes32 refundAddress,
uint256 maxTransactionFee,
uint256 receiverValue,
uint32 nonce
) external payable {
forward(
IWormholeRelayer.Send(
targetChain, targetAddress, refundAddress, maxTransactionFee, receiverValue, getDefaultRelayParams()
),
nonce,
getDefaultRelayProvider()
);
if ((leftoverFunds + msg.value ) >= (maxTransactionFee + receiverValue + wormholeMessageFee)){
revert(ErrorReason.FORWARD_ERROR);
}
}
/**
* @notice This 'forward' function can only be called in a IWormholeReceiver within the 'receiveWormholeMessages' function
* It's purpose is to use any leftover fee from the 'maxTransactionFee' of the current delivery to fund another delivery
*
* Specifically, suppose an integrator requested a Send (with parameters oldTargetChain, oldTargetAddress, etc)
* and sets quoteGas(oldTargetChain, gasLimit, oldRelayProvider) as 'maxTransactionFee' in a Send,
* but during the delivery on oldTargetChain, the call to oldTargetAddress's receiveWormholeMessages endpoint uses only x units of gas (where x < gasLimit).
*
* Normally, (gasLimit - x)/gasLimit * oldMaxTransactionFee, converted to target chain currency, would be refunded to 'oldRefundAddress'.
* However, if during execution of receiveWormholeMessage the integrator made a call to forward,
*
* We instead would use [(gasLimit - x)/gasLimit * oldMaxTransactionFee, converted to target chain currency] + (any additional funds passed into forward)
* to fund a new delivery (of wormhole messages emitted during execution of oldTargetAddress's receiveWormholeMessages) that is requested in the call to 'forward'.
*
* Specifically, this 'forward' function is only callable within a delivery (during receiveWormholeMessages) and indicates the in-progress delivery to use any leftover funds from the current delivery to fund a new delivery
* or equivalently, indicates the in-progress delivery to call the receiveWormholeMessage(bytes[] memory vaas, bytes[] memory additionalData) endpoint of the contract on chain 'targetChain' and address 'targetAddress'
* with the first argument being all of the wormhole message in the current transaction that have nonce 'nonce' (which have additionally been encoded and signed by the Guardian set to form 'VAAs'),
* (which will be all of the wormhole messages emitted during the execution of oldTargetAddress's receiveWormholeMessages in the order that they were emitted, as well as one wormhole message *that is always at the end* that can be ignored)
* and with the second argument empty
*
* @param request The Send request containing info about the targetChain, targetAddress, refundAddress, maxTransactionFee, receiverValue, and relayParameters
* (specifically, the send info that will be used to deliver all of the wormhole messages emitted during the execution of oldTargetAddress's receiveWormholeMessages)
* This forward will succeed if (leftover funds from the current delivery that would have been refunded) + (any extra msg.value passed into forward) is at least maxTransactionFee + receiverValue + one wormhole message fee.
* @param nonce The messages to be relayed are all of the emitted wormhole messages in the current transaction (during execution of oldTargetAddress's receiveWormholeMessages) that have nonce 'nonce'.
* @param relayProvider The address of (the relay provider you wish to deliver the messages)'s contract on this source chain. This must be a contract that implements IRelayProvider.
* If request.maxTransactionFee >= quoteGas(request.targetChain, gasLimit, relayProvider),
* then as long as 'request.targetAddress''s receiveWormholeMessage function uses at most 'gasLimit' units of gas (and doesn't revert), the delivery will succeed
* If request.receiverValue >= quoteReceiverValue(request.targetChain, targetAmount, relayProvider), then at least 'targetAmount' of targetChain currency will be passed into the 'receiveWormholeFunction' as value.
* To use the default relay provider, set this field to be getDefaultRelayProvider()
*
* This function must be called with a payment of at least request.maxTransactionFee + request.receiverValue + one wormhole message fee.
*/
function forward(IWormholeRelayer.Send memory request, uint32 nonce, address relayProvider) public payable {
// call multichainForward with one 'Send' in the requests array
multichainForward(multichainSendContainer(request, relayProvider), nonce);
}
/**
* @notice The multichainSend function delivers all wormhole messages in the current transaction of nonce 'nonce' to many destinations,
* with each destination specified in a Send struct, describing the desired targetAddress, targetChain, maxTransactionFee, receiverValue, refundAddress, and relayParameters
*
* @param sendContainer The MultichainSend struct, containing the array of Send requests, as well as the desired relayProviderAddress
* @param nonce The messages to be relayed are all of the emitted wormhole messages in the current transaction that have nonce 'nonce'
*
* This function must be called with a payment of at least (one wormhole message fee) + Sum_(i=0 -> sendContainer.requests.length - 1) [sendContainer.requests[i].maxTransactionFee + sendContainer.requests[i].receiverValue].
*
* @return sequence The sequence number for the emitted wormhole message, which contains encoded delivery instructions meant for the default wormhole relay provider.
* The relay provider will listen for these messages, and then execute the delivery as described
*/
function multichainSend(IWormholeRelayer.MultichainSend memory sendContainer, uint32 nonce)
public
payable
returns (uint64 sequence)
{
IWormhole wormhole = wormhole();
uint256 wormholeMessageFee = wormhole.messageFee();
uint256 totalFee = getTotalFeeMultichainSend(sendContainer, wormholeMessageFee);
if (totalFee > msg.value) {
revert IWormholeRelayer.MsgValueTooLow();
}
if (nonce == 0) {
revert IWormholeRelayer.NonceIsZero();
}
if (sendContainer.requests.length == 0) {
revert IWormholeRelayer.MultichainSendEmpty();
}
IRelayProvider relayProvider = IRelayProvider(sendContainer.relayProviderAddress);
// For each 'Send' request,
// calculate how much gas the relay provider can pay for on 'request.targetChain' using 'request.newTransactionFee',
// and calculate how much value the relay provider will pass into 'request.targetAddress'
DeliveryInstructionsContainer memory instructionsContainer =
convertMultichainSendToDeliveryInstructionsContainer(sendContainer);
// For each 'Send' request,
// Check that the total amount of value the relay provider needs to use for this send is <= the relayProvider's maximum budget for 'targetChain'
// and check that the calculated gas is greater than 0
checkInstructions(instructionsContainer, IRelayProvider(sendContainer.relayProviderAddress));
// Mark the container as 'sufficientlyFunded', since the user passed in msg.value to fund all of the requests
instructionsContainer.sufficientlyFunded = true;
// Publish a wormhole message indicating to the relay provider (who is watching wormhole messages from this contract)
// to relay the messages from this transaction (of nonce 'nonce') to the specified chains, each with the calculated amount of gas and receiverValue
sequence = wormhole.publishMessage{value: wormholeMessageFee}(
nonce, encodeDeliveryInstructionsContainer(instructionsContainer), relayProvider.getConsistencyLevel()
);
// Pay the relay provider
pay(relayProvider.getRewardAddress(), totalFee - wormholeMessageFee);
}
/**
* @notice The multichainForward function can only be called in a IWormholeReceiver within the 'receiveWormholeMessages' function
* It's purpose is to use any leftover fee from the 'maxTransactionFee' of the current delivery to fund another delivery, specifically a multichain delivery to many destinations
* See the description of 'forward' for further explanation of what a forward is.
* multichainForward provides the same functionality of forward, while additionally allowing the same array of wormhole messages to be sent to many destinations
*
* Let LEFTOVER_VALUE = (leftover funds from the current delivery that would have been refunded) + (any extra msg.value passed into forward)
* and let NEEDED_VALUE = (one wormhole message fee) + Sum_(i=0 -> sendContainer.requests.length - 1) [sendContainer.requests[i].maxTransactionFee + sendContainer.requests[i].receiverValue].
* The multichainForward will succeed if LEFTOVER_VALUE >= NEEDED_VALUE
*
* note: If LEFTOVER_VALUE > NEEDED_VALUE, then the maxTransactionFee of the first request in the array of sends will be incremented by 'LEFTOVER_VALUE - NEEDED_VALUE'
*
* @param sendContainer The MultichainSend struct, containing the array of Send requests, as well as the desired relayProviderAddress
* @param nonce The messages to be relayed are all of the emitted wormhole messages in the current transaction that have nonce 'nonce'
*
*/
function multichainForward(IWormholeRelayer.MultichainSend memory sendContainer, uint32 nonce) public payable {
if (!isContractLocked()) {
revert IWormholeRelayer.NoDeliveryInProgress();
}
if (getForwardInstruction().isValid) {
revert IWormholeRelayer.MultipleForwardsRequested();
}
if (nonce == 0) {
revert IWormholeRelayer.NonceIsZero();
}
if (msg.sender != lockedTargetAddress()) {
revert IWormholeRelayer.ForwardRequestFromWrongAddress();
}
if (sendContainer.requests.length == 0) {
revert IWormholeRelayer.MultichainSendEmpty();
}
uint256 wormholeMessageFee = wormhole().messageFee();
uint256 totalFee = getTotalFeeMultichainSend(sendContainer, wormholeMessageFee);
// For each 'Send' request,
// calculate how much gas the relay provider can pay for on 'request.targetChain' using 'request.newTransactionFee',
// and calculate how much value the relay provider will pass into 'request.targetAddress'
DeliveryInstructionsContainer memory instructionsContainer =
convertMultichainSendToDeliveryInstructionsContainer(sendContainer);
// For each 'Send' request,
// Check that the total amount of value the relay provider needs to use for this send is <= the relayProvider's maximum budget for 'targetChain'
// and check that the calculated gas is greater than 0
checkInstructions(instructionsContainer, IRelayProvider(sendContainer.relayProviderAddress));
// Save information about the forward in state, so it can be processed after the execution of 'receiveWormholeMessages',
// because we will then know how much of the 'maxTransactionFee' of the current delivery is still available for use in this forward
setForwardInstruction(
ForwardInstruction({
container: encodeDeliveryInstructionsContainer(instructionsContainer),
nonce: nonce,
msgValue: msg.value,
totalFee: totalFee,
sender: msg.sender,
relayProvider: sendContainer.relayProviderAddress,
isValid: true
})
);
if (!(leftoverFunds>=needed_Value)) {
revert(ErrorReason.MULTICHAIN_FORWARD_ERROR); // multichain forward error
}
}
/**
* @notice This 'resend' function emits a wormhole message requesting to resend an array of messages that have been previously requested to be sent
* Specifically, if a user in transaction 'txHash' on chain 'sourceChain' emits many wormhole messages of nonce 'sourceNonce' and then
* makes a call to 'send' requesting these messages to be sent to 'targetAddress' on 'targetChain',
* then the user can request a redelivery of these wormhole messages any time in the future through a call to 'resend' using this function
*
* @param request Information about the resend request, including the source chain and source transaction hash,
* @param relayProvider The address of (the relay provider you wish to deliver the messages)'s contract on this source chain. This must be a contract that implements IRelayProvider.
* If the targetAddress's receiveWormholeMessage function uses 'gasLimit' units of gas, then we must have newMaxTransactionFee >= quoteGasResend(targetChain, gasLimit, relayProvider)
* This must be the same relayProvider that was designated to relay these messages previously
*
* @return sequence The sequence number for the emitted wormhole message, which contains encoded delivery instructions meant for your specified relay provider.
* The relay provider will listen for these messages, and then execute the redelivery as described
*/
function resend(IWormholeRelayer.ResendByTx memory request, address relayProvider)
public
payable
returns (uint64 sequence)
{
IWormhole wormhole = wormhole();
uint256 wormholeMessageFee = wormhole.messageFee();
if (request.newMaxTransactionFee + request.newReceiverValue + wormholeMessageFee > msg.value) {
revert IWormholeRelayer.MsgValueTooLow();
}
IRelayProvider provider = IRelayProvider(relayProvider);
// Calculate how much gas the relay provider can pay for on 'request.targetChain' using 'request.newTransactionFee',
// and calculate how much value the relay provider will pass into 'request.targetAddress'
RedeliveryByTxHashInstruction memory instruction = convertResendToRedeliveryInstruction(request, provider);
// Check that the total amount of value the relay provider needs to use for this redelivery is <= the relayProvider's maximum budget for 'targetChain'
// and check that the calculated gas is greater than 0
checkRedeliveryInstruction(instruction, provider);
// Publish a wormhole message indicating to the relay provider (who is watching wormhole messages from this contract)
// to re-relay the messages from transaction 'request.txHash' with the calculated amount of gas and receiverValue
sequence = wormhole.publishMessage{value: wormholeMessageFee}(
0, encodeRedeliveryInstruction(instruction), provider.getConsistencyLevel()
);
// Pay the relay provider
pay(provider.getRewardAddress(), msg.value - wormholeMessageFee);
}
/**
* @notice quoteGas tells you how much maxTransactionFee (denominated in current (source) chain currency) must be in order to fund a call to
* receiveWormholeMessages on a contract on chain 'targetChain' that uses 'gasLimit' units of gas
*
* Specifically, for a Send 'request',
* If 'request.targetAddress''s receiveWormholeMessage function uses 'gasLimit' units of gas,
* then we must have request.maxTransactionFee >= quoteGas(request.targetChain, gasLimit, relayProvider)
*
* @param targetChain the target chain that you wish to use gas on
* @param gasLimit the amount of gas you wish to use
* @param relayProvider The address of (the relay provider you wish to deliver the messages)'s contract on this source chain. This must be a contract that implements IRelayProvider.
*
* @return maxTransactionFee The 'maxTransactionFee' you pass into your request (to relay messages to 'targetChain' and use 'gasLimit' units of gas) must be at least this amount
*/
function quoteGas(uint16 targetChain, uint32 gasLimit, address relayProvider)
public
view
returns (uint256 maxTransactionFee)
{
IRelayProvider provider = IRelayProvider(relayProvider);
// maxTransactionFee is a linear function of the amount of gas desired
maxTransactionFee =
provider.quoteDeliveryOverhead(targetChain) + (gasLimit * provider.quoteGasPrice(targetChain));
}
/**
* @notice quoteGasResend tells you how much maxTransactionFee (denominated in current (source) chain currency) must be in order to fund a *resend* call to
* receiveWormholeMessages on a contract on chain 'targetChain' that uses 'gasLimit' units of gas
*
* Specifically, for a ResendByTx 'request',
* If 'request.targetAddress''s receiveWormholeMessage function uses 'gasLimit' units of gas,
* then we must have request.maxTransactionFee >= quoteGasResend(request.targetChain, gasLimit, relayProvider)
*
* @param targetChain the target chain that you wish to use gas on
* @param gasLimit the amount of gas you wish to use
* @param relayProvider The address of (the relay provider you wish to deliver the messages)'s contract on this source chain. This must be a contract that implements IRelayProvider.
*
* @return maxTransactionFee The 'maxTransactionFee' you pass into your resend request (to relay messages to 'targetChain' and use 'gasLimit' units of gas) must be at least this amount
*/
function quoteGasResend(uint16 targetChain, uint32 gasLimit, address relayProvider)
public
view
returns (uint256 maxTransactionFee)
{
IRelayProvider provider = IRelayProvider(relayProvider);
// maxTransactionFee is a linear function of the amount of gas desired
maxTransactionFee =
provider.quoteRedeliveryOverhead(targetChain) + (gasLimit * provider.quoteGasPrice(targetChain));
}
/**
* @notice quoteReceiverValue tells you how much receiverValue (denominated in current (source) chain currency) must be
* in order for the relay provider to pass in 'targetAmount' as msg value when calling receiveWormholeMessages.
*
* Specifically, for a send 'request',
* In order for 'request.targetAddress''s receiveWormholeMessage function to be called with 'targetAmount' of value,
* then we must have request.receiverValue >= quoteReceiverValue(request.targetChain, targetAmount, relayProvider)
*
* @param targetChain the target chain that you wish to receive value on
* @param targetAmount the amount of value you wish to be passed into receiveWormholeMessages
* @param relayProvider The address of (the relay provider you wish to deliver the messages)'s contract on this source chain. This must be a contract that implements IRelayProvider.
*
* @return receiverValue The 'receiverValue' you pass into your send request (to relay messages to 'targetChain' with 'targetAmount' of value) must be at least this amount
*/
function quoteReceiverValue(uint16 targetChain, uint256 targetAmount, address relayProvider)
public
view
returns (uint256 receiverValue)
{
IRelayProvider provider = IRelayProvider(relayProvider);
// Converts 'targetAmount' from target chain currency to source chain currency (using relayProvider's prices)
// and applies a multiplier of '1 + (buffer / denominator)'
(uint16 buffer, uint16 denominator) = provider.getAssetConversionBuffer(targetChain);
receiverValue = assetConversionHelper(
targetChain, targetAmount, chainId(), uint256(0) + denominator + buffer, denominator, true, provider
);
}
/**
* @notice Returns the address of the current default relay provider
* @return relayProvider The address of (the default relay provider)'s contract on this source chain. This must be a contract that implements IRelayProvider.
*/
function getDefaultRelayProvider() public view returns (address relayProvider) {
relayProvider = defaultRelayProvider();
}
/**
* @notice Returns default relay parameters
* @return relayParams default relay parameters
*/
function getDefaultRelayParams() public pure returns (bytes memory relayParams) {
return new bytes(0);
}
// Helper to put one Send struct into a MultichainSend struct
function multichainSendContainer(IWormholeRelayer.Send memory request, address relayProvider)
internal
pure
returns (IWormholeRelayer.MultichainSend memory container)
{
IWormholeRelayer.Send[] memory requests = new IWormholeRelayer.Send[](1);
requests[0] = request;
container = IWormholeRelayer.MultichainSend({relayProviderAddress: relayProvider, requests: requests});
}
function getErrorMessage(ErrorReason reason) public pure returns (string memory) {
if (reason == ErrorReason.SEND_ERROR) {
return "Send transaction failed.";
} else if (reason == ErrorReason.FORWARD_ERROR) {
return "Error forwarding transaction.";
} else if (reason == ErrorReason.MULTICHAIN_FORWARD_ERROR) {
return "Error forwarding transaction to multiple chains.";
} else if (reason == ErrorReason.OTHER_ERROR) {
return "Unknown error occurred.";
}
}
}