Skip to content

Commit

Permalink
chore: refactor Retrieval* -> Source*
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Feb 9, 2024
1 parent b45f8e1 commit 33e4bf4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
8 changes: 4 additions & 4 deletions src/lib/radon/artifacts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Class as Retrieval } from "./retrievals"
import { Class as Retrieval } from "./sources"
import { Class as Reducer, Mode } from "./reducers"

export type Args = string[] | string[][];
Expand All @@ -14,7 +14,7 @@ export class Class {
public specs: Specs
constructor(specs: Specs) {
if (!specs.retrieve || !Array.isArray(specs.retrieve) || specs.retrieve.length == 0) {
throw EvalError("\x1b[1;33mArtifact: cannot build if no retrievals are specified\x1b[0m")
throw EvalError("\x1b[1;33mArtifact: cannot build if no sources are specified\x1b[0m")
}
specs.retrieve?.forEach((retrieval, index) => {
if (retrieval === undefined) {
Expand Down Expand Up @@ -48,7 +48,7 @@ export class Template extends Class {
})
this.argsCount = retrieve.map(retrieval => retrieval?.argsCount).reduce((prev, curr) => Math.max(prev, curr), 0)
if (this.argsCount == 0) {
throw EvalError("\x1b[1;33mTemplate: cannot build w/ unparameterized retrievals\x1b[0m")
throw EvalError("\x1b[1;33mTemplate: cannot build w/ unparameterized sources\x1b[0m")
}
if (tests) {
Object.keys(tests).forEach(test => {
Expand Down Expand Up @@ -110,7 +110,7 @@ export class Precompiled extends Class {
})
let argsCount = retrieve.map(retrieval => retrieval.argsCount).reduce((prev, curr) => prev + curr)
if (argsCount > 0) {
throw EvalError("\x1b[1;33mPrecompiled: static requests cannot be built w/ parameterized retrievals\x1b[0m")
throw EvalError("\x1b[1;33mPrecompiled: static requests cannot be built w/ parameterized sources\x1b[0m")
}
}
}
31 changes: 15 additions & 16 deletions src/lib/radon/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
import * as _Artifacts from "./artifacts"
import * as _Filters from "./filters"
import * as _Reducers from "./reducers"
import * as _Retrievals from "./retrievals"
import * as _Sources from "./sources"
import * as _RPC from "./ccdr"
import * as _Types from "./types"

/**
* Web3 deployable artifacts that can be declared within
* Witnet resource files:
* Witnet asset files:
* - data request templates,
* - parameterized requests,
* - precompiled requests.
*/
export const Artifacts: typeof _Artifacts = _Artifacts;

/**
* Radon Filters that can be used within both
* Radon Filtering operators that can be used within both
* the Aggregate and Tally scripts of a Data Request.
*/
export const Filters: typeof _Filters = _Filters;

/**
* Radon Reducers that can be used within both
* Radon Reducing operators that can be used within both
* the Aggregate and Tally scripts of a Data Request.
*/
export const Reducers: typeof _Reducers = _Reducers;

/**
* Data retrieving methods that can be added as
* part of a Data Request.
* Supported data source types that can be added as part of a Data Request.
*/
export const Retrievals: typeof _Retrievals = _Retrievals;
export const Sources: typeof _Sources = _Sources;

/**
* Precompiled Remote Procedure Calls that can be included within
* Cross Chain Data Retrievals (i.e. `Witnet.Retrievals.CrossChainDataRetrieval({ .. })`,
* Cross Chain Data Requests (i.e. `Witnet.Sources.CrossChainDataSource({ .. })`,
* grouped by JSON-RPC protocol:
* - JSON ETH/RPC endpoints
* - JSON WIT/RPC endpoints
Expand Down Expand Up @@ -120,7 +119,7 @@ export function PriceTickerRequest (dict: any, tags: Map<string, string | string
})
};

export function PriceTickerTemplate (specs: { retrieve: _Retrievals.Class[], tests?: Map<string, string[][]> }) {
export function PriceTickerTemplate (specs: { retrieve: _Sources.Class[], tests?: Map<string, string[][]> }) {
return new _Artifacts.Template(
{
retrieve: specs.retrieve,
Expand All @@ -136,10 +135,10 @@ export function RequestFromDictionary (specs: {
aggregate?: _Reducers.Class,
tally?: _Reducers.Class
}) {
const retrievals: _Retrievals.Class[] = []
const sources: _Sources.Class[] = []
const args: string[][] = []
Object.keys(specs.retrieve.tags).forEach(key => {
const retrieval: _Retrievals.Class = specs.retrieve.dict[key]
const retrieval: _Sources.Class = specs.retrieve.dict[key]
const value: any = (specs.retrieve.tags as any)[key]
if (typeof value === 'string') {
if (retrieval?.tuples) {
Expand All @@ -150,10 +149,10 @@ export function RequestFromDictionary (specs: {
} else {
args.push(value || [])
}
retrievals.push(retrieval)
sources.push(retrieval)
})
return new _Artifacts.Parameterized(
new _Artifacts.Template({ retrieve: retrievals, aggregate: specs.aggregate, tally: specs.tally }),
new _Artifacts.Template({ retrieve: sources, aggregate: specs.aggregate, tally: specs.tally }),
args
)
};
Expand All @@ -163,7 +162,7 @@ export function RequestFromTemplate (template: _Artifacts.Template, args: string
};

export function RequestTemplate (specs: {
retrieve: _Retrievals.Class[],
retrieve: _Sources.Class[],
aggregate?: _Reducers.Class,
tally?: _Reducers.Class,
tests?: Map<string, string[] | string[][]>,
Expand All @@ -177,7 +176,7 @@ export function RequestTemplate (specs: {
);
};

export function RequestTemplateSingleSource (retrieval: _Retrievals.Class, tests?: Map<string, string[] | string[][]>) {
export function RequestTemplateSingleSource (retrieval: _Sources.Class, tests?: Map<string, string[] | string[][]>) {
return new _Artifacts.Template(
{
retrieve: [ retrieval ],
Expand All @@ -189,7 +188,7 @@ export function RequestTemplateSingleSource (retrieval: _Retrievals.Class, tests
};

export function StaticRequest (specs: {
retrieve: _Retrievals.Class[],
retrieve: _Sources.Class[],
aggregate?: _Reducers.Class,
tally?: _Reducers.Class
}) {
Expand Down
20 changes: 10 additions & 10 deletions src/lib/radon/retrievals.ts → src/lib/radon/sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export class Class {
} else if (!specs?.url && (method == Methods.HttpPost || method == Methods.HttpGet)) {
throw EvalError("\x1b[1;33mRetrieval: URL must be specified\x1b[0m");
} else if (specs?.body && method == Methods.HttpGet) {
throw EvalError("\x1b[1;33mWitnet.Retrievals: body cannot be specified here\x1b[0m")
throw EvalError("\x1b[1;33mWitnet.Sources: body cannot be specified here\x1b[0m")
}
this.method = method
this.headers = []
Expand Down Expand Up @@ -74,8 +74,8 @@ export class Class {
* are replaced by the given values. Fails if the retrieval refers no parameters,
* or if the specified index is not referred by it.
* @param argIndex Index of the parameter upon which the new instances will be created.
* @param values Values used for the creation of the new retrievals.
* @returns An array with as many retrievals as spawning values were specified.
* @param values Values used for the creation of the new sources.
* @returns An array with as many sources as spawning values were specified.
*/
public spawn(argIndex: number, values: string[]): Class[] {
const spawned: Class[] = []
Expand Down Expand Up @@ -115,7 +115,7 @@ export function RNG (script?: any) { return new Class(Methods.RNG, { script });
/**
* Creates a Witnet HTTP/GET Radon Retrieval.
* @param specs Retrieval parameters: URL, http headers (optional), Radon script (optional),
* pre-set tuples (optional to parameterized retrievals, only).
* pre-set tuples (optional to parameterized sources, only).
*/
export function HttpGet (specs: {
url: string,
Expand All @@ -136,7 +136,7 @@ export function HttpGet (specs: {
/**
* Creates a Witnet HTTP/HEAD Radon Retrieval.
* @param specs Retrieval parameters: URL, http headers (optional), Radon script (optional),
* pre-set tuples (optional to parameterized retrievals, only).
* pre-set tuples (optional to parameterized sources, only).
*/
export function HttpHead (specs: {
url: string,
Expand All @@ -157,7 +157,7 @@ export function HttpHead (specs: {
/**
* Creates a Witnet HTTP/POST Radon Retrieval.
* @param specs Retrieval parameters: URL, HTTP body (optional), HTTP headers (optional), Radon Script (optional),
* pre-set tuples (optional to parameterized retrievals, only).
* pre-set tuples (optional to parameterized sources, only).
*/
export function HttpPost (specs?: {
url: string,
Expand All @@ -180,7 +180,7 @@ export function HttpPost (specs?: {
/**
* Creates a Witnet GraphQL Radon Retrieval (built on top of an HTTP/POST request).
* @param specs Retrieval parameters: URL, GraphQL query string, Radon Script (optional),
* pre-set tuples (optional to parameterized retrievals, only).
* pre-set tuples (optional to parameterized sources, only).
*/
export function GraphQLQuery (specs: {
url: string,
Expand All @@ -198,11 +198,11 @@ export function GraphQLQuery (specs: {
};

/**
* Creates a Cross Chain Data Retrieval built on top of a HTTP/POST request.
* Creates a Cross Chain Data Source built on top of a HTTP/POST request.
* @param specs Retrieval parameters: RPC provider URL, RPC object encapsulating method and parameters,
* Radon Script (optional) to apply to returned value, and pre-set tuples (optional to parameterized retrievals, only).
* Radon Script (optional) to apply to returned value, and pre-set tuples (optional to parameterized sources, only).
*/
export function CrossChainDataRetrieval (specs: {
export function CrossChainDataSource (specs: {
url: string,
rpc: RPC,
script?: Script,
Expand Down

0 comments on commit 33e4bf4

Please sign in to comment.