diff --git a/quickstart-template/discord_tutorial/webhook-transfer.js b/quickstart-template/discord_tutorial/webhook-transfer.js index d508d876..1aad72e8 100644 --- a/quickstart-template/discord_tutorial/webhook-transfer.js +++ b/quickstart-template/discord_tutorial/webhook-transfer.js @@ -29,7 +29,7 @@ const webhookNotificationUri = process.env.WEBHOOK_NOTIFICATION_URL; const saveSeed = myWallet.saveSeed(seedPath); console.log("✅ Seed saved: ", saveSeed); } - + const balance = await myWallet.getBalance(Coinbase.assets.Usdc); console.log(`💰 Wallet USDC balance:`, balance); if (balance <= 0) { @@ -38,13 +38,13 @@ const webhookNotificationUri = process.env.WEBHOOK_NOTIFICATION_URL; // Wait for the faucet transaction to confirm. await faucetTx.wait(); - + console.log("✅ Funds added!"); - + // Sometimes funds take a few seconds to be available on the wallet, so lets wait 5 secs await sleep(5000) } - + // Now use below code to get wallets addresses so we can use it for adding it to the webhook filter. let myWalletAddress = await myWallet.getDefaultAddress(); const myWalletAddressId = myWalletAddress.getId(); @@ -77,7 +77,7 @@ const webhookNotificationUri = process.env.WEBHOOK_NOTIFICATION_URL; destination: anotherWallet, gasless: true, // for USDC, you can add gasless flag, so you don't need to add ETH funds for paying for gas fees }); - + // Wait for the transfer to complete or fail on-chain await transfer.wait({ intervalSeconds: 1, // check for transfer completion each 1 second @@ -114,4 +114,4 @@ function transformConfig(filePath) { function sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); -} \ No newline at end of file +} diff --git a/quickstart-template/index.js b/quickstart-template/index.js index 23b2fba4..0ccd7e68 100644 --- a/quickstart-template/index.js +++ b/quickstart-template/index.js @@ -12,6 +12,9 @@ let address = await wallet.getDefaultAddress(); console.log(`Default address for the wallet: `, address.toString()); const faucetTransaction = await wallet.faucet(); + +// Wait for the faucet transaction to complete or fail on-chain. +await faucetTransaction.wait(); console.log(`Faucet transaction successfully completed: `, faucetTransaction.toString()); let anotherWallet = await Wallet.create(); diff --git a/quickstart-template/mass-payout.js b/quickstart-template/mass-payout.js index a1c2d47c..7131b0ab 100644 --- a/quickstart-template/mass-payout.js +++ b/quickstart-template/mass-payout.js @@ -46,6 +46,9 @@ async function createAndFundSendingWallet() { // Fund sending Wallet. const faucetTransaction = await sendingWallet.faucet(); + + // Wait for the faucet transaction to complete or fail on-chain. + await faucetTransaction.wait(); console.log(`Faucet transaction successfully completed: `, faucetTransaction.toString()); return sendingWallet; diff --git a/quickstart-template/wallet-history.js b/quickstart-template/wallet-history.js index 41de027f..921c9c1c 100644 --- a/quickstart-template/wallet-history.js +++ b/quickstart-template/wallet-history.js @@ -18,12 +18,14 @@ Coinbase.configureFromJson({ filePath: "~/Downloads/cdp_api_key.json" }); console.log('myWallet address: ', myWalletAddressId); console.log('anotherWallet address: ', anotherWalletAddressId); - - // Faucet wallet to add some funds - await myWallet.faucet(Coinbase.assets.Usdc); // USDC funds to actual transaction - await myWallet.faucet(Coinbase.assets.Eth); // ETH funds for transfer gas fee (USDC gas fee is charged in ETH) - + const usdcFaucetTx = await myWallet.faucet(Coinbase.assets.Usdc); // USDC funds to actual transaction + const ethFaucetTx = await myWallet.faucet(Coinbase.assets.Eth); // ETH funds for transfer gas fee (USDC gas fee is charged in ETH) + + // Wait for the faucet transactions to complete or fail on-chain. + await usdcFaucetTx.wait(); + await ethFaucetTx.wait(); + console.log('\nFunds added to myWallet! ...'); console.log('Funds available on myWallet:', (await myWallet.listBalances()).toString()); console.log('Funds available on anotherWallet:', (await anotherWallet.listBalances()).toString()); diff --git a/quickstart-template/webhook-wallet-transfer.js b/quickstart-template/webhook-wallet-transfer.js index 21733657..df135333 100644 --- a/quickstart-template/webhook-wallet-transfer.js +++ b/quickstart-template/webhook-wallet-transfer.js @@ -12,66 +12,71 @@ const webhookNotificationUri = '' console.log('------------------------------------------------------------------------------------------------------------------'); console.log('Please press Enter to continue...'); await waitForEnter(); - + // Create wallets for transferring funds between each other let myWallet = await Wallet.create(); let anotherWallet = await Wallet.create(); - + console.log('Wallets created! ...'); - + // Wallets come with a single default Address, accessible via getDefaultAddress() let myWalletAddress = await myWallet.getDefaultAddress(); let anotherWalletAddress = await anotherWallet.getDefaultAddress(); - + // Get both wallet addresses const myWalletAddressId = myWalletAddress.getId(); const anotherWalletAddressId = anotherWalletAddress.getId(); - + console.log('Wallets addresses fetched! ...'); - + try { // Faucet wallet to add some funds - await myWallet.faucet(Coinbase.assets.Usdc); // USDC funds to actual transaction - await myWallet.faucet(Coinbase.assets.Eth); // ETH funds for transfer gas fee (USDC gas fee is charged in ETH) + const usdcFaucetTx = await myWallet.faucet(Coinbase.assets.Usdc); // USDC funds to actual transaction + const ethFaucetTx = await myWallet.faucet(Coinbase.assets.Eth); // ETH funds for transfer gas fee (USDC gas fee is charged in ETH) + + // Wait for the faucet transactions to complete or fail on-chain. + await usdcFaucetTx.wait(); + await ethFaucetTx.wait(); + console.log('Funds added to myWallet! ...'); } catch(e) { console.log('------------------------------------------------------------------------------------------------------------------'); console.log("We're not able to add funds to the wallet we just created."); - + console.log(`Please add some funds to wallet: ${myWalletAddressId}`); console.log('Please press Enter after you added the funds to continue...'); await waitForEnter(); } - + console.log('Funds available on myWallet:', (await myWallet.listBalances()).toString()); - + // Let's create a webhook that will be triggered when a transfer happens between the two wallets created above // Don't forget to replace the notificationUri and signatureHeader let webhook = await Webhook.create({ networkId: Coinbase.networks.BaseSepolia, // Listening on sepolia testnet transactions notificationUri: webhookNotificationUri, // Your webhook address eventType: 'erc20_transfer', - eventFilters: [{ + eventFilters: [{ // Webhook will only be triggered when these filter criteria are met from_address: myWalletAddressId, to_address: anotherWalletAddressId, }], }); - + console.log(`\nWebhook successfully created: `, webhook.toString()); - + // You can fetch all the information used on webhook creation using getters functions: console.log(`\nWebhook event filters: `, webhook.getEventFilters()); console.log(`Webhook event type: `, webhook.getEventType()); console.log(`Webhook network id: `, webhook.getNetworkId()); console.log(`Webhook notification URI: `, webhook.getNotificationURI()); - + console.log('\n\n------------------------------------------------------------------------------------------------------------------'); console.log(`Before transferring, please make sure your webhook is listening for requests.`); console.log('------------------------------------------------------------------------------------------------------------------'); console.log('Please press Enter to continue...'); await waitForEnter(); - + console.log('Creating transfer...'); // Create transfer from myWallet to anotherWallet const transfer = await myWallet.createTransfer({ @@ -80,7 +85,7 @@ const webhookNotificationUri = '' destination: anotherWallet, gasless: true, // for USDC, you can also add gasless flag, so you don't need to add ETH funds for paying for gas fees }); - + // Wait for the transfer to complete or fail on-chain await transfer.wait(); console.log(`Transfer successfully completed: `, transfer.toString());