-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(oracle)!: replace
getQueryObject
with OracleBase
BREAKING CHANGE: `getQueryObject` removed Use `Oracle:getQuery`, `OracleClient:getQuery` instead.
- Loading branch information
Showing
7 changed files
with
85 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { decode, Encoded } from '../utils/encoder'; | ||
import Node from '../Node'; | ||
|
||
export type OracleQueryNode = Awaited<ReturnType<Node['getOracleQueriesByPubkey']>>['oracleQueries'][number]; | ||
interface OracleQuery extends OracleQueryNode { | ||
// TODO: type should be corrected in node api | ||
id: Encoded.OracleQueryId; | ||
decodedQuery: string; | ||
decodedResponse: string; | ||
} | ||
|
||
function decodeQuery(queryEntry: OracleQueryNode): OracleQuery { | ||
return { | ||
...queryEntry, | ||
id: queryEntry.id as Encoded.OracleQueryId, | ||
decodedQuery: decode(queryEntry.query as Encoded.OracleQuery).toString(), | ||
decodedResponse: decode(queryEntry.response as Encoded.OracleResponse).toString(), | ||
}; | ||
} | ||
|
||
/** | ||
* This class is needed because `getOracleQuery` would return different values depending on the | ||
* oracle type. | ||
*/ | ||
export default class OracleBase { | ||
/** | ||
* @param address - Oracle public key | ||
*/ | ||
constructor( | ||
public readonly address: Encoded.OracleAddress, | ||
public options: { onNode: Node }, | ||
) {} | ||
|
||
/** | ||
* Get oracle entry from the node | ||
* @param options - Options object | ||
*/ | ||
async getState(options: { onNode?: Node } = {}): ReturnType<Node['getOracleByPubkey']> { | ||
const opt = { ...this.options, ...options }; | ||
return opt.onNode.getOracleByPubkey(this.address); | ||
} | ||
|
||
/** | ||
* Get oracle query entry from the node | ||
* @param queryId - Oracle query ID | ||
* @param options - Options object | ||
*/ | ||
async getQuery( | ||
queryId: Encoded.OracleQueryId, | ||
options: { onNode?: Node } = {}, | ||
): Promise<OracleQuery> { | ||
const { onNode } = { ...this.options, ...options }; | ||
const queryEntry = await onNode.getOracleQueryByPubkeyAndQueryId(this.address, queryId); | ||
return decodeQuery(queryEntry); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters