Remix for Theta Deploy Contract Interact With Dapp Builder Help Examples Broadcast TXN Donate




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

contract PaymentProcessor {

    mapping (uint256 => uint256) public accountLookup;

    address owner;
    uint256 public memberCount;
    uint public monthlyFee;
    uint public sixMonthFee;
    uint public yearFee;
    uint public balanceReceived;

    constructor() {
    memberCount = 0;
        monthlyFee = 10000000000000000000;
        sixMonthFee = 20000000000000000000;
        yearFee = 30000000000000000000;
        owner = msg.sender;
    }
   
    function memberPayment(uint256 accountNumber, uint256 number ) public payable {
        require(number > 0 && number < 4 , "No term selected");
        if (number == 1) {require(msg.value == monthlyFee, "Wrong amount sent to contract");}
        if (number == 2) {require(msg.value == sixMonthFee, "Wrong amount sent to contract");}
        if (number == 3) {require(msg.value == yearFee, "Wrong amount sent to contract");}
        uint256 result;
        uint256 time;
        balanceReceived += msg.value;
       
        result = accountLookup[accountNumber];
       
        if (result > block.timestamp){
            if (number == 1){time = result + 30 days;}
            if (number == 2){time = result + 182 days;}
            if (number == 3){time = result + 365 days;}
        }else{
            if (number == 1){time = block.timestamp + 30 days;}
            if (number == 2){time = block.timestamp + 182 days;}
            if (number == 3){time = block.timestamp + 365 days;}
        }
        if (result == 0){
            memberCount++;
        }
        accountLookup[accountNumber] = time;
    }

    function membershipExpire(uint256 accountNumber) public view returns(uint256) {
        return accountLookup[accountNumber];
    }

    function updateRates(uint256 monthly, uint256 sixMonth, uint256 year ) public {
        require(msg.sender == owner, "Not the Owner");
        monthlyFee = monthly * 1000000000000000000;
        sixMonthFee = sixMonth * 1000000000000000000;
        yearFee = year * 1000000000000000000;
    }

    function getContractBalance() public view returns(uint) {
        return address(this).balance;
    }

    function withdrawTfuel() public {
        require(msg.sender == owner, "Not the Owner");
        address payable to = payable(msg.sender);
        to.transfer(getContractBalance());
    }

    function transferOwnership(address receivingAddress) public {
        require(msg.sender == owner, "Not the Owner");
        owner = receivingAddress;
    }
}



In this example we used the Dapp Builder to compile a front end example and an administration example.

This is a real world example and use case for accepting tFuel to gain access to a subscription service such as a video delivery platform. We will be using an account number and timestamp for access and validating payment to the platform. The subscription service could then use the tFuel received to pay for use of the Theta Video API.

A user would have an account number then make a payment for 30 days, 6 months, or 1 year. The contract will check if they have a active subscription, based on the account's timestamp in the contract. For example if they have 5 days left and buy 30 days then they will have 35 days to use the platform. If they are new or their subscription has ran out it will add the time from the current block time.

The function "memberPayment" is called to pay for the membership. When using this funtion the account number and term lenght must be specified. (1 for 30 days, 2 for 6 months, and 3 for a year.) We used one function instead of a function for each payment type. It will check to ensure the exact amount is being sent, if more or less tFuel was sent it will revert the transaction.

After you have deployed the contract above enter the contract address below then click Download Front End and Download Admin Dapp to save the HTML files. Then upload them to a web server and test interacting with your contract using a Dapp.

In a real world use case the account number would be provided after they login and a user would not have to enter it. For this example you will need to enter one when using the front end (Example: 123).

Note: If you make changes to the contract the Dapp may not work correctly. It may need to be rebuilt using the Dapp Builder and your new contract.









*Thetascan.io will never ask for your private key. All interactions with the blockchain in the Smart Contract HQ use the MetaMask Wallet to interact with the blockchain.


© 2022 Thetascan.io