cic-staff-client/src/app/_helpers/accountIndex.ts

49 lines
1.4 KiB
TypeScript

import { AccountRegistry } from '@src/assets/js/eth_account_index';
// @ts-ignore
import * as accountIndex from '@src/assets/json/accountIndex.abi.json';
import {environment} from '@src/environments/environment';
const Web3 = require('web3');
const web3 = new Web3(environment.web3Provider);
// @ts-ignore
const abi = accountIndex.default;
export class AccountIndex {
contractAddress: string;
signerAddress: string;
contract: any;
constructor(contractAddress: string, signerAddress?: string) {
this.contractAddress = contractAddress;
if (signerAddress) {
this.signerAddress = signerAddress;
}
this.contract = new AccountRegistry(web3, abi, this.contractAddress, this.signerAddress);
}
async totalAccounts(): Promise<number> {
return await this.contract.count();
}
async haveAccount(address: string): Promise<boolean> {
return await this.contract.have(address);
}
async addAccount(address: string): Promise<boolean> {
return await this.contract.add(address);
}
async last(numberOfAccounts: number): Promise<Array<string>> {
return await this.contract.last(numberOfAccounts);
}
async addToAccountRegistry(address: string): Promise<boolean> {
if (!await this.contract.have(address)) {
await this.contract.add(address);
return await this.contract.have(address);
} else {
return await this.contract.have(address);
}
}
}