Skip to content

Commit

Permalink
fix: tying working
Browse files Browse the repository at this point in the history
  • Loading branch information
jahabeebs committed Oct 30, 2024
1 parent 27b315c commit 48d6a78
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 71 deletions.
171 changes: 123 additions & 48 deletions apps/agent/test/e2e/scenarios/01_happy_path/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Address,
createTestClient,
getAbiItem,
getContract,
Hex,
http,
keccak256,
Expand All @@ -38,8 +39,8 @@ import {
} from "../../utils/prophet-e2e-scaffold/index.js";
import { killAgent, spawnAgent } from "../../utils/prophet-e2e-scaffold/spawnAgent.js";

const E2E_SCENARIO_SETUP_TIMEOUT = 60_000;
const E2E_TEST_TIMEOUT = 60_000;
const E2E_SCENARIO_SETUP_TIMEOUT = 120_000;
const E2E_TEST_TIMEOUT = 120_000;

// TODO: it'd be nice to have zod here
const KEYSTORE_PASSWORD = process.env.KEYSTORE_PASSWORD || "";
Expand Down Expand Up @@ -98,7 +99,7 @@ describe.sequential("single agent", () => {
});

afterAll(() => {
fs.rmSync(tmpConfigDir, { recursive: true, force: true });
// fs.rmSync(tmpConfigDir, { recursive: true, force: true });
});

beforeEach(async () => {
Expand Down Expand Up @@ -130,7 +131,15 @@ describe.sequential("single agent", () => {
chain: PROTOCOL_L2_CHAIN,
grtHolder: GRT_HOLDER,
grtContractAddress: GRT_CONTRACT_ADDRESS,
grtFundAmount: parseEther("50"),
grtFundAmount: parseEther("5000"),
}),
await setUpAccount({
localRpcUrl: PROTOCOL_L2_LOCAL_URL,
deployedContracts: protocolContracts,
chain: PROTOCOL_L2_CHAIN,
grtHolder: GRT_HOLDER,
grtContractAddress: GRT_CONTRACT_ADDRESS,
grtFundAmount: parseEther("5000"),
}),
];

Expand All @@ -140,7 +149,7 @@ describe.sequential("single agent", () => {
grtAddress: GRT_CONTRACT_ADDRESS,
horizonStakingAddress: HORIZON_STAKING_ADDRESS,
chainsToAdd: [PROTOCOL_L2_CHAIN_ID],
grtProvisionAmount: parseEther("45"),
grtProvisionAmount: parseEther("4500"),
anvilClient: createTestClient({
mode: "anvil",
transport: http(PROTOCOL_L2_LOCAL_URL),
Expand Down Expand Up @@ -738,44 +747,7 @@ describe.sequential("single agent", () => {
expect(responseProposedEvent).toBeDefined();

// Setting up a second account to act as the disputer and pledger
const account2 = await setUpAccount({
localRpcUrl: PROTOCOL_L2_LOCAL_URL,
chain: PROTOCOL_L2_CHAIN,
deployedContracts: protocolContracts,
grtHolder: GRT_HOLDER,
grtContractAddress: GRT_CONTRACT_ADDRESS,
grtFundAmount: parseEther("50"),
});

// Impersonate the arbitrator address
await anvilClient.impersonateAccount({ address: ARBITRATOR_ADDRESS });
// TODO: issue funding wallet
// console.log(`Adding 1 ETH to arbitrator ${ARBITRATOR_ADDRESS}...`);
// const fundArbitratorTxHash = await anvilClient.sendTransaction({
// account: GRT_HOLDER,
// to: ARBITRATOR_ADDRESS,
// value: parseEther('1'),
// });
// await anvilClient.waitForTransactionReceipt({ hash: fundArbitratorTxHash });
// console.log(`Arbitrator funded.`);

// Setting up Prophet for account2
await setUpProphet({
accounts: [account2.account],
arbitratorAddress: ARBITRATOR_ADDRESS,
grtAddress: GRT_CONTRACT_ADDRESS,
horizonStakingAddress: HORIZON_STAKING_ADDRESS,
chainsToAdd: [PROTOCOL_L2_CHAIN_ID],
grtProvisionAmount: parseEther("45"),
anvilClient: createTestClient({
mode: "anvil",
transport: http(PROTOCOL_L2_LOCAL_URL),
chain: PROTOCOL_L2_CHAIN,
})
.extend(publicActions)
.extend(walletActions),
deployedContracts: protocolContracts,
});
const account2 = accounts[1];

// Account2 disputes RESP1, creating DISP1
const protocolProviderAccount2 = new ProtocolProvider(
Expand Down Expand Up @@ -904,11 +876,11 @@ describe.sequential("single agent", () => {
filter: {
address: protocolContracts["BondEscalationModule"],
fromBlock: initBlock,
event: getAbiItem({ abi: bondEscalationModuleAbi, name: "PledgeAgainstDispute" }),
event: getAbiItem({ abi: bondEscalationModuleAbi, name: "PledgedAgainstDispute" }),
strict: true,
},
matcher: (log) => {
// TODO: no pledger?
console.log(log.args._pledger, accounts[0].account.address);
return log.args._pledger === accounts[0].account.address;
},
pollingIntervalMs: 100,
Expand All @@ -917,8 +889,111 @@ describe.sequential("single agent", () => {

expect(pledgeAgainstEvent).toBeDefined();

// Increase time to pass the dispute window and tying buffer
await anvilClient.increaseTime({ seconds: 60 * 60 * 24 * 7 * 4 });
// After the pledge against event is confirmed
const escalation = await protocolProvider.bondEscalationContract.read.getEscalation([
dispute.requestId,
]);

console.log(
"Current pledges - For:",
escalation.amountOfPledgesForDispute.toString(),
"Against:",
escalation.amountOfPledgesAgainstDispute.toString(),
);

// Keep pledging for until we match the against amount
while (escalation.amountOfPledgesForDispute < escalation.amountOfPledgesAgainstDispute) {
await protocolProviderAccount2.pledgeForDispute(
requestCreatedEvent.args._request,
dispute,
);

// Get updated escalation status
const updatedEscalation =
await protocolProvider.bondEscalationContract.read.getEscalation([
dispute.requestId,
]);

if (
updatedEscalation.amountOfPledgesForDispute >=
updatedEscalation.amountOfPledgesAgainstDispute
) {
break;
}
}

// Verify that pledges are equal
const finalEscalation = await protocolProvider.bondEscalationContract.read.getEscalation([
dispute.requestId,
]);

console.log(
"Final pledges - For:",
finalEscalation.amountOfPledgesForDispute.toString(),
"Against:",
finalEscalation.amountOfPledgesAgainstDispute.toString(),
);

expect(finalEscalation.amountOfPledgesForDispute).toBe(
finalEscalation.amountOfPledgesAgainstDispute,
);

const disputeParams = await protocolProvider.bondEscalationContract.read.decodeRequestData([
requestCreatedEvent.args._request.disputeModuleData,
]);

console.log(disputeParams, "disputeParams");
console.log(disputeParams._params, "disputeParams._params");

console.log("Dispute parameters:", {
bondEscalationDeadline: disputeParams.bondEscalationDeadline.toString(),
tyingBuffer: disputeParams.tyingBuffer.toString(),
disputeWindow: disputeParams.disputeWindow.toString(),
});

// Increase time to pass both the dispute window and tying buffer
const timeToIncrease =
Number(disputeParams.bondEscalationDeadline) + Number(disputeParams.tyingBuffer) + 3600;
console.log(`Increasing time by ${timeToIncrease} seconds`);
await anvilClient.increaseTime({ seconds: timeToIncrease });
await anvilClient.mine({ blocks: 1 });

const escalationAfterTimeIncrease =
await protocolProvider.bondEscalationContract.read.getEscalation([dispute.requestId]);

console.log("Escalation state after time increase:", {
disputeId: dispute.requestId,
status: escalationAfterTimeIncrease.status,
pledgesFor: escalationAfterTimeIncrease.amountOfPledgesForDispute.toString(),
pledgesAgainst: escalationAfterTimeIncrease.amountOfPledgesAgainstDispute.toString(),
});

try {
await protocolProvider.bondEscalationContract.write.settleBondEscalation([
requestCreatedEvent.args._request,
responseProposedEvent.args._response,
dispute,
]);
} catch (error) {
console.log("Settlement error:", error);

// If we get ShouldBeEscalated, try to escalate directly through the Oracle
if (error.message.includes("BondEscalationModule_ShouldBeEscalated")) {
console.log("Attempting direct escalation...");

const oracleContract = getContract({
address: protocolContracts["Oracle"],
abi: oracleAbi,
client: anvilClient,
});

await oracleContract.write.escalateDispute([
requestCreatedEvent.args._request,
responseProposedEvent.args._response,
dispute,
]);
}
}

// Wait for A1 to escalate the dispute
const disputeEscalatedEvent = await waitForEvent({
Expand All @@ -933,7 +1008,7 @@ describe.sequential("single agent", () => {
return log.args._caller === accounts[0].account.address;
},
pollingIntervalMs: 100,
blockTimeout: initBlock + 1000n,
blockTimeout: initBlock + 5000n,
});

expect(disputeEscalatedEvent).toBeDefined();
Expand Down
40 changes: 17 additions & 23 deletions apps/agent/test/e2e/utils/prophet-e2e-scaffold/eboCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,37 +292,31 @@ async function approveEboProphetModules(
deployedContracts["BondedResponseModule"],
deployedContracts["BondEscalationModule"],
];

for (const account of accounts) {
await Promise.all(
modulesToBeApproved.map(async (moduleAddress, index) => {
console.log(
`Approving ${moduleAddress} through HorizonAccountingExtension ${deployedContracts["HorizonAccountingExtension"]}`,
);

const hash = await anvilClient.sendTransaction({
account: account,
to: deployedContracts["HorizonAccountingExtension"],
data: encodeFunctionData({
abi: parseAbi(["function approveModule(address) external"]),
args: [moduleAddress],
}),
nonce: index,
});

await anvilClient.waitForTransactionReceipt({
hash: hash,
});
}),
);
for (const module of modulesToBeApproved) {
console.log(
`Approving ${module} through HorizonAccountingExtension ${deployedContracts["HorizonAccountingExtension"]}`,
);

const hash = await anvilClient.sendTransaction({
account: account,
to: deployedContracts["HorizonAccountingExtension"],
data: encodeFunctionData({
abi: parseAbi(["function approveModule(address) external"]),
args: [module],
}),
});

await anvilClient.waitForTransactionReceipt({
hash: hash,
});
}
const approvedModules = await anvilClient.readContract({
address: deployedContracts["HorizonAccountingExtension"],
abi: parseAbi(["function approvedModules(address) external view returns (address[])"]),
functionName: "approvedModules",
args: [account.address],
});

console.log(`Modules approved for ${account.address}:`);
console.dir(approvedModules);
}
Expand Down

0 comments on commit 48d6a78

Please sign in to comment.