generated from PolymeshAssociation/typescript-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transfer restrictions): add logic to fetch transfer restrictions
- Loading branch information
Showing
12 changed files
with
406 additions
and
184 deletions.
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
50 changes: 17 additions & 33 deletions
50
src/api/entities/SecurityToken/TransferRestrictions/Percentage.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 |
---|---|---|
@@ -1,49 +1,33 @@ | ||
import { | ||
AddPercentageTransferRestrictionParams, | ||
addTransferRestriction, | ||
AddTransferRestrictionParams, | ||
Context, | ||
Namespace, | ||
SecurityToken, | ||
} from '~/internal'; | ||
import { TransferRestrictionBase } from '~/api/entities/SecurityToken/TransferRestrictions/TransferRestrictionBase'; | ||
import { AddPercentageTransferRestrictionParams } from '~/internal'; | ||
import { ActiveTransferRestrictions, PercentageTransferRestriction } from '~/types'; | ||
import { ProcedureMethod, TransferRestrictionType } from '~/types/internal'; | ||
import { createProcedureMethod } from '~/utils/internal'; | ||
|
||
/** | ||
* Handles all Percentage Transfer Restriction related functionality | ||
*/ | ||
export class Percentage extends Namespace<SecurityToken> { | ||
/** | ||
* @hidden | ||
*/ | ||
constructor(parent: SecurityToken, context: Context) { | ||
super(parent, context); | ||
|
||
const { ticker } = parent; | ||
|
||
this.addRestriction = createProcedureMethod< | ||
Omit<AddPercentageTransferRestrictionParams, 'type'>, | ||
AddTransferRestrictionParams, | ||
number | ||
>( | ||
args => [ | ||
addTransferRestriction, | ||
{ ...args, type: TransferRestrictionType.Percentage, ticker }, | ||
], | ||
context | ||
); | ||
} | ||
export class Percentage extends TransferRestrictionBase<TransferRestrictionType.Percentage> { | ||
protected type = TransferRestrictionType.Percentage as const; | ||
|
||
/** | ||
* Add a Percentage Transfer Restriction to this Security Token | ||
* | ||
* @param args.percentage - limit on the percentage of the total supply of this Security Token that a single (unique) investor can hold at once | ||
* @param args.percentage - limit on the proportion of the total supply of this Security Token that can be held by a single investor at once | ||
* @param args.exempted - array of Scope IDs that are exempted from the Restriction | ||
* | ||
* * @note the result is the total amount of restrictions after the procedure has run | ||
* @note the result is the total amount of restrictions after the procedure has run | ||
*/ | ||
public addRestriction: ProcedureMethod< | ||
public addRestriction!: ProcedureMethod< | ||
Omit<AddPercentageTransferRestrictionParams, 'type'>, | ||
number | ||
>; | ||
|
||
/** | ||
* Retrieve all active Percentage Transfer Restrictions | ||
* | ||
* @note there is a maximum number of restrictions allowed accross all types. | ||
* The `availableSlots` property of the result represents how many more restrictions can be added | ||
* before reaching that limit | ||
*/ | ||
public get!: () => Promise<ActiveTransferRestrictions<PercentageTransferRestriction>>; | ||
} |
130 changes: 130 additions & 0 deletions
130
src/api/entities/SecurityToken/TransferRestrictions/TransferRestrictionBase.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,130 @@ | ||
import { AddPercentageTransferRestrictionParams } from '~/api/procedures/addTransferRestriction'; | ||
import { | ||
AddCountTransferRestrictionParams, | ||
addTransferRestriction, | ||
AddTransferRestrictionParams, | ||
Context, | ||
Namespace, | ||
SecurityToken, | ||
} from '~/internal'; | ||
import { | ||
ActiveTransferRestrictions, | ||
CountTransferRestriction, | ||
PercentageTransferRestriction, | ||
} from '~/types'; | ||
import { ProcedureMethod, TransferRestrictionType } from '~/types/internal'; | ||
import { MAX_TRANSFER_MANAGERS } from '~/utils/constants'; | ||
import { | ||
scopeIdToString, | ||
stringToTicker, | ||
transferManagerToTransferRestriction, | ||
} from '~/utils/conversion'; | ||
import { createProcedureMethod } from '~/utils/internal'; | ||
|
||
type AddRestrictionParams<T> = Omit< | ||
T extends TransferRestrictionType.Count | ||
? AddCountTransferRestrictionParams | ||
: AddPercentageTransferRestrictionParams, | ||
'type' | ||
>; | ||
|
||
type GetReturnType<T> = ActiveTransferRestrictions< | ||
T extends TransferRestrictionType.Count ? CountTransferRestriction : PercentageTransferRestriction | ||
>; | ||
|
||
/** | ||
* Base class for managing Transfer Restrictions | ||
*/ | ||
export abstract class TransferRestrictionBase< | ||
T extends TransferRestrictionType | ||
> extends Namespace<SecurityToken> { | ||
protected abstract type: T; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
constructor(parent: SecurityToken, context: Context) { | ||
super(parent, context); | ||
|
||
const { ticker } = parent; | ||
|
||
this.addRestriction = createProcedureMethod< | ||
AddRestrictionParams<T>, | ||
AddTransferRestrictionParams, | ||
number | ||
>( | ||
args => [ | ||
addTransferRestriction, | ||
({ ...args, type: this.type, ticker } as unknown) as AddTransferRestrictionParams, | ||
], | ||
context | ||
); | ||
} | ||
|
||
/** | ||
* Add a Transfer Restriction of the corresponding type to this Security Token | ||
* | ||
* @param args.exempted - array of Scope IDs that are exempted from the Restriction | ||
* | ||
* @note the result is the total amount of restrictions after the procedure has run | ||
*/ | ||
public addRestriction: ProcedureMethod<AddRestrictionParams<T>, number>; | ||
|
||
/** | ||
* Retrieve all active Transfer Restrictions of the corresponding type | ||
* | ||
* @note there is a maximum number of restrictions allowed accross all types. | ||
* The `availableSlots` property of the result represents how many more restrictions can be added | ||
* before reaching that limit | ||
*/ | ||
public async get(): Promise<GetReturnType<T>> { | ||
const { | ||
parent: { ticker }, | ||
context: { | ||
polymeshApi: { | ||
query: { statistics }, | ||
}, | ||
}, | ||
context, | ||
} = this; | ||
|
||
const rawTicker = stringToTicker(ticker, context); | ||
const activeTms = await statistics.activeTransferManagers(rawTicker); | ||
const filteredTms = activeTms.filter(tm => { | ||
if (this.type === TransferRestrictionType.Count) { | ||
return tm.isCountTransferManager; | ||
} | ||
|
||
return tm.isPercentageTransferManager; | ||
}); | ||
|
||
const rawExemptedLists = await Promise.all( | ||
filteredTms.map(tm => statistics.exemptEntities.entries([rawTicker, tm])) | ||
); | ||
|
||
const restrictions = rawExemptedLists.map((list, index) => { | ||
const exempted = list.map(([{ args: [, scopeId] }]) => scopeIdToString(scopeId)); | ||
const { value } = transferManagerToTransferRestriction(filteredTms[index]); | ||
let restriction; | ||
|
||
if (this.type === TransferRestrictionType.Count) { | ||
restriction = { | ||
count: value, | ||
}; | ||
} else { | ||
restriction = { | ||
percentage: value, | ||
}; | ||
} | ||
return { | ||
...restriction, | ||
exempted, | ||
}; | ||
}); | ||
|
||
return { | ||
restrictions, | ||
availableSlots: MAX_TRANSFER_MANAGERS - activeTms.length, | ||
} as GetReturnType<T>; | ||
} | ||
} |
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
Oops, something went wrong.