Added tests to check deposit and withdraw.

This commit is contained in:
Filipe Soccol 2022-10-25 15:21:54 -03:00
parent 960468e23e
commit c9243a38d1
4 changed files with 89 additions and 8 deletions

View File

@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/95a2398b2b781a5a5893b30eeccd0c46.json"
"buildInfo": "../../build-info/e163883cb06c01d261c8d27101a410f2.json"
}

File diff suppressed because one or more lines are too long

View File

@ -5,6 +5,7 @@ contract P2PIX {
event DepositAdded(address indexed seller, bytes32 depositID, address token, uint256 amount);
event DepositClosed(address indexed seller, bytes32 depositID);
event DepositWithdrawn(address indexed seller, bytes32 depositID, uint256 amount);
event LockAdded(address indexed buyer, bytes32 lockID, uint256 amount);
event LockReleased(address indexed buyer, bytes32 lockId);
@ -58,15 +59,16 @@ contract P2PIX {
uint256 amount,
string calldata pixTarget
) public returns (bytes32 depositID){
depositID = keccak256(abi.encodePacked(pixTarget, amount));
require(!mapDeposits[depositID].valid, 'P2PIX: Deposit already exist and it is still valid');
// TODO Prevent seller to use same depositID
// TODO Transfer tokens to this address
Deposit memory d = Deposit(msg.sender, token, amount, true, pixTarget);
depositID = keccak256(abi.encodePacked(pixTarget, amount));
mapDeposits[depositID] = d;
emit DepositAdded(msg.sender, depositID, token, amount);
}
// Relayer interage adicionando um lock na ordem de venda.
// Relayer interaje adicionando um lock na ordem de venda.
// O lock precisa incluir address do comprador + address do relayer + reembolso/premio relayer + valor.
// ** poder ter um lock em aberto para cada (ordem de venda, valor)**.
// pode fazer lock de ordens que não estão invalidadas(Passo 5).
@ -151,7 +153,9 @@ contract P2PIX {
unlockExpired(depositID);
// TODO Transfer remaining tokens back to the seller
// Withdraw remaining tokens from mapDeposit[depositID]
uint256 amount = mapDeposits[depositID].remaining;
mapDeposits[depositID].remaining = 0;
emit DepositWithdrawn(msg.sender, depositID, amount);
}
}

View File

@ -4,18 +4,70 @@ const { ethers } = require("hardhat");
describe("P2PIX deposit test", function () {
let owner, wallet2, wallet3, wallet4;
let p2pix; // Contract instance
let depositID;
it("Deploy contracts", async function () {
it("Will deploy contracts", async function () {
[owner, wallet2, wallet3, wallet4] = await ethers.getSigners();
const P2PIX = await ethers.getContractFactory("P2PIX");
const p2pix = await P2PIX.deploy(2, [owner.address, wallet2.address]);
p2pix = await P2PIX.deploy(2, [owner.address, wallet2.address]);
await p2pix.deployed();
// Verify values at deployment
expect(await p2pix.validBacenSigners(owner.address)).to.equal(true);
expect(await p2pix.validBacenSigners(wallet2.address)).to.equal(true);
});
it("Should allow create a deposit", async function () {
const transaction = await p2pix.deposit(ethers.constants.AddressZero, 1000, 'SELLER PIX KEY');
depositID = ethers.utils.solidityKeccak256(['string', 'uint256'], ['SELLER PIX KEY', 1000])
await expect(transaction).to.emit(p2pix, 'DepositAdded').withArgs(
owner.address,
depositID,
ethers.constants.AddressZero,
1000
)
})
it("Should prevent create same deposit", async function () {
await expect(p2pix.deposit(ethers.constants.AddressZero, 1000, 'SELLER PIX KEY'))
.to.be.revertedWith('P2PIX: Deposit already exist and it is still valid');
})
it("Should allow cancel the deposit", async function () {
const transaction = await p2pix.cancelDeposit(depositID);
await expect(transaction).to.emit(p2pix, 'DepositClosed').withArgs(
owner.address,
depositID
)
})
it("Should allow recreate the deposit", async function () {
const transaction = await p2pix.deposit(ethers.constants.AddressZero, 1000, 'SELLER PIX KEY');
depositID = ethers.utils.solidityKeccak256(['string', 'uint256'], ['SELLER PIX KEY', 1000])
await expect(transaction).to.emit(p2pix, 'DepositAdded').withArgs(
owner.address,
depositID,
ethers.constants.AddressZero,
1000
)
})
it("Should allow cancel the deposit again", async function () {
const transaction = await p2pix.cancelDeposit(depositID);
await expect(transaction).to.emit(p2pix, 'DepositClosed').withArgs(
owner.address,
depositID
)
})
it("Should allow withdraw the deposit", async function () {
const transaction = await p2pix.withdraw(depositID);
await expect(transaction).to.emit(p2pix, 'DepositWithdrawn').withArgs(
owner.address,
depositID,
1000
)
})
})