Connect to Web3 Wallet
Use this method to allow users to login via Metamask or WalletConnect.
import { MetamaskConnector } from "@altura/altura-js/lib/connector";
const wallet = new MetamaskConnector(window.ethereum);
wallet.connect(); // connect to the wallet
First install WalletConnect web3-provider :
npm i @walletconnect/web3-provider
Now import WalletConnect web3-provider and WalletConnectConnector from Altura:
import { WalletConnectConnector } from "@altura/altura-js/lib/connector";
import WalletConnectProvider from "@walletconnect/web3-provider";
Now, instantiate your WalletConnect web3-provider using the following options: Infura or Custom RPC mapping
Infura
Custom RPC
import WalletConnectProvider from "@walletconnect/web3-provider";
// Create WalletConnect Provider
const provider = new WalletConnectProvider({
infuraId: "27e484dcd9e3efcfd25a83a78777cdf1",
});
// Enable session (triggers QR Code modal)
await provider.enable();
import WalletConnectProvider from "@walletconnect/web3-provider";
// Create WalletConnect Provider
const provider = new WalletConnectProvider({
rpc: {
1: "https://mainnet.mycustomnode.com",
3: "https://ropsten.mycustomnode.com",
100: "https://dai.poa.network",
// ...
},
});
// Enable session (triggers QR Code modal)
await provider.enable();
Now integrate it with WalletConnectConnector:
const wallet = new WalletConnectConnector(provider);
const walletAddress = await wallet.address();
console.log(walletAddress);
const walletAddress = await wallet.address();
console.log(walletAddress);
try {
await wallet.sign("Hi Altura!");
} catch (error) {
console.log("Denied the sign", error);
}
try {
const tx = {
to: "0x4976fb03C32e5B8cfe2b6cCB31c09Ba78EBaBa41",
// `function addr(namehash("ricmoo.eth")) view returns (address)`
data: "0x3b3b57debf074faa138b72c65adbdcfb329847e4f2c04bde7f7dd7fcad5a52d2f395a558",
};
await wallet.sendTransaction(tx);
} catch (error) {
console.log("Error on sending transaction", error);
}
const getNetworkData = await wallet.getNetwork();
console.log(getNetworkData);
const getGasPrice = await wallet.getGasPrice();
console.log(getGasPrice);
const getFeeData = await wallet.getFeeData();
console.log(getFeeData);
const getBlockNumber = await wallet.getBlockNumber();
console.log(getBlockNumber);
Last modified 7mo ago