Skip to content

Instantly share code, notes, and snippets.

@Sm4rty-1
Created September 21, 2023 15:31
Show Gist options
  • Save Sm4rty-1/e887cca8f1e7e8c71c24ae1492ef5a1d to your computer and use it in GitHub Desktop.
Save Sm4rty-1/e887cca8f1e7e8c71c24ae1492ef5a1d to your computer and use it in GitHub Desktop.
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import {
EvaluableStructOutput,
FlowInitializedEvent,
} from "../typechain/contracts/flow/FlowCommon";
import { Flow, ReserveToken18 } from "../typechain";
import { ethers } from "hardhat";
import { RESERVE_ONE, assertError, basicDeploy, getEvents } from "../utils";
import { arrayify, keccak256, solidityKeccak256 } from "ethers/lib/utils";
import {
ADD_DELIVERABLE_OPERATION,
D7,
CANCEL_OPERATION,
START_MEDIATION_OPERATION,
APPROVE_DELIVERABLE_OPERATION,
MEDIATION_RESULT_OPERATION,
createFlowConfig,
} from "./utils";
import { deployFlowClone } from "../utils/deploy/flow/basic/deploy";
import { cloneFactory, implementation } from "./deploy.test";
import assert from "assert";
import { SignedContextV1Struct } from "../typechain/contracts/lobby/Lobby";
describe("start Mediation test", () => {
let signers: SignerWithAddress[];
let deployer: SignerWithAddress;
let client: SignerWithAddress; // caller
let contractor: SignerWithAddress; // caller
let erc20: ReserveToken18;
let flowContract: Flow;
let endDate: number;
let invoiceDataHash: string;
let dispatchCancel: EvaluableStructOutput,
dispatchClientWithdraw: EvaluableStructOutput,
dispatchContractorWithdraw: EvaluableStructOutput,
dispatchAddDeliverables: EvaluableStructOutput,
dispatchApproveDeliverables: EvaluableStructOutput,
dispatchFeedbackDeliverables: EvaluableStructOutput,
dispatchStartMediation: EvaluableStructOutput,
dispatchClientMediationWithdraw: EvaluableStructOutput,
dispatchContractorMediationWithdraw: EvaluableStructOutput,
dispatchMediationResult: EvaluableStructOutput;
beforeEach(async () => {
signers = await ethers.getSigners();
deployer = signers[0];
client = signers[1];
contractor = signers[2];
erc20 = (await basicDeploy("ReserveToken18", {})) as ReserveToken18;
await erc20.initialize();
endDate = Date.now();
invoiceDataHash = solidityKeccak256(
["uint256[]"],
[[client.address, contractor.address, RESERVE_ONE, endDate]]
);
const flowConfig = await createFlowConfig(
erc20.address,
invoiceDataHash,
endDate,
D7,
deployer.address
);
const { flow } = await deployFlowClone(
deployer,
cloneFactory,
implementation,
flowConfig
);
const flowInitialized = (await getEvents(
flow.deployTransaction,
"FlowInitialized",
flow
)) as FlowInitializedEvent["args"][];
flowContract = flow;
dispatchCancel = flowInitialized[0].evaluable;
dispatchClientWithdraw = flowInitialized[1].evaluable;
dispatchContractorWithdraw = flowInitialized[2].evaluable;
dispatchAddDeliverables = flowInitialized[3].evaluable;
dispatchApproveDeliverables = flowInitialized[4].evaluable;
dispatchFeedbackDeliverables = flowInitialized[5].evaluable;
dispatchStartMediation = flowInitialized[6].evaluable;
dispatchClientMediationWithdraw = flowInitialized[7].evaluable;
dispatchContractorMediationWithdraw = flowInitialized[8].evaluable;
dispatchMediationResult = flowInitialized[9].evaluable;
});
it.only("client should be able to approve the contractor after starting mediation", async () => {
await erc20.transfer(flowContract.address, RESERVE_ONE);
assert.deepEqual(await erc20.balanceOf(flowContract.address), RESERVE_ONE);
const context = [client.address, contractor.address, RESERVE_ONE, endDate];
const addDeliverableContext = [
invoiceDataHash,
ADD_DELIVERABLE_OPERATION,
ethers.BigNumber.from(
keccak256([
...Buffer.from(
"https://emn178.github.io/online-tools/keccak_256.html"
),
])
),
];
const addDeliverableHash = solidityKeccak256(["uint256[]"], [addDeliverableContext]);
const addDeliverableSignature = await contractor.signMessage(arrayify(addDeliverableHash));
const addDeliverableSignedContext: SignedContextV1Struct[] = [
{
signature: addDeliverableSignature,
signer: contractor.address,
context: addDeliverableContext,
},
];
await flowContract
.connect(contractor)
.flow(dispatchAddDeliverables, context, addDeliverableSignedContext);
console.log("The contractor added the Deliverables");
const startMediationcontext = [invoiceDataHash, START_MEDIATION_OPERATION];
const startMediationhash = solidityKeccak256(["uint256[]"], [startMediationcontext]);
const startMediationSignature = client.signMessage(arrayify(startMediationhash));
const startMediationsignedContext: SignedContextV1Struct[] = [
{
signature: startMediationSignature,
signer: client.address,
context: startMediationcontext,
},
];
await flowContract
.connect(client)
.flow(dispatchStartMediation, context, startMediationsignedContext);
console.log("Client Started Mediation");
const approveDeliverableContext = [
invoiceDataHash,
APPROVE_DELIVERABLE_OPERATION,
ethers.BigNumber.from(
keccak256([
...Buffer.from(
"https://emn178.github.io/online-tools/keccak_256.html"
),
])
),
];
const approveDeliverableHash = solidityKeccak256(
["uint256[]"],
[approveDeliverableContext]
);
const approveDeliverableSignature = client.signMessage(
arrayify(approveDeliverableHash)
);
const approveDeliverableSignedContext: SignedContextV1Struct[] = [
{
signature: approveDeliverableSignature,
signer: client.address,
context: approveDeliverableContext,
},
];
await flowContract
.connect(client)
.flow(
dispatchApproveDeliverables,
context,
approveDeliverableSignedContext
);
console.log("The client approved the Deliverables");
const flow_contract_balance = await erc20.balanceOf(flowContract.address);
console.log("Balance of Flow Contract:", flow_contract_balance);
const contractor_balance = await erc20.balanceOf(contractor.address);
console.log("Balance of Contractor:", contractor_balance);
assert.deepEqual(await erc20.balanceOf(contractor.address), RESERVE_ONE);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment