Skip to content

Commit

Permalink
fix: feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
shuffledex committed Mar 11, 2020
1 parent b1fdd1e commit 36dce6a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
23 changes: 12 additions & 11 deletions src/api/procedures/__tests__/reserveTicker.ts
Expand Up @@ -173,47 +173,48 @@ describe('reserveTicker procedure', () => {
);
});

test('should throw an error if extendPeriod property is set to true and the token has already been launched', async () => {
const expiryDate = null;
test('should throw an error if extendPeriod property is set to true and the ticker has not reserved or the reservation has expired', async () => {
const expiryDate = new Date(2019, 1, 1);
mockTickerReservation.mock('details', {
ownerDid: 'someDid',
expiryDate,
status: TickerReservationStatus.Free,
});
const proc = mockProcedure.getMockInstance();
proc.context = mockContext;

return expect(prepareReserveTicker.call(proc, { ...args, extendPeriod: true })).rejects.toThrow(
'Ticker has already been launched'
'Ticker not reserved or the reservation has expired'
);
});

test('should throw an error if extendPeriod property is set to true and the ticker has not reserved or the reservation has expired', async () => {
const expiryDate = new Date(2019, 1, 1);
test("should throw an error if extendPeriod property is set to true and the signing account doesn't have enough balance", () => {
const expiryDate = new Date(new Date().getTime() + 1000);
mockTickerReservation.mock('details', {
ownerDid: 'someDid',
expiryDate,
status: TickerReservationStatus.Free,
});
mockFactory.createQueryStub('asset', 'tickerRegistrationFee', createMockBalance(600000000));
const proc = mockProcedure.getMockInstance();
proc.context = mockContext;

return expect(prepareReserveTicker.call(proc, { ...args, extendPeriod: true })).rejects.toThrow(
'Ticker not reserved or the reservation has expired'
'Not enough POLY balance to pay for ticker period extension'
);
});

test("should throw an error if extendPeriod property is set to true and the signing account doesn't have enough balance", () => {
test('should throw an error if extendPeriod property is set to true and the signing account is not the ticker owner', () => {
const expiryDate = new Date(new Date().getTime() + 1000);
mockTickerReservation.mock('details', {
ownerDid: 'someDid',
ownerDid: 'anotherDid',
expiryDate,
status: TickerReservationStatus.Reserved,
});
mockFactory.createQueryStub('asset', 'tickerRegistrationFee', createMockBalance(600000000));
const proc = mockProcedure.getMockInstance();
proc.context = mockContext;

return expect(prepareReserveTicker.call(proc, { ...args, extendPeriod: true })).rejects.toThrow(
'Not enough POLY balance to pay for ticker period extension'
'You must be the owner of the ticker to extend its registration period'
);
});

Expand Down
32 changes: 16 additions & 16 deletions src/api/procedures/reserveTicker.ts
Expand Up @@ -50,23 +50,23 @@ export async function prepareReserveTicker(
rawFee,
balance,
{ max_ticker_length: rawMaxTickerLength },
{ expiryDate, status },
{ ownerDid, expiryDate, status },
] = await Promise.all([
query.asset.tickerRegistrationFee(),
context.accountBalance(),
query.asset.tickerConfig(),
reservation.details(),
]);

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

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

throw new PolymeshError({
Expand All @@ -75,8 +75,15 @@ export async function prepareReserveTicker(
!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 registration period',
});
}
}

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

if (ticker.length > maxTickerLength) {
Expand All @@ -86,13 +93,6 @@ export async function prepareReserveTicker(
});
}
} else {
if (expiryDate === null) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
message: 'Ticker has already been launched',
});
}

if (status === TickerReservationStatus.Free) {
throw new PolymeshError({
code: ErrorCode.ValidationError,
Expand Down
2 changes: 1 addition & 1 deletion src/testUtils/mocks/PolkadotMockFactory.ts
Expand Up @@ -223,7 +223,7 @@ export class PolkadotMockFactory {
*/
private initContext(opts: ContextOptions): void {
const currentIdentity = opts.withSeed
? { getIdentityBalance: sinon.stub().resolves(opts.balance) }
? { getIdentityBalance: sinon.stub().resolves(opts.balance), did: 'someDid' }
: undefined;
const currentPair = opts.withSeed
? ({
Expand Down

0 comments on commit 36dce6a

Please sign in to comment.