Replace bytes32 ids on deposits for uint256 counters.

This commit is contained in:
Filipe Soccol 2022-11-11 16:10:57 -03:00
parent 5c5327eedd
commit 67f9cbb9a7
5 changed files with 77 additions and 89 deletions

View File

@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/fa666dddb19de15d02d4eaf695a7b974.json"
"buildInfo": "../../build-info/7de52626ee9f8f66be6a4217c265b719.json"
}

File diff suppressed because one or more lines are too long

View File

@ -3,13 +3,16 @@ pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract P2PIX is Ownable {
event DepositAdded(address indexed seller, bytes32 depositID, address token, uint256 premium, uint256 amount);
event DepositClosed(address indexed seller, bytes32 depositID);
event DepositWithdrawn(address indexed seller, bytes32 depositID, uint256 amount);
event LockAdded(address indexed buyer, bytes32 indexed lockID, bytes32 depositID, uint256 amount);
using Counters for Counters.Counter;
event DepositAdded(address indexed seller, uint256 depositID, address token, uint256 premium, uint256 amount);
event DepositClosed(address indexed seller, uint256 depositID);
event DepositWithdrawn(address indexed seller, uint256 depositID, uint256 amount);
event LockAdded(address indexed buyer, bytes32 indexed lockID, uint256 depositID, uint256 amount);
event LockReleased(address indexed buyer, bytes32 lockId);
event LockReturned(address indexed buyer, bytes32 lockId);
// Events
@ -25,7 +28,7 @@ contract P2PIX is Ownable {
}
struct Lock {
bytes32 depositID;
uint256 depositID;
address targetAddress; // Where goes the tokens when validated
address relayerAddress; // Relayer address that facilitated this transaction
uint256 relayerPremium; // Amount to be paid for relayer
@ -33,19 +36,20 @@ contract P2PIX is Ownable {
uint256 expirationBlock; // If not paid at this block will be expired
}
Counters.Counter public depositCount;
// Default blocks that lock will hold tokens
uint256 public defaultLockBlocks;
// List of valid Bacen signature addresses
mapping(address => bool) public validBacenSigners;
// Seller list of deposits
mapping(bytes32 => Deposit) mapDeposits;
mapping(uint256 => Deposit) mapDeposits;
// List of Locks
mapping(bytes32 => Lock) mapLocks;
// List of Pix transactions already signed
mapping(bytes32 => bool) usedTransactions;
modifier onlySeller(bytes32 depositID) {
modifier onlySeller(uint256 depositID) {
require(mapDeposits[depositID].seller == msg.sender, "P2PIX: Only seller could call this function.");
_;
}
@ -62,18 +66,19 @@ contract P2PIX is Ownable {
address token,
uint256 amount,
string calldata pixTarget
) public payable returns (bytes32 depositID){
depositID = keccak256(abi.encodePacked(pixTarget, amount));
) public payable returns (uint256 depositID){
depositID = depositCount.current();
require(!mapDeposits[depositID].valid, 'P2PIX: Deposit already exist and it is still valid');
IERC20 t = IERC20(token);
t.transferFrom(msg.sender, address(this), amount);
Deposit memory d = Deposit(msg.sender, token, amount, msg.value, true, pixTarget);
mapDeposits[depositID] = d;
depositCount.increment();
emit DepositAdded(msg.sender, depositID, token, msg.value, amount);
}
// Vendedor pode invalidar da ordem de venda impedindo novos locks na mesma (isso não afeta nenhum lock que esteja ativo).
function cancelDeposit(bytes32 depositID) public onlySeller(depositID) {
function cancelDeposit(uint256 depositID) public onlySeller(depositID) {
mapDeposits[depositID].valid = false;
emit DepositClosed(mapDeposits[depositID].seller, depositID);
}
@ -85,7 +90,7 @@ contract P2PIX is Ownable {
// Essa etapa pode ser feita pelo vendedor conjuntamente com a parte 1.
// Retorna um LockID.
function lock(
bytes32 depositID,
uint256 depositID,
address targetAddress,
address relayerAddress,
uint256 relayerPremium,
@ -159,7 +164,7 @@ contract P2PIX is Ownable {
// Após os locks expirarem, vendedor pode interagir c/ o contrato e recuperar os tokens de um depósito específico.
function withdraw(
bytes32 depositID,
uint256 depositID,
bytes32[] calldata expiredLocks
) public onlySeller(depositID) {
unlockExpired(expiredLocks);

View File

@ -6,7 +6,6 @@ describe("P2PIX deposit test", function () {
let owner, wallet2, wallet3, wallet4;
let p2pix; // Contract instance
let erc20; // Token instance
let depositID;
it("Will deploy contracts", async function () {
@ -30,11 +29,11 @@ describe("P2PIX deposit test", function () {
it("Should allow create a deposit", async function () {
let transaction = await erc20.approve(p2pix.address,ethers.utils.parseEther('1000'));
let transaction = await erc20.approve(p2pix.address,ethers.utils.parseEther('2000'));
await expect(transaction).to.emit(erc20, 'Approval').withArgs(
owner.address,
p2pix.address,
ethers.utils.parseEther('1000')
ethers.utils.parseEther('2000')
)
transaction = await p2pix.deposit(
@ -43,72 +42,44 @@ describe("P2PIX deposit test", function () {
'SELLER PIX KEY',
{value:ethers.utils.parseEther('0.1')}
);
depositID = ethers.utils.solidityKeccak256(['string', 'uint256'], ['SELLER PIX KEY', ethers.utils.parseEther('1000')])
await expect(transaction).to.emit(p2pix, 'DepositAdded').withArgs(
owner.address,
depositID,
0,
erc20.address,
ethers.utils.parseEther('0.1'),
ethers.utils.parseEther('1000')
)
})
it("Should prevent create same deposit", async function () {
await expect(p2pix.deposit(
erc20.address,
ethers.utils.parseEther('1000'),
'SELLER PIX KEY',
{value:ethers.utils.parseEther('0.1')}
))
.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 () {
let transaction = await erc20.approve(p2pix.address,ethers.utils.parseEther('1000'));
await expect(transaction).to.emit(erc20, 'Approval').withArgs(
owner.address,
p2pix.address,
ethers.utils.parseEther('1000')
)
it("Should allow create second deposit", async function () {
transaction = await p2pix.deposit(
erc20.address,
ethers.utils.parseEther('1000'),
'SELLER PIX KEY',
{value:ethers.utils.parseEther('0.1')}
);
depositID = ethers.utils.solidityKeccak256(['string', 'uint256'], ['SELLER PIX KEY', ethers.utils.parseEther('1000')])
)
await expect(transaction).to.emit(p2pix, 'DepositAdded').withArgs(
owner.address,
depositID,
1,
erc20.address,
ethers.utils.parseEther('0.1'),
ethers.utils.parseEther('1000')
)
})
it("Should allow cancel the deposit again", async function () {
const transaction = await p2pix.cancelDeposit(depositID);
it("Should allow cancel first deposit", async function () {
const transaction = await p2pix.cancelDeposit(0);
await expect(transaction).to.emit(p2pix, 'DepositClosed').withArgs(
owner.address,
depositID
0
)
})
it("Should allow withdraw the deposit", async function () {
const transaction = await p2pix.withdraw(depositID, []);
const transaction = await p2pix.withdraw(0, []);
await expect(transaction).to.emit(p2pix, 'DepositWithdrawn').withArgs(
owner.address,
depositID,
0,
ethers.utils.parseEther('1000')
)
})

View File

@ -6,7 +6,7 @@ describe("P2PIX lock/release test", function () {
let owner, wallet2, wallet3, wallet4;
let p2pix; // Contract instance
let erc20; // Token instance
let depositID, lockID;
let lockID;
it("Will deploy contracts", async function () {
@ -44,10 +44,9 @@ describe("P2PIX lock/release test", function () {
'SELLER PIX KEY',
{value:ethers.utils.parseEther('0.1')}
);
depositID = ethers.utils.solidityKeccak256(['string', 'uint256'], ['SELLER PIX KEY', ethers.utils.parseEther('1000')])
await expect(transaction).to.emit(p2pix, 'DepositAdded').withArgs(
owner.address,
depositID,
0,
erc20.address,
ethers.utils.parseEther('0.1'),
ethers.utils.parseEther('1000')
@ -57,22 +56,22 @@ describe("P2PIX lock/release test", function () {
it("Should allow create a new lock", async function () {
transaction = await p2pix.connect(wallet3).lock(
depositID,
0,
wallet3.address,
ethers.constants.AddressZero,
'0',
ethers.utils.parseEther('100'),
[]
)
lockID = ethers.utils.solidityKeccak256(['bytes32', 'uint256', 'address'], [
depositID,
lockID = ethers.utils.solidityKeccak256(['uint256', 'uint256', 'address'], [
0,
ethers.utils.parseEther('100'),
wallet3.address
])
await expect(transaction).to.emit(p2pix, 'LockAdded').withArgs(
wallet3.address,
lockID,
depositID,
0,
ethers.utils.parseEther('100')
)
console.log('GAS USED:', (await transaction.wait()).cumulativeGasUsed.toString())
@ -117,29 +116,29 @@ describe("P2PIX lock/release test", function () {
it("Should allow recreate same lock", async function () {
transaction = await p2pix.connect(wallet3).lock(
depositID,
0,
wallet3.address,
ethers.constants.AddressZero,
'0',
ethers.utils.parseEther('100'),
[]
)
lockID = ethers.utils.solidityKeccak256(['bytes32', 'uint256', 'address'], [
depositID,
lockID = ethers.utils.solidityKeccak256(['uint256', 'uint256', 'address'], [
0,
ethers.utils.parseEther('100'),
wallet3.address
])
await expect(transaction).to.emit(p2pix, 'LockAdded').withArgs(
wallet3.address,
lockID,
depositID,
0,
ethers.utils.parseEther('100'),
)
})
it("Should prevent create again same lock", async function () {
await expect(p2pix.connect(wallet3).lock(
depositID,
0,
wallet3.address,
ethers.constants.AddressZero,
'0',
@ -187,9 +186,9 @@ describe("P2PIX lock/release test", function () {
)).to.be.revertedWith('P2PIX: Lock already released or returned');
})
it("Should prevent create a 800 lock", async function () {
it("Should prevent create a 900 lock", async function () {
await expect(p2pix.connect(wallet3).lock(
depositID,
0,
wallet3.address,
ethers.constants.AddressZero,
'0',
@ -200,22 +199,22 @@ describe("P2PIX lock/release test", function () {
it("Should allow recreate same lock again", async function () {
transaction = await p2pix.connect(wallet3).lock(
depositID,
0,
wallet3.address,
ethers.constants.AddressZero,
'0',
ethers.utils.parseEther('100'),
[]
)
lockID = ethers.utils.solidityKeccak256(['bytes32', 'uint256', 'address'], [
depositID,
lockID = ethers.utils.solidityKeccak256(['uint256', 'uint256', 'address'], [
0,
ethers.utils.parseEther('100'),
wallet3.address
])
await expect(transaction).to.emit(p2pix, 'LockAdded').withArgs(
wallet3.address,
lockID,
depositID,
0,
ethers.utils.parseEther('100')
)
})