Skip to content

Commit

Permalink
feat: Added Response status
Browse files Browse the repository at this point in the history
  • Loading branch information
xlassix committed Aug 29, 2024
1 parent 888b9db commit 8e171d4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 20 deletions.
32 changes: 14 additions & 18 deletions orchestrator/src/indexer/rooch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default class RoochIndexer {
private oracleAddress: string,
) {
this.keyPair = Secp256k1Keypair.fromSecretKey(this.privateKey);
this.orchestrator = `0x${this.keyPair.getSchnorrPublicKey()}`.toLowerCase();
this.orchestrator = this.keyPair.getRoochAddress().toHexAddress();
log.info(`Rooch Indexer initialized`);
log.info(`Chain ID: ${this.chainId}`);
log.info(`Oracle Address: ${this.oracleAddress}`);
Expand Down Expand Up @@ -77,19 +77,10 @@ export default class RoochIndexer {
}
}

async sendFulfillment(data: IRequestAdded, result: string) {
async sendFulfillment(data: IRequestAdded, status: number, result: string) {
const client = new RoochClient({
url: getRoochNodeUrl(this.chainId),
});
const session = await client.createSession({
sessionArgs: {
appName: "your app name",
appUrl: "your app url",
scopes: [`${this.oracleAddress}::oracles::fulfil_request`],
},
signer: this.keyPair,
});

const tx = new Transaction();
tx.callFunction({
target: `${this.oracleAddress}::oracles::fulfil_request`,
Expand All @@ -98,7 +89,7 @@ export default class RoochIndexer {

const receipt = await client.signAndExecuteTransaction({
transaction: tx,
signer: session,
signer: this.keyPair,
});

log.debug(receipt);
Expand All @@ -109,9 +100,9 @@ export default class RoochIndexer {
log.debug("processing:", data.request_id);
const token = xInstance.getAccessToken();

if (data.oracle.toLowerCase() !== this.orchestrator) {
return null;
}
// if (data.oracle.toLowerCase() !== this.orchestrator) {
// return null;
// }
const url = data.params.value.url?.includes("http") ? data.params.value.url : `https://${data.params.value.url}`;
try {
const _url = new URL(url);
Expand Down Expand Up @@ -151,9 +142,9 @@ export default class RoochIndexer {
try {
const result = await run(data.pick, JSON.stringify(request.data), { input: "string" });
log.debug({ result });
return result;
return { status: request.status, message: result };
} catch {
return { status: 406, message: "`Pick` value provided could not be resolved on the returned response" };
return { status: 409, message: "`Pick` value provided could not be resolved on the returned response" };
}
// return { status: request.status, message: result };
} catch (error: any) {
Expand Down Expand Up @@ -201,7 +192,11 @@ export default class RoochIndexer {
const data = await this.processRequestAddedEvent(event.decoded_event_data.value);
if (data) {
try {
const temp = await this.sendFulfillment(event.decoded_event_data.value, JSON.stringify(data));
const temp = await this.sendFulfillment(
event.decoded_event_data.value,
data.status,
JSON.stringify(data.message),
);
await prismaClient.events.create({
data: {
eventHandleId: event.event_id.event_handle_id,
Expand All @@ -216,6 +211,7 @@ export default class RoochIndexer {
},
});
} catch (err) {
log.error(err);
await prismaClient.events.create({
data: {
eventHandleId: event.event_id.event_handle_id,
Expand Down
7 changes: 5 additions & 2 deletions rooch/sources/oracles.move
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ module verity::oracles {
params: HTTPRequest,
pick: String, // An optional JQ string to pick the value from the response JSON data structure.
oracle: address,
status: u8,
response: Option<String>
// recommendation include a optional Int field for status to contain errors_code/statuscode
}

// Global params for the oracle system
Expand Down Expand Up @@ -127,7 +127,8 @@ module verity::oracles {
params,
pick,
oracle,
response: option::none()
status: 0,
response: option::none(),
});
let request_id = object::id(&request);
object::transfer(request, oracle); // transfer to oracle to ensure permission
Expand All @@ -152,6 +153,7 @@ module verity::oracles {
public entry fun fulfil_request(
sender: &signer,
id: ObjectID,
status:u8,
result: String
// proof: String
) {
Expand All @@ -168,6 +170,7 @@ module verity::oracles {

// Fulfil the request
request.response = option::some(result);
request.status = status;

// TODO: Move gas from module escrow to Oracle

Expand Down

0 comments on commit 8e171d4

Please sign in to comment.