Skip to content

Commit

Permalink
feat: Added Pick functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
xlassix committed Aug 28, 2024
1 parent 0c003c0 commit 888b9db
Show file tree
Hide file tree
Showing 4 changed files with 302 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ Finally, send a new request transaction to have it indexed. Make sure to replace

```bash
cd rooch
rooch move run --function <contractAddress>::example_caller::request_data --sender-account default --args 'string:api.x.com' --args 'string:POST' --args 'string:v2v3v' --args 'string:{"test":1}' --args 'string:' --args 'address:0xd5ea168fcbeb42ca8c891a1b3cf54edf48035389f0dacaa7d374fcc483927f7d'
rooch move run --function <contractAddress>::example_caller::request_data --sender-account default --args 'string:https://api.x.com/2/users/by/username/elonmusk?user.fields=public_metrics' --args 'string:GET' --args 'string:{}' --args 'string:{}' --args 'string:.data.public_metrics.followers_count' --args 'address:0xd5ea168fcbeb42ca8c891a1b3cf54edf48035389f0dacaa7d374fcc483927f7d'
```


35 changes: 27 additions & 8 deletions orchestrator/src/indexer/rooch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { log } from "@/logger";
import { xInstance } from "@/request/twitter";
import { type IRequestAdded, type JsonRpcResponse, RequestStatus, type RoochNetwork } from "@/types";
import { Args, RoochClient, Secp256k1Keypair, Transaction, getRoochNodeUrl } from "@roochnetwork/rooch-sdk";
import axios from "axios";
import axios, { type AxiosResponse } from "axios";
import { run } from "node-jq";
import prismaClient from "../../prisma";

const ALLOWED_HOST = ["x.com", "api.x.com", "twitter.com", "api.twitter.com"];
Expand Down Expand Up @@ -38,7 +39,7 @@ export default class RoochIndexer {
}

async fetchEvents<T>(
eventName: "RequestAdded" | "FulfilmentAdded",
eventName: "RequestAdded" | "Fulfilment",
last_processed: null | number = null,
): Promise<JsonRpcResponse<T> | null> {
try {
Expand Down Expand Up @@ -95,13 +96,17 @@ export default class RoochIndexer {
args: [Args.objectId(data.request_id), Args.string(result)],
});

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

log.debug(receipt);
return receipt;
}

async processRequestAddedEvent(data: IRequestAdded) {
log.debug("processing:", data.request_id);
const token = xInstance.getAccessToken();

if (data.oracle.toLowerCase() !== this.orchestrator) {
Expand All @@ -119,8 +124,9 @@ export default class RoochIndexer {
}

try {
let request: AxiosResponse<any, any>;
if (isValidJson(data.params.value.headers)) {
const request = await axios({
request = await axios({
method: data.params.value.method,
data: data.params.value.body,
url: url,
Expand All @@ -129,19 +135,32 @@ export default class RoochIndexer {
Authorization: `Bearer ${token}`,
},
});
return { status: request.status, message: request.data };
// return { status: request.status, message: request.data };
} else {
const request = await axios({
request = await axios({
method: data.params.value.method,
data: data.params.value.body,
url: url,
headers: {
Authorization: `Bearer ${token}`,
},
});
return { status: request.status, message: request.data };
}
} catch (error) {

log.debug({ responseData: request.data });
try {
const result = await run(data.pick, JSON.stringify(request.data), { input: "string" });
log.debug({ result });
return result;
} catch {
return { status: 406, message: "`Pick` value provided could not be resolved on the returned response" };
}
// return { status: request.status, message: result };
} catch (error: any) {
log.debug({
error: error.message,
});

if (axios.isAxiosError(error)) {
// Handle Axios-specific errors
if (error.response) {
Expand Down
20 changes: 17 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
"engines": {
"node": ">=18"
},
"keywords": ["web3", "crypto", "blockchain", "move", "verity", "smart-contracts"],
"keywords": [
"web3",
"crypto",
"blockchain",
"move",
"verity",
"smart-contracts"
],
"author": "Ryan Soury <[email protected]>",
"license": "LGPL-2.1",
"bugs": {
Expand All @@ -31,10 +38,12 @@
"@types/jest": "^29.5.12",
"@types/mocha": "^10.0.7",
"@types/node": "^22.4.2",
"chalk": "^5.3.0",
"husky": "^9.1.5",
"jest": "^29.7.0",
"lint-staged": "^15.2.9",
"prisma": "5.18.0",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"tsup": "^8.2.4",
"typescript": "^5.5.4"
Expand All @@ -48,16 +57,21 @@
"cron": "^3.1.7",
"dotenv": "^16.4.5",
"joi": "^17.13.3",
"node-jq": "^6.0.1",
"tslog": "^4.9.3"
},
"lint-staged": {
"*.{js,ts,cjs,mjs,d.cts,d.mts,json,jsonc}": ["biome check --apply --no-errors-on-unmatched"]
"*.{js,ts,cjs,mjs,d.cts,d.mts,json,jsonc}": [
"biome check --apply --no-errors-on-unmatched"
]
},
"prisma": {
"schema": "orchestrator/prisma/schema.prisma"
},
"tsup": {
"entry": ["./orchestrator/src"],
"entry": [
"./orchestrator/src"
],
"splitting": false,
"sourcemap": true,
"clean": true
Expand Down
Loading

0 comments on commit 888b9db

Please sign in to comment.