// @ts-ignore import * as accountIndex from '@src/assets/js/block-sync/data/AccountRegistry.json'; import {environment} from '@src/environments/environment'; const Web3 = require('web3'); const web3 = new Web3(environment.web3Provider); const abi = accountIndex.default; 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 { return this.contract.methods.count().call(); } public async haveAccount(address: string): Promise { return await this.contract.methods.accountIndex(address).call() !== 0; } public async last(numberOfAccounts: number): Promise> { const count = await this.totalAccounts(); let lowest = count - numberOfAccounts - 1; if (lowest < 0) { lowest = 0; } let accounts = []; for (let i = count - 1; i > lowest; i--) { const account = await this.contract.methods.accounts(i).call(); accounts.push(account); } return accounts; } public async addToAccountRegistry(address: string): Promise { if (!await this.haveAccount(address)) { return await this.contract.methods.add(address).send({from: this.signerAddress}); } else { return await this.haveAccount(address); } } }