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
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(); |
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
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; | |
} |
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
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 { |
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
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; | |
} |
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
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!'"); |
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
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"); | |
}); |
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
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 () { | |
/** |
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
//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); | |
} |
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
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; | |
} |
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
function getHash(uint amount) public view returns(bytes32 result){ | |
return keccak256(abi.encodePacked(msg.sender, deposit_count, amount)); | |
} |