Loading...
Hello, World!
Sometimes when you works like freelancer you don't sure about employer, for example: you can think that you made, sent the whole work and "employer" never pays to you, but instead if you are the employer, maybe you pay for a job that never exist it.
So, how can i make this transaction surer for me?
I made this smart contract like practice, it has three functions:
Maybe for the employer this isn't 100% safe but freelancer, it is. The problem is when owner upload link where it's all work's information, he could upload something like this: https://www.ididntityouareasilly.com and the code still runing.
address owner; // Contract owner.
address buyer; // Employer.
bool isDeposit; //Deposit is false
bool isUpload; // Upload is false
string link; // Data
uint priceService; // Work's price
There are two events, one when the employer deposit eth and the second, the freelancer upload data.
event Deposited(uint _amount, address _user);
event Uploaded(string data, address _user);
The constructor accepts two parameters: employer's address and work's price.
constructor(address _buyer, uint _cost) {
owner = msg.sender;
buyer = _buyer;
priceService = _cost;
}
There are two modifiers, both work restricting access to functions.
modifier Owner() {
require(owner == msg.sender);
_;
}
modifier MyBuyer() {
require(buyer == msg.sender);
_;
}
How i said before, this smart contract has three functions, im going to explan each one:
function deposit() public payable MyBuyer {
require(msg.value == getPriceRate(priceService), "Verify pay.");
uint amount = msg.value;
isDeposit = true;
emit Deposited(amount, msg.sender);
}
It uses when employer(MyBuyer) deposit eth inside the contract, when the condition is true, isDeposit is activated and event is emited, getPriceRate came from this Chainlink code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract PriceConsumerV3 {
AggregatorV3Interface internal priceFeed;
constructor() {
//Rinkeby address
priceFeed = AggregatorV3Interface(0x8A753747A1Fa494EC906cE90E9f37563A8AF630e);
}
function getPriceRate(uint _amount) public view returns (uint) {
(, int price,,,) = priceFeed.latestRoundData();
uint adjust_price = uint(price) * 1e10;
uint usd = _amount * 1e18;
uint rate = (usd * 1e18) / adjust_price;
return rate;
}
}
Its similar to deposit function, it just need the bool isDeposit be true and owner has to upload his work.
function upload(string memory _link) public Owner {
require(isDeposit == true, "Wait for deposit.");
link = _link;
isUpload = true;
emit Uploaded(link, owner);
}
function withdraw() public payable Owner {
require(address(this).balance > 0, "There's not money.");
require(isUpload == true, "Owner has to add document.");
isDeposit = false;
isUpload = false;
payable(owner).transfer(address(this).balance);
}
If all conditions are true, owner can use this function and recieve his money.
Full code (whitout chainlink api) :
//SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
import "./Price.sol";
contract work is PriceConsumerV3 {
address owner;
address buyer;
bool isDeposit;
bool isUpload;
string link;
uint priceService;
event Deposited(uint _amount, address _user);
event Uploaded(string data, address _user);
constructor(address _buyer, uint _cost) {
owner = msg.sender;
buyer = _buyer;
priceService = _cost;
}
modifier Owner() {
require(owner == msg.sender);
_;
}
modifier MyBuyer() {
require(buyer == msg.sender);
_;
}
function deposit() public payable MyBuyer {
require(msg.value == getPriceRate(priceService), "Verify pay.");
uint amount = msg.value;
isDeposit = true;
emit Deposited(amount, msg.sender);
}
function upload(string memory _link) public Owner {
require(isDeposit == true, "Wait for deposit.");
link = _link;
isUpload = true;
emit Uploaded(link, owner);
}
function withdraw() public payable Owner {
require(address(this).balance > 0, "There's not money.");
require(isUpload == true, "Owner has to add document.");
isDeposit = false;
isUpload = false;
payable(owner).transfer(address(this).balance);
}
}
Sorry if i didnt write clear, Spanish is my native lenguage and i'm learning English. If you have any question or i made some mistake, you can ask me in comments section or :