usage-guide
Note
Currently developing documentation, contributions are always welcome
Setup metamask service
// import Metamask service on component in which you need
import { MetaMaskService } from "ng-blockchainx";
// add metamask service as DI ( dependency injection )
constructor(private metaMaskService: MetaMaskService) {}
To connect metamask wallet
this.metaMaskService.connectMetaMask()
.then((response:any) => {
// DO your success logic here
})
.catch((error:any) => {
// DO your failure logic here
});
Once metamask successfully connected need to pass what are the networks your application going to support by passing their corresponding chain id's.
this.metaMaskService.setSupportedChains(['0x61', '0x38']);
In above example ['0x61', '0x38']
are the chain id's of BSC testnet and mainnet
To change active network on metamask wallet
// 'chainId = chain id of the network which to be active
this.metaMaskService.changeNetwork('chainID')
.then((response:any) => {
// DO your success logic here
})
.catch((error:any) => {
// DO your failure logic here
});
Check connection
To check if metamask connected or not on page load call below method
const accounts: any = await this.metaMaskService.getAddress().catch((error: any) => {
console.log(error);
});
if (accounts.length > 0) {
// metamask connected successfully
} else {
// metamask not connected
}
export interface Response {
status: boolean;
code: number;
message: string;
data: Data | undefined | any;
}
ngOnInit () {
this.metaMaskSubscription = this.metaMaskService.connectionListener
.subscribe((response:Response) => {
console.log(response);
// Do your logic here
});
}
ngOnDestroy() {
this.metaMaskSubscription.unsubscribe()
}
Response code | Message | Description |
---|---|---|
250511 | Metamask not installed on browser | |
250512 | Metamask not connected | |
250500 | Un expected error | |
250601 | Metamask connected successfully | |
250609 | Service init success | Just ignore this event |
250610 | Chain changed | |
250611 | account changed | |
250612 | metamask connection closed | |
250641 | Current network is not supported |