Skip to main content

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 codeMessageDescription
250511Metamask not installed on browser
250512Metamask not connected
250500Un expected error
250601Metamask connected successfully
250609Service init successJust ignore this event
250610Chain changed
250611account changed
250612metamask connection closed
250641Current network is not supported