-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from PolymathNetwork/feat/transfer-ticker-own…
…ership feat: transfer ticker ownership procedure
- Loading branch information
Showing
5 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Procedure } from './Procedure'; | ||
import { | ||
ProcedureType, | ||
PolyTransactionTag, | ||
TransferReservationOwnershipProcedureArgs, | ||
ErrorCode, | ||
} from '../types'; | ||
import { PolymathError } from '../PolymathError'; | ||
import { SecurityTokenReservation } from '../entities'; | ||
import { Factories } from '../Context'; | ||
|
||
export class TransferReservationOwnership extends Procedure< | ||
TransferReservationOwnershipProcedureArgs | ||
> { | ||
public type = ProcedureType.TransferReservationOwnership; | ||
|
||
public async prepareTransactions() { | ||
const { newOwner, symbol } = this.args; | ||
const { | ||
contractWrappers: { securityTokenRegistry }, | ||
currentWallet, | ||
factories, | ||
} = this.context; | ||
|
||
const { status, owner } = await securityTokenRegistry.getTickerDetails({ | ||
ticker: symbol, | ||
}); | ||
|
||
const account = await currentWallet.address(); | ||
|
||
if (status) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `The ${symbol} Security Token has already been launched, ownership cannot be transferred`, | ||
}); | ||
} | ||
|
||
if (account !== owner) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `Only the reservation owner can transfer ownership to another wallet`, | ||
}); | ||
} | ||
|
||
if (newOwner === owner) { | ||
throw new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `New owner must be different from the current one to transfer ownership`, | ||
}); | ||
} | ||
|
||
await this.addTransaction(securityTokenRegistry.transferTickerOwnership, { | ||
tag: PolyTransactionTag.TransferReservationOwnership, | ||
resolvers: [createTransferReservationOwnershipResolver(factories, symbol)], | ||
})({ newOwner, ticker: symbol }); | ||
} | ||
} | ||
|
||
export const createTransferReservationOwnershipResolver = ( | ||
factories: Factories, | ||
symbol: string | ||
) => async () => { | ||
return factories.securityTokenReservationFactory.refresh( | ||
SecurityTokenReservation.generateId({ symbol }) | ||
); | ||
}; |
172 changes: 172 additions & 0 deletions
172
src/procedures/__tests__/TransferReservationOwnership.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import { ImportMock, MockManager } from 'ts-mock-imports'; | ||
import { spy, restore } from 'sinon'; | ||
import * as contractWrappersModule from '@polymathnetwork/contract-wrappers'; | ||
import * as contextModule from '../../Context'; | ||
import * as wrappersModule from '../../PolymathBase'; | ||
import * as tokenFactoryModule from '../../testUtils/MockedTokenFactoryModule'; | ||
import { TransferReservationOwnership } from '../../procedures/TransferReservationOwnership'; | ||
import { Procedure } from '../../procedures/Procedure'; | ||
import { ProcedureType, PolyTransactionTag, ErrorCode } from '../../types'; | ||
import { PolymathError } from '../../PolymathError'; | ||
import * as TransferReservationOwnershipModule from '../../procedures/TransferReservationOwnership'; | ||
import * as securityTokenReservationFactoryModule from '../../entities/factories/SecurityTokenReservationFactory'; | ||
import { mockFactories } from '../../testUtils/mockFactories'; | ||
import { Wallet } from '../../Wallet'; | ||
import { Factories } from '../../Context'; | ||
import { SecurityTokenReservation } from '../../entities'; | ||
|
||
const params = { | ||
symbol: 'TEST', | ||
newOwner: '0x3333333333333333333333333333333333333333', | ||
}; | ||
|
||
describe('TransferReservationOwnership', () => { | ||
let target: TransferReservationOwnership; | ||
let contextMock: MockManager<contextModule.Context>; | ||
let wrappersMock: MockManager<wrappersModule.PolymathBase>; | ||
let tokenFactoryMock: MockManager<tokenFactoryModule.MockedTokenFactoryModule>; | ||
let securityTokenMock: MockManager<contractWrappersModule.SecurityToken_3_0_0>; | ||
let securityTokenRegistryMock: MockManager<contractWrappersModule.SecurityTokenRegistry>; | ||
let securityTokenReservationFactoryMock: MockManager< | ||
securityTokenReservationFactoryModule.SecurityTokenReservationFactory | ||
>; | ||
let factoryMockSetup: Factories; | ||
|
||
beforeEach(() => { | ||
// Mock the context, wrappers, tokenFactory and securityToken to test TransferReservationOwnership | ||
contextMock = ImportMock.mockClass(contextModule, 'Context'); | ||
wrappersMock = ImportMock.mockClass(wrappersModule, 'PolymathBase'); | ||
tokenFactoryMock = ImportMock.mockClass(tokenFactoryModule, 'MockedTokenFactoryModule'); | ||
securityTokenMock = ImportMock.mockClass(contractWrappersModule, 'SecurityToken_3_0_0'); | ||
securityTokenRegistryMock = ImportMock.mockClass( | ||
contractWrappersModule, | ||
'SecurityTokenRegistry' | ||
); | ||
securityTokenReservationFactoryMock = ImportMock.mockClass( | ||
securityTokenReservationFactoryModule, | ||
'SecurityTokenReservationFactory' | ||
); | ||
|
||
securityTokenRegistryMock.mock( | ||
'getTickerDetails', | ||
Promise.resolve({ status: false, owner: '0x01' }) | ||
); | ||
|
||
tokenFactoryMock.mock( | ||
'getSecurityTokenInstanceFromTicker', | ||
securityTokenMock.getMockInstance() | ||
); | ||
|
||
factoryMockSetup = mockFactories(); | ||
factoryMockSetup.securityTokenReservationFactory = securityTokenReservationFactoryMock.getMockInstance(); | ||
|
||
wrappersMock.set('tokenFactory', tokenFactoryMock.getMockInstance()); | ||
wrappersMock.set('securityTokenRegistry', securityTokenRegistryMock.getMockInstance()); | ||
contextMock.set('contractWrappers', wrappersMock.getMockInstance()); | ||
contextMock.set('currentWallet', new Wallet({ address: () => Promise.resolve('0x02') })); | ||
contextMock.set('factories', factoryMockSetup); | ||
|
||
// Instantiate TransferReservationOwnership | ||
target = new TransferReservationOwnership(params, contextMock.getMockInstance()); | ||
}); | ||
|
||
afterEach(() => { | ||
restore(); | ||
}); | ||
|
||
describe('Types', () => { | ||
test('should extend procedure and have TransferReservationOwnership type', async () => { | ||
expect(target instanceof Procedure).toBe(true); | ||
expect(target.type).toBe(ProcedureType.TransferReservationOwnership); | ||
}); | ||
}); | ||
|
||
describe('TransferReservationOwnership', () => { | ||
test('should throw error if token is already launched', async () => { | ||
securityTokenRegistryMock.mock( | ||
'getTickerDetails', | ||
Promise.resolve({ status: true, owner: '0x01' }) | ||
); | ||
|
||
await expect(target.prepareTransactions()).rejects.toThrowError( | ||
new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `The ${ | ||
params.symbol | ||
} Security Token has already been launched, ownership cannot be transferred`, | ||
}) | ||
); | ||
}); | ||
|
||
test('should throw error if current wallet is not the reservation owner', async () => { | ||
await expect(target.prepareTransactions()).rejects.toThrowError( | ||
new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `Only the reservation owner can transfer ownership to another wallet`, | ||
}) | ||
); | ||
}); | ||
|
||
test('should throw error if new owner is equal to the current one', async () => { | ||
contextMock.set('currentWallet', new Wallet({ address: () => Promise.resolve('0x01') })); | ||
|
||
target = new TransferReservationOwnership( | ||
{ ...params, newOwner: '0x01' }, | ||
contextMock.getMockInstance() | ||
); | ||
|
||
await expect(target.prepareTransactions()).rejects.toThrowError( | ||
new PolymathError({ | ||
code: ErrorCode.ProcedureValidationError, | ||
message: `New owner must be different from the current one to transfer ownership`, | ||
}) | ||
); | ||
}); | ||
|
||
test('should add a transaction to the queue to transfer ownership of the reservation', async () => { | ||
contextMock.set('currentWallet', new Wallet({ address: () => Promise.resolve('0x01') })); | ||
|
||
const addTransactionSpy = spy(target, 'addTransaction'); | ||
securityTokenRegistryMock.mock( | ||
'transferTickerOwnership', | ||
Promise.resolve('TransferTickerOwnership') | ||
); | ||
|
||
// Real call | ||
await target.prepareTransactions(); | ||
|
||
// Verifications | ||
expect( | ||
addTransactionSpy | ||
.getCall(0) | ||
.calledWith(securityTokenRegistryMock.getMockInstance().transferTickerOwnership) | ||
).toEqual(true); | ||
expect(addTransactionSpy.getCall(0).lastArg.tag).toEqual( | ||
PolyTransactionTag.TransferReservationOwnership | ||
); | ||
expect(addTransactionSpy.callCount).toEqual(1); | ||
}); | ||
|
||
test('should successfully refresh the security token reservation with the new owner address', async () => { | ||
const refreshStub = securityTokenReservationFactoryMock.mock( | ||
'refresh', | ||
Promise.resolve(undefined) | ||
); | ||
|
||
const resolverValue = await TransferReservationOwnershipModule.createTransferReservationOwnershipResolver( | ||
factoryMockSetup, | ||
params.symbol | ||
)(); | ||
|
||
expect( | ||
refreshStub.getCall(0).calledWithExactly( | ||
SecurityTokenReservation.generateId({ | ||
symbol: params.symbol, | ||
}) | ||
) | ||
).toEqual(true); | ||
expect(resolverValue).toEqual(undefined); | ||
expect(refreshStub.callCount).toEqual(1); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters