Can't get Events emitted by Smart contract using Nodit Indexer
about 2 months ago by Prisca Chidimma
Hello, I have a smart contract that emits an event when upload_content
is called. I see the events emitted when I check Testnet explorer, however, if I try to get it with Nodes index, it doesn't show.
Query:
query EventQuery {
events(
offset: 0
limit: 100
where: {
account_address: {\_eq: "0x8901b80adb798b523d74b08ef680e936227595595ec76ef4c5b346910c8126ed"},
#type: {\_eq: "0xbca47e0e304b5dcd2b54c9d6683d1cd11010d6453798da34acd1ae5065c4ff5f::BoxPeer::Content"}
}
) {
transaction_version
account_address
creation_number
event_index
type
data
}
}
Response
{
"data": {
"events": [
{
"transaction_version": 6084278466,
"account_address": "0x8901b80adb798b523d74b08ef680e936227595595ec76ef4c5b346910c8126ed",
"creation_number": 0,
"event_index": 1,
"type": "0x1::account::CoinRegisterEvent",
"data": {
"type_info": {
"module_name": "0x6170746f735f636f696e",
"struct_name": "0x4170746f73436f696e",
"account_address": "0x1"
}
}
},
.....,
{
"transaction_version": 6084285077,
"account_address": "0x8901b80adb798b523d74b08ef680e936227595595ec76ef4c5b346910c8126ed",
"creation_number": 3,
"event_index": 0,
"type": "0x1::coin::WithdrawEvent",
"data": {
"amount": "1"
}
}
]
}
}
Events for transaction 0x64b01da835cbd7fa931ffba8eb1aee4593e8b09433893be8da9a3fd461f0dd29
on explorer:
https://explorer.aptoslabs.com/txn/6084285077/events?network=testnet You can see that event 0xbca47e0e304b5dcd2b54c9d6683d1cd11010d6453798da34acd1ae5065c4ff5f::BoxPeer::Content
was emitted.
Here is my smart contract
module BoxPeer_addr::BoxPeer {
use std::signer;
use std::string;
use std::vector;
use aptos_framework::coin;
use aptos_framework::event;
use aptos_framework::aptos_coin::AptosCoin;
const CONTRACT_ADDRESS: address = @0xbca47e0e304b5dcd2b54c9d6683d1cd11010d6453798da34acd1ae5065c4ff5f;
// Error Codes
const ECONTENT_NOT_FOUND: u64 = 1000;
const EINSUFFICIENT_FUNDS: u64 = 1001;
const ENODES_NOT_REGISTERED: u64 = 1002;
struct NodeRegistered has store, copy, drop {
node_address: address,
}
// Holds content metadata and ownership
#[event]
struct Content has store, drop {
owner: address,
cid: string::String,
nodes: vector<address>,
fee_paid: u64,
consumer_fee: u64,
file_type: string::String,
}
// Holds a collection of contents
#[event]
struct ContentRegistry has key {
contents: vector<Content>,
}
// Initializes a ContentRegistry for an account
public entry fun init_registry(account: &signer) {
move_to(account, ContentRegistry {
contents: vector::empty<Content>(),
});
}
// Upload Content
public entry fun upload_content(
account: &signer,
cid: string::String,
nodes: vector<address>,
fee_paid: u64,
consumer_fee: u64,
file_type: string::String,
) acquires ContentRegistry {
if (!exists<ContentRegistry>(signer::address_of(account))) {
init_registry(account);
};
let registry = borrow_global_mut<ContentRegistry>(signer::address_of(account));
// Check if there are nodes to distribute the fee
let num_nodes = vector::length(&nodes);
assert!(num_nodes > 0, ENODES_NOT_REGISTERED);
let balance = coin::balance<AptosCoin>(signer::address_of(account));
assert!(balance >= fee_paid, EINSUFFICIENT_FUNDS);
// Calculate the fee per node
let fee_per_node = fee_paid / num_nodes;
let remainder = fee_paid % num_nodes;
// Transfer fee to each node
let i = 0;
while (i < num_nodes) {
let node_address = vector::borrow(&nodes, i);
coin::transfer<AptosCoin>(account, *node_address, fee_per_node);
i = i + 1;
};
if (remainder > 0) {
let first_node_address = vector::borrow(&nodes, 0);
coin::transfer<AptosCoin>(account, *first_node_address, remainder);
};
//coin::transfer<AptosCoin>(account, CONTRACT_ADDRESS, fee_paid);
let new_content = Content {
owner: signer::address_of(account),
cid,
nodes,
fee_paid,
consumer_fee,
file_type,
};
vector::push_back(&mut registry.contents,new_content);
let event = Content {
owner: signer::address_of(account),
cid,
nodes,
fee_paid,
consumer_fee,
file_type,
};
event::emit(event);
}
}