Skip to content

Commit

Permalink
feat: change a token from indivisible to divisible
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Mar 12, 2020
1 parent e84c8dc commit b24145c
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/api/entities/SecurityToken/index.ts
@@ -1,4 +1,5 @@
import { Entity } from '~/base';
import { modifyToken, ModifyTokenParams } from '~/api/procedures';
import { Entity, TransactionQueue } from '~/base';
import { Context } from '~/context';

/**
Expand Down Expand Up @@ -40,4 +41,21 @@ export class SecurityToken extends Entity<UniqueIdentifiers> {

this.ticker = ticker;
}

/**
* Modify some properties of the Security Token
*
* @param args.makeDivisible - makes an indivisible token divisible. Only called by the token owner
*/
public async modify(
args: Omit<ModifyTokenParams, 'ticker'>
): Promise<TransactionQueue<SecurityToken>> {
// pedir details() de security token y obtener el owner y si ya es divisible.
/*
VALIDAR: si es divisible tirar error:
VALIDAR: si el usuario no es el owner, tirar error: You must be the owner of the token to make it divisible
*/
const { ticker } = this;
return modifyToken.prepare({ ...args, ticker }, this.context);
}
}
1 change: 1 addition & 0 deletions src/api/procedures/index.ts
@@ -1 +1,2 @@
export { reserveTicker, ReserveTickerParams } from './reserveTicker';
export { modifyToken, ModifyTokenParams } from '/modifyToken';
114 changes: 114 additions & 0 deletions src/api/procedures/modifyToken.ts
@@ -0,0 +1,114 @@
import { TxTags } from '@polymathnetwork/polkadot/api/types';
import { SecurityToken, Ticker } from '@polymathnetwork/polkadot/types/interfaces';
import { ISubmittableResult } from '@polymathnetwork/polkadot/types/types';

import { TickerReservation } from '~/api/entities';
import { PolymeshError, PostTransactionValue, Procedure } from '~/base';
import { Context } from '~/context';
import { ErrorCode, TickerReservationStatus } from '~/types';
import { balanceToBigNumber, findEventRecord, stringToTicker, tickerToString } from '~/utils';

export interface ModifyTokenParams {
ticker: string;
makeDivisible?: boolean;
}

/**
* @hidden
*/
export async function prepareModifyToken(
this: Procedure<ModifyTokenParams, SecurityToken>,
args: ModifyTokenParams
): Promise<PostTransactionValue<TickerReservation>> {
const {
context: {
polymeshApi: { query, tx },
},
context,
} = this;
const { ticker, makeDivisible } = args;

const rawTicker = stringToTicker(ticker, context);

const reservation = new TickerReservation({ ticker }, context);

const [
rawFee,
balance,
{ max_ticker_length: rawMaxTickerLength },
{ ownerDid, expiryDate, status },
] = await Promise.all([
query.asset.tickerRegistrationFee(),
context.accountBalance(),
query.asset.tickerConfig(),
reservation.details(),
]);

if (status === TickerReservationStatus.TokenCreated) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: `A Security Token with ticker "${ticker} already exists`,
});
}

if (status === TickerReservationStatus.Reserved) {
if (!extendPeriod) {
const isPermanent = expiryDate === null;

throw new PolymeshError({
code: ErrorCode.ValidationError,
message: `Ticker "${ticker}" already reserved. The current reservation will ${
!isPermanent ? '' : 'not '
}expire${!isPermanent ? ` at ${expiryDate}` : ''}`,
});
} else if (ownerDid !== context.currentIdentity?.did) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'You must be the owner of the ticker to extend its reservation period',
});
}
}

if (!extendPeriod) {
const maxTickerLength = rawMaxTickerLength.toNumber();

if (ticker.length > maxTickerLength) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: `Ticker length cannot exceed ${maxTickerLength}`,
});
}
} else {
if (status === TickerReservationStatus.Free) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'Ticker not reserved or the reservation has expired',
});
}
}

const fee = balanceToBigNumber(rawFee);

if (balance.lt(fee)) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: `Not enough POLY balance to pay for ticker ${
extendPeriod ? 'period extension' : 'reservation'
}`,
});
}

const [newReservation] = this.addTransaction(
tx.asset.registerTicker,
{
tag: TxTags.asset.RegisterTicker,
fee,
resolvers: [createTickerReservationResolver(context)],
},
rawTicker
);

return newReservation;
}

export const modifyToken = new Procedure(prepareModifyToken);

0 comments on commit b24145c

Please sign in to comment.