1.0.6 • Published 3 years ago
@energi/energi-wallet v1.0.6
Energi Wallets
Requirements
- nodejs v16.10.0
- yarn v1.22.11
Build
yarn
yarn buildPublish
yarn login
yarn publishHow to use
Install
Add the library to your project by the following commands:
npm install @energi/energi-wallet
# or
yarn add @energi/energi-walletUsage
import { useMetamask } from '@energi/energi-wallet';
const MyComponent = () => {
const { connected, connect, disconnect, switchToNetwork } = useMetamask();
const RINKEBY_CHAIN_ID = 4;
return (
<button onClick={() => connected ? disconnect() : connect()}>
{connected ? 'Disconnect' : 'Connect'}
</button>
<button onClick={() => switchToNetwork(RINKEBY_CHAIN_ID)}>
Switch to Rinkeby network
</button>
)
}Send Transaction Example
// import section ...
import { Metamask } from '@energi/energi-wallet';
// Component body ...
const { sendTx } = Metamask;
const handleSendTransaction = async (data) => {
try {
const tx = await sendTx(data);
return tx;
} catch(error) {
throw new Error(`Transaction failed: ${error}`)
}
return null;
}
// On an event ...
handleSendTransaction({
from: 'wallet_address',
to: 'to_wallet_or_contract',
gasLimit: 'gas_limit_to_hex',
data: 'encoded_tx_data'
})Currently, only metamask is supported.
Methods and Properties
Hooks
useMetamask
Properties
installed: if the wallet is installedconnected: if the wallet is connectedchainId: the network/chain IDaddress: the wallet addressaccounts: all accounts/addresses related to the wallet
Methods
connect: connects to the walletdisconnect: disconnects the app from the walletswitchToNetwork: switches the network or chainsendTx: for sending transaction from metamaskaddNewToken: adding a new token to the metamasksign: Sign messages with the private key of the address
Classes
Metamask
Methods
isInstalled: returns if the wallet is installedisConnected: returns if the wallet is connectedgetChainId: returns the network/chain IDgetAddress: returns the wallet addressgetAccounts: returns all accounts/addresses related to the walletconnect: connects the the walletdisconnect: disconnects the app from the walletswitchToNetwork: switches the network or chainsendTx: for sending transaction from metamaskaddNewToken: adding a new token to the metamasksign: Sign messages with the private key of the addressgetMetaMask: Returns the web3 provider
Disclaimer
Filing an issue does not guarantee addition to this package.