2021-02-06 15:08:37 +01:00
|
|
|
// @ts-ignore
|
2021-03-06 07:28:29 +01:00
|
|
|
import * as accountIndex from '@src/assets/js/block-sync/data/AccountRegistry.json';
|
2021-02-17 13:49:04 +01:00
|
|
|
import {environment} from '@src/environments/environment';
|
2021-01-15 05:30:19 +01:00
|
|
|
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;
|
2021-02-24 16:09:50 +01:00
|
|
|
this.contract = new web3.eth.Contract(abi, this.contractAddress);
|
2021-01-15 05:30:19 +01:00
|
|
|
if (signerAddress) {
|
|
|
|
this.signerAddress = signerAddress;
|
2021-02-24 16:09:50 +01:00
|
|
|
} else {
|
|
|
|
this.signerAddress = web3.eth.accounts[0];
|
2021-01-15 05:30:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 16:09:50 +01:00
|
|
|
public async totalAccounts(): Promise<number> {
|
2021-03-10 10:47:01 +01:00
|
|
|
return await this.contract.methods.count().call();
|
2021-01-15 05:30:19 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 16:09:50 +01:00
|
|
|
public async haveAccount(address: string): Promise<boolean> {
|
|
|
|
return await this.contract.methods.accountIndex(address).call() !== 0;
|
2021-01-15 05:30:19 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 16:09:50 +01:00
|
|
|
public async last(numberOfAccounts: number): Promise<Array<string>> {
|
|
|
|
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;
|
2021-01-15 05:30:19 +01:00
|
|
|
}
|
|
|
|
|
2021-02-24 16:09:50 +01:00
|
|
|
public async addToAccountRegistry(address: string): Promise<boolean> {
|
|
|
|
if (!await this.haveAccount(address)) {
|
|
|
|
return await this.contract.methods.add(address).send({from: this.signerAddress});
|
2021-01-15 05:30:19 +01:00
|
|
|
} else {
|
2021-02-24 16:09:50 +01:00
|
|
|
return await this.haveAccount(address);
|
2021-01-15 05:30:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|