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(checkpoints): add
Checkpoints.get
and create Checkpoint entity
Added `totalSupply` and `timestamp` to Checkpoint
- Loading branch information
Showing
9 changed files
with
389 additions
and
18 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
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,75 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { Context, Entity } from '~/internal'; | ||
import { balanceToBigNumber, momentToDate, numberToU64, stringToTicker } from '~/utils/conversion'; | ||
|
||
export interface UniqueIdentifiers { | ||
id: BigNumber; | ||
ticker: string; | ||
} | ||
|
||
/** | ||
* Represents a snapshot of the Security Token's holders and their respective balances | ||
* at a certain point in time | ||
*/ | ||
export class Checkpoint extends Entity<UniqueIdentifiers> { | ||
/** | ||
* @hidden | ||
* Check if a value is of type [[UniqueIdentifiers]] | ||
*/ | ||
public static isUniqueIdentifiers(identifier: unknown): identifier is UniqueIdentifiers { | ||
const { id, ticker } = identifier as UniqueIdentifiers; | ||
|
||
return id instanceof BigNumber && typeof ticker === 'string'; | ||
} | ||
|
||
/** | ||
* checkpoint identifier number | ||
*/ | ||
public id: BigNumber; | ||
|
||
/** | ||
* ticker of the Security Token whose balances are being recorded | ||
*/ | ||
public ticker: string; | ||
|
||
/** | ||
* @hidden | ||
*/ | ||
public constructor(identifiers: UniqueIdentifiers, context: Context) { | ||
super(identifiers, context); | ||
|
||
const { id, ticker } = identifiers; | ||
|
||
this.id = id; | ||
this.ticker = ticker; | ||
} | ||
|
||
/** | ||
* Retrieve the Security Token's total supply at this checkpoint | ||
*/ | ||
public async totalSupply(): Promise<BigNumber> { | ||
const { context, ticker, id } = this; | ||
|
||
const rawSupply = await context.polymeshApi.query.checkpoint.totalSupply( | ||
stringToTicker(ticker, context), | ||
numberToU64(id, context) | ||
); | ||
|
||
return balanceToBigNumber(rawSupply); | ||
} | ||
|
||
/** | ||
* Retrieve this Checkpoint's creation date | ||
*/ | ||
public async createdAt(): Promise<Date> { | ||
const { context, ticker, id } = this; | ||
|
||
const creationTime = await context.polymeshApi.query.checkpoint.timestamps( | ||
stringToTicker(ticker, context), | ||
numberToU64(id, context) | ||
); | ||
|
||
return momentToDate(creationTime); | ||
} | ||
} |
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
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,82 @@ | ||
import BigNumber from 'bignumber.js'; | ||
|
||
import { Checkpoint, Context, Entity } from '~/internal'; | ||
import { dsMockUtils } from '~/testUtils/mocks'; | ||
|
||
describe('Checkpoint class', () => { | ||
let context: Context; | ||
|
||
let id: BigNumber; | ||
let ticker: string; | ||
|
||
beforeAll(() => { | ||
dsMockUtils.initMocks(); | ||
|
||
id = new BigNumber(1); | ||
ticker = 'SOME_TICKER'; | ||
}); | ||
|
||
beforeEach(() => { | ||
context = dsMockUtils.getContextInstance(); | ||
}); | ||
|
||
afterEach(() => { | ||
dsMockUtils.reset(); | ||
}); | ||
|
||
afterAll(() => { | ||
dsMockUtils.cleanup(); | ||
}); | ||
|
||
test('should extend entity', () => { | ||
expect(Checkpoint.prototype instanceof Entity).toBe(true); | ||
}); | ||
|
||
describe('constructor', () => { | ||
test('should assign ticker and id to instance', () => { | ||
const checkpoint = new Checkpoint({ id, ticker }, context); | ||
|
||
expect(checkpoint.ticker).toBe(ticker); | ||
expect(checkpoint.id).toEqual(id); | ||
}); | ||
}); | ||
|
||
describe('method: isUniqueIdentifiers', () => { | ||
test('should return true if the object conforms to the interface', () => { | ||
expect(Checkpoint.isUniqueIdentifiers({ id: new BigNumber(1), ticker: 'symbol' })).toBe(true); | ||
expect(Checkpoint.isUniqueIdentifiers({})).toBe(false); | ||
expect(Checkpoint.isUniqueIdentifiers({ id: new BigNumber(1) })).toBe(false); | ||
expect(Checkpoint.isUniqueIdentifiers({ id: 'id' })).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('method: createdAt', () => { | ||
test("should return the Checkpoint's creation date", async () => { | ||
const checkpoint = new Checkpoint({ id, ticker }, context); | ||
const timestamp = 12000; | ||
|
||
dsMockUtils.createQueryStub('checkpoint', 'timestamps', { | ||
returnValue: dsMockUtils.createMockMoment(timestamp), | ||
}); | ||
|
||
const result = await checkpoint.createdAt(); | ||
|
||
expect(result).toEqual(new Date(timestamp)); | ||
}); | ||
}); | ||
|
||
describe('method: totalSupply', () => { | ||
test("should return the Checkpoint's total supply", async () => { | ||
const checkpoint = new Checkpoint({ id, ticker }, context); | ||
const balance = 10000000000; | ||
|
||
dsMockUtils.createQueryStub('checkpoint', 'totalSupply', { | ||
returnValue: dsMockUtils.createMockBalance(balance), | ||
}); | ||
|
||
const result = await checkpoint.totalSupply(); | ||
|
||
expect(result).toEqual(new BigNumber(balance).shiftedBy(-6)); | ||
}); | ||
}); | ||
}); |
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.