Skip to content

Commit

Permalink
Fix smart contract event stg (#524)
Browse files Browse the repository at this point in the history
* fix: improve query for cw20 event

* fix: remove redundant join tx in smart contract event

* fix: test

* refactor: order smart contract event

* Update smart_contract.ts

* fix: test cw20

---------

Co-authored-by: phamphong9981 <[email protected]>
Co-authored-by: Phan Anh Tuan <[email protected]>
  • Loading branch information
3 people authored Dec 7, 2023
1 parent 0925dda commit 7ff767f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
24 changes: 10 additions & 14 deletions src/common/utils/smart_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,26 @@ export async function getContractActivities(
const contractActivities: IContractMsgInfo[] = [];
const wasmEvents = await Event.query()
.alias('event')
.withGraphJoined(
'[attributes(selectAttribute), transaction(filterSuccess,selectTransaction)]'
)
.withGraphFetched('[attributes(selectAttribute)]')
.modifiers({
selectAttribute(builder) {
builder.select('event_id', 'index', 'key', 'value');
},
selectTransaction(builder) {
builder.select('hash', 'height');
},
filterSuccess(builder) {
builder.where('code', 0);
builder.select('event_id', 'index', 'key', 'value').orderBy([
{ column: 'event_id', order: 'asc' },
{ column: 'index', order: 'asc' },
]);
},
// NOTE: wasm events emitted should mean succeeded transactions
// filterSuccess(builder) {
// builder.where('code', 0);
// },
})
.whereIn('event.type', [
Event.EVENT_TYPE.WASM,
Event.EVENT_TYPE.INSTANTIATE,
])
.where('event.block_height', '>', fromBlock)
.andWhere('event.block_height', '<=', toBlock)
.orderBy([
{ column: 'attributes.event_id', order: 'ASC' },
{ column: 'attributes.index', order: 'ASC' },
]);
.orderBy('event.id', 'asc');

wasmEvents.forEach((wasmEvent, index) => {
const wasmAttribute: { key: string; value: string }[] =
Expand Down
7 changes: 3 additions & 4 deletions src/services/cw20/cw20.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ export default class Cw20Service extends BullableService {
.map((event) =>
Cw20Activity.fromJson({
smart_contract_event_id: event.smart_contract_event_id,
sender: event.sender,
sender: event.message.sender,
action: event.action,
cw20_contract_id: cw20ContractsByAddress[event.contract_address].id,
amount: getAttributeFrom(
Expand Down Expand Up @@ -221,8 +221,8 @@ export default class Cw20Service extends BullableService {
page?: { prevId: number; limit: number }
) {
return SmartContractEvent.query()
.withGraphFetched('attributes(selectAttribute)')
.joinRelated('[message, tx, smart_contract.code]')
.withGraphFetched('[message, attributes(selectAttribute)]')
.joinRelated('[tx, smart_contract.code]')
.where('smart_contract:code.type', 'CW20')
.where('tx.height', '>', startBlock)
.andWhere('tx.height', '<=', endBlock)
Expand All @@ -238,7 +238,6 @@ export default class Cw20Service extends BullableService {
}
})
.select(
'message.sender as sender',
'smart_contract.address as contract_address',
'smart_contract_event.action',
'smart_contract_event.event_id',
Expand Down
52 changes: 38 additions & 14 deletions test/unit/services/cw20/cw20.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -391,13 +391,15 @@ export default class Cw20 {
smart_contract_event_id: '1',
height: 1530,
hash: txhash,
message: {},
},
{
contract_address: this.codeId.contracts[1].address,
smart_contract_id: 2,
smart_contract_event_id: '1',
height: 1520,
hash: txhash,
message: {},
},
];
const mockContractsInfo = [
Expand All @@ -423,11 +425,15 @@ export default class Cw20 {
);
Cw20Contract.getInstantiateBalances = jest.fn(() => Promise.resolve([]));
await this.cw20Service.handleCw20Instantiate(
cw20Events.map((event) => SmartContractEvent.fromJson(event)),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
cw20Events as SmartContractEvent[],
trx
);
await this.cw20Service.handleCw20Histories(
cw20Events.map((event) => SmartContractEvent.fromJson(event)),
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
cw20Events as SmartContractEvent[],
trx
);
const cw20ContractEvent1 = await Cw20Activity.query()
Expand All @@ -452,7 +458,7 @@ export default class Cw20 {
expect(
extractData.map((data) => ({
action: data.action,
sender: data.sender,
message: data.message,
contractAddress: data.contract_address,
attributes: data.attributes,
hash: data.hash,
Expand All @@ -461,7 +467,9 @@ export default class Cw20 {
).toEqual([
{
action: 'instantiate',
sender: this.txInsert.messages[0].sender,
message: expect.objectContaining({
sender: this.txInsert.messages[0].sender,
}),
contractAddress:
this.txInsert.messages[0].events[0].attributes[0].value,
attributes: [
Expand All @@ -473,7 +481,9 @@ export default class Cw20 {
},
{
action: this.txInsert.messages[0].events[1].attributes[1].value,
sender: this.txInsert.messages[0].sender,
message: expect.objectContaining({
sender: this.txInsert.messages[0].sender,
}),
contractAddress:
this.txInsert.messages[0].events[1].attributes[0].value,
attributes: [
Expand All @@ -486,7 +496,9 @@ export default class Cw20 {
},
{
action: this.txInsert.messages[0].events[2].attributes[1].value,
sender: this.txInsert.messages[0].sender,
message: expect.objectContaining({
sender: this.txInsert.messages[0].sender,
}),
contractAddress:
this.txInsert.messages[0].events[2].attributes[0].value,
attributes: [
Expand All @@ -499,7 +511,9 @@ export default class Cw20 {
},
{
action: this.txInsert.messages[0].events[3].attributes[1].value,
sender: this.txInsert.messages[0].sender,
message: expect.objectContaining({
sender: this.txInsert.messages[0].sender,
}),
contractAddress:
this.txInsert.messages[0].events[3].attributes[0].value,
attributes: [
Expand All @@ -524,7 +538,7 @@ export default class Cw20 {
expect(
extractData.map((data) => ({
action: data.action,
sender: data.sender,
message: data.message,
contractAddress: data.contract_address,
attributes: data.attributes,
hash: data.hash,
Expand All @@ -533,7 +547,9 @@ export default class Cw20 {
).toEqual([
{
action: 'instantiate',
sender: this.txInsert.messages[0].sender,
message: expect.objectContaining({
sender: this.txInsert.messages[0].sender,
}),
contractAddress:
this.txInsert.messages[0].events[0].attributes[0].value,
attributes: [
Expand All @@ -545,7 +561,9 @@ export default class Cw20 {
},
{
action: this.txInsert.messages[0].events[1].attributes[1].value,
sender: this.txInsert.messages[0].sender,
message: expect.objectContaining({
sender: this.txInsert.messages[0].sender,
}),
contractAddress:
this.txInsert.messages[0].events[1].attributes[0].value,
attributes: [
Expand All @@ -558,7 +576,9 @@ export default class Cw20 {
},
{
action: this.txInsert.messages[0].events[3].attributes[1].value,
sender: this.txInsert.messages[0].sender,
message: expect.objectContaining({
sender: this.txInsert.messages[0].sender,
}),
contractAddress:
this.txInsert.messages[0].events[3].attributes[0].value,
attributes: [
Expand All @@ -580,7 +600,7 @@ export default class Cw20 {
expect(
extractData2.map((data) => ({
action: data.action,
sender: data.sender,
message: data.message,
contractAddress: data.contract_address,
attributes: data.attributes,
hash: data.hash,
Expand All @@ -589,7 +609,9 @@ export default class Cw20 {
).toEqual([
{
action: 'instantiate',
sender: this.txInsert.messages[0].sender,
message: expect.objectContaining({
sender: this.txInsert.messages[0].sender,
}),
contractAddress:
this.txInsert.messages[0].events[0].attributes[0].value,
attributes: [
Expand All @@ -601,7 +623,9 @@ export default class Cw20 {
},
{
action: this.txInsert.messages[0].events[1].attributes[1].value,
sender: this.txInsert.messages[0].sender,
message: expect.objectContaining({
sender: this.txInsert.messages[0].sender,
}),
contractAddress:
this.txInsert.messages[0].events[1].attributes[0].value,
attributes: [
Expand Down

0 comments on commit 7ff767f

Please sign in to comment.