From 1223ccc48e6cd4649e638ff4e87d948fe97d39e3 Mon Sep 17 00:00:00 2001 From: Spencer Ofwiti Date: Tue, 20 Apr 2021 11:28:40 +0300 Subject: [PATCH] Retire internal registry class. --- src/app/_eth/index.ts | 1 - src/app/_eth/registry.spec.ts | 8 ------ src/app/_eth/registry.ts | 32 ---------------------- src/app/_services/block-sync.service.ts | 18 +++++------- src/app/_services/registry.service.spec.ts | 16 +++++++++++ src/app/_services/registry.service.ts | 28 +++++++++++++++++++ src/app/_services/token.service.ts | 28 ++++++++++--------- src/app/_services/transaction.service.ts | 19 ++++++++----- src/app/_services/user.service.ts | 13 ++++++--- 9 files changed, 87 insertions(+), 76 deletions(-) delete mode 100644 src/app/_eth/registry.spec.ts delete mode 100644 src/app/_eth/registry.ts create mode 100644 src/app/_services/registry.service.spec.ts create mode 100644 src/app/_services/registry.service.ts diff --git a/src/app/_eth/index.ts b/src/app/_eth/index.ts index aed7851..23abfa9 100644 --- a/src/app/_eth/index.ts +++ b/src/app/_eth/index.ts @@ -1,3 +1,2 @@ export * from '@app/_eth/accountIndex'; -export * from '@app/_eth/registry'; export * from '@app/_eth/token-registry'; diff --git a/src/app/_eth/registry.spec.ts b/src/app/_eth/registry.spec.ts deleted file mode 100644 index ffc4a39..0000000 --- a/src/app/_eth/registry.spec.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { Registry } from '@app/_eth/registry'; -import {environment} from '@src/environments/environment'; - -describe('Registry', () => { - it('should create an instance', () => { - expect(new Registry(environment.registryAddress)).toBeTruthy(); - }); -}); diff --git a/src/app/_eth/registry.ts b/src/app/_eth/registry.ts deleted file mode 100644 index 451506e..0000000 --- a/src/app/_eth/registry.ts +++ /dev/null @@ -1,32 +0,0 @@ -// @ts-ignore -import * as registry from '@src/assets/js/block-sync/data/Registry.json'; -import {environment} from '@src/environments/environment'; -const Web3 = require('web3'); - -const web3 = new Web3(environment.web3Provider); -const abi = registry.default; - -export class Registry { - contractAddress: string; - signerAddress: string; - contract: any; - - constructor(contractAddress: string, signerAddress?: string) { - this.contractAddress = contractAddress; - this.contract = new web3.eth.Contract(abi, contractAddress); - if (signerAddress) { - this.signerAddress = signerAddress; - } else { - this.signerAddress = web3.eth.accounts[0]; - } - } - - public async owner(): Promise { - return await this.contract.methods.owner().call(); - } - - public async addressOf(identifier: string): Promise { - const id = '0x' + web3.utils.padRight(new Buffer(identifier).toString('hex'), 64); - return await this.contract.methods.addressOf(id).call(); - } -} diff --git a/src/app/_services/block-sync.service.ts b/src/app/_services/block-sync.service.ts index d96d28f..d1b87c8 100644 --- a/src/app/_services/block-sync.service.ts +++ b/src/app/_services/block-sync.service.ts @@ -1,12 +1,11 @@ import {Injectable} from '@angular/core'; import {Settings} from '@app/_models'; -import Web3 from 'web3'; -import {CICRegistry, TransactionHelper} from 'cic-client'; +import {TransactionHelper} from 'cic-client'; import {first} from 'rxjs/operators'; import {TransactionService} from '@app/_services/transaction.service'; import {environment} from '@src/environments/environment'; -import {HttpGetter} from '@app/_helpers'; import {LoggingService} from '@app/_services/logging.service'; +import {RegistryService} from '@app/_services/registry.service'; @Injectable({ providedIn: 'root' @@ -14,23 +13,20 @@ import {LoggingService} from '@app/_services/logging.service'; export class BlockSyncService { readyStateTarget: number = 2; readyState: number = 0; - fileGetter = new HttpGetter(); constructor( private transactionService: TransactionService, - private loggingService: LoggingService + private loggingService: LoggingService, + private registryService: RegistryService, ) { } blockSync(address: string = null, offset: number = 0, limit: number = 100): any { this.transactionService.resetTransactionsList(); const settings = new Settings(this.scan); - const provider = environment.web3Provider; const readyStateElements = { network: 2 }; - settings.w3.provider = provider; - settings.w3.engine = new Web3(provider); - settings.registry = new CICRegistry(settings.w3.engine, environment.registryAddress, this.fileGetter, - ['../../assets/js/block-sync/data']); - settings.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); + settings.w3.provider = environment.web3Provider; + settings.w3.engine = this.registryService.getWeb3(); + settings.registry = this.registryService.getRegistry(); settings.txHelper = new TransactionHelper(settings.w3.engine, settings.registry); settings.txHelper.ontransfer = async (transaction: any): Promise => { diff --git a/src/app/_services/registry.service.spec.ts b/src/app/_services/registry.service.spec.ts new file mode 100644 index 0000000..fa9d231 --- /dev/null +++ b/src/app/_services/registry.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { RegistryService } from './registry.service'; + +describe('RegistryService', () => { + let service: RegistryService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(RegistryService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/_services/registry.service.ts b/src/app/_services/registry.service.ts new file mode 100644 index 0000000..17ebd81 --- /dev/null +++ b/src/app/_services/registry.service.ts @@ -0,0 +1,28 @@ +import { Injectable } from '@angular/core'; +import Web3 from 'web3'; +import {environment} from '@src/environments/environment'; +import {CICRegistry} from 'cic-client'; +import {HttpGetter} from '@app/_helpers'; + +@Injectable({ + providedIn: 'root' +}) +export class RegistryService { + web3 = new Web3(environment.web3Provider); + fileGetter = new HttpGetter(); + registry = new CICRegistry(this.web3, environment.registryAddress, 'CICRegistry', this.fileGetter, + ['../../assets/js/block-sync/data']); + + constructor() { + this.registry.declaratorHelper.addTrust(environment.trustedDeclaratorAddress); + this.registry.load(); + } + + getRegistry(): any { + return this.registry; + } + + getWeb3(): any { + return this.web3; + } +} diff --git a/src/app/_services/token.service.ts b/src/app/_services/token.service.ts index 2924f53..f3ec3ef 100644 --- a/src/app/_services/token.service.ts +++ b/src/app/_services/token.service.ts @@ -1,32 +1,35 @@ import { Injectable } from '@angular/core'; import {environment} from '@src/environments/environment'; import {BehaviorSubject, Observable} from 'rxjs'; -import {HttpGetter} from '@app/_helpers'; import {CICRegistry} from 'cic-client'; -import Web3 from 'web3'; -import {Registry, TokenRegistry} from '@app/_eth'; +import {TokenRegistry} from '@app/_eth'; import {HttpClient} from '@angular/common/http'; +import {RegistryService} from '@app/_services/registry.service'; @Injectable({ providedIn: 'root' }) export class TokenService { - web3 = new Web3(environment.web3Provider); - fileGetter = new HttpGetter(); - registry = new Registry(environment.registryAddress); - cicRegistry = new CICRegistry(this.web3, environment.registryAddress, this.fileGetter, ['../../assets/js/block-sync/data']); + registry: CICRegistry; + tokenRegistry: TokenRegistry; tokens: any = ''; private tokensList = new BehaviorSubject(this.tokens); tokensSubject = this.tokensList.asObservable(); constructor( private httpClient: HttpClient, - ) { } + private registryService: RegistryService, + ) { + this.registry = registryService.getRegistry(); + this.registry.load(); + this.registry.onload = async (address: string): Promise => { + this.tokenRegistry = new TokenRegistry(await this.registry.getContractAddressByName('TokenRegistry')); + }; + } async getTokens(): Promise { - const tokenRegistryQuery = new TokenRegistry(await this.registry.addressOf('TokenRegistry')); - const count = await tokenRegistryQuery.totalTokens(); - return Array.from({length: count}, async (v, i) => await tokenRegistryQuery.entry(i)); + const count = await this.tokenRegistry.totalTokens(); + return Array.from({length: count}, async (v, i) => await this.tokenRegistry.entry(i)); } getTokenBySymbol(symbol: string): Observable { @@ -34,8 +37,7 @@ export class TokenService { } async getTokenBalance(address: string): Promise { - const tokenRegistryQuery = new TokenRegistry(await this.registry.addressOf('TokenRegistry')); - const sarafuToken = await this.cicRegistry.addToken(await tokenRegistryQuery.entry(0)); + const sarafuToken = await this.registry.addToken(await this.tokenRegistry.entry(0)); return await sarafuToken.methods.balanceOf(address).call(); } } diff --git a/src/app/_services/transaction.service.ts b/src/app/_services/transaction.service.ts index c1f9ba9..414724f 100644 --- a/src/app/_services/transaction.service.ts +++ b/src/app/_services/transaction.service.ts @@ -13,9 +13,9 @@ import * as secp256k1 from 'secp256k1'; import {AuthService} from '@app/_services/auth.service'; import {defaultAccount} from '@app/_models'; import {LoggingService} from '@app/_services/logging.service'; -import {Registry} from '@app/_eth'; import {HttpClient} from '@angular/common/http'; -const Web3 = require('web3'); +import {CICRegistry} from 'cic-client'; +import {RegistryService} from '@app/_services/registry.service'; const vCard = require('vcard-parser'); @Injectable({ @@ -26,15 +26,20 @@ export class TransactionService { private transactionList = new BehaviorSubject(this.transactions); transactionsSubject = this.transactionList.asObservable(); userInfo: any; - web3 = new Web3(environment.web3Provider); - registry = new Registry(environment.registryAddress); + web3: any; + registry: CICRegistry; constructor( private httpClient: HttpClient, private authService: AuthService, private userService: UserService, - private loggingService: LoggingService - ) { } + private loggingService: LoggingService, + private registryService: RegistryService, + ) { + this.web3 = this.registryService.getWeb3(); + this.registry = registryService.getRegistry(); + this.registry.load(); + } getAllTransactions(offset: number, limit: number): Observable { return this.httpClient.get(`${environment.cicCacheUrl}/tx/${offset}/${limit}`); @@ -100,7 +105,7 @@ export class TransactionService { } async transferRequest(tokenAddress: string, senderAddress: string, recipientAddress: string, value: number): Promise { - const transferAuthAddress = await this.registry.addressOf('TransferAuthorization'); + const transferAuthAddress = await this.registry.getContractAddressByName('TransferAuthorization'); const hashFunction = new Keccak(256); hashFunction.update('createRequest(address,address,address,uint256)'); const hash = hashFunction.digest(); diff --git a/src/app/_services/user.service.ts b/src/app/_services/user.service.ts index 6b85554..ce42e81 100644 --- a/src/app/_services/user.service.ts +++ b/src/app/_services/user.service.ts @@ -7,8 +7,10 @@ import {ArgPair, Envelope, Phone, Syncable, User} from 'cic-client-meta'; import {AccountDetails, MetaResponse} from '@app/_models'; import {LoggingService} from '@app/_services/logging.service'; import {TokenService} from '@app/_services/token.service'; -import {AccountIndex, Registry} from '@app/_eth'; +import {AccountIndex} from '@app/_eth'; import {MutableKeyStore, MutablePgpKeyStore, PGPSigner, Signer} from '@app/_pgp'; +import {RegistryService} from '@app/_services/registry.service'; +import {CICRegistry} from 'cic-client'; const vCard = require('vcard-parser'); @Injectable({ @@ -18,7 +20,7 @@ export class UserService { headers: HttpHeaders = new HttpHeaders({'x-cic-automerge': 'client'}); keystore: MutableKeyStore = new MutablePgpKeyStore(); signer: Signer = new PGPSigner(this.keystore); - registry = new Registry(environment.registryAddress); + registry: CICRegistry; accountsMeta = []; accounts: any = []; @@ -36,8 +38,11 @@ export class UserService { constructor( private httpClient: HttpClient, private loggingService: LoggingService, - private tokenService: TokenService + private tokenService: TokenService, + private registryService: RegistryService, ) { + this.registry = registryService.getRegistry(); + this.registry.load(); } resetPin(phone: string): Observable { @@ -152,7 +157,7 @@ export class UserService { async loadAccounts(limit: number = 100, offset: number = 0): Promise { this.resetAccountsList(); - const accountIndexAddress = await this.registry.addressOf('AccountRegistry'); + const accountIndexAddress = await this.registry.getContractAddressByName('AccountRegistry'); const accountIndexQuery = new AccountIndex(accountIndexAddress); const accountAddresses = await accountIndexQuery.last(await accountIndexQuery.totalAccounts()); this.loggingService.sendInfoLevelMessage(accountAddresses);