forked from UMAprotocol/protocol
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sdk): allow oo client to fetch request by transaction and option… (
UMAprotocol#3819) * feat(sdk): allow oo client to fetch request by transaction and optional event index Signed-off-by: David <[email protected]> * improve(sdk): improve logic in setactiverequestbytransaction, improve ethers type exports Signed-off-by: David <[email protected]>
- Loading branch information
Showing
12 changed files
with
170 additions
and
15 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
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
79 changes: 79 additions & 0 deletions
79
packages/sdk/src/oracle/services/statemachines/setActiveRequestByTransaction.ts
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,79 @@ | ||
import assert from "assert"; | ||
import { Update } from "../update"; | ||
import Store from "../../store"; | ||
import { Handlers as GenericHandlers } from "../../types/statemachine"; | ||
import { optimisticOracle } from "../../../clients"; | ||
|
||
// required exports for state machine | ||
export type Params = { chainId: number; transactionHash: string; eventIndex?: number }; | ||
export type Memory = { error?: Error }; | ||
export function initMemory(): Memory { | ||
return {}; | ||
} | ||
export function Handlers(store: Store): GenericHandlers<Params, Memory> { | ||
const update = new Update(store); | ||
return { | ||
async start(params: Params, memory: Memory) { | ||
const { chainId, transactionHash, eventIndex = 0 } = params; | ||
|
||
// have to do all of this to fetch the identifier, ancData, requester and timestamp from the request | ||
const provider = store.read().provider(chainId); | ||
const receipt = await provider.getTransactionReceipt(transactionHash); | ||
const oracleAddress = store.read().oracleAddress(chainId); | ||
// filter out logs that originate from oracle contract | ||
const oracleLogs = receipt.logs.filter((log) => log.address.toLowerCase() === oracleAddress.toLowerCase()); | ||
// decode logs using abi | ||
const decodedLogs = oracleLogs.map((log) => optimisticOracle.contractInterface.parseLog(log)); | ||
|
||
// this is the event we care about, we index into the appropriate oracle event generated from this tx | ||
const log = decodedLogs[eventIndex]; | ||
// we dont actually know the type of the log, so we need to do some validation before continuing | ||
assert(log, `Unable to find optimistic oracle event at ${transactionHash} eventIndex ${eventIndex}`); | ||
assert(log.args, `Unable to find optimistic oracle event args at ${transactionHash} eventIndex ${eventIndex}`); | ||
assert( | ||
log.args.timestamp, | ||
`Unable to find optimistic oracle event.timestamp at ${transactionHash} eventIndex ${eventIndex}` | ||
); | ||
assert( | ||
log.args.requester, | ||
`Unable to find optimistic oracle event.requester at ${transactionHash} eventIndex ${eventIndex}` | ||
); | ||
assert( | ||
log.args.ancillaryData, | ||
`Unable to find optimistic oracle event.ancillaryData at ${transactionHash} eventIndex ${eventIndex}` | ||
); | ||
assert( | ||
log.args.identifier, | ||
`Unable to find optimistic oracle event.identifier at ${transactionHash} eventIndex ${eventIndex}` | ||
); | ||
|
||
// we can parse out the necessary params to kick off fetching the state of the request | ||
const requestInput = { | ||
timestamp: log.args.timestamp, | ||
requester: log.args.requester, | ||
ancillaryData: log.args.ancillaryData, | ||
identifier: log.args.identifier, | ||
chainId, | ||
}; | ||
|
||
store.write((write) => write.inputs().request(requestInput)); | ||
|
||
try { | ||
// these could fail at any point if user isnt set, but thats ok, state machine will catch error, and use can inspect. | ||
// this will rerun when user is set. | ||
await update.oracle(); | ||
// get current time of chain when switching request | ||
await update.currentTime(); | ||
await update.request(); | ||
await update.collateralProps(); | ||
// order is important, these should be last because they depend on user being set | ||
await update.userCollateralBalance(); | ||
await update.oracleAllowance(); | ||
} catch (err) { | ||
// its ok to ignore these errors | ||
memory.error = (err as unknown) as Error; | ||
} | ||
return "done"; | ||
}, | ||
}; | ||
} |
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 |
---|---|---|
@@ -1,6 +1,11 @@ | ||
import type { Event } from "ethers"; | ||
import { LogDescription, Interface } from "@ethersproject/abi"; | ||
|
||
export type { Signer, BigNumber, BigNumberish, Contract } from "ethers"; | ||
export type { Overrides } from "@ethersproject/contracts"; | ||
export { Provider, JsonRpcSigner, JsonRpcProvider, Web3Provider, FallbackProvider } from "@ethersproject/providers"; | ||
export { TransactionRequest, TransactionReceipt, TransactionResponse } from "@ethersproject/abstract-provider"; | ||
export type { Event }; | ||
export type { Event, LogDescription }; | ||
|
||
// taken from ethers code https://github.com/ethers-io/ethers.js/blob/master/packages/abi/src.ts/interface.ts#L654 | ||
export type Log = Parameters<Interface["parseLog"]>[0]; |
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