Skip to content

Commit

Permalink
Updated README and last minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
marcvelmer committed Nov 14, 2023
1 parent f6564cb commit e1d155f
Show file tree
Hide file tree
Showing 9 changed files with 477 additions and 42 deletions.
408 changes: 408 additions & 0 deletions README.md

Large diffs are not rendered by default.

15 changes: 4 additions & 11 deletions src/api/census3/census.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface ICensus3CensusResponse {
/**
* The identifier of the census
*/
censusID: number;
ID: number;

/**
* The identifier of the strategy of the built census
Expand Down Expand Up @@ -70,7 +70,7 @@ export interface ICensus3CensusQueueResponse {
/**
* The string of the error
*/
err: string;
error: string;
};

/**
Expand Down Expand Up @@ -130,24 +130,17 @@ export abstract class Census3CensusAPI extends Census3API {
}

/**
* Requests the creation of a new census with the strategy provided for the blockNumber.
* Requests the creation of a new census with the strategy provided.
*
* @param {string} url API endpoint URL
* @param {number} strategyId The strategy identifier
* @param {boolean} anonymous If the census has to be anonymous
* @param {number} blockNumber The number of the block
* @returns {Promise<ICensus3QueueResponse>} The queue identifier
*/
public static create(
url: string,
strategyId: number,
anonymous: boolean = false,
blockNumber?: number
): Promise<ICensus3QueueResponse> {
public static create(url: string, strategyId: number, anonymous: boolean = false): Promise<ICensus3QueueResponse> {
return axios
.post<ICensus3QueueResponse>(url + Census3CensusAPIMethods.CREATE, {
strategyID: strategyId,
blockNumber,
anonymous,
})
.then((response) => response.data)
Expand Down
6 changes: 3 additions & 3 deletions src/api/census3/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ enum Census3StrategyAPIMethods {
STRATEGY = '/strategies/{id}',
SIZE = '/strategies/{id}/size',
SIZE_QUEUE = '/strategies/{id}/size/queue/{queueId}',
VALIDATE_PREDICATE = '/strategies/predicate/parse',
VALIDATE_PREDICATE = '/strategies/predicate/validate',
OPERATORS = '/strategies/predicate/operators',
}

Expand Down Expand Up @@ -101,7 +101,7 @@ export interface ICensus3StrategySizeQueueResponse {
/**
* The string of the error
*/
err: string;
error: string;
};

/**
Expand All @@ -128,7 +128,7 @@ export interface ICensus3StrategyImportQueueResponse {
/**
* The string of the error
*/
err: string;
error: string;
};

/**
Expand Down
12 changes: 2 additions & 10 deletions src/api/census3/token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,11 @@ export type Census3Token = {
};
};

export type Census3TokenSummary = Pick<
Census3Token,
'ID' | 'name' | 'type' | 'startBlock' | 'symbol' | 'tags' | 'chainID' | 'externalID' | 'chainAddress' | 'status'
>;

export interface ICensus3TokenListResponse {
/**
* The list of the tokens
*/
tokens: Array<Census3TokenSummary>;
tokens: Array<Census3Token>;
}

export interface ICensus3TokenListResponsePaginated extends ICensus3TokenListResponse {
Expand Down Expand Up @@ -207,13 +202,12 @@ export abstract class Census3TokenAPI extends Census3API {
}

/**
* Triggers a new scan for the provided token, starting from the defined block.
* Triggers a new scan for the provided token.
*
* @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[]} tags The tags assigned for the token
* @param {string} externalId The identifier used by external provider
* @returns {Promise<IFileCIDResponse>} promised IFileCIDResponse
Expand All @@ -223,7 +217,6 @@ export abstract class Census3TokenAPI extends Census3API {
id: string,
type: string,
chainId: number,
startBlock: number,
tags?: string,
externalId?: string
): Promise<void> {
Expand All @@ -232,7 +225,6 @@ export abstract class Census3TokenAPI extends Census3API {
ID: id,
type,
chainID: chainId,
startBlock,
tags,
externalID: externalId,
})
Expand Down
27 changes: 14 additions & 13 deletions src/census3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
ICensus3CensusResponse,
ICensus3SupportedChain,
Census3Token,
Census3TokenSummary,
Census3Strategy,
Census3CreateStrategyToken,
ICensus3ValidatePredicateResponse,
Expand All @@ -20,7 +19,6 @@ import { TokenCensus } from './types';
import { delay } from './util/common';

export type Token = Omit<Census3Token, 'tags'> & { tags: string[] };
export type TokenSummary = Census3TokenSummary;
export type Strategy = Census3Strategy;
export type StrategyToken = Census3CreateStrategyToken;
export type Census3Census = ICensus3CensusResponse;
Expand Down Expand Up @@ -50,12 +48,18 @@ export class VocdoniCensus3Client {
}

/**
* Returns a list of summarized tokens supported by the service
* Returns a list of tokens supported by the service
*
* @returns {Promise<TokenSummary[]>} Token summary list
* @returns {Promise<Token[]>} Token list
*/
getSupportedTokens(): Promise<TokenSummary[]> {
return Census3TokenAPI.list(this.url, { pageSize: -1 }).then((list) => list.tokens ?? []);
getSupportedTokens(): Promise<Token[]> {
return Census3TokenAPI.list(this.url, { pageSize: -1 }).then(
(list) =>
list?.tokens?.map((token) => ({
...token,
tags: token.tags?.split(',') ?? [],
})) ?? []
);
}

/**
Expand Down Expand Up @@ -126,20 +130,18 @@ export class VocdoniCensus3Client {
* @param {number} chainId The chain id of the token
* @param {string} externalId The identifier used by external provider
* @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,
chainId: number = 1,
externalId: string = '',
tags: string[] = [],
startBlock: number = 0
tags: string[] = []
): 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, chainId, startBlock, tags?.join(), externalId);
return Census3TokenAPI.create(this.url, address, type, chainId, tags?.join(), externalId);
}

/**
Expand Down Expand Up @@ -300,10 +302,9 @@ export class VocdoniCensus3Client {
*
* @param {number} strategyId The id of the strategy
* @param {boolean} anonymous If the census has to be anonymous
* @param {number} blockNumber The block number
* @returns {Promise<Census3Census>} The census information
*/
createCensus(strategyId: number, anonymous: boolean = false, blockNumber?: number): Promise<Census3Census> {
createCensus(strategyId: number, anonymous: boolean = false): Promise<Census3Census> {
invariant(strategyId || strategyId >= 0, 'No strategy id');

const waitForQueue = (queueId: string, wait?: number, attempts?: number): Promise<Census3Census> => {
Expand All @@ -326,7 +327,7 @@ export class VocdoniCensus3Client {
});
};

return Census3CensusAPI.create(this.url, strategyId, anonymous, blockNumber)
return Census3CensusAPI.create(this.url, strategyId, anonymous)
.then((createCensus) => createCensus.queueID)
.then((queueId) => waitForQueue(queueId));
}
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', 1, 0);
await Census3TokenAPI.create(URL, '0x0', 'erc20', 1);
}).rejects.toThrow(ErrCantGetToken);
}, 5000);
it('should throw when creating an already existent token', async () => {
try {
await Census3TokenAPI.create(URL, '0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 1, 0);
await Census3TokenAPI.create(URL, '0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 1);
} catch (e) {}
await expect(async () => {
await Census3TokenAPI.create(URL, '0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 1, 0);
await Census3TokenAPI.create(URL, '0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 1);
}).rejects.toThrow(ErrTokenAlreadyExists);
}, 15000);
it('should throw when fetching a non existent token', async () => {
Expand Down
41 changes: 41 additions & 0 deletions test/census3/integration/census.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { EnvOptions, VocdoniCensus3Client } from '../../../src';

describe('Census3 censuses integration tests', () => {
it('should return the supported censuses information', async () => {
const client = new VocdoniCensus3Client({ env: EnvOptions.DEV });
const strategies = await client.getStrategies();
if (strategies.length > 0) {
const censuses = await client.getCensuses(strategies[0].ID);
censuses.forEach((census) => {
expect(census).toMatchObject({
ID: expect.any(Number),
strategyID: expect.any(Number),
merkleRoot: expect.any(String),
uri: expect.any(String),
size: expect.any(Number),
weight: expect.any(String),
anonymous: expect.any(Boolean),
});
});
}
}, 15000);
it('should return the census information', async () => {
const client = new VocdoniCensus3Client({ env: EnvOptions.DEV });
const strategies = await client.getStrategies();
if (strategies.length > 0) {
const censuses = await client.getCensuses(strategies[0].ID);
if (censuses.length > 0) {
const census = await client.getCensus(censuses[0].ID);
expect(census).toMatchObject({
ID: expect.any(Number),
strategyID: expect.any(Number),
merkleRoot: expect.any(String),
uri: expect.any(String),
size: expect.any(Number),
weight: expect.any(String),
anonymous: expect.any(Boolean),
});
}
}
}, 15000);
});
File renamed without changes.
4 changes: 2 additions & 2 deletions test/census3/integration/token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('Census3 token integration tests', () => {
it('should create the given token in the census3 service', async () => {
const client = new VocdoniCensus3Client({ env: EnvOptions.DEV });
try {
await client.createToken('0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 1, null, ['test', 'test2'], 0);
await client.createToken('0xa117000000f279d81a1d3cc75430faa017fa5a2e', 'erc20', 1, null, ['test', 'test2']);
} catch (e) {}
const token = await client.getToken('0xa117000000f279d81a1d3cc75430faa017fa5a2e', 1);
expect(token).toMatchObject({
Expand All @@ -66,7 +66,7 @@ describe('Census3 token integration tests', () => {
chainAddress: expect.any(String),
tags: expect.any(Array),
});
}, 5000);
}, 25000);
it('should check if the given holder in a token exists', async () => {
const client = new VocdoniCensus3Client({ env: EnvOptions.DEV });
const supportedTokens = await client.getSupportedTokens();
Expand Down

0 comments on commit e1d155f

Please sign in to comment.