-
Notifications
You must be signed in to change notification settings - Fork 292
/
Copy pathexec-redeem.js
88 lines (77 loc) · 2.93 KB
/
exec-redeem.js
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
const { providers, Wallet } = require('ethers');
const {
arbLog,
requireEnvVariables,
addCustomNetworkFromFile,
} = require('arb-shared-dependencies');
const { ParentTransactionReceipt, ParentToChildMessageStatus } = require('@arbitrum/sdk');
require('dotenv').config();
requireEnvVariables(['PRIVATE_KEY', 'CHAIN_RPC', 'PARENT_CHAIN_RPC']);
/**
* Set up: instantiate wallets connected to providers
*/
const walletPrivateKey = process.env.PRIVATE_KEY;
const parentChainProvider = new providers.JsonRpcProvider(process.env.PARENT_CHAIN_RPC);
const childChainProvider = new providers.JsonRpcProvider(process.env.CHAIN_RPC);
const childChainWallet = new Wallet(walletPrivateKey, childChainProvider);
const main = async (parentChainTransactionHash) => {
await arbLog('Redeem a pending retryable ticket');
/**
* Add the custom network configuration to the SDK if present
*/
addCustomNetworkFromFile();
/**
* We start with a transaction hash from the parent chain.
* This is a transaction that triggered the creation of a retryable ticket
*/
if (
!parentChainTransactionHash.startsWith('0x') ||
parentChainTransactionHash.trim().length != 66
) {
throw new Error(`Hmm, ${parentChainTransactionHash} doesn't look like a txn hash...`);
}
/**
* First, we check if our parent-to-chain message is already redeemed on the child chain
*/
const parentChainTransactionReceipt = new ParentTransactionReceipt(
await parentChainProvider.getTransactionReceipt(parentChainTransactionHash),
);
const messages = await parentChainTransactionReceipt.getParentToChildMessages(childChainWallet);
const message = messages[0];
const messageResult = await message.waitForStatus();
const status = messageResult.status;
if (status === ParentToChildMessageStatus.REDEEMED) {
console.log(
`Retryable ticket is executed on the child chain 🥳 ${messageResult.childTxReceipt.transactionHash}`,
);
} else {
console.log(
`Retryable ticket failed to execute on the child chain. Status: ${ParentToChildMessageStatus[status]}`,
);
}
/**
* We use the redeem() method from Arbitrum SDK to manually redeem our ticket
*/
console.log(`Redeeming the ticket now...`);
const childChainTransaction = await message.redeem();
const childChainTransactionReceipt = await childChainTransaction.waitForRedeem();
console.log(
'The retryable ticket was successfully redeemed 🥳 :',
childChainTransactionReceipt.transactionHash,
);
};
// Getting the transaction hash from the command arguments
if (process.argv.length < 3) {
console.log(
`Missing the transaction hash on the parent chain that created the pending retryable ticket`,
);
console.log(`Usage: yarn run redeemPendingRetryable 0x<transaction-hash>`);
process.exit(1);
}
const transactionHash = process.argv[2];
main(transactionHash)
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});