Skip to content

Commit 01a28af

Browse files
committed
Support "config.factoryAddress" to create tokens using a custom factory
Signed-off-by: Andrew Richardson <andrew.richardson@kaleido.io>
1 parent c101c90 commit 01a28af

5 files changed

Lines changed: 49 additions & 44 deletions

File tree

.eslintrc.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ module.exports = {
44
project: 'tsconfig.json',
55
sourceType: 'module',
66
},
7-
plugins: [
8-
'@typescript-eslint/eslint-plugin',
9-
'eslint-plugin-import',
10-
],
7+
plugins: ['@typescript-eslint/eslint-plugin', 'eslint-plugin-import'],
118
extends: [
129
'eslint:recommended',
1310
'plugin:@typescript-eslint/recommended',
@@ -27,22 +24,22 @@ module.exports = {
2724
'@typescript-eslint/no-explicit-any': 'off',
2825
'@typescript-eslint/no-floating-promises': 'error',
2926
'@typescript-eslint/require-await': 'error',
30-
'@typescript-eslint/strict-boolean-expressions': 'error',
3127
'@typescript-eslint/unbound-method': 'error',
3228
'@typescript-eslint/no-unsafe-call': 'warn',
3329
'@typescript-eslint/no-unsafe-return': 'warn',
3430
'@typescript-eslint/ban-types': 'warn',
3531
'@typescript-eslint/no-unused-vars': [
36-
'warn', {
37-
'varsIgnorePattern': '^_',
38-
'argsIgnorePattern': '^_',
32+
'warn',
33+
{
34+
varsIgnorePattern: '^_',
35+
argsIgnorePattern: '^_',
3936
},
4037
],
4138
'import/first': 'warn',
4239
'import/no-duplicates': 'warn',
4340
'import/order': 'warn',
44-
'semi': ['warn', 'always'],
45-
'eqeqeq': 'error',
41+
semi: ['warn', 'always'],
42+
eqeqeq: 'error',
4643
'eol-last': 'warn',
4744
'no-trailing-spaces': 'warn',
4845
},

src/tokens/tokens.interfaces.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ export class TokenPoolConfig {
125125
@IsOptional()
126126
address?: string;
127127

128+
@ApiProperty()
129+
@IsOptional()
130+
factoryAddress?: string;
131+
128132
@ApiProperty()
129133
@IsOptional()
130134
blockNumber?: string;

src/tokens/tokens.listener.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,6 @@ export class TokenListener implements EventListener {
130130
const { data: output } = event;
131131
const decodedData = decodeHex(output.data ?? '');
132132

133-
if (event.address.toLowerCase() !== this.service.factoryAddress) {
134-
this.logger.warn(`Ignoring token pool creation from unknown address: ${event.address}`);
135-
return undefined;
136-
}
137-
138133
const type = output.is_fungible ? TokenType.FUNGIBLE : TokenType.NONFUNGIBLE;
139134
const decimals = output.is_fungible
140135
? await this.mapper.getDecimals(ctx, output.contract_address)

src/tokens/tokens.service.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ const APPROVE_ALL_NO_DATA = 'setApprovalForAll';
8383

8484
const MINT_WITH_DATA = 'mintWithData';
8585
const MINT_WITH_URI = 'mintWithURI';
86-
const SUPPORTS_INTERFACE = 'supportsInterface';
8786
const TRANSFER_WITH_DATA = 'transferWithData';
8887
const BURN_WITH_DATA = 'burnWithData';
8988
const APPROVE_WITH_DATA = 'approveWithData';

src/tokens/tokens.service.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,24 @@ export class TokensService {
100100
async init(ctx: Context) {
101101
this.stream = await this.getStream(ctx);
102102
if (this.factoryAddress !== '') {
103-
const eventABI = this.mapper.getCreateEvent();
104-
const methodABI = this.mapper.getCreateMethod();
105-
if (eventABI !== undefined && methodABI !== undefined) {
106-
await this.eventstream.getOrCreateSubscription(
107-
ctx,
108-
this.baseUrl,
109-
eventABI,
110-
this.stream.id,
111-
packSubscriptionName(this.factoryAddress, eventABI.name),
112-
this.factoryAddress,
113-
[methodABI],
114-
'0',
115-
);
116-
}
103+
await this.createFactorySubscription(ctx, this.factoryAddress);
104+
}
105+
}
106+
107+
private async createFactorySubscription(ctx: Context, address: string) {
108+
const eventABI = this.mapper.getCreateEvent();
109+
const methodABI = this.mapper.getCreateMethod();
110+
if (eventABI !== undefined && methodABI !== undefined) {
111+
await this.eventstream.getOrCreateSubscription(
112+
ctx,
113+
this.baseUrl,
114+
eventABI,
115+
this.stream.id,
116+
packSubscriptionName(address, eventABI.name),
117+
address,
118+
[methodABI],
119+
'0',
120+
);
117121
}
118122
}
119123

@@ -148,6 +152,7 @@ export class TokensService {
148152
return false;
149153
}
150154

155+
const createABI = this.mapper.getCreateEvent();
151156
const foundEvents = new Map<string, string[]>();
152157
for (const sub of subscriptions) {
153158
const parts = unpackSubscriptionName(sub.name);
@@ -158,7 +163,8 @@ export class TokensService {
158163
);
159164
return true;
160165
}
161-
if (parts.poolLocator === this.factoryAddress) {
166+
if (parts.event === createABI?.name) {
167+
// Skip "create" subscriptions (assume they are factories)
162168
continue;
163169
}
164170
const key = packSubscriptionName(parts.poolLocator, '', parts.poolData);
@@ -230,17 +236,20 @@ export class TokensService {
230236
}
231237

232238
async createPool(ctx: Context, dto: TokenPool): Promise<TokenPoolEvent | AsyncResponse> {
233-
if (dto.config?.address !== undefined && dto.config.address !== '') {
234-
this.logger.log(`Create token pool from existing: '${dto.config.address}'`);
235-
return this.createFromExisting(ctx, dto.config.address, dto);
239+
const contractAddress = dto.config?.address || undefined;
240+
if (contractAddress !== undefined) {
241+
this.logger.log(`Create token pool from existing: '${contractAddress}'`);
242+
return this.createFromExisting(ctx, contractAddress, dto);
236243
}
237-
if (this.factoryAddress === '') {
244+
245+
const factoryAddress = dto.config?.factoryAddress || this.factoryAddress || undefined;
246+
if (factoryAddress === undefined) {
238247
throw new BadRequestException(
239248
'config.address was unspecified, and no token factory is configured!',
240249
);
241250
}
242-
this.logger.log(`Create token pool from factory: '${this.factoryAddress}'`);
243-
return this.createFromFactory(ctx, dto);
251+
this.logger.log(`Create token pool from factory: '${factoryAddress}'`);
252+
return this.createFromFactory(ctx, factoryAddress, dto);
244253
}
245254

246255
private async createFromExisting(
@@ -281,16 +290,17 @@ export class TokensService {
281290
};
282291
}
283292

284-
private async createFromFactory(ctx: Context, dto: TokenPool): Promise<AsyncResponse> {
285-
const { method, params } = await this.mapper.getCreateMethodAndParams(
286-
ctx,
287-
this.factoryAddress,
288-
dto,
289-
);
293+
private async createFromFactory(
294+
ctx: Context,
295+
address: string,
296+
dto: TokenPool,
297+
): Promise<AsyncResponse> {
298+
await this.createFactorySubscription(ctx, address);
299+
const { method, params } = await this.mapper.getCreateMethodAndParams(ctx, address, dto);
290300
const response = await this.blockchain.sendTransaction(
291301
ctx,
292302
dto.signer,
293-
this.factoryAddress,
303+
address,
294304
dto.requestId,
295305
method,
296306
params,

0 commit comments

Comments
 (0)