From 0ab934dfc462dba9f053f09307e2d2e49fa9e31f Mon Sep 17 00:00:00 2001 From: Marc Velmer Date: Tue, 12 Sep 2023 16:17:56 +0200 Subject: [PATCH] Added `chainID` in tokens for the Census3 service --- src/api/census3/token.ts | 22 ++++++++++++++++++++-- src/census3.ts | 11 +++++++++-- test/census3/api/token.test.ts | 6 +++--- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/api/census3/token.ts b/src/api/census3/token.ts index afae7263..73aecaa2 100644 --- a/src/api/census3/token.ts +++ b/src/api/census3/token.ts @@ -24,6 +24,11 @@ export interface ICensus3Token { */ type: string; + /** + * The chain id of the token. + */ + chainID: number; + /** * The creation block. */ @@ -101,6 +106,11 @@ export interface ICensus3TokenSummary { */ type: string; + /** + * The chain id of the token. + */ + chainID: number; + /** * The creation block. */ @@ -180,13 +190,21 @@ export abstract class Census3TokenAPI extends Census3API { * @param {string} url API endpoint URL * @param {string} id The token address * @param {string} type The type of the token + * @param {number} chainId The chain id of the token * @param {number} startBlock The start block * @param {string[]} tag The tags assigned for the token * @returns {Promise} promised IFileCIDResponse */ - public static create(url: string, id: string, type: string, startBlock: number, tag?: string[]): Promise { + public static create( + url: string, + id: string, + type: string, + chainId: number, + startBlock: number, + tag?: string[] + ): Promise { return axios - .post(url + Census3TokenAPIMethods.CREATE, { id, type, startBlock, tag: tag?.join() }) + .post(url + Census3TokenAPIMethods.CREATE, { id, type, chainID: chainId, startBlock, tag: tag?.join() }) .then((response) => response.data) .catch(this.isApiError); } diff --git a/src/census3.ts b/src/census3.ts index e9e2f93d..d3112623 100644 --- a/src/census3.ts +++ b/src/census3.ts @@ -78,14 +78,21 @@ export class VocdoniCensus3Client { * * @param {string} address The address of the token * @param {string} type The type of the token + * @param {number} chainId The chain id of the token * @param {string} tags The tag list to associate the token with * @param {string} startBlock The start block where to start scanning */ - createToken(address: string, type: string, tags: string[] = [], startBlock: number = 0): Promise { + createToken( + address: string, + type: string, + chainId: number = 1, + tags: string[] = [], + startBlock: number = 0 + ): Promise { invariant(address, 'No token address'); invariant(type, 'No token type'); invariant(isAddress(address), 'Incorrect token address'); - return Census3TokenAPI.create(this.url, address, type, startBlock, tags); + return Census3TokenAPI.create(this.url, address, type, chainId, startBlock, tags); } /** diff --git a/test/census3/api/token.test.ts b/test/census3/api/token.test.ts index 5fdf85bb..bbce74a6 100644 --- a/test/census3/api/token.test.ts +++ b/test/census3/api/token.test.ts @@ -5,15 +5,15 @@ import { Census3TokenAPI, ErrCantGetToken, ErrNotFoundToken, ErrTokenAlreadyExis describe('Census3 token API tests', () => { it('should throw when creating a non existent token', async () => { await expect(async () => { - await Census3TokenAPI.create(URL, '0x0', 'erc20', 0); + await Census3TokenAPI.create(URL, '0x0', 'erc20', 1, 0); }).rejects.toThrow(ErrCantGetToken); }, 5000); it('should throw when creating an already existent token', async () => { try { - await Census3TokenAPI.create(URL, '0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 0); + await Census3TokenAPI.create(URL, '0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 1, 0); } catch (e) {} await expect(async () => { - await Census3TokenAPI.create(URL, '0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 0); + await Census3TokenAPI.create(URL, '0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 1, 0); }).rejects.toThrow(ErrTokenAlreadyExists); }, 5000); it('should throw when fetching a non existent token', async () => {