Add web3 singleton service.

This commit is contained in:
Spencer Ofwiti 2021-05-20 21:27:06 +03:00
parent a4c0e26be9
commit c60d28a053
8 changed files with 46 additions and 9 deletions

View File

@ -1,8 +1,8 @@
import { environment } from '@src/environments/environment';
import Web3 from 'web3';
import {Web3Service} from '@app/_services/web3.service';
const abi: Array<any> = require('@src/assets/js/block-sync/data/AccountsIndex.json');
const web3: Web3 = new Web3(environment.web3Provider);
const web3: Web3 = Web3Service.getInstance();
export class AccountIndex {
contractAddress: string;

View File

@ -1,8 +1,8 @@
import Web3 from 'web3';
import { environment } from '@src/environments/environment';
import {Web3Service} from '@app/_services/web3.service';
const abi: Array<any> = require('@src/assets/js/block-sync/data/TokenUniqueSymbolIndex.json');
const web3: Web3 = new Web3(environment.web3Provider);
const web3: Web3 = Web3Service.getInstance();
export class TokenRegistry {
contractAddress: string;

View File

@ -6,6 +6,7 @@ import { TransactionService } from '@app/_services/transaction.service';
import { environment } from '@src/environments/environment';
import { LoggingService } from '@app/_services/logging.service';
import { RegistryService } from '@app/_services/registry.service';
import {Web3Service} from '@app/_services/web3.service';
@Injectable({
providedIn: 'root',
@ -28,7 +29,7 @@ export class BlockSyncService {
const settings: Settings = new Settings(this.scan);
const readyStateElements: { network: number } = { network: 2 };
settings.w3.provider = environment.web3Provider;
settings.w3.engine = RegistryService.web3;
settings.w3.engine = Web3Service.getInstance();
settings.registry = await RegistryService.getRegistry();
settings.txHelper = new TransactionHelper(settings.w3.engine, settings.registry);

View File

@ -6,3 +6,4 @@ export * from '@app/_services/block-sync.service';
export * from '@app/_services/location.service';
export * from '@app/_services/logging.service';
export * from '@app/_services/error-dialog.service';
export * from '@app/_services/web3.service';

View File

@ -1,14 +1,13 @@
import { Injectable } from '@angular/core';
import Web3 from 'web3';
import { environment } from '@src/environments/environment';
import { CICRegistry, FileGetter } from 'cic-client';
import { HttpGetter } from '@app/_helpers';
import {Web3Service} from '@app/_services/web3.service';
@Injectable({
providedIn: 'root',
})
export class RegistryService {
static web3: Web3 = new Web3(environment.web3Provider);
static fileGetter: FileGetter = new HttpGetter();
private static registry: CICRegistry;
@ -17,7 +16,7 @@ export class RegistryService {
public static async getRegistry(): Promise<CICRegistry> {
if (!RegistryService.registry) {
RegistryService.registry = new CICRegistry(
RegistryService.web3,
Web3Service.getInstance(),
environment.registryAddress,
'Registry',
RegistryService.fileGetter,

View File

@ -17,6 +17,7 @@ import { HttpClient } from '@angular/common/http';
import { CICRegistry } from 'cic-client';
import { RegistryService } from '@app/_services/registry.service';
import Web3 from 'web3';
import {Web3Service} from '@app/_services/web3.service';
const vCard = require('vcard-parser');
@Injectable({
@ -36,7 +37,7 @@ export class TransactionService {
private userService: UserService,
private loggingService: LoggingService
) {
this.web3 = RegistryService.web3;
this.web3 = Web3Service.getInstance();
}
async init(): Promise<void> {

View File

@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';
import { Web3Service } from './web3.service';
describe('Web3Service', () => {
let service: Web3Service;
beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(Web3Service);
});
it('should be created', () => {
expect(service).toBeTruthy();
});
});

View File

@ -0,0 +1,19 @@
import { Injectable } from '@angular/core';
import Web3 from 'web3';
import {environment} from '@src/environments/environment';
@Injectable({
providedIn: 'root'
})
export class Web3Service {
private static web3: Web3;
constructor() { }
public static getInstance(): Web3 {
if (!Web3Service.web3) {
Web3Service.web3 = new Web3(environment.web3Provider);
}
return Web3Service.web3;
}
}