Overview
Introduction
API Key
Connect wallet
npm install web3
npm install --save web3modalimport { Injectable } from '@angular/core';
import Web3 from "web3";
import Web3Modal from "web3modal";
import WalletConnectProvider from "@walletconnect/web3-provider";
import { Subject } from 'rxjs';
@Injectable({
providedIn: 'root'
})
export class ContractService {
private web3js: any;
private provider: any;
private accounts: any;
web3Modal
private accountStatusSource = new Subject<any>();
accountStatus$ = this.accountStatusSource.asObservable();
constructor() {
const providerOptions = {
walletconnect: {
package: WalletConnectProvider, // required
options: {
infuraId: "INFURA_ID" // required
}
}
};
this.web3Modal = new Web3Modal({
network: "mainnet", // optional
cacheProvider: true, // optional
providerOptions, // required
});
}
async connectAccount() {
this.web3Modal.clearCachedProvider();
this.provider = await this.web3Modal.connect(); // set provider
this.web3js = new Web3(this.provider); // create web3 instance
this.accounts = await this.web3js.eth.getAccounts();
this.accountStatusSource.next(this.accounts)
}
}
Last updated