Skip to content

Repository files navigation

IOFY Protocol

Associated Repository Links:

INSTALLATION

  • clone the repository
git clone https://github.com/IOFY-Protocol/blockchain.git
  • install the packages
npm install
  • compile the contracts
npx hardhat compile
  • run the unit tests
npx hardhat test
  • run test coverage
npx hardhat coverage

SMART CONTRACT INTEGRATION DOCS

This section contains details of the different endpoints available in the smart contract.

During the deployment of this smart contct, the following varianles are passed into the constructor:

constructor(address token, uint256 fee);

where,

Syntaxdescription
tokenthe smart contract address of the ERC20 standard token to be used in the contract for transactions.
feethe fee value (integer) to be set. When the value is set to 100, it means that the contract will charge 1% fee.

The address that deploys the the smart contract is automatically set to be the owner() address. This is also modifyable in the contract. See Openzeppelin.

Read Methods

Find the available read methods below:

function getFee() external view returns (uint256 fee);

where,

Syntaxdescription
feethe fee value (integer) charged from the device owner by IOFY whenever payment is made to rent a device. When the return value is 100, it means that IOFY charges 1% fee.
function getAvailableFees() external view returns (uint256 available);

where,

Syntaxdescription
availablethe available amount (integer) accumulated by the contract from all the fees. This tells you how much revenue can be withdrawn by the admin at the current time
function getTotalRaisedInDeals() external view returns (uint256 total);

where,

Syntaxdescription
totalthe total amount (integer) raised in deals within the contract from all the deals
function getLastestOrderId() external view returns (uint256 id);

where,

Syntaxdescription
idthe value (integer) of the ID assigned to the lastly created order in the smart contract.
struct IoTDevice {
uint256 iotDeviceId;
uint256 costPerHour;
uint256 totalRaised;
uint256 txCount;
string cid;
address owner;
bool isActive;
}
function getIoTDevice(uint256 ioTDeviceId) external view returns (IoTDevice memory)

where,

Syntaxdescription
iotDeviceIdid (integer) of the IoT device to query.
costPerHourthe hourly cost of using the IoT device in USD of the IoT device to query.
totalRaisedthe total amount realized by this IoT device.
txCountthe number of times this IoT device has been rented.
cidthe hash string of the IoT device (From IPFS, Orbit-DB).
ownerthe wallet address of the owner of this IoT device.
isActivea boolean showing if the IoT device is availabe for use.
struct IoTDeviceOwner {
uint256 totalRaised;
uint256 totalWithdrawal;
uint256[] ioTDeviceIds;
}
function getIoTOwnerInfo(address ownerAddr) external view returns (IoTDeviceOwner memory)

where,

Syntaxdescription
ownerAddrthe wallet address to query for
totalRaisedthe total amount realized from all the devices owned by this owner.
totalWithdrawalthe total amount of funds withdrawn by this user from their earnings
ioTDeviceIdsan array of IDs of all IoT devices owned by this owner

NOTE

  • To know the balance available for the ownerAddr to withdraw, you would need to use this equation: balance = totalRaised - totalWithdrawal.
struct User {
uint256 spent;
Order[] orders;
}
function getUserInfo(address userAddr) external view returns (User memory)

where,

Syntaxdescription
userAddrthe wallet address to query for
spentthe total amount spent by the user on this platform.
ordersan array of Order's cerated by this user. (see next method for Order info)
struct Order {
uint256 ioTDeviceId;
uint256 amountPaid;
uint256 startTimestamp;
uint256 endTimestamp;
address user;
}
function getOrderInfo(uint256 orderId) external view returns (Order memory)

where,

Syntaxdescription
orderIdid (integer) of the order to query.
iotDeviceIdid (integer) of the IoT device on this order.
amountPaidthe amount paid on this order.
startTimestampthe epoch timestamp when the user's time will be activated by the IoT device
endTimestampthe epoch timestamp when the user's time will be deactivated by the IoT device
userthe wallet address of the user who created this order.
function getDeviceIds() external view returns (uint256[] memory ids);

where,

Syntaxdescription
idsan array of created device IDs in order of creation

Write Methods

Here, we will split the write methods in 3 categories: Admin, Lender, Renter

    • Admin Methods
function setFee(uint256 fee) external;

where,

Syntaxdescription
feethe fee value (integer) to be set by the admin of the smart contract. When the value is set to 100, it means that the contract will charge 1% fee.

NOTE: This method will fail if:

  • it is executed by a non admin wallet.
function takeFee(address recipient, uint256 amount) external;

where,

Syntaxdescription
recipientthe wallet address to receive the fee.
amountthe fee amount (integer) to be set by the admin of the smart contract. When the value is set to 100, it means that the contract will charge 1% fee.

NOTE: This method will fail if:

  • it is executed by a non admin wallet.

  • recipient is a ZERO address.

  • fee is greater than what is available. See getAvailableFees().

    • Lender Methods

The lender is one who owns an IoT device and lists it inthe smart contract for renting

function createIoTDevice(string memory cid, uint256 iotDeviceId, uint256 costPerHour) external;

where,

Syntaxdescription
cidthe hash string of the IoT device (From IPFS, Orbit-DB).
costPerHourthe hourly cost of using the IoT device in USD of the IoT device to query.
iotDeviceIdthe id of the newly created iot device.

NOTE: This method will fail if:

  • the costPerHour is equal to 0.
function modifyIoTDevice(uint256 iotDeviceId, uint256 costPerHour, bool isActive) external;

where,

Syntaxdescription
iotDeviceIdthe IoT device id to modify.
costPerHourthe hourly cost of using the IoT device in USD of the IoT device to query.
isActiveif device is active or not.

NOTE: This method is used to modify 3 features at the same time. If you don't want to change the value of either costPerHour or isActive, then you MUST pass in the current value of that param as stored in the smart contract. to access this information, see getIoTDevice(uint256 ioTDeviceId).

This method will fail if:

  • executed by a different wallet other than the wallet that created this particular ioTDeviceId.
  • the costPerHour is equal to 0.
function withdraw(address recipient, uint256 amount) external;

where,

Syntaxdescription
recipientthe wallet address to receive the fee.
amountthe amount (integer) to withdraw.

NOTE: This method is used to withdraw acquired funds from owned IoT devices. It will fail if:

  • recipient is a ZERO address.

  • amount is greater than the executor's balance. See getIoTOwnerInfo(address executorAddress).

  • amount is equal to 0.

    • Renter Methods

These method(s) can be executed by users who want to rent active IoT devices.

function rentIoT(uint256 iotDeviceId, address user, uint256 amount, uint256 startsIn) external returns (uint256 start, uint256 end);

where,

Syntaxdescription
iotDeviceIdthe IoT device id to rent.
userthe wallet address of the person who will use the IoT device. This will enable users to rent IoT devices for others as well.
amountthe amount you wish to pay. The contract calculates the usage period based on this value, and the cost per hour set in this iotDeviceId.
startsInthis tells the contract the period to wait before starting your renting time. If you wish to start immediately, you should make this value 0.

NOTE: This method is used to rent an IoT device from the marketplace. It will fail if:

  • iotDeviceId is inactive.
  • amount is 0.
  • the smart contract does not have enough approval in the token smart contract used to make purchases.

See the guide below to properly sort out approvals:

Using the allowance, approve, and totalSupply methods in the ERC20 token contract, you need to do the folloing steps:

  1. query the allowance read method in the token contract.
function allowance(address owner, address spender) external override returns (uint256) {

where,

Syntaxdescription
ownerthe address of the user who wants to make the purchase.
spenderthe smart contract address of IOFY marketplace.

If the return value is less than the amount which the user wishes to pay, then the IOFY smart contract won't have the authority to withraw the amount from the user. Hence the transaction will FAIL. To avoud this, you need to take the next step.

  1. execute the approve method in the token contract.
function approve(address spender, uint256 amount) external returns (bool) {

where,

Syntaxdescription
spenderthe smart contract address of IOFY marketplace.
amountthe total supply of the token. You can get this by querying totalSupply in the token contract. We want to approve the total supply so that this user (the executor) will not need to make the approve call again even when he wants to pay to rent future devices. We do this to reduce the number of times the users will have to make write transactions that require gas fees.

Mock Token Address:

Ethereum address: 0x8AD10a05189cAC762Fc74b7b3F3eFFb1BEFb8FA9

FVM address: t410frliqubiytswhml6hjn5t6px7wg7pxd5je6osqgy

Iofy Contract Address:

Ethereum address: 0xF94c74dbD81c8Bd4DB7AF4034e93BcA310ce2FCC

FVM address: t410f7fghjw6ydsf5jw326qbu5e54umim4l6mwrmlnvi

About

This is the smart contract repository of IOFY

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages