I'm new in Solidity, therefore if I have a wrong concept of anything kindly do explain.
I'm working for a personal escrow smart contract that should include these functions:
1- function deposit from the owner wallet to the smart contract
2- function gethash this function requires (amount) and returns hashcode
- amount should not equal 0
3- function transfer amount to address this function requires (hashcode, address)
the amount is taken from the hashcode generated by gethash function
check that hashcode is not in conflict or used before
4- function withdraw to withdraw all money in the smart contract to the owner
I use this code :
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract mypayable {
// Payable address can receive Ether
address payable public owner;
// Payable constructor can receive Ether
constructor() payable {
owner = payable(msg.sender);
}
function deposit() public payable {}
uint deposit_count;
mapping(bytes32 => uint256) balances;
function getHash(uint amount) public view returns(bytes32 result){
return keccak256(abi.encodePacked(msg.sender, deposit_count, amount,block.timestamp));
}
function transferto(address payable _to, bytes32 trx_hash, uint amount ) external {
balances[trx_hash] = amount;
deposit_count++;
// Transaction hash cannot be empty
require(trx_hash[0] != 0, "Transaction hash cannot be empty!");
// Escrow amount cannot be equal to 0
require(amount != 0, "Escrow amount cannot be equal to 0.");
// Transaction hash is already in use
require(balances[trx_hash] == 0, "Unique hash conflict, hash is already in use.");
// 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!");
(bool success, ) = _to.call{value: balances[trx_hash]}("");
require(success, "Failed to send Ether");
// If all is done, status amounts to 0
balances[trx_hash] = 0;
}
function withdraw() public {
// get the amount of Ether stored in this contract
uint amount = address(this).balance;
// send all Ether to the owner
// Owner can receive Ether since the address of the owner is payable
(bool success, ) = owner.call{value: amount}("");
require(success, "Failed to send Ether");
}
}
functions deposit and withdraw work correctly no issues
function gethash returned hash code correctly and changeable
I guess the whole issue in function transferto:
firstly: I added parameter {amount} because I didn't know how to get the amount value from gethash function
second: I faced this error when I tried the transferto function
Gas estimation errored with the following message (see below). The transaction execution will likely fail. Do you want to force sending?
execution reverted: Unique hash conflict, hash is already in use.
{
"originalError": {
"code": 3,
"data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002d556e69717565206861736820636f6e666c6963742c206861736820697320616c726561647920696e207573652e00000000000000000000000000000000000000",
"message": "execution reverted: Unique hash conflict, hash is already in use."
}
}
What is the best way to generate and store code/hashcode including amount value and check it in another function like transferto function?
so the withdrawal will input his address and code I provide it and he will receive the exact amount in gethash function
if there's something missing please tell me to clear more Thanks in Advance
---------------------------- update
I would like to thank (Jeremy Then) for his helping
the message error was gone, but there's something wrong
the hashcode is valid forever not expired
I can send many transactions in the same hashcode and this is not logical
Also, If I input the amount in the gethash function with different values of the amount in transferto, the code will take the amount value of the transferto function
I would like if can take the amount from the gethash function and delete the parameter (amount) from the function transferto.