I'm new at Solidity and smart contracts. Would appreciate any help.
I'm trying to call createEscrow function to create a child contract via a frontend UI. This is on sepolia testnet.
Transaction can be seen at 0xcf959988c15f01f224df01a9eeee55eb8e25aedbffe02a96017e52b3900d684b
Smart contract:
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
contract EscrowFactory {
mapping(address => address[]) public userToEscrow;
event CreateLog(address addr, uint256 timestamp);
event AddLog(address childAddr);
function createEscrow(
address toAddr,
uint256 withdrawalDuration
) public payable
{
Escrow escrow = new Escrow(
msg.sender,
toAddr,
withdrawalDuration,
false
);
userToEscrow[msg.sender].push(address(escrow));
userToEscrow[toAddr].push(address(escrow));
emit CreateLog(address(escrow), block.timestamp);
}
function getUserToEscrow() public view returns (address[] memory) {
return userToEscrow[msg.sender];
}
}
contract Escrow {
address private fromAddr;
address private toAddr;
address private withdrawalAddr;
uint256 private withdrawalDuration;
bool private withdrawn;
address private owner;
uint256 private revertTimestamp;
bool private reverted;
event checkint(uint256 amt);
event checkaddr(address amt);
event Deposit(address sender, uint256 amount);
constructor(
address _fromAddr,
address _toAddr,
uint256 _withdrawalDuration,
bool _withdrawn
) {
fromAddr = _fromAddr;
toAddr = _toAddr;
withdrawalAddr = _toAddr;
withdrawalDuration = _withdrawalDuration;
withdrawn = _withdrawn;
owner = _fromAddr;
revertTimestamp = block.timestamp + withdrawalDuration;
}
function deposit() public payable {
require(owner == msg.sender && owner == fromAddr, "Not owner.");
require(msg.value > 0, "Deposit must be more than 0.");
emit Deposit(msg.sender, msg.value);
transferOwner();
}
function withdraw() public {
if (block.timestamp >= revertTimestamp) {
withdrawalAddr = fromAddr;
}
require(
msg.sender == withdrawalAddr,
"This address is not permitted to withdraw."
);
require(!withdrawn, "Balance has already been withdrawn");
require(address(this).balance > 0, "No balance in this contract.");
payable(withdrawalAddr).transfer(address(this).balance);
withdrawn = true;
}
function transferOwner() private {
require(owner == fromAddr, "Owner already changed.");
owner = address(this);
}
function getBalance() public view returns (uint256 balance) {
return address(this).balance;
}
function getWithdrawalPermission()
public
view
returns (bool withdrawalPermission)
{
if (block.timestamp >= revertTimestamp) {
return (msg.sender == fromAddr);
} else {
return (msg.sender == toAddr);
}
}
function getRevertTimestamp() public view returns(uint256 _revertTimestamp) {
return revertTimestamp;
}
}
JS function:
ESCROWFACTORY_CONTRACT.methods
.createEscrow(this.state.toAddress, WITHDRAWAL_DURATION)
.send({ from: this.state.selectedAddr, gasLimit: '200000', value:0 })
.then(function (receipt) {
console.log(receipt);
});
Error given:
TransactionRevertedWithoutReasonError: Transaction has been reverted by the EVM:
{"blockHash":"0x087766d52c3223af5fda95aefced5e738ad905d4676d3cf650060fc38abb911a","blockNumber":"4623054","cumulativeGasUsed":"9104770","effectiveGasPrice":"2500000025","from":"0xa3b0d5008c7bc2ac0107f7ff3b37a13b007d0f8e","gasUsed":"21046","logs":[],"logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","status":"0","to":"0x26e80a5f533af6e2694cbd565008c4353429003a","transactionHash":"0xcf959988c15f01f224df01a9eeee55eb8e25aedbffe02a96017e52b3900d684b","transactionIndex":"9","type":"2"}