Skip to content

Commit

Permalink
Merge pull request #27 from remon-nashid/fix/wallet-accounts
Browse files Browse the repository at this point in the history
Fix/wallet accounts
  • Loading branch information
monitz87 authored May 14, 2019
2 parents ec41b0f + 65f4811 commit bf95bad
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 216 deletions.
114 changes: 38 additions & 76 deletions src/LowLevel/DividendCheckpoint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import BigNumber from 'bignumber.js';
import { TransactionObject } from 'web3/eth/types';
import { zipWith, range, flatten } from 'lodash';
import { Module } from './Module';
import { Context } from './LowLevel';
import { TransactionObject } from 'web3/eth/types';
import { SecurityToken } from './SecurityToken';
import {
GenericContract,
Expand All @@ -15,16 +16,9 @@ import {
GetDividendsByCheckpointArgs,
GetDividendArgs,
SetDividendsWalletArgs,
} from './types';
import {
fromUnixTimestamp,
fromWei,
toWei,
fromDivisible,
toAscii,
} from './utils';
import { TaxWithholding, DividendModuleTypes } from './types';
import { zipWith, range, flatten } from 'lodash';
TaxWithholding, DividendModuleTypes } from './types';
import { fromUnixTimestamp, fromWei, toWei, fromDivisible, toAscii, getOptions } from './utils';


interface InternalDividend {
checkpointId: string;
Expand Down Expand Up @@ -87,14 +81,9 @@ interface DividendCheckpointContract<T extends GenericContract> {
methods: {
getCheckpointData(checkpointId: number): TransactionObject<CheckpointData>;
getDividendIndex(checkpointId: number): TransactionObject<string[]>;
getDividendProgress(
dividendIndex: number
): TransactionObject<DividendProgress>;
getDividendProgress(dividendIndex: number): TransactionObject<DividendProgress>;
dividends(index: number): TransactionObject<InternalDividend>;
setWithholding(
investors: string[],
percentages: BigNumber[]
): TransactionObject<void>;
setWithholding(investors: string[], percentages: BigNumber[]): TransactionObject<void>;
reclaimDividend(dividendIndex: number): TransactionObject<void>;
withdrawWithholding(dividendIndex: number): TransactionObject<void>;
pushDividendPaymentToAddresses(
Expand All @@ -111,25 +100,17 @@ export abstract class DividendCheckpoint<
T extends GenericContract = GenericContract
> extends Module<DividendCheckpointContract<T>> {
public abstract dividendType: DividendModuleTypes;
constructor({
address,
abi,
context,
}: {
address: string;
abi: any[];
context: Context;
}) {

constructor({ address, abi, context }: { address: string; abi: any[]; context: Context }) {
super({ address, abi, context });
}

public async getTaxWithholdingList({
checkpointIndex,
}: GetTaxWithholdingListArgs): Promise<TaxWithholding[]> {
const {
0: investors,
2: percentages,
} = await this.contract.methods.getCheckpointData(checkpointIndex).call();
const { 0: investors, 2: percentages } = await this.contract.methods
.getCheckpointData(checkpointIndex)
.call();

return zipWith(investors, percentages, (address, percentage) => ({
address,
Expand All @@ -138,9 +119,7 @@ export abstract class DividendCheckpoint<
}

public async getInvestors({ dividendIndex }: GetDividendInvestorsArgs) {
const { 0: investors } = await this.contract.methods
.getDividendProgress(dividendIndex)
.call();
const { 0: investors } = await this.contract.methods.getDividendProgress(dividendIndex).call();

return investors;
}
Expand All @@ -149,16 +128,10 @@ export abstract class DividendCheckpoint<
return fromDivisible(value, 18);
}

public abstract getDividend({
dividendIndex,
}: GetDividendArgs): Promise<Dividend>;
public abstract getDividend({ dividendIndex }: GetDividendArgs): Promise<Dividend>;

public async getDividendsByCheckpoint({
checkpointIndex,
}: GetDividendsByCheckpointArgs) {
const dividendIndexes = await this.contract.methods
.getDividendIndex(checkpointIndex)
.call();
public async getDividendsByCheckpoint({ checkpointIndex }: GetDividendsByCheckpointArgs) {
const dividendIndexes = await this.contract.methods.getDividendIndex(checkpointIndex).call();

const dividends = await Promise.all(
dividendIndexes.map(dividendIndex =>
Expand All @@ -179,9 +152,7 @@ export abstract class DividendCheckpoint<
const currentCheckpointIndex = await securityToken.currentCheckpointId();
const checkpointIndexes = range(1, currentCheckpointIndex + 1);
const dividends = await Promise.all(
checkpointIndexes.map(checkpointIndex =>
this.getDividendsByCheckpoint({ checkpointIndex })
)
checkpointIndexes.map(checkpointIndex => this.getDividendsByCheckpoint({ checkpointIndex }))
);

return flatten(dividends).sort((a, b) => a.index - b.index);
Expand All @@ -191,49 +162,42 @@ export abstract class DividendCheckpoint<
return this.contract.methods.wallet().call();
};

public setWithholding = async ({
investors,
percentages,
}: SetWithholdingArgs) => {
public setWithholding = async ({ investors, percentages }: SetWithholdingArgs) => {
const percentagesInWei = percentages.map(toWei);

return () =>
this.contract.methods
.setWithholding(investors, percentagesInWei)
.send({ from: this.context.account });
const method = this.contract.methods.setWithholding(investors, percentagesInWei);
const options = await getOptions(method, { from: this.context.account });
return () => method.send(options);
};

public reclaimDividend = async ({ dividendIndex }: ReclaimDividendArgs) => {
return () =>
this.contract.methods
.reclaimDividend(dividendIndex)
.send({ from: this.context.account });
const method = this.contract.methods.reclaimDividend(dividendIndex);
const options = await getOptions(method, { from: this.context.account });
return () => method.send(options);
};

public withdrawWithholding = async ({
dividendIndex,
}: WithdrawWithholdingArgs) => {
return () =>
this.contract.methods
.withdrawWithholding(dividendIndex)
.send({ from: this.context.account });
public withdrawWithholding = async ({ dividendIndex }: WithdrawWithholdingArgs) => {
const method = this.contract.methods.withdrawWithholding(dividendIndex);
const options = await getOptions(method, { from: this.context.account });
return () => method.send(options);
};

public pushDividendPayment = async ({
dividendIndex,
investorAddresses,
}: PushDividendPaymentArgs) => {
return () =>
this.contract.methods
.pushDividendPaymentToAddresses(dividendIndex, investorAddresses)
.send({ from: this.context.account });
const method = this.contract.methods.pushDividendPaymentToAddresses(
dividendIndex,
investorAddresses
);
const options = await getOptions(method, { from: this.context.account });
return () => method.send(options);
};

public setWallet = async ({ address }: SetDividendsWalletArgs) => {
return () =>
this.contract.methods
.changeWallet(address)
.send({ from: this.context.account });
const method = this.contract.methods.changeWallet(address);
const options = await getOptions(method, { from: this.context.account });
return () => method.send(options);
};

protected async _getDividend({
Expand All @@ -245,9 +209,7 @@ export abstract class DividendCheckpoint<
decimals: number;
}): Promise<Dividend> {
const { methods } = this.contract;
const dividend = await this.contract.methods
.dividends(dividendIndex)
.call();
const dividend = await this.contract.methods.dividends(dividendIndex).call();

const {
0: addresses,
Expand Down
45 changes: 14 additions & 31 deletions src/LowLevel/Erc20.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import { TransactionObject } from 'web3/eth/types';
import BigNumber from 'bignumber.js';
import { Contract } from './Contract';
import { Context } from './LowLevel';
import { ERC20Abi } from './abis/ERC20Abi';
import { NonStandardERC20Abi } from './abis/NonStandardERC20Abi';
import {
GenericContract,
ApproveArgs,
AllowanceArgs,
BalanceOfArgs,
} from './types';
import { fromDivisible, toDivisible, toAscii } from './utils';
import BigNumber from 'bignumber.js';
import { GenericContract, ApproveArgs, AllowanceArgs, BalanceOfArgs } from './types';
import { fromDivisible, toDivisible, toAscii, getOptions } from './utils';
import { web3 } from '../LowLevel/web3Client';

interface Erc20Contract extends GenericContract {
Expand All @@ -22,25 +17,17 @@ interface Erc20Contract extends GenericContract {
balanceOf(address: string): TransactionObject<string>;
allowance(tokenOwner: string, spender: string): TransactionObject<string>;
transfer(address: string, amount: BigNumber): TransactionObject<void>;
transferFrom(
from: string,
to: string,
amount: BigNumber
): TransactionObject<void>;
decreaseApproval(
address: string,
amount: BigNumber
): TransactionObject<void>;
increaseApproval(
address: string,
amount: BigNumber
): TransactionObject<void>;
transferFrom(from: string, to: string, amount: BigNumber): TransactionObject<void>;
decreaseApproval(address: string, amount: BigNumber): TransactionObject<void>;
increaseApproval(address: string, amount: BigNumber): TransactionObject<void>;
};
}

export class Erc20 extends Contract<Erc20Contract> {
private decimalPlaces: number | null = null;

private tokenSymbol: string | null = null;

private nonStandardContract: Erc20Contract;

constructor({ address, context }: { address: string; context: Context }) {
Expand Down Expand Up @@ -81,10 +68,10 @@ export class Erc20 extends Contract<Erc20Contract> {
public approve = async ({ spender, amount }: ApproveArgs) => {
const decimals = await this.decimals();
const amountInWei = toDivisible(amount, decimals);
return () =>
this.contract.methods
.approve(spender, amountInWei)
.send({ from: this.context.account });

const method = this.contract.methods.approve(spender, amountInWei);
const options = await getOptions(method, { from: this.context.account });
return () => method.send(options);
};

public balanceOf = async ({ address }: BalanceOfArgs) => {
Expand All @@ -95,9 +82,7 @@ export class Erc20 extends Contract<Erc20Contract> {
};

public allowance = async ({ tokenOwner, spender }: AllowanceArgs) => {
const allowance = await this.contract.methods
.allowance(tokenOwner, spender)
.call();
const allowance = await this.contract.methods.allowance(tokenOwner, spender).call();

const decimals = await this.decimals();

Expand Down Expand Up @@ -136,9 +121,7 @@ export class Erc20 extends Contract<Erc20Contract> {
methods.totalSupply().call(),
methods.approve(dummyAccount, zeroValue).call(callParams),
methods.allowance(dummyAccount, dummyAccount).call(),
methods
.transferFrom(dummyAccount, dummyAccount, zeroValue)
.call(callParams),
methods.transferFrom(dummyAccount, dummyAccount, zeroValue).call(callParams),
methods.transfer(dummyAccount, zeroValue).call(callParams),
methods.balanceOf(dummyAccount).call(),
]);
Expand Down
61 changes: 27 additions & 34 deletions src/LowLevel/Erc20DividendCheckpoint.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Web3 from 'web3';
import BigNumber from 'bignumber.js';
import { ERC20DividendCheckpointAbi } from './abis/ERC20DividendCheckpointAbi';
import { toUnixTimestamp, toDivisible } from './utils';
import { TransactionObject } from 'web3/eth/types';
import { ERC20DividendCheckpointAbi } from './abis/ERC20DividendCheckpointAbi';
import { toUnixTimestamp, toDivisible, getOptions } from './utils';
import { DividendCheckpoint } from './DividendCheckpoint';
import { Erc20 } from './Erc20';
import { Context } from './LowLevel';
Expand Down Expand Up @@ -38,10 +38,9 @@ interface Erc20DividendCheckpointContract extends GenericContract {
};
}

export class Erc20DividendCheckpoint extends DividendCheckpoint<
Erc20DividendCheckpointContract
> {
export class Erc20DividendCheckpoint extends DividendCheckpoint<Erc20DividendCheckpointContract> {
public dividendType = DividendModuleTypes.Erc20;

constructor({ address, context }: { address: string; context: Context }) {
super({ address, abi: ERC20DividendCheckpointAbi.abi, context });
}
Expand All @@ -65,39 +64,33 @@ export class Erc20DividendCheckpoint extends DividendCheckpoint<
const divisibleAmount = toDivisible(amount, decimals);

if (excludedAddresses) {
return () =>
this.contract.methods
.createDividendWithCheckpointAndExclusions(
maturity,
expiry,
tokenAddress,
divisibleAmount,
checkpointId,
excludedAddresses,
nameInBytes
)
.send({ from: this.context.account });
const method = this.contract.methods.createDividendWithCheckpointAndExclusions(
maturity,
expiry,
tokenAddress,
divisibleAmount,
checkpointId,
excludedAddresses,
nameInBytes
);
const options = await getOptions(method, { from: this.context.account });
return () => method.send(options);
}

return () =>
this.contract.methods
.createDividendWithCheckpoint(
maturity,
expiry,
tokenAddress,
divisibleAmount,
checkpointId,
nameInBytes
)
.send({ from: this.context.account });
const method = this.contract.methods.createDividendWithCheckpoint(
maturity,
expiry,
tokenAddress,
divisibleAmount,
checkpointId,
nameInBytes
);
const options = await getOptions(method, { from: this.context.account });
return () => method.send(options);
};

public async getDividend({
dividendIndex,
}: GetDividendArgs): Promise<Dividend> {
const tokenAddress = await this.contract.methods
.dividendTokens(dividendIndex)
.call();
public async getDividend({ dividendIndex }: GetDividendArgs): Promise<Dividend> {
const tokenAddress = await this.contract.methods.dividendTokens(dividendIndex).call();

const token = new Erc20({ address: tokenAddress, context: this.context });

Expand Down
Loading

0 comments on commit bf95bad

Please sign in to comment.