Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ DOME_EVENTS_CONTRACT_ABI=[ { "anonymous": false, "inputs": [ { "indexed": false,
DOME_PRODUCTION_BLOCK_NUMBER=118733266
RPC_ADDRESS=https://red-t.alastria.io/v0/9461d9f4292b41230527d57ee90652a6
ISS=0x43b27fef24cfe8a0b797ed8a36de2884f9963c0c2a0da640e3ec7ad6cd0c493d
NETWORK=foo
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,18 @@
"binary-search": "^1.3.6",
"dotenv": "^16.3.1",
"ethers": "^5.7.1",
"express": "^4.17.3",
"express": "^4.21.2",
"jest": "^29.7.0",
"morgan": "^1.10.0",
"nodemon": "^3.0.2",
"supertest": "^6.3.3",
"swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.1",
"ts-jest": "^29.1.1",
"ts-node": "^10.7.0",
"typescript": "^4.7.4"
"tsoa": "^6.6.0",
"typescript": "^4.7.4",
"yamljs": "^0.3.0"
},
"scripts": {
"dev": "nodemon src/server.ts",
Expand Down
111 changes: 107 additions & 4 deletions src/api/DLTInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import { DOMEEvent } from "../utils/types";
const debugLog = debug("DLT Interface Service: ");
const errorLog = debug("DLT Interface Service:error ");


/**
* Publish DOME event as a blockchain event.
*
* Publishes DOME event as a blockchain event
*
* @param eventType the name of the dome event
* @param dataLocation the storage or location of the data associated with the event.
* @param relevantMetadata additional information or metadata relevant to the event.
Expand Down Expand Up @@ -77,6 +78,9 @@ export async function publishDOMEEvent(
relevantMetadata,
});

debugLog(" > Adding netwotk:",process.env.NETWORK);
const metadata = [...relevantMetadata, process.env.NETWORK];

const provider = new ethers.providers.JsonRpcProvider(rpcAddress);

const wallet = new ethers.Wallet(process.env.PRIVATE_KEY!, provider);
Expand All @@ -98,7 +102,8 @@ export async function publishDOMEEvent(
previousEntityHash,
eventType,
dataLocation,
relevantMetadata
// relevantMetadata
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Esto lo dejamos como estaba porque no recuerdo exactamente si esto implica modificarlo en el smart contract también

metadata
);
debugLog(" > Transaction waiting to be mined...");
await tx.wait();
Expand Down Expand Up @@ -372,7 +377,6 @@ export async function getActiveDOMEEventsByDate(
return allActiveDOMEEvents;
}


/**
* Returns all the DOME active blockchain events from the blockchain between given dates
* @param DOMEEvents some DOME blockchain events
Expand Down Expand Up @@ -452,4 +456,103 @@ async function getAllActiveDOMEBlockchainEventsBetweenDates(DOMEEvents: ethers.E
}

return activeEvents;
}

/**
* Returns all the DOME active events from the blockchain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realmente lo que hace esto es devolver todos los eventos de la cadena recorriendo previamente: es muy costoso y hay una forma más sencilla que complicarse así.

*
* @param rpcAddress the blockchain node RPC address
* @returns a JSON with all the DOME active events from the blockchain
*/
export async function getAllDOMEEvents(
rpcAddress: string
): Promise<DOMEEvent[]>{
if(rpcAddress === ""){
throw new IllegalArgumentError("The RPC address is blank");
}

debugLog(">>>> Getting active events");

const provider = new ethers.providers.JsonRpcProvider(rpcAddress);
const DOMEEventsContract = new ethers.Contract(
process.env.DOME_EVENT_CONTRACT_ADDRESS!,
process.env.DOME_EVENTS_CONTRACT_ABI!,
provider
);
debugLog(">>>> Connecting to blockchain node...");
debugLog(" >> rpcAddress: " + rpcAddress);
let blockNumber = await provider.getBlockNumber();
debugLog(" >> Blockchain block number is " + blockNumber);
let allDOMEEvents = await DOMEEventsContract.queryFilter(
"*",
parseInt(process.env.DOME_PRODUCTION_BLOCK_NUMBER!),
blockNumber
);
allDOMEEvents = allDOMEEvents.slice(1);
let DOMEEvents: DOMEEvent[] = [];
allDOMEEvents.forEach((event) => {
if(event.args == undefined || event.args!.length == 0){
debugLog("Event with no args, passing to next...")
return;
}

let eventJson: DOMEEvent = {
id: event.args![0],
timestamp: event.args![1],
eventType: event.args![5],
dataLocation: event.args![6],
relevantMetadata: event.args![7],
entityId: event.args![3],
previousEntityHash: event.args![4]
};

debugLog(eventJson);
DOMEEvents.push(eventJson);
});
debugLog(">>>> Events found: " + DOMEEvents.length);
return DOMEEvents;
}


/**
* Subscribes to all DOME Events
*
* @param rpcAddress the blockchain node address to be used for event subscription
* @param ownIss the organization identifier hash
* @param notificationEndpoint the user's endpoint to be notified to of the events of interest.
* The notification is sent as a POST
* @param handler an optional function to handle the events
*/
export async function subscribeToAllEvents(
rpcAddress: string,
ownIss: string,
notificationEndpoint?: string,
handler?: (event: object) => void
){
if (rpcAddress === null || rpcAddress === undefined) {
throw new IllegalArgumentError("The rpc address is null.");
}
if (ownIss === "") {
throw new IllegalArgumentError("The ownIss is blank.");
}
if (ownIss === null || ownIss === undefined) {
throw new IllegalArgumentError("The ownIss is null.");
}
try{
debugLog(">>>> Retrieving all DOME Events...");
const allEvents = await getAllDOMEEvents(rpcAddress);
debugLog(">>>> Subscribing to " + allEvents.length + " events");
allEvents.forEach(async (event) => {
await subscribeToDOMEEvents(
[event.eventType],
rpcAddress,
ownIss,
notificationEndpoint,
handler
);
});
} catch (error) {
errorLog(" > !! Error subscribing to DOME Events");
throw error;
}
}
Loading