Created
September 21, 2023 15:33
-
-
Save Sm4rty-1/b7d87e0ecf5d9c10045a137b69636233 to your computer and use it in GitHub Desktop.
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
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, | |
APPROVE_DELIVERABLE_OPERATION, | |
D7, | |
CANCEL_OPERATION, | |
START_MEDIATION_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("contractor should be able to cancel the contract 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 cancelContext = [invoiceDataHash, CANCEL_OPERATION]; | |
const cancelHash = solidityKeccak256(["uint256[]"], [cancelContext]); | |
const cancelSignature = await contractor.signMessage(arrayify(cancelHash)); | |
const cancelSignedContext: SignedContextV1Struct[] = [ | |
{ | |
signature: cancelSignature, | |
signer: contractor.address, | |
context: cancelContext, | |
}, | |
]; | |
await flowContract | |
.connect(contractor) | |
.flow(dispatchCancel, context, cancelSignedContext); | |
console.log("The contractor cancelled the contract"); | |
const client_balance = await erc20.balanceOf(client.address); | |
console.log("Balance of Client:", client_balance); | |
const flow_contract_balance = await erc20.balanceOf(flowContract.address); | |
console.log("Balance of flow contract:", flow_contract_balance); | |
assert.deepEqual(await erc20.balanceOf(client.address), RESERVE_ONE); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment