Skip to content

Commit

Permalink
chore: Update quickstart template with faucet handling (#340)
Browse files Browse the repository at this point in the history
This handles updating the quickstart template with waiting for
the faucet transaction.
  • Loading branch information
alex-stone authored Dec 16, 2024
1 parent 5842b30 commit e9ccb2c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
12 changes: 6 additions & 6 deletions quickstart-template/discord_tutorial/webhook-transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -114,4 +114,4 @@ function transformConfig(filePath) {

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
}
3 changes: 3 additions & 0 deletions quickstart-template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 3 additions & 0 deletions quickstart-template/mass-payout.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 7 additions & 5 deletions quickstart-template/wallet-history.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
39 changes: 22 additions & 17 deletions quickstart-template/webhook-wallet-transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,71 @@ const webhookNotificationUri = '<YOUR_NOTIFICATION_URL>'
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({
Expand All @@ -80,7 +85,7 @@ const webhookNotificationUri = '<YOUR_NOTIFICATION_URL>'
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());
Expand Down

0 comments on commit e9ccb2c

Please sign in to comment.