Skip to content

Instantly share code, notes, and snippets.

it("Happy Path: withdrawalEscrow", async function () {
const contractWithSigner = contract.connect(happyPathAccount);
const trxHash = await contract.getHash(amount);
const submitEscrowTx = await contractWithSigner.submitEscrow(trxHash, amount);
await submitEscrowTx.wait();
expect(
(await erc20.balanceOf(happyPathAccount.address)).toString()
).to.equal("50000000000000000000");
const withdrawalEscrowTx = await contractWithSigner.withdrawalEscrow(trxHash);
await withdrawalEscrowTx.wait();
it("Unhappy Path: depositEscrow - ERC20: insufficient allowance", async function () {
const contractWithSigner = contract.connect(unhappyPathAccount);
const trxHash = await contract.getHash(amount);
let err = "";
try {
await contractWithSigner.depositEscrow(trxHash, amount)
}
catch(e) {
err = e.message;
}
it("Unhappy Path: depositEscrow - Unique hash conflict, hash is already in use.", async function () {
const contractWithSigner = contract.connect(happyPathAccount);
const trxHash = await contract.getHash(amount);
const depositEscrowTx = await contractWithSigner.depositEscrow(trxHash, amount);
await depositEscrowTx.wait();
expect(
(await erc20.balanceOf(happyPathAccount.address)).toString()
).to.equal("60000000000000000000");
let err = "";
try {
it("Unhappy Path: depositEscrow - Escrow amount cannot be equal to 0.", async function () {
const contractWithSigner = contract.connect(unhappyPathAccount);
const trxHash = await contract.getHash(amount);
let err = "";
try {
await contractWithSigner.depositEscrow(trxHash, ethers.utils.parseUnits("0"))
}
catch(e) {
err = e.message;
}
it("Unhappy Path: depositEscrow - Transaction hash cannot be empty!", async function () {
const contractWithSigner = contract.connect(unhappyPathAccount);
let err = "";
try {
await contractWithSigner.depositEscrow(ethers.constants.HashZero, amount)
}
catch(e) {
err = e.message;
}
expect(err).to.equal("VM Exception while processing transaction: reverted with reason string 'Transaction hash cannot be empty!'");
it("Happy Path: depositEscrow", async function () {
const contractWithSigner = contract.connect(happyPathAccount);
const trxHash = await contract.getHash(amount);
const depositEscrowTx = await contractWithSigner.depositEscrow(trxHash, amount);
await depositEscrowTx.wait();
expect(
(await erc20.balanceOf(happyPathAccount.address)).toString()
).to.equal("70000000000000000000");
});
const { expect } = require('chai');
describe('Escrow', function () {
let contract;
let erc20;
let happyPathAccount;
let unhappyPathAccount;
const amount = ethers.utils.parseUnits("10.0");
before(async function () {
/**
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;
import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract Escrow {
IERC20 public _token;
constructor(address ERC20Address) {
deposit_count = 0;
_token = IERC20(ERC20Address);
}
function withdrawalEscrow(bytes32 trx_hash) external {
// Transaction hash cannot be empty
require(trx_hash[0] != 0, "Transaction hash cannot be empty!");
// Check if trx_hash exists in balances
require(balances[trx_hash] != 0, "Escrow with transaction hash doesn't exist.");
// Transfer escrow to sender
require(_token.transfer(msg.sender, balances[trx_hash]), "Escrow retrieval failed!");
// If all is done, status is amounted to 0
balances[trx_hash] = 0;
}
function getHash(uint amount) public view returns(bytes32 result){
return keccak256(abi.encodePacked(msg.sender, deposit_count, amount));
}