Skip to Content

@cfxdevkit/compiler


@cfxdevkit/compiler / ESCROW_SOURCE

Variable: ESCROW_SOURCE

const ESCROW_SOURCE: ”// SPDX-License-Identifier: MIT\npragma solidity ^0.8.20;\n\n/**\n * @title Escrow\n * @notice A three-party escrow contract. The depositor locks ETH/CFX, the\n * arbiter decides to release funds to the beneficiary or refund the\n * depositor. Teaches: payable functions, value transfers, state machines.\n */\ncontract Escrow {\n enum State { AWAITING_PAYMENT, AWAITING_DELIVERY, COMPLETE, REFUNDED }\n\n address public depositor;\n address public beneficiary;\n address public arbiter;\n uint256 public amount;\n State public state;\n\n event Deposited(address indexed depositor, uint256 amount);\n event Released(address indexed beneficiary, uint256 amount);\n event Refunded(address indexed depositor, uint256 amount);\n\n modifier onlyArbiter() {\n require(msg.sender == arbiter, “Escrow: not the arbiter”);\n _;\n }\n\n modifier inState(State expected) {\n require(state == expected, “Escrow: invalid state”);\n _;\n }\n\n /// @param _arbiter Trusted third party who can release or refund\n /// @param _beneficiary Address that will receive funds on approval\n constructor(address _arbiter, address _beneficiary) {\n require(_arbiter != address(0) && _beneficiary != address(0), “Escrow: zero address”);\n arbiter = _arbiter;\n beneficiary = _beneficiary;\n depositor = msg.sender;\n state = State.AWAITING_PAYMENT;\n }\n\n /// @notice Deposit funds into escrow. Must be called by the depositor.\n function deposit() external payable inState(State.AWAITING_PAYMENT) {\n require(msg.sender == depositor, “Escrow: only depositor can fund”);\n require(msg.value > 0, “Escrow: must send CFX”);\n amount = msg.value;\n state = State.AWAITING_DELIVERY;\n emit Deposited(msg.sender, msg.value);\n }\n\n /// @notice Release funds to the beneficiary (arbiter only).\n function release() external onlyArbiter inState(State.AWAITING_DELIVERY) {\n state = State.COMPLETE;\n uint256 bal = address(this).balance;\n payable(beneficiary).transfer(bal);\n emit Released(beneficiary, bal);\n }\n\n /// @notice Refund depositor (arbiter only).\n function refund() external onlyArbiter inState(State.AWAITING_DELIVERY) {\n state = State.REFUNDED;\n uint256 bal = address(this).balance;\n payable(depositor).transfer(bal);\n emit Refunded(depositor, bal);\n }\n\n /// @notice Returns the current balance held in escrow.\n function balance() external view returns (uint256) {\n return address(this).balance;\n }\n\n /// @notice Human-readable state name.\n function stateName() external view returns (string memory) {\n if (state == State.AWAITING_PAYMENT) return “AWAITING_PAYMENT”;\n if (state == State.AWAITING_DELIVERY) return “AWAITING_DELIVERY”;\n if (state == State.COMPLETE) return “COMPLETE”;\n return “REFUNDED”;\n }\n}”

Defined in: templates/escrow.ts:12