MoonProvider
@moonup/ethers Package Documentation
Summary
The @moonup/ethers
package is a utility package that provides a MoonProvider
class for interacting with Ethereum-based blockchains using the MoonSDK from the @moonup/moon-sdk
package. This provider can be used to sign messages, transactions, and typed data using the MoonSDK, and it implements the Provider
and IEthereumProvider
interfaces from the ethers
library.
Usage Examples
Here's a basic example of how to use the MoonProvider
class:
import { MoonSDK } from '@moonup/moon-sdk';
import { MoonProvider } from '@moonup/ethers';
import { ethers } from 'ethers';
const SDK = new MoonSDK({ /* your configuration */ });
const address = '0xYourEthereumAddress';
const chainId = 1; // mainnet
const provider = new MoonProvider({ SDK, address, chainId });
// Now you can use the provider with ethers
const signer = provider.getSigner();
const balance = await signer.getBalance();
console.log(`Balance: ${ethers.utils.formatEther(balance)} ETH`);
API Reference
MoonProvider
MoonProvider
A provider for interacting with Ethereum-based blockchains using the MoonSDK.
Constructor
options
(Object)SDK
(MoonSDK): An instance of the MoonSDK.address
(string): The Ethereum address of the account.chainId
(number): The ID of the Ethereum chain.
Methods
request(args: RequestArguments)
: Sends a JSON-RPC request.const blockNumber = await provider.request({ method: 'eth_blockNumber' }); console.log(`Current block number: ${blockNumber}`);
updateConfig(options: MoonProviderOptions)
: Updates the configuration options.provider.updateConfig({ chainId: 4, address: '0xNewEthereumAddress' });
connect()
: Connects to the MoonSDK.await provider.connect();
disconnect()
: Disconnects from the MoonSDK.await provider.disconnect();
sendAsync(args: RequestArguments, callback: (error: Error | null, response: any) => void)
: Sends a JSON-RPC request asynchronously.provider.sendAsync({ method: 'eth_blockNumber' }, (error, result) => { if (error) { console.error(error); } else { console.log(`Current block number: ${result}`); } });
enable()
: Enables the provider.const accounts = await provider.enable(); console.log(`Connected accounts: ${accounts}`);
isMoonProvider()
: Returnstrue
if the provider is aMoonProvider
.console.log(`Is MoonProvider: ${provider.isMoonProvider()}`);
getChainId()
: Returns the ID of the Ethereum chain.console.log(`Chain ID: ${provider.getChainId()}`);
getSigner()
: Returns theMoonSigner
instance.const signer = provider.getSigner();
getNetwork()
: Returns the network information.const network = await provider.getNetwork(); console.log(`Network name: ${network.name}`);
getBlockNumber()
: Returns the current block number.const blockNumber = await provider.getBlockNumber(); console.log(`Current block number: ${blockNumber}`);
getGasPrice()
: Returns the current gas price.const gasPrice = await provider.getGasPrice(); console.log(`Current gas price: ${gasPrice.toString()} wei`);
getBalance(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag> | undefined)
: Returns the balance of an account.const balance = await provider.getBalance('0xYourEthereumAddress'); console.log(`Balance: ${ethers.utils.formatEther(balance)} ETH`);
getTransactionCount(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag> | undefined)
: Returns the number of transactions sent from an account.const transactionCount = await provider.getTransactionCount('0xYourEthereumAddress'); console.log(`Transaction count: ${transactionCount}`);
getCode(addressOrName: string | Promise<string>, blockTag?: BlockTag | Promise<BlockTag> | undefined)
: Returns the code of a contract.const code = await provider.getCode('0xYourContractAddress'); console.log(`Contract code: ${code}`);
getStorageAt(addressOrName: string | Promise<string>, position: BigNumberish | Promise<BigNumberish>, blockTag?: BlockTag | Promise<BlockTag> | undefined)
: Returns the value of a storage slot of a contract.const storageValue = await provider.getStorageAt('0xYourContractAddress', 0); console.log(`Storage value: ${storageValue}`);
sendTransaction(signedTransaction: string | Promise<string>)
: Sends a transaction.const signer = provider.getSigner(); const transaction = { to: '0xRecipientAddress', value: ethers.utils.parseEther('1.0'), }; const tx = await signer.sendTransaction(transaction); console.log(`Transaction hash: ${tx.hash}`);
call(transaction: Deferrable<TransactionRequest>, blockTag?: BlockTag | Promise<BlockTag> | undefined)
: Calls a contract method.const contract = new ethers.Contract('0xYourContractAddress', abi, provider); const result = await contract.myMethod(); console.log(`Method result: ${result}`);
estimateGas(transaction: Deferrable<TransactionRequest>)
: Estimates the gas required for a transaction.const signer = provider.getSigner(); const transaction = { to: '0xRecipientAddress', value: ethers.utils.parseEther('1.0'), }; const gasLimit = await signer.estimateGas(transaction); console.log(`Estimated gas limit: ${gasLimit.toString()}`);
getBlock(blockHashOrBlockTag: BlockTag | Promise<BlockTag>)
: Returns a block.const block = await provider.getBlock('latest'); console.log(`Block number: ${block.number}`);
getBlockWithTransactions(blockHashOrBlockTag: BlockTag | Promise<BlockTag>)
: Returns a block with its transactions.const block = await provider.getBlockWithTransactions('latest'); console.log(`Block number: ${block.number}`); console.log(`Transactions: ${block.transactions.length}`);
getTransaction(transactionHash: string)
: Returns a transaction.const transaction = await provider.getTransaction('0xYourTransactionHash'); console.log(`Transaction hash: ${transaction.hash}`);
getTransactionReceipt(transactionHash: string)
: Returns a transaction receipt.const receipt = await provider.getTransactionReceipt('0xYourTransactionHash'); console.log(`Transaction status: ${receipt.status}`);
getLogs(filter: Filter)
: Returns logs that match a filter.const filter = { address: '0xYourContractAddress', topics: [ethers.utils.id('MyEvent()')], }; const logs = await provider.getLogs(filter); console.log(`Number of logs: ${logs.length}`);
resolveName(name: string | Promise<string>)
: Resolves an ENS name to an address.const address = await provider.resolveName('myname.eth'); console.log(`Address: ${address}`);
lookupAddress(address: string | Promise<string>)
: Resolves an address to an ENS name.const name = await provider.lookupAddress('0xYourEthereumAddress'); console.log(`Name: ${name}`);
emit(eventName: EventType, ...args: any[])
: Emits an event.provider.on('block', (blockNumber) => { console.log(`New block: ${blockNumber}`); }); provider.emit('block', 1234567);
listenerCount(eventName?: EventType | undefined)
: Returns the number of listeners for an event.const listenerCount = provider.listenerCount('block'); console.log(`Number of listeners: ${listenerCount}`);
listeners(eventName?: EventType | undefined)
: Returns the listeners for an event.const listeners = provider.listeners('block'); console.log(`Number of listeners: ${listeners.length}`);
removeAllListeners(eventName?: EventType | undefined)
: Removes all listeners for an event.provider.removeAllListeners('block');
waitForTransaction(transactionHash: string, confirmations?: number | undefined, timeout?: number | undefined)
: Waits for a transaction to be mined.const receipt = await provider.waitForTransaction('0xYourTransactionHash'); console.log(`Transaction status: ${receipt.status}`);
on(eventName: EventType, listener: Listener)
: Adds a listener for an event.provider.on('block', (blockNumber) => { console.log(`New block: ${blockNumber}`); });
once(eventName: EventType, listener: Listener)
: Adds a one-time listener for an event.provider.once('block', (blockNumber) => { console.log(`New block: ${blockNumber}`); });
off(eventName: EventType, listener?: Listener | undefined)
: Removes a listener for an event.const listener = (blockNumber) => { console.log(`New block: ${blockNumber}`); }; provider.on('block', listener); provider.off('block', listener);
Example
const provider = new MoonProvider({ SDK, address, chainId });
window.ethereum = moon;
Last updated