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

51 lines
1.5 KiB
TypeScript

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 = Web3Service.getInstance();
export class AccountIndex {
contractAddress: string;
signerAddress: string;
contract: any;
constructor(contractAddress: string, signerAddress?: string) {
this.contractAddress = contractAddress;
this.contract = new web3.eth.Contract(abi, this.contractAddress);
if (signerAddress) {
this.signerAddress = signerAddress;
} else {
this.signerAddress = web3.eth.accounts[0];
}
}
public async totalAccounts(): Promise<number> {
return await this.contract.methods.entryCount().call();
}
public async haveAccount(address: string): Promise<boolean> {
return (await this.contract.methods.have(address).call()) !== 0;
}
public async last(numberOfAccounts: number): Promise<Array<string>> {
const count: number = await this.totalAccounts();
let lowest: number = count - numberOfAccounts;
if (lowest < 0) {
lowest = 0;
}
const accounts: Array<string> = [];
for (let i = count - 1; i >= lowest; i--) {
const account: string = await this.contract.methods.entry(i).call();
accounts.push(account);
}
return accounts;
}
public async addToAccountRegistry(address: string): Promise<boolean> {
if (!(await this.haveAccount(address))) {
return await this.contract.methods.add(address).send({ from: this.signerAddress });
}
return true;
}
}