Skip to content

Commit

Permalink
Added chainID in tokens for the Census3 service
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Sep 12, 2023
1 parent a41db87 commit 0ab934d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
22 changes: 20 additions & 2 deletions src/api/census3/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export interface ICensus3Token {
*/
type: string;

/**
* The chain id of the token.
*/
chainID: number;

/**
* The creation block.
*/
Expand Down Expand Up @@ -101,6 +106,11 @@ export interface ICensus3TokenSummary {
*/
type: string;

/**
* The chain id of the token.
*/
chainID: number;

/**
* The creation block.
*/
Expand Down Expand Up @@ -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<IFileCIDResponse>} promised IFileCIDResponse
*/
public static create(url: string, id: string, type: string, startBlock: number, tag?: string[]): Promise<void> {
public static create(
url: string,
id: string,
type: string,
chainId: number,
startBlock: number,
tag?: string[]
): Promise<void> {
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);
}
Expand Down
11 changes: 9 additions & 2 deletions src/census3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
createToken(
address: string,
type: string,
chainId: number = 1,
tags: string[] = [],
startBlock: number = 0
): Promise<void> {
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);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/census3/api/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down

0 comments on commit 0ab934d

Please sign in to comment.