0

For a group project at the university, I'm looking for help by extending a current smart contract "Safe Remote Purchase".

The contract will look like that:

1. Seller deposits 1 (originally 2, but in my opinion a seller takes a risk of sending the item without receiving any funds), item put on sale for 1, contract balance is now 1.

2. Buyer offers to buy, pays in 2, half of which is the sale price and half of which is a deposit. Contract balance is now 3.

3. Seller ships item.

4. Buyer confirms receipt, gets their deposit of 1 back. That leaves a contract balance of 2, which is sent to the seller: 1 for the seller's deposit, 1 for the sale price.

Now I want to include the following two functions and need help by that:

1st function – Time Function:

  • After the seller deposited his funds, there should be a time function, that the buyer will have 2 days to deposit his funds.

-> Buyer deposits his funds and Seller ships the item.

Buyer has now 1 year to confirm the receipt of the item. If he won't confirm it within 1 year, funds which were deposited, should be sent to a dead address.

2nd function: Is there a function to send the funds automatically to a dead address after one year if Buyer did not confirm?

Thanks in advance!

0

1 Answer 1

0

Here the code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;

contract PurchaseAgreement {
uint public value;
address payable public seller;
address payable public buyer;

enum State { Created, Locked, Release, Inactive }
State public state;

constructor() payable {
    seller = payable(msg.sender);
    value = msg.value / 2;
}

/// The function cannot be called at the current state.
error InvalidState();
///  Ony the buyer can call this function
error OnlyBuyer();
///  Ony the seller can call this function
error OnlySeller();

modifier inState(State state_) {
    if (state != state_) {
        revert InvalidState();
    }
    _;
}

modifier onlyBuyer() {
    if (msg.sender != buyer) {
        revert OnlyBuyer();
    }
    _;
}

modifier onlySeller() {
    if (msg.sender != seller) {
        revert OnlySeller();
    }
    _;
}

function confirmPurchase() external inState(State.Created) payable {
    require(msg.value == (2 * value), "Please send in 2x the purchase amount");
    buyer = payable(msg.sender);
    state = State.Locked;
}

function confirmReceived() external onlyBuyer inState(State.Locked) {
    state = State.Release;
    buyer.transfer(value);
}

function paySeller() external onlySeller inState(State.Release) {
  state = State.Inactive;
  seller.transfer(3 * value);
}

function abort() external onlySeller inState(State.Created) {
    state = State.Inactive;
    seller.transfer(address(this).balance);
}


}

Not the answer you're looking for? Browse other questions tagged or ask your own question.