Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement oracle rpc calls #39

Merged
merged 13 commits into from
Sep 16, 2024
41 changes: 24 additions & 17 deletions packages/automated-dispute/src/eboActor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,24 +576,27 @@ export class EboActor {
* @param chainId the CAIP-2 compliant chain ID
*/
private async proposeResponse(chainId: Caip2ChainId): Promise<void> {
const response = await this.buildResponse(chainId);
const responseBody = await this.buildResponse(chainId);
const request = this.getActorRequest();

if (this.alreadyProposed(response.epoch, response.chainId, response.block)) {
throw new ResponseAlreadyProposed(response);
if (this.alreadyProposed(responseBody.epoch, responseBody.chainId, responseBody.block)) {
throw new ResponseAlreadyProposed(responseBody);
}

const response: Response["prophetData"] = {
// TODO: check if this is the correct proposer
proposer: request.prophetData.requester,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The proposer must be the address of the transaction sender as Prophet actually checks that the sender === propose.proposer [1].

Viem has some methods that might be helpful for this like getAddresses [2] [3].

[1] https://github.com/defi-wonderland/prophet-core/blob/0954a47f7dbd225ee0d48c9face954a7db6aed30/solidity/contracts/Oracle.sol#L118-L119
[2] https://viem.sh/docs/clients/wallet.html#account-optional
[3] https://viem.sh/docs/actions/wallet/getAddresses.html

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ I added a new function to protocolProvider to get the writeClient account address, added a new custom error and some tests

requestId: request.id,
response: responseBody,
};

try {
await this.protocolProvider.proposeResponse(
this.actorRequest.id,
response.epoch,
response.chainId,
response.block,
);
await this.protocolProvider.proposeResponse(request.prophetData, response);
} catch (err) {
if (err instanceof ContractFunctionRevertedError) {
this.logger.warn(
`Block ${response.block} for epoch ${response.epoch} and ` +
`chain ${response.chainId} was not proposed. Skipping proposal...`,
`Block ${responseBody.block} for epoch ${responseBody.epoch} and ` +
`chain ${responseBody.chainId} was not proposed. Skipping proposal...`,
);
} else {
this.logger.error(
Expand All @@ -617,15 +620,19 @@ export class EboActor {

if (this.equalResponses(actorResponse, eventResponse.response)) {
this.logger.info(`Response ${event.metadata.responseId} was validated. Skipping...`);

return;
}

await this.protocolProvider.disputeResponse(
event.metadata.requestId,
event.metadata.responseId,
event.metadata.response.proposer,
);
const request = this.getActorRequest();

const dispute: Dispute["prophetData"] = {
// TODO: check if this is the correct disputer
disputer: this.actorRequest.id,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the proposeResponse case. The disputer is the transaction submitter address.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proposer: eventResponse.proposer,
responseId: event.metadata.responseId,
requestId: request.id,
};
await this.protocolProvider.disputeResponse(request.prophetData, eventResponse, dispute);
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/automated-dispute/src/exceptions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export * from "./chainNotAdded.exception.js";
export * from "./invalidEpoch.exception.js";
export * from "./invalidRequestBody.exception.js";
export * from "./invalidRequester.exception.js";
export * from "./transactionExecutionError.exception.js";
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class TransactionExecutionError extends Error {
constructor(message = "Transaction failed") {
super(message);
this.name = "TransactionExecutionError";
}
}
26 changes: 12 additions & 14 deletions packages/automated-dispute/src/interfaces/protocolProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Caip2ChainId } from "@ebo-agent/blocknumber/dist/types.js";
import { Timestamp } from "@ebo-agent/shared";
import { Address } from "viem";

Expand Down Expand Up @@ -73,29 +72,28 @@ export interface IWriteProvider {
/**
* Proposes a response to a request.
*
* @param _requestId The ID of the request.
* @param _epoch The epoch of the request.
* @param _chainId The chain ID where the request was made.
* @param _blockNumber The block number associated with the response.
* @param _request The request data.
* @param _response The response data.
* @returns A promise that resolves when the response is proposed.
*/
proposeResponse(
_requestId: string,
_epoch: bigint,
_chainId: Caip2ChainId,
_blockNumber: bigint,
_request: Request["prophetData"],
_response: Response["prophetData"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a small detail, we want to remove those _ prefixes for arguments.

I think they were left there to be consistent with the methods being defined but not implemented (so the params had to be prefixed with an underscore so ESLint could pass).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

): Promise<void>;

/**
* Disputes a proposed response.
*
* @param _requestId The ID of the request.
* @param _responseId The ID of the response to dispute.
* @param _proposer The address of the proposer.
* @param _request The request data.
* @param _response The response data.
* @param _dispute The dispute data.
* @returns A promise that resolves when the response is disputed.
*/
disputeResponse(_requestId: string, _responseId: string, _proposer: Address): Promise<void>;

disputeResponse(
_request: Request["prophetData"],
_response: Response["prophetData"],
_dispute: Dispute["prophetData"],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ got rid of the _ in the whole file--no lint issues

): Promise<void>;
/**
* Pledges support for a dispute.
*
Expand Down
214 changes: 184 additions & 30 deletions packages/automated-dispute/src/protocolProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Caip2ChainId } from "@ebo-agent/blocknumber/dist/types.js";
import { Timestamp } from "@ebo-agent/shared";
import {
Address,
Expand All @@ -22,7 +21,7 @@ import { arbitrum } from "viem/chains";
import type { EboEvent, EboEventName } from "./types/events.js";
import type { Dispute, Request, Response } from "./types/prophet.js";
import { eboRequestCreatorAbi, epochManagerAbi, oracleAbi } from "./abis/index.js";
import { RpcUrlsEmpty } from "./exceptions/rpcUrlsEmpty.exception.js";
import { RpcUrlsEmpty, TransactionExecutionError } from "./exceptions/index.js";
import {
IProtocolProvider,
IReadProvider,
Expand Down Expand Up @@ -252,11 +251,6 @@ export class ProtocolProvider implements IProtocolProvider {
* Creates a request on the EBO Request Creator contract by simulating the transaction
* and then executing it if the simulation is successful.
*
* This function first simulates the `createRequests` call on the EBO Request Creator contract
* to validate that the transaction will succeed. If the simulation is successful, the transaction
* is executed by the `writeContract` method of the wallet client. The function also handles any
* potential errors that may occur during the simulation or transaction execution.
*
* @param {bigint} epoch - The epoch for which the request is being created.
* @param {string[]} chains - An array of chain identifiers where the request should be created.
* @throws {Error} Throws an error if the chains array is empty or if the transaction fails.
Expand Down Expand Up @@ -288,7 +282,7 @@ export class ProtocolProvider implements IProtocolProvider {
});

if (receipt.status !== "success") {
throw new Error("Transaction failed");
throw new TransactionExecutionError("createRequest transaction failed");
}
} catch (error) {
if (error instanceof BaseError) {
Expand All @@ -304,23 +298,98 @@ export class ProtocolProvider implements IProtocolProvider {
}
}

/**
* Proposes a response for a given request.
*
* @param {Request["prophetData"]} request - The request data.
* @param {Response["prophetData"]} response - The response data to propose.
* @throws {TransactionExecutionError} Throws if the transaction fails during execution.
* @throws {ContractFunctionRevertedError} Throws if the contract function reverts.
* @returns {Promise<void>}
*/
async proposeResponse(
_requestId: string,
_epoch: bigint,
_chainId: Caip2ChainId,
_blockNumber: bigint,
request: Request["prophetData"],
response: Response["prophetData"],
): Promise<void> {
// TODO: implement actual method
return;
try {
const { request: simulatedRequest } = await this.readClient.simulateContract({
address: this.oracleContract.address,
abi: oracleAbi,
functionName: "proposeResponse",
args: [request, response],
account: this.writeClient.account,
});

const hash = await this.writeClient.writeContract(simulatedRequest);

const receipt = await this.readClient.waitForTransactionReceipt({
hash,
confirmations: TRANSACTION_RECEIPT_CONFIRMATIONS,
});

if (receipt.status !== "success") {
throw new TransactionExecutionError("proposeResponse transaction failed");
}
} catch (error) {
if (error instanceof BaseError) {
const revertError = error.walk(
(err) => err instanceof ContractFunctionRevertedError,
);
if (revertError instanceof ContractFunctionRevertedError) {
const errorName = revertError.data?.errorName ?? "";
throw ErrorFactory.createError(errorName);
}
}
throw error;
}
}

/**
* Disputes a proposed response.
*
* @param {Request["prophetData"]} request - The request data.
* @param {Response["prophetData"]} response - The response data to dispute.
* @param {Dispute["prophetData"]} dispute - The dispute data.
* @throws {TransactionExecutionError} Throws if the transaction fails during execution.
* @throws {ContractFunctionRevertedError} Throws if the contract function reverts.
* @returns {Promise<void>}
*/
async disputeResponse(
_requestId: string,
_responseId: string,
_proposer: Address,
request: Request["prophetData"],
response: Response["prophetData"],
dispute: Dispute["prophetData"],
): Promise<void> {
// TODO: implement actual method
return;
try {
const { request: simulatedRequest } = await this.readClient.simulateContract({
address: this.oracleContract.address,
abi: oracleAbi,
functionName: "disputeResponse",
args: [request, response, dispute],
account: this.writeClient.account,
});

const hash = await this.writeClient.writeContract(simulatedRequest);

const receipt = await this.readClient.waitForTransactionReceipt({
hash,
confirmations: TRANSACTION_RECEIPT_CONFIRMATIONS,
});

if (receipt.status !== "success") {
throw new TransactionExecutionError("disputeResponse transaction failed");
}
} catch (error) {
if (error instanceof BaseError) {
const revertError = error.walk(
(err) => err instanceof ContractFunctionRevertedError,
);
if (revertError instanceof ContractFunctionRevertedError) {
const errorName = revertError.data?.errorName ?? "";
throw ErrorFactory.createError(errorName);
}
}
throw error;
}
}

async pledgeForDispute(
Expand Down Expand Up @@ -348,23 +417,108 @@ export class ProtocolProvider implements IProtocolProvider {
return;
}

/**
* Escalates a dispute to a higher authority.
*
* This function simulates the `escalateDispute` call on the Oracle contract
* to validate that the transaction will succeed. If the simulation is successful, the transaction
* is executed by the `writeContract` method of the wallet client. The function also handles any
* potential errors that may occur during the simulation or transaction execution.
*
* @param {Request["prophetData"]} request - The request data.
* @param {Response["prophetData"]} response - The response data.
* @param {Dispute["prophetData"]} dispute - The dispute data.
* @throws {TransactionExecutionError} Throws if the transaction fails during execution.
* @throws {ContractFunctionRevertedError} Throws if the contract function reverts.
* @returns {Promise<void>}
*/
async escalateDispute(
_request: Request["prophetData"],
_response: Response["prophetData"],
_dispute: Dispute["prophetData"],
request: Request["prophetData"],
response: Response["prophetData"],
dispute: Dispute["prophetData"],
): Promise<void> {
// TODO: implement actual method
return;
try {
const { request: simulatedRequest } = await this.readClient.simulateContract({
address: this.oracleContract.address,
abi: oracleAbi,
functionName: "escalateDispute",
args: [request, response, dispute],
account: this.writeClient.account,
});

const hash = await this.writeClient.writeContract(simulatedRequest);

const receipt = await this.readClient.waitForTransactionReceipt({
hash,
confirmations: TRANSACTION_RECEIPT_CONFIRMATIONS,
});

if (receipt.status !== "success") {
throw new TransactionExecutionError("escalateDispute transaction failed");
}
} catch (error) {
if (error instanceof BaseError) {
const revertError = error.walk(
(err) => err instanceof ContractFunctionRevertedError,
);
if (revertError instanceof ContractFunctionRevertedError) {
const errorName = revertError.data?.errorName ?? "";
throw ErrorFactory.createError(errorName);
}
}
throw error;
}
}

// Pending confirmation from onchain team
// releasePledge(args):void;
/**
* Finalizes a request with a given response.
*
* This function simulates the `finalize` call on the Oracle contract
* to validate that the transaction will succeed. If the simulation is successful, the transaction
* is executed by the `writeContract` method of the wallet client. The function also handles any
* potential errors that may occur during the simulation or transaction execution.
*
* @param {Request["prophetData"]} request - The request data.
* @param {Response["prophetData"]} response - The response data to finalize.
* @throws {TransactionExecutionError} Throws if the transaction fails during execution.
* @throws {ContractFunctionRevertedError} Throws if the contract function reverts.
* @returns {Promise<void>}
*/

async finalize(
_request: Request["prophetData"],
_response: Response["prophetData"],
request: Request["prophetData"],
response: Response["prophetData"],
): Promise<void> {
//TODO: implement actual method
return;
try {
const { request: simulatedRequest } = await this.readClient.simulateContract({
address: this.oracleContract.address,
abi: oracleAbi,
functionName: "finalize",
args: [request, response],
account: this.writeClient.account,
});

const hash = await this.writeClient.writeContract(simulatedRequest);

const receipt = await this.readClient.waitForTransactionReceipt({
hash,
confirmations: TRANSACTION_RECEIPT_CONFIRMATIONS,
});

if (receipt.status !== "success") {
throw new TransactionExecutionError("finalize transaction failed");
}
} catch (error) {
if (error instanceof BaseError) {
const revertError = error.walk(
(err) => err instanceof ContractFunctionRevertedError,
);
if (revertError instanceof ContractFunctionRevertedError) {
const errorName = revertError.data?.errorName ?? "";
throw ErrorFactory.createError(errorName);
}
}
throw error;
}
}
}
Loading