Skip to content

Commit

Permalink
Merge pull request #129 from kaleido-io/pooldata
Browse files Browse the repository at this point in the history
Encode the values passed in "poolData"
  • Loading branch information
nguyer committed Apr 27, 2023
2 parents 1cb1d6c + 1ff6ab1 commit a989283
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/tokens/tokens.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,9 @@ export class TokenPoolEvent extends tokenEventBase {
@ApiProperty({ type: 'integer' })
decimals: number;

@ApiProperty()
poolData?: string;

@ApiProperty()
info: TokenPoolEventInfo;
}
Expand Down
11 changes: 5 additions & 6 deletions src/tokens/tokens.listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,13 @@ const approvalForAllEventSignature = 'ApprovalForAll(address,address,bool)';
export class TokenListener implements EventListener {
private readonly logger = new Logger(TokenListener.name);

constructor(
private service: TokensService,
private mapper: AbiMapperService,
private blockchain: BlockchainConnectorService,
) {}
constructor(private mapper: AbiMapperService, private blockchain: BlockchainConnectorService) {}

async onEvent(subName: string, event: Event, process: EventProcessor) {
const signature = this.trimEventSignature(event.signature);
switch (signature) {
case tokenCreateEventSignature:
process(await this.transformTokenPoolCreationEvent(event));
process(await this.transformTokenPoolCreationEvent(subName, event));
break;
case transferEventSignature:
process(await this.transformTransferEvent(subName, event));
Expand Down Expand Up @@ -124,10 +120,12 @@ export class TokenListener implements EventListener {
}

private async transformTokenPoolCreationEvent(
subName: string,
event: TokenPoolCreationEvent,
): Promise<WebSocketMessage | undefined> {
const ctx = newContext();
const { data: output } = event;
const unpackedSub = unpackSubscriptionName(subName);
const decodedData = decodeHex(output.data ?? '');

const type = output.is_fungible ? TokenType.FUNGIBLE : TokenType.NONFUNGIBLE;
Expand All @@ -146,6 +144,7 @@ export class TokenListener implements EventListener {
data: <TokenPoolEvent>{
standard: type === TokenType.FUNGIBLE ? 'ERC20' : 'ERC721',
interfaceFormat: InterfaceFormat.ABI,
poolData: unpackedSub.poolData,
poolLocator: packPoolLocator(poolLocator),
type,
signer: event.inputSigner,
Expand Down
4 changes: 4 additions & 0 deletions src/tokens/tokens.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ describe('TokensService', () => {
};

const response: TokenPoolEvent = {
poolData: 'ns1',
poolLocator: ERC20_NO_DATA_POOL_ID,
standard: 'ERC20',
interfaceFormat: InterfaceFormat.ABI,
Expand Down Expand Up @@ -530,6 +531,7 @@ describe('TokensService', () => {
};

const response: TokenPoolEvent = {
poolData: 'ns1',
poolLocator: ERC20_WITH_DATA_POOL_ID,
standard: 'ERC20',
interfaceFormat: InterfaceFormat.ABI,
Expand Down Expand Up @@ -723,6 +725,7 @@ describe('TokensService', () => {
};

const response: TokenPoolEvent = {
poolData: 'ns1',
poolLocator: ERC721_NO_DATA_POOL_ID,
standard: 'ERC721',
interfaceFormat: InterfaceFormat.ABI,
Expand Down Expand Up @@ -931,6 +934,7 @@ describe('TokensService', () => {
};

const response: TokenPoolEvent = {
poolData: 'ns1',
poolLocator: ERC721_WITH_DATA_POOL_ID,
standard: 'ERC721',
interfaceFormat: InterfaceFormat.ABI,
Expand Down
3 changes: 2 additions & 1 deletion src/tokens/tokens.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class TokensService {
this.topic = topic;
this.factoryAddress = factoryAddress.toLowerCase();
this.proxy.addConnectionListener(this);
this.proxy.addEventListener(new TokenListener(this, this.mapper, this.blockchain));
this.proxy.addEventListener(new TokenListener(this.mapper, this.blockchain));
}

async onConnect() {
Expand Down Expand Up @@ -380,6 +380,7 @@ export class TokensService {

const poolInfo = await this.queryPool(ctx, poolLocator);
const tokenPoolEvent: TokenPoolEvent = {
poolData: dto.poolData,
poolLocator: dto.poolLocator,
standard: poolLocator.type === TokenType.FUNGIBLE ? 'ERC20' : 'ERC721',
interfaceFormat: InterfaceFormat.ABI,
Expand Down
8 changes: 8 additions & 0 deletions src/tokens/tokens.util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ describe('Util', () => {
).toEqual(
'fft:address=0x5bb034ca2fd1ac18e46978a7bbdbe4923e158d83&standard=ERC20WithData&type=fungible:mintWithData:ns1',
);
expect(packSubscriptionName('0x123', 'create', 'ns1:test')).toEqual(
'fft:0x123:create:ns1%3Atest',
);
});

it('unpackSubscriptionName', () => {
Expand All @@ -67,6 +70,11 @@ describe('Util', () => {
poolLocator: '0x123456',
event: undefined,
});
expect(unpackSubscriptionName('fft:0x123:create:ns1%3Atest')).toEqual({
poolData: 'ns1:test',
poolLocator: '0x123',
event: 'create',
});
});

it('packPoolLocator', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/tokens/tokens.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function decodeHex(data: string) {

export function packSubscriptionName(poolLocator: string, event: string, poolData?: string) {
if (poolData !== undefined) {
return [SUBSCRIPTION_PREFIX, poolLocator, event, poolData].join(':');
return [SUBSCRIPTION_PREFIX, poolLocator, event, encodeURIComponent(poolData)].join(':');
}
return [SUBSCRIPTION_PREFIX, poolLocator, event].join(':');
}
Expand All @@ -55,7 +55,7 @@ export function unpackSubscriptionName(data: string) {
return {
poolLocator: parts[1],
event: parts[2],
poolData: parts[3],
poolData: decodeURIComponent(parts[3]),
};
} else if (parts.length === 3) {
return {
Expand Down

0 comments on commit a989283

Please sign in to comment.