Skip to content

Commit

Permalink
Merge branch 'feat/ibc_app_ics20' of github.com:aura-nw/horoscope-v2 …
Browse files Browse the repository at this point in the history
…into feat/ibc_app_ics20_dev
  • Loading branch information
phamphong9981 committed Aug 29, 2023
2 parents a61ca2b + 0c7e603 commit 4586cf5
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 35 deletions.
2 changes: 1 addition & 1 deletion migrations/20230823070516_ics20_model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function up(knex: Knex): Promise<void> {
table.string('receiver').index().notNullable();
table.decimal('amount', 80, 0).notNullable();
table.string('denom').notNullable().index();
table.boolean('ack_status');
table.boolean('status').defaultTo(true);
table.foreign('ibc_message_id').references('ibc_message.id');
});
}
Expand Down
7 changes: 7 additions & 0 deletions src/models/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ export class Event extends BaseModel {
};
}

getAttributesFrom(attributesType: string[]) {
return attributesType.map(
(attributeType) =>
this.attributes?.find((attr: any) => attr.key === attributeType)?.value
);
}

static EVENT_TYPE = {
STORE_CODE: 'store_code',
SUBMIT_PROPOSAL: 'submit_proposal',
Expand Down
2 changes: 1 addition & 1 deletion src/models/ibc_ics20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class IbcIcs20 extends BaseModel {

denom!: string;

ack_status!: boolean;
status!: boolean;

ibc_message!: IbcMessage;

Expand Down
5 changes: 5 additions & 0 deletions src/models/ibc_message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Model } from 'objection';
import BaseModel from './base';
import { IbcChannel } from './ibc_channel';
import { TransactionMessage } from './transaction_message';
import config from '../../config.json' assert { type: 'json' };

export class IbcMessage extends BaseModel {
id!: number;
Expand Down Expand Up @@ -92,4 +93,8 @@ export class IbcMessage extends BaseModel {
ACKNOWLEDGE_PACKET: 'acknowledge_packet',
TIMEOUT_PACKET: 'timeout_packet',
};

static PORTS = {
ICS20: config.crawlIbcIcs20.port,
};
}
44 changes: 22 additions & 22 deletions src/services/ibc/crawl_ibc_ics20.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ import {
IbcIcs20,
IbcMessage,
} from '../../models';
import { getAttributesFrom } from '../../common/utils/utils';

const PORT = config.crawlIbcIcs20.port;
@Service({
name: SERVICE.V1.CrawlIBCIcs20Service.key,
version: 1,
Expand Down Expand Up @@ -59,7 +57,7 @@ export default class CrawlIBCIcs20Service extends BullableService {
) {
const ics20Sends = await IbcMessage.query()
.joinRelated('message.transaction')
.where('src_port_id', PORT)
.where('src_port_id', IbcMessage.PORTS.ICS20)
.andWhere('ibc_message.type', IbcMessage.EVENT_TYPE.SEND_PACKET)
.andWhere('message:transaction.height', '>', startHeight)
.andWhere('message:transaction.height', '<=', endHeight)
Expand Down Expand Up @@ -91,7 +89,7 @@ export default class CrawlIBCIcs20Service extends BullableService {
.orWhere('type', IbcIcs20.EVENT_TYPE.DENOM_TRACE);
},
})
.where('dst_port_id', PORT)
.where('dst_port_id', IbcMessage.PORTS.ICS20)
.andWhere('ibc_message.type', IbcMessage.EVENT_TYPE.RECV_PACKET)
.andWhere('message:transaction.height', '>', startHeight)
.andWhere('message:transaction.height', '<=', endHeight)
Expand All @@ -106,13 +104,16 @@ export default class CrawlIBCIcs20Service extends BullableService {
throw Error(`Recv ibc hasn't emmitted events: ${msg.id}`);
}
const [sender, receiver, amount, originalDenom, ackStatus] =
getAttributesFrom(recvEvent.attributes, [
recvEvent.getAttributesFrom([
EventAttribute.ATTRIBUTE_KEY.SENDER,
EventAttribute.ATTRIBUTE_KEY.RECEIVER,
EventAttribute.ATTRIBUTE_KEY.AMOUNT,
EventAttribute.ATTRIBUTE_KEY.DENOM,
EventAttribute.ATTRIBUTE_KEY.SUCCESS,
]);
if (originalDenom === undefined) {
throw Error(`Recv ibc hasn't emit denom: ${msg.id}`);
}
const denomTraceEvent = msg.message.events.find(
(e) => e.type === IbcIcs20.EVENT_TYPE.DENOM_TRACE
);
Expand All @@ -128,7 +129,7 @@ export default class CrawlIBCIcs20Service extends BullableService {
receiver,
amount,
denom,
ack_status: ackStatus === 'true',
status: ackStatus === 'true',
});
});
await IbcIcs20.query().insert(ibcIcs20s).transacting(trx);
Expand All @@ -148,7 +149,7 @@ export default class CrawlIBCIcs20Service extends BullableService {
builder.where('type', IbcIcs20.EVENT_TYPE.FUNGIBLE_TOKEN_PACKET);
},
})
.where('src_port_id', PORT)
.where('src_port_id', IbcMessage.PORTS.ICS20)
.andWhere('ibc_message.type', IbcMessage.EVENT_TYPE.ACKNOWLEDGE_PACKET)
.andWhere('message:transaction.height', '>', startHeight)
.andWhere('message:transaction.height', '<=', endHeight)
Expand All @@ -160,23 +161,25 @@ export default class CrawlIBCIcs20Service extends BullableService {
if (ackEvents.length !== 2) {
throw Error(`Ack ibc hasn't emmitted enough events: ${msg.id}`);
}
const [sender, receiver, amount, denom, success] = getAttributesFrom(
[...ackEvents[0].attributes, ...ackEvents[1].attributes],
[
const [sender, receiver, amount, denom, success] = [
...ackEvents[0].getAttributesFrom([
EventAttribute.ATTRIBUTE_KEY.SENDER,
EventAttribute.ATTRIBUTE_KEY.RECEIVER,
EventAttribute.ATTRIBUTE_KEY.AMOUNT,
EventAttribute.ATTRIBUTE_KEY.DENOM,
]),
...ackEvents[1].getAttributesFrom([
EventAttribute.ATTRIBUTE_KEY.SUCCESS,
]
);
]),
];

return IbcIcs20.fromJson({
ibc_message_id: msg.id,
sender,
receiver,
amount,
denom,
ack_status: success !== undefined,
status: success !== undefined,
});
});
await IbcIcs20.query().insert(ibcIcs20s).transacting(trx);
Expand All @@ -196,7 +199,7 @@ export default class CrawlIBCIcs20Service extends BullableService {
builder.where('type', IbcIcs20.EVENT_TYPE.TIMEOUT);
},
})
.where('src_port_id', PORT)
.where('src_port_id', IbcMessage.PORTS.ICS20)
.andWhere('ibc_message.type', IbcMessage.EVENT_TYPE.TIMEOUT_PACKET)
.andWhere('message:transaction.height', '>', startHeight)
.andWhere('message:transaction.height', '<=', endHeight)
Expand All @@ -205,14 +208,11 @@ export default class CrawlIBCIcs20Service extends BullableService {
if (ics20Timeouts.length > 0) {
const ibcIcs20s = ics20Timeouts.map((msg) => {
const timeoutEvent = msg.message.events[0];
const [receiver, amount, denom] = getAttributesFrom(
timeoutEvent.attributes,
[
EventAttribute.ATTRIBUTE_KEY.REFUND_RECEIVER,
EventAttribute.ATTRIBUTE_KEY.REFUND_AMOUNT,
EventAttribute.ATTRIBUTE_KEY.REFUND_DENOM,
]
);
const [receiver, amount, denom] = timeoutEvent.getAttributesFrom([
EventAttribute.ATTRIBUTE_KEY.REFUND_RECEIVER,
EventAttribute.ATTRIBUTE_KEY.REFUND_AMOUNT,
EventAttribute.ATTRIBUTE_KEY.REFUND_DENOM,
]);
return IbcIcs20.fromJson({
ibc_message_id: msg.id,
receiver,
Expand Down
34 changes: 23 additions & 11 deletions test/unit/services/ibc/crawl_ibc_ics20.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,21 @@ export default class CrawlIbcIcs20Test {
sender: 'cosmos1e8288j8swfy7rwkyx0h3lz82fe58vz2m6xx0en',
},
});
await IbcMessage.query().insert(ibcMessage).transacting(trx);
const message = await IbcMessage.query()
.insert(ibcMessage)
.transacting(trx);
await this.crawlIbcIcs20Serivce.handleIcs20Send(
this.block.height - 1,
this.block.height,
trx
);
const result = await IbcIcs20.query().first().transacting(trx);
expect(result?.ibc_message_id).toEqual(message.id);
expect(result?.sender).toEqual(ibcMessage.data.sender);
expect(result?.receiver).toEqual(ibcMessage.data.receiver);
expect(result?.amount).toEqual(ibcMessage.data.amount);
expect(result?.denom).toEqual(ibcMessage.data.denom);
expect(result?.ack_status).toBeNull();
expect(result?.status).toEqual(true);
await trx.rollback();
});
}
Expand All @@ -134,7 +137,9 @@ export default class CrawlIbcIcs20Test {
sender: 'cosmos1e8288j8swfy7rwkyx0h3lz82fe58vz2m6xx0en',
},
});
await IbcMessage.query().insert(ibcMessage).transacting(trx);
const ibcMsg = await IbcMessage.query()
.insert(ibcMessage)
.transacting(trx);
const event1Attrs = [
{
key: 'module',
Expand Down Expand Up @@ -216,6 +221,7 @@ export default class CrawlIbcIcs20Test {
trx
);
const result = await IbcIcs20.query().first().transacting(trx);
expect(result?.ibc_message_id).toEqual(ibcMsg.id);
expect(result?.receiver).toEqual(
getAttributeFrom(event1Attrs, EventAttribute.ATTRIBUTE_KEY.RECEIVER)
);
Expand All @@ -230,7 +236,7 @@ export default class CrawlIbcIcs20Test {
ibcMessage.dst_channel_id
}/${getAttributeFrom(event1Attrs, EventAttribute.ATTRIBUTE_KEY.DENOM)}`
);
expect(result?.ack_status).toEqual(true);
expect(result?.status).toEqual(true);
await trx.rollback();
});
}
Expand All @@ -255,7 +261,9 @@ export default class CrawlIbcIcs20Test {
sender: 'cosmos1e8288j8swfy7rwkyx0h3lz82fe58vz2m6xx0en',
},
});
await IbcMessage.query().insert(ibcMessage).transacting(trx);
const ibcMsg = await IbcMessage.query()
.insert(ibcMessage)
.transacting(trx);
const event1Attrs = [
{
key: 'module',
Expand Down Expand Up @@ -310,6 +318,7 @@ export default class CrawlIbcIcs20Test {
trx
);
const result = await IbcIcs20.query().first().transacting(trx);
expect(result?.ibc_message_id).toEqual(ibcMsg.id);
expect(result?.receiver).toEqual(
getAttributeFrom(event1Attrs, EventAttribute.ATTRIBUTE_KEY.RECEIVER)
);
Expand All @@ -320,7 +329,7 @@ export default class CrawlIbcIcs20Test {
getAttributeFrom(event1Attrs, EventAttribute.ATTRIBUTE_KEY.AMOUNT)
);
expect(result?.denom).toEqual('uatom');
expect(result?.ack_status).toEqual(true);
expect(result?.status).toEqual(true);
await trx.rollback();
});
}
Expand Down Expand Up @@ -390,7 +399,7 @@ export default class CrawlIbcIcs20Test {
type: IbcIcs20.EVENT_TYPE.FUNGIBLE_TOKEN_PACKET,
block_height: this.block.height,
source: 'TX_EVENT',
attributes: event2Attrs.map((e, index) => {
attributes: event1Attrs.map((e, index) => {
Object.assign(e, {
block_height: this.block.height,
event_id: 1,
Expand All @@ -405,7 +414,7 @@ export default class CrawlIbcIcs20Test {
type: IbcIcs20.EVENT_TYPE.FUNGIBLE_TOKEN_PACKET,
block_height: this.block.height,
source: 'TX_EVENT',
attributes: event1Attrs.map((e, index) => {
attributes: event2Attrs.map((e, index) => {
Object.assign(e, {
block_height: this.block.height,
event_id: 1,
Expand Down Expand Up @@ -434,7 +443,7 @@ export default class CrawlIbcIcs20Test {
expect(result?.denom).toEqual(
getAttributeFrom(event1Attrs, EventAttribute.ATTRIBUTE_KEY.DENOM)
);
expect(result?.ack_status).toEqual(false);
expect(result?.status).toEqual(false);
await trx.rollback();
});
}
Expand All @@ -459,7 +468,9 @@ export default class CrawlIbcIcs20Test {
sender: 'cosmos1e8288j8swfy7rwkyx0h3lz82fe58vz2m6xx0en',
},
});
await IbcMessage.query().insert(ibcMessage).transacting(trx);
const ibcMsg = await IbcMessage.query()
.insert(ibcMessage)
.transacting(trx);
const event1Attrs = [
{
key: 'module',
Expand Down Expand Up @@ -506,6 +517,7 @@ export default class CrawlIbcIcs20Test {
trx
);
const result = await IbcIcs20.query().first().transacting(trx);
expect(result?.ibc_message_id).toEqual(ibcMsg.id);
expect(result?.receiver).toEqual(
getAttributeFrom(
event1Attrs,
Expand All @@ -522,7 +534,7 @@ export default class CrawlIbcIcs20Test {
expect(result?.denom).toEqual(
getAttributeFrom(event1Attrs, EventAttribute.ATTRIBUTE_KEY.REFUND_DENOM)
);
expect(result?.ack_status).toBeNull();
expect(result?.status).toBe(true);
await trx.rollback();
});
}
Expand Down

0 comments on commit 4586cf5

Please sign in to comment.