Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/beta' into docs/wallet-queue-fac…
Browse files Browse the repository at this point in the history
…tory
  • Loading branch information
Victor Wiebe committed Jan 14, 2020
2 parents d6167fa + c9bfc59 commit 3d24d3a
Show file tree
Hide file tree
Showing 5 changed files with 870 additions and 82 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@polymathnetwork/sdk",
"version": "2.0.1-beta.91",
"version": "2.0.1-beta.92",
"description": "A Javascript SDK for interacting with the Polymath network for the browser and Node.js",
"bugs": {
"url": "https://github.com/PolymathNetwork/polymath-sdk/issues"
Expand Down
21 changes: 20 additions & 1 deletion src/entities/TieredSto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ export interface Tier {
export interface Params extends StoParams {
currentTier: number;
tiers: Tier[];
nonAccreditedInvestmentLimit: BigNumber;
minimumInvestment: BigNumber;
stableCoinAddresses: string[];
}

interface BaseParams {
Expand Down Expand Up @@ -64,15 +67,31 @@ export class TieredSto extends Sto<Params> {

public currentTier: number;

public nonAccreditedInvestmentLimit: BigNumber;

public minimumInvestment: BigNumber;

public stableCoinAddresses: string[];

public tiers: Tier[];

constructor(params: Params & UniqueIdentifiers, context: Context) {
const { currentTier, tiers, ...rest } = params;
const {
currentTier,
tiers,
nonAccreditedInvestmentLimit,
minimumInvestment,
stableCoinAddresses,
...rest
} = params;

super(rest, context);

const { securityTokenId, address, stoType } = rest;

this.nonAccreditedInvestmentLimit = nonAccreditedInvestmentLimit;
this.minimumInvestment = minimumInvestment;
this.stableCoinAddresses = stableCoinAddresses;
this.currentTier = currentTier;
this.tiers = tiers;
this.uid = TieredSto.generateId({ address, stoType, securityTokenId });
Expand Down
8 changes: 8 additions & 0 deletions src/entities/factories/TieredStoFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export class TieredStoFactory extends Factory<TieredSto, Params, UniqueIdentifie
raisedFundsWallet,
unsoldTokensWallet,
numberOfTiers,
minimumInvestment,
nonAccreditedInvestmentLimit,
{
tokensSold,
capPerTier,
Expand All @@ -49,8 +51,11 @@ export class TieredStoFactory extends Factory<TieredSto, Params, UniqueIdentifie
module.wallet(),
contractWrappers.getTreasuryWallet({ module }),
module.getNumberOfTiers(),
module.minimumInvestmentUSD(),
module.nonAccreditedLimitUSD(),
module.getSTODetails(),
]);
const stableCoinAddresses = await module.getUsdTokens();

let preIssueAllowed = false;
let tiers: Tier[];
Expand Down Expand Up @@ -133,6 +138,9 @@ export class TieredStoFactory extends Factory<TieredSto, Params, UniqueIdentifie
isFinalized,
preIssueAllowed,
beneficialInvestmentsAllowed,
minimumInvestment,
nonAccreditedInvestmentLimit,
stableCoinAddresses,
};
};

Expand Down
141 changes: 61 additions & 80 deletions src/procedures/ModifyTieredStoData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
isUSDTieredSTO_3_1_0,
FundRaiseType,
} from '@polymathnetwork/contract-wrappers';
import { sortBy, toUpper, isEqual, range } from 'lodash';
import { sortBy, toUpper, isEqual } from 'lodash';
import { Procedure } from './Procedure';
import {
ProcedureType,
Expand All @@ -13,25 +13,21 @@ import {
ModifyTieredStoDataProcedureArgs,
StoType,
Currency,
StoTier,
} from '../types';
import { PolymathError } from '../PolymathError';
import { areSameAddress } from '../utils';
import { SecurityToken, TieredSto } from '../entities';
import { TieredStoFactory } from '../entities/factories';

const createRefreshResolver = (
export const createTieredStoFactoryRefreshResolver = (
tieredStoFactory: TieredStoFactory,
addedTransactions: PolyTransactionTag[],
tag: PolyTransactionTag,
securityTokenId: string,
stoAddress: string
tieredStoId: string
) => async () => {
// refresh will only be called once at the last transaction
if (addedTransactions[addedTransactions.length - 1] === tag) {
return tieredStoFactory.refresh(
TieredSto.generateId({ securityTokenId, stoType: StoType.Tiered, address: stoAddress })
);
return tieredStoFactory.refresh(tieredStoId);
}

return undefined;
Expand Down Expand Up @@ -81,24 +77,35 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA
});
}

const [
storageWallet,
treasuryWallet,
numberOfTiers,
usdTokens,
minInvestment,
investmentLimit,
{ isRaisedInETH, isRaisedInPOLY, isRaisedInSC, startTime, endTime },
] = await Promise.all([
stoModule.wallet(),
const securityTokenId = SecurityToken.generateId({ symbol });
const tieredStoId = TieredSto.generateId({
securityTokenId,
stoType: StoType.Tiered,
address: stoAddress,
});

const [sto, treasuryWallet] = await Promise.all([
tieredStoFactory.fetch(tieredStoId),
contractWrappers.getTreasuryWallet({ module: stoModule }),
stoModule.getNumberOfTiers(),
stoModule.getUsdTokens(),
stoModule.minimumInvestmentUSD(),
stoModule.nonAccreditedLimitUSD(),
stoModule.getSTODetails(),
]);

const {
nonAccreditedInvestmentLimit: investmentLimit,
minimumInvestment: minInvestment,
startDate: startTime,
endDate: endTime,
raisedFundsWallet: storageWallet,
tiers: allTiers,
fundraiseCurrencies,
stableCoinAddresses: usdTokens,
} = sto;

const [isRaisedInETH, isRaisedInPOLY, isRaisedInSC] = [
fundraiseCurrencies.includes(FundRaiseType.ETH),
fundraiseCurrencies.includes(FundRaiseType.POLY),
fundraiseCurrencies.includes(FundRaiseType.StableCoin),
];

// STO can't have started
if (startTime <= new Date()) {
throw new PolymathError({
Expand All @@ -109,7 +116,6 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA

// list of added transactions to keep track of the last added tx in order to refresh the entity only once
const addedTransactions: PolyTransactionTag[] = [];
const securityTokenId = SecurityToken.generateId({ symbol });

if (!startDate) {
startDate = startTime;
Expand All @@ -125,12 +131,11 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA
await this.addTransaction(stoModule.modifyTimes, {
tag,
resolvers: [
createRefreshResolver(
createTieredStoFactoryRefreshResolver(
tieredStoFactory,
addedTransactions,
tag,
securityTokenId,
stoAddress
tieredStoId
),
],
})({ startTime: startDate, endTime: endDate });
Expand All @@ -152,9 +157,9 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA
}
}

const willRaiseinEth = currencies.find(cur => cur === Currency.ETH);
const willRaiseinPoly = currencies.find(cur => cur === Currency.POLY);
const willRaiseinSc = currencies.find(cur => cur === Currency.StableCoin);
const willRaiseinEth = currencies.includes(Currency.ETH);
const willRaiseinPoly = currencies.includes(Currency.POLY);
const willRaiseinSc = currencies.includes(Currency.StableCoin);

const areSameCurrencies =
((!isRaisedInETH && !willRaiseinEth) || (isRaisedInETH && willRaiseinEth)) &&
Expand All @@ -167,33 +172,19 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA
await this.addTransaction(stoModule.modifyFunding, {
tag,
resolvers: [
createRefreshResolver(
createTieredStoFactoryRefreshResolver(
tieredStoFactory,
addedTransactions,
tag,
securityTokenId,
stoAddress
tieredStoId
),
],
})({ fundRaiseTypes: currencies });
}

let assembledTiers: StoTier[];
let rawTiers;

// this is needed because the return types of `tier` are different in the two versions
// even if the properties used here are the same for both. Also custom currencies are only supported in 3.1
if (isUSDTieredSTO_3_1_0(stoModule)) {
rawTiers = await Promise.all(range(numberOfTiers).map(tier => stoModule.tiers({ tier })));
assembledTiers = rawTiers.map(
({ tokenTotal, rate, tokensDiscountPoly, rateDiscountPoly }) => ({
tokensOnSale: tokenTotal,
price: rate,
tokensWithDiscount: tokensDiscountPoly,
discountedPrice: rateDiscountPoly,
})
);

const [
currentEthOracleAddress,
currentPolyOracleAddress,
Expand Down Expand Up @@ -229,51 +220,39 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA
}

if (
currencySymbol !== denominatedCurrency &&
ethOracleAddress !== currentEthOracleAddress &&
currencySymbol !== denominatedCurrency ||
ethOracleAddress !== currentEthOracleAddress ||
polyOracleAddress !== currentPolyOracleAddress
) {
const tag = PolyTransactionTag.ModifyOracles;
addedTransactions.push(tag);
await this.addTransaction(stoModule.modifyOracles, {
tag,
resolvers: [
createRefreshResolver(
createTieredStoFactoryRefreshResolver(
tieredStoFactory,
addedTransactions,
tag,
securityTokenId,
stoAddress
tieredStoId
),
],
})({
denominatedCurrencySymbol: currencySymbol,
customOracleAddresses: [ethOracleAddress, polyOracleAddress],
});
}
} else {
if (customCurrency) {
throw new PolymathError({
code: ErrorCode.ProcedureValidationError,
message: 'Custom currency not supported in Tiered STO v3.0',
});
}

rawTiers = await Promise.all(range(numberOfTiers).map(tier => stoModule.tiers({ tier })));
assembledTiers = rawTiers.map(
({ tokenTotal, rate, tokensDiscountPoly, rateDiscountPoly }) => ({
tokensOnSale: tokenTotal,
price: rate,
tokensWithDiscount: tokensDiscountPoly,
discountedPrice: rateDiscountPoly,
})
);
} else if (customCurrency) {
throw new PolymathError({
code: ErrorCode.ProcedureValidationError,
message: 'Custom currency not supported in Tiered STO v3.0',
});
}

if (!tiers) {
tiers = assembledTiers;
tiers = allTiers;
}

const areSameTiers = isEqual(tiers, assembledTiers);
const areSameTiers = isEqual(tiers, allTiers);

if (!areSameTiers) {
const tokensPerTierTotal: BigNumber[] = [];
Expand All @@ -297,15 +276,19 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA
await this.addTransaction(stoModule.modifyTiers, {
tag,
resolvers: [
createRefreshResolver(
createTieredStoFactoryRefreshResolver(
tieredStoFactory,
addedTransactions,
tag,
securityTokenId,
stoAddress
tieredStoId
),
],
})({ ratePerTier, tokensPerTierTotal, tokensPerTierDiscountPoly, ratePerTierDiscountPoly });
})({
ratePerTier,
tokensPerTierTotal,
tokensPerTierDiscountPoly,
ratePerTierDiscountPoly,
});
}

if (!minimumInvestment) {
Expand All @@ -325,12 +308,11 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA
await this.addTransaction(stoModule.modifyLimits, {
tag,
resolvers: [
createRefreshResolver(
createTieredStoFactoryRefreshResolver(
tieredStoFactory,
addedTransactions,
tag,
securityTokenId,
stoAddress
tieredStoId
),
],
})({
Expand Down Expand Up @@ -366,12 +348,11 @@ export class ModifyTieredStoData extends Procedure<ModifyTieredStoDataProcedureA
await this.addTransaction(stoModule.modifyAddresses, {
tag,
resolvers: [
createRefreshResolver(
createTieredStoFactoryRefreshResolver(
tieredStoFactory,
addedTransactions,
tag,
securityTokenId,
stoAddress
tieredStoId
),
],
})({
Expand Down
Loading

0 comments on commit 3d24d3a

Please sign in to comment.