@steerprotocol/data-connector-loader 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/blockFinder.d.ts +8 -0
- package/lib/blockFinder.js +36 -0
- package/lib/dataConnector.d.ts +5 -2
- package/lib/dataConnector.js +20 -13
- package/lib/index.js +8 -7
- package/package.json +3 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Provider } from 'ethers';
|
|
2
|
+
/**
|
|
3
|
+
* Iterates down from current block to find the block previous or equal to the given timestamp
|
|
4
|
+
* Not the most efficient for far past timestamps, a binary search could be used for better performance
|
|
5
|
+
* @param timestamp
|
|
6
|
+
* @param Idenity
|
|
7
|
+
*/
|
|
8
|
+
export declare function getBlockFromTimestamp(timestamp: number, provider: Provider): Promise<number>;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.getBlockFromTimestamp = void 0;
|
|
13
|
+
// import { providers, } from "ethers";
|
|
14
|
+
/**
|
|
15
|
+
* Iterates down from current block to find the block previous or equal to the given timestamp
|
|
16
|
+
* Not the most efficient for far past timestamps, a binary search could be used for better performance
|
|
17
|
+
* @param timestamp
|
|
18
|
+
* @param Idenity
|
|
19
|
+
*/
|
|
20
|
+
function getBlockFromTimestamp(timestamp, provider) {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
let currentBlock = (yield provider.getBlockNumber()) - 1;
|
|
24
|
+
let runningTimestamp = (_a = (yield provider.getBlock(currentBlock))) === null || _a === void 0 ? void 0 : _a.timestamp;
|
|
25
|
+
if (!runningTimestamp)
|
|
26
|
+
return currentBlock;
|
|
27
|
+
while (runningTimestamp > timestamp) {
|
|
28
|
+
currentBlock -= 1;
|
|
29
|
+
runningTimestamp = (_b = (yield provider.getBlock(currentBlock))) === null || _b === void 0 ? void 0 : _b.timestamp;
|
|
30
|
+
if (!runningTimestamp)
|
|
31
|
+
return currentBlock;
|
|
32
|
+
}
|
|
33
|
+
return currentBlock;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
exports.getBlockFromTimestamp = getBlockFromTimestamp;
|
package/lib/dataConnector.d.ts
CHANGED
|
@@ -1,19 +1,22 @@
|
|
|
1
1
|
import { WasmModule } from '@steerprotocol/app-loader';
|
|
2
2
|
import { ExecutionContext } from './types';
|
|
3
|
+
import { Provider } from 'ethers';
|
|
3
4
|
export declare class DataConnector {
|
|
4
5
|
private wasmFile;
|
|
5
6
|
module?: WasmModule;
|
|
6
7
|
config: string;
|
|
7
8
|
configObj: {
|
|
8
|
-
[key: string]:
|
|
9
|
+
[key: string]: any;
|
|
9
10
|
};
|
|
10
11
|
executionContext?: ExecutionContext & {
|
|
11
12
|
epochTimestamp: number;
|
|
12
13
|
};
|
|
13
|
-
|
|
14
|
+
providerIdentity?: Provider;
|
|
15
|
+
constructor(wasmFile: string | ArrayBuffer, _config: string, identity?: Provider);
|
|
14
16
|
load(): Promise<WasmModule>;
|
|
15
17
|
setExecutionContext(executionContext: ExecutionContext): void;
|
|
16
18
|
execute(response: string): Promise<string>;
|
|
19
|
+
view(config: any): Promise<string>;
|
|
17
20
|
getResults(): string;
|
|
18
21
|
version(): number;
|
|
19
22
|
private subtractDate;
|
package/lib/dataConnector.js
CHANGED
|
@@ -11,11 +11,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.DataConnector = void 0;
|
|
13
13
|
const app_loader_1 = require("@steerprotocol/app-loader");
|
|
14
|
+
const ethers_1 = require("ethers");
|
|
15
|
+
const blockFinder_1 = require("./blockFinder");
|
|
14
16
|
class DataConnector {
|
|
15
|
-
|
|
17
|
+
// Identity should come from the node's IdentityManager.wallet
|
|
18
|
+
constructor(wasmFile, _config, identity) {
|
|
16
19
|
this.wasmFile = wasmFile;
|
|
17
20
|
this.config = _config;
|
|
18
21
|
this.configObj = JSON.parse(_config);
|
|
22
|
+
this.providerIdentity = identity;
|
|
19
23
|
}
|
|
20
24
|
load() {
|
|
21
25
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -52,21 +56,24 @@ class DataConnector {
|
|
|
52
56
|
if (!this.module)
|
|
53
57
|
throw new Error('Module not initialized');
|
|
54
58
|
// Call the main function with the response
|
|
55
|
-
return this.module
|
|
59
|
+
return this.module["execute(param_1: string)"](response);
|
|
56
60
|
});
|
|
57
61
|
}
|
|
58
62
|
// Makes contract instance and calls, returns stringified result
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
63
|
+
view(config) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
const contract = new ethers_1.ethers.Contract(config.address, config.abi, this.providerIdentity);
|
|
66
|
+
// For formatting the parameters if needed
|
|
67
|
+
const paramsArray = config.arguments;
|
|
68
|
+
// Calculate block number for determinism
|
|
69
|
+
// Best way to handle contextually non-null values?
|
|
70
|
+
const syncedBlock = yield (0, blockFinder_1.getBlockFromTimestamp)(this.executionContext.epochTimestamp, this.providerIdentity); //@note uncomment for deterministic blocks
|
|
71
|
+
// Call the desired method with the specified arguements at synced block
|
|
72
|
+
const result = yield contract[config.method](...paramsArray, { blockTag: syncedBlock }); //@note uncomment for deterministic blocks
|
|
73
|
+
// This gets stringified before calling main
|
|
74
|
+
return result;
|
|
75
|
+
});
|
|
76
|
+
}
|
|
70
77
|
getResults() {
|
|
71
78
|
if (!this.module)
|
|
72
79
|
throw new Error('Module not initialized');
|
package/lib/index.js
CHANGED
|
@@ -53,13 +53,14 @@ class DataConnectorExecution {
|
|
|
53
53
|
while (request != 'true') {
|
|
54
54
|
const parsedRequest = JSON.parse(request);
|
|
55
55
|
// On Chain
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
56
|
+
if (this.dataConnector.configObj.isChainRead == true) {
|
|
57
|
+
response = yield this.dataConnector.view(parsedRequest);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
// Axios logic (off-chain)
|
|
61
|
+
const ax_instance = axios_1.default.create();
|
|
62
|
+
response = (yield ax_instance.request(parsedRequest)).data;
|
|
63
|
+
}
|
|
63
64
|
// break;
|
|
64
65
|
request = yield this.dataConnector.execute(JSON.stringify(response));
|
|
65
66
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@steerprotocol/data-connector-loader",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "App Loader for Steer Protocol",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"lib/**/*"
|
|
47
47
|
],
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@steerprotocol/app-loader": "
|
|
49
|
+
"@steerprotocol/app-loader": "0.1.6",
|
|
50
50
|
"@steerprotocol/strategy-utils": "^2.1.0",
|
|
51
51
|
"axios": "^1.3.2",
|
|
52
|
-
"ethers": "^6.0
|
|
52
|
+
"ethers": "^6.1.0",
|
|
53
53
|
"pondjs": "1.0.0-alpha.10"
|
|
54
54
|
}
|
|
55
55
|
}
|