src/app/_eth/accountIndex.ts
Properties |
Methods |
|
constructor(contractAddress: string, signerAddress?: string)
|
Defined in src/app/_eth/accountIndex.ts:10
|
contract |
Type : any
|
Defined in src/app/_eth/accountIndex.ts:10
|
contractAddress |
Type : string
|
Defined in src/app/_eth/accountIndex.ts:8
|
signerAddress |
Type : string
|
Defined in src/app/_eth/accountIndex.ts:9
|
Public Async addToAccountRegistry | ||||||
addToAccountRegistry(address: string)
|
||||||
Defined in src/app/_eth/accountIndex.ts:44
|
||||||
Parameters :
Returns :
Promise<boolean>
|
Public Async haveAccount | ||||||
haveAccount(address: string)
|
||||||
Defined in src/app/_eth/accountIndex.ts:26
|
||||||
Parameters :
Returns :
Promise<boolean>
|
Public Async last | ||||||
last(numberOfAccounts: number)
|
||||||
Defined in src/app/_eth/accountIndex.ts:30
|
||||||
Parameters :
Returns :
Promise<Array<string>>
|
Public Async totalAccounts |
totalAccounts()
|
Defined in src/app/_eth/accountIndex.ts:22
|
Returns :
Promise<number>
|
import {environment} from '@src/environments/environment';
import Web3 from 'web3';
const abi: Array<any> = require('@src/assets/js/block-sync/data/AccountRegistry.json');
const web3: Web3 = new Web3(environment.web3Provider);
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.count().call();
}
public async haveAccount(address: string): Promise<boolean> {
return await this.contract.methods.accountIndex(address).call() !== 0;
}
public async last(numberOfAccounts: number): Promise<Array<string>> {
const count: number = await this.totalAccounts();
let lowest: number = count - numberOfAccounts - 1;
if (lowest < 0) {
lowest = 0;
}
const accounts: Array<string> = [];
for (let i = count - 1; i > lowest; i--) {
const account: string = await this.contract.methods.accounts(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;
}
}