Skip to main content

Advanced Usage

This section explores advanced features and best practices for using the Gopher SDK in your decentralized applications (DApps).

Handling Errors​

The DApp is responsible for handling errors that occur during the transaction process. Ensure to implement proper error handling mechanisms to provide a smooth user experience.

Example​

gopher.getUserBalances()
.then(balances => {
console.log('User balances:', balances);
})
.catch(error => {
console.error('Error fetching balances:', error);
});

Explanation​

  • catch: The catch method is used to handle any errors that occur during the promise's execution, ensuring that your application can manage and log errors effectively.

Customizing Requests​

The Gopher SDK allows you to customize requests to suit your application's needs. Here’s how to provide additional options when fetching solutions.

Example​

gopher.getSolution({
token: "USDC",
destinationChain: 42161, // Destination chain ID
amount: "1000000000" // Amount in the smallest unit (e.g., wei)
}).then(solution => {
console.log('Transaction solution:', solution);
});

Explanation​

  • token, destinationChain, amount: These parameters specify the token, destination chain, and amount for the transaction.

Integrating with Other Libraries​

You can integrate the Gopher SDK with other libraries and tools in your DApp to enhance functionality.

Example with Web3.js v4​

import { Gopher } from '@chainsafe/gopher-sdk';
import Web3 from 'web3';

async function integrateWithWeb3() {
const web3 = new Web3(window.ethereum);
const accounts = await web3.eth.requestAccounts();

const gopher = new Gopher(window.ethereum);

const balances = await gopher.getUserBalances();
console.log('User balances:', balances);

const solution = await gopher.getSolution({
token: "USDC",
destinationChain: 42161,
amount: "1000000000"
});

console.log('Transaction solution:', solution);

// Execute transaction using Web3.js
const tx = solution[0].transaction;

web3.eth.sendTransaction(tx)
.on('transactionHash', (hash) => {
console.log('Transaction hash:', hash);
})
.on('receipt', (receipt) => {
console.log('Transaction receipt:', receipt);
})
.on('error', console.error);
}

integrateWithWeb3().catch(console.error);

Explanation​

  • Web3.js v4: A library for interacting with the Ethereum blockchain, used here to manage accounts and send transactions.
  • solution[0].transaction: The transaction object provided by Gopher's solution that is used to execute the transaction.

Best Practices​

Follow these best practices to ensure smooth and efficient integration of the Gopher SDK in your DApp:

  • Error Handling: Always implement comprehensive error handling to manage issues gracefully.
  • User Experience: Provide clear feedback to users about the status of their transactions and any errors that occur.
  • Security: Ensure that your application handles sensitive information securely and follows best practices for interacting with blockchain networks.
  • Optimization: Use the SDK’s features to optimize cross-chain transactions and minimize costs.

Next Steps​

  • Class API Reference: Get detailed information about the classes and methods provided by the SDK.
  • API Reference: Get detailed information about the classes and methods provided by the SDK.
  • Getting Started: Review the basic setup and core concepts.