Skip to content

Commit

Permalink
feat: use reversible uuids in fetch functions
Browse files Browse the repository at this point in the history
  • Loading branch information
monitz87 committed Apr 25, 2019
1 parent 65fff71 commit 1f37036
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 151 deletions.
160 changes: 126 additions & 34 deletions src/Polymath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ import {
Erc20DividendsModule as Erc20DividendsModuleEntity,
EthDividendsModule as EthDividendsModuleEntity,
Erc20TokenBalance as Erc20TokenBalanceEntity,
Dividend,
Erc20TokenBalance,
Checkpoint,
TaxWithholding,
} from '~/entities';

import {
Expand All @@ -38,6 +42,7 @@ import { PolymathNetworkParams } from '~/types';
import BigNumber from 'bignumber.js';
import { includes } from 'lodash';
import { SetDividendsWallet } from '~/procedures/SetDividendsWallet';
import { DividendsModule } from './entities/DividendsModule';

// TODO @RafaelVidaurre: Type this correctly. It should return a contextualized
// version of T
Expand Down Expand Up @@ -317,13 +322,31 @@ export class Polymath {
* Retrieve a list of investor addresses and their corresponding tax withholding
* percentages
*/
public getDividendsTaxWithholdingList = async (args: {
symbol: string;
dividendType: DividendModuleTypes;
checkpointIndex: number;
}) => {
public getDividendsTaxWithholdingList = async (
args:
| {
symbol: string;
dividendType: DividendModuleTypes;
checkpointIndex: number;
}
| string
) => {
const { securityTokenRegistry } = this.context;
const { symbol: securityTokenSymbol, dividendType, checkpointIndex } = args;

let securityTokenSymbol: string,
dividendType: DividendModuleTypes,
checkpointIndex: number;

// fetch by UUID
if (typeof args === 'string') {
({
securityTokenSymbol,
dividendType,
checkpointIndex,
} = TaxWithholding.unserialize(args));
} else {
({ symbol: securityTokenSymbol, dividendType, checkpointIndex } = args);
}

const securityToken = await securityTokenRegistry.getSecurityToken({
ticker: securityTokenSymbol,
Expand Down Expand Up @@ -358,6 +381,7 @@ export class Polymath {
securityTokenSymbol,
securityTokenId,
dividendType,
checkpointIndex,
})
);
};
Expand Down Expand Up @@ -406,17 +430,29 @@ export class Polymath {
});
};

public getCheckpoint = async (args: {
symbol: string;
checkpointIndex: number;
dividendTypes?: DividendModuleTypes[];
}) => {
public getCheckpoint = async (
args:
| {
symbol: string;
checkpointIndex: number;
dividendTypes?: DividendModuleTypes[];
}
| string
) => {
const { securityTokenRegistry } = this.context;
const {
symbol: securityTokenSymbol,
checkpointIndex,
dividendTypes = [DividendModuleTypes.Erc20, DividendModuleTypes.Eth],
} = args;

let securityTokenSymbol: string,
checkpointIndex: number,
dividendTypes: DividendModuleTypes[] | undefined;

// fetch by UUID
if (typeof args === 'string') {
({ securityTokenSymbol, index: checkpointIndex } = Checkpoint.unserialize(
args
));
} else {
({ symbol: securityTokenSymbol, checkpointIndex, dividendTypes } = args);
}

const securityToken = await securityTokenRegistry.getSecurityToken({
ticker: securityTokenSymbol,
Expand Down Expand Up @@ -486,12 +522,29 @@ export class Polymath {
return dividends;
};

public getDividend = async (args: {
symbol: string;
dividendType: DividendModuleTypes;
dividendIndex: number;
}) => {
const { symbol, dividendType, dividendIndex } = args;
public getDividend = async (
args:
| {
symbol: string;
dividendType: DividendModuleTypes;
dividendIndex: number;
}
| string
) => {
let symbol: string,
dividendType: DividendModuleTypes,
dividendIndex: number;

// fetch by UUID
if (typeof args === 'string') {
({
securityTokenSymbol: symbol,
index: dividendIndex,
dividendType,
} = Dividend.unserialize(args));
} else {
({ symbol, dividendType, dividendIndex } = args);
}

const checkpoints = await this.getCheckpoints({
symbol,
Expand All @@ -515,12 +568,26 @@ export class Polymath {
);
};

public getDividendsModule = async (args: {
symbol: string;
dividendType: DividendModuleTypes;
}) => {
public getDividendsModule = async (
args:
| {
symbol: string;
dividendType: DividendModuleTypes;
}
| string
) => {
const { securityTokenRegistry } = this.context;
const { symbol: securityTokenSymbol, dividendType } = args;

let securityTokenSymbol: string, dividendType: DividendModuleTypes;

// fetch by UUID
if (typeof args === 'string') {
({ securityTokenSymbol, dividendType } = DividendsModule.unserialize(
args
));
} else {
({ symbol: securityTokenSymbol, dividendType } = args);
}

const securityToken = await securityTokenRegistry.getSecurityToken({
ticker: securityTokenSymbol,
Expand Down Expand Up @@ -576,14 +643,26 @@ export class Polymath {
return null;
};

public getErc20DividendsModule = async (args: { symbol: string }) => {
public getErc20DividendsModule = async (
args: { symbol: string } | string
) => {
// fetch by UUID
if (typeof args === 'string') {
return this.getDividendsModule(args);
}

return this.getDividendsModule({
symbol: args.symbol,
dividendType: DividendModuleTypes.Erc20,
});
};

public getEthDividendsModule = async (args: { symbol: string }) => {
public getEthDividendsModule = async (args: { symbol: string } | string) => {
// fetch by UUID
if (typeof args === 'string') {
return this.getDividendsModule(args);
}

return this.getDividendsModule({
symbol: args.symbol,
dividendType: DividendModuleTypes.Eth,
Expand All @@ -595,11 +674,23 @@ export class Polymath {
return this.lowLevel.isValidErc20({ address });
};

public getErc20TokenBalance = async (args: {
tokenAddress: string;
walletAddress: string;
}) => {
const { tokenAddress, walletAddress } = args;
public getErc20TokenBalance = async (
args:
| {
tokenAddress: string;
walletAddress: string;
}
| string
) => {
let tokenAddress: string, walletAddress: string;

// fetch by UUID
if (typeof args === 'string') {
({ tokenAddress, walletAddress } = Erc20TokenBalance.unserialize(args));
} else {
({ tokenAddress, walletAddress } = args);
}

const token = await this.lowLevel.getErc20Token({ address: tokenAddress });
const [symbol, balance] = await Promise.all([
token.symbol(),
Expand All @@ -610,6 +701,7 @@ export class Polymath {
tokenSymbol: symbol,
tokenAddress,
balance,
walletAddress,
});
};

Expand Down
20 changes: 19 additions & 1 deletion src/entities/DividendsModule.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { Polymath } from '~/Polymath';
import { Entity } from './Entity';
import { unserialize } from '~/utils';
import { DividendModuleTypes } from '~/types';

export interface UniqueIdentifiers {
securityTokenSymbol: string;
dividendType: DividendModuleTypes;
}

export function isUniqueIdentifiers(
function isUniqueIdentifiers(
identifiers: any
): identifiers is UniqueIdentifiers {
const { securityTokenSymbol } = identifiers;
Expand All @@ -25,6 +28,17 @@ export abstract class DividendsModule extends Entity {
public securityTokenSymbol: string;
public securityTokenId: string;
public storageWalletAddress: string;
public dividendType: DividendModuleTypes;

public static unserialize(serialized: string) {
const unserialized = unserialize(serialized);

if (!isUniqueIdentifiers(unserialized)) {
throw new Error('Wrong dividends module ID format.');
}

return unserialized;
}

constructor(params: Params, polyClient?: Polymath) {
super(polyClient);
Expand All @@ -34,12 +48,14 @@ export abstract class DividendsModule extends Entity {
securityTokenSymbol,
securityTokenId,
storageWalletAddress,
dividendType,
} = params;

this.address = address;
this.securityTokenSymbol = securityTokenSymbol;
this.securityTokenId = securityTokenId;
this.storageWalletAddress = storageWalletAddress;
this.dividendType = dividendType;
}

public toPojo() {
Expand All @@ -49,6 +65,7 @@ export abstract class DividendsModule extends Entity {
securityTokenSymbol,
securityTokenId,
storageWalletAddress,
dividendType,
} = this;

return {
Expand All @@ -57,6 +74,7 @@ export abstract class DividendsModule extends Entity {
securityTokenSymbol,
securityTokenId,
storageWalletAddress,
dividendType,
};
}
}
52 changes: 30 additions & 22 deletions src/entities/Erc20DividendsModule.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,45 @@
import {
DividendsModule,
Params,
UniqueIdentifiers,
isUniqueIdentifiers,
} from './DividendsModule';
import { DividendsModule, Params, UniqueIdentifiers } from './DividendsModule';
import { Polymath } from '~/Polymath';
import { serialize, unserialize } from '~/utils';
import { serialize } from '~/utils';
import { DividendModuleTypes, Omit } from '~/types';

export class Erc20DividendsModule extends DividendsModule {
public static generateId({ securityTokenSymbol }: UniqueIdentifiers) {
public static generateId({
securityTokenSymbol,
dividendType,
}: UniqueIdentifiers) {
return serialize('erc20DividendsModule', {
securityTokenSymbol,
dividendType,
});
}

public static unserialize(serialized: string) {
const unserialized = unserialize(serialized);

if (!isUniqueIdentifiers(unserialized)) {
throw new Error('Wrong erc20 dividends module ID format.');
}

return unserialized;
}

public entityType: string = 'erc20DividendsModule';
public uid: string;

constructor(params: Params, polyClient?: Polymath) {
super(params, polyClient);
constructor(
{
securityTokenSymbol,
securityTokenId,
address,
storageWalletAddress,
}: Omit<Params, 'dividendType'>,
polyClient?: Polymath
) {
const dividendType = DividendModuleTypes.Erc20;
super(
{
securityTokenId,
securityTokenSymbol,
address,
storageWalletAddress,
dividendType,
},
polyClient
);

this.uid = Erc20DividendsModule.generateId({
securityTokenSymbol: params.securityTokenSymbol,
securityTokenSymbol,
dividendType,
});
}
}
Loading

0 comments on commit 1f37036

Please sign in to comment.