@dsks/angular-web3-module
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Angular Web3 Access Module

This library was generated with Angular CLI version 14.2.0.

How to use

Import module on app.module.ts

import { Web3ModuleModule } from '@dsks/angular-web3-module';

...

imports: [
  ...,
  Web3ModuleModule
],

Use it on html template

<lib-web3-module></lib-web3-module>

Import service on component

import { Web3ModuleService } from '@dsks/angular-web3-module';

Add it on the constructor

constructor(private web3ModuleService: Web3ModuleService)

Suscribe to service events

constructor(private web3ModuleService: Web3ModuleService) {
    // Suscribe to connect event
    this.web3ModuleService.getWeb3UserSubject().subscribe((user: any) => {
        console.log("User", user);
        if(user != null){
        // this.web3ModuleService.addSmartContract('test', '0x8a75B7F4237e56D126B64efCf1dAa88094033721', [{"inputs": [{"internalType": "uint256","name": "num","type": "uint256"}],"name": "store","outputs": [],"stateMutability": "nonpayable","type": "function"},{"inputs": [],"name": "retrieve","outputs": [{"internalType": "uint256","name": "","type": "uint256"}],"stateMutability": "view","type": "function"}]);
        // let testSmartcontract = this.web3ModuleService.getSmartContract('test');
        // if(testSmartcontract != null && testSmartcontract.ethContract != null){
        //   testSmartcontract.ethContract.methods.retrieve().call().then((result: any) => {
        //     console.log("Result", result);
        //   });
        // }        
        }
    });

    // Suscribe to wallet events (ChainChanged & AccountsChanged)
    this.web3ModuleService.getWeb3EventSubject().subscribe((event: any) => {
        console.log("Event", event);
    });

    // Suscribe to errors (Wrong network, no wallet, etc.)
    this.web3ModuleService.getWeb3ErrorSubject().subscribe((error: any) => {
        console.log("Error", error);
    });
}

After View init set Network id, init module and forche change network

ngAfterViewInit() {
    // Init web3 module
    this.web3ModuleService.web3InitSubject.next([ new Network('137', 'Polygon Mainnet', 'MATIC', 'MATIC', 18, ['https://polygon-rpc.com/'], ["https://polygonscan.com/"]), 
                                                  new Network('80001', 'Polygon testnet (Mumbai)', 'MATIC', 'MATIC', 18, ['https://matic-mumbai.chainstacklabs.com/'], ['https://mumbai.polygonscan.com/'])]);
    this.web3ModuleService.forceChangeNetwork('80001');
}

Package Sidebar

Install

npm i @dsks/angular-web3-module

Weekly Downloads

1

Version

1.0.0

License

CC BY-SA

Unpacked Size

131 kB

Total Files

24

Last publish

Collaborators

  • dsks