پروژه تماس تحت پوشش(Covered call) در قرارداد هوشمند Solidity

پروژه زیر یک قرارداد هوشمند تماس تحت پوشش Solidityاست. فروشنده توکن ها را به قرارداد واریز می کند و از فروش قرارداد کارمزدی دریافت می کند. این قرارداد به خریدار این حق را می دهد که در صورت رعایت شرایط قرارداد اقدام به خرید توکن کند. قرارداد تحت پوشش در نظر گرفته می شود زیرا توکن ها در قرارداد واریز می شوند. اگر شرایط قرارداد برآورده نشود، فروشنده می تواند توکن های خود را در انقضا پس بگیرد.
مثال از تماس تحت پوشش
آلیس یک قرارداد اختیاری ایجاد می کند و مبلغ قرارداد را با مقدار توکن های مورد نیاز در قرارداد تأمین می کند. در اصل این یک قرارداد اختیار خرید تحت پوشش است. باب قرارداد را می خرد و اگر منطقی باشد می تواند قرارداد را اجرا کند. اگر قرارداد اجرا نشود آلیس می تواند توکن های خود را پس بگیرد و حق بیمه پرداخت شده توسط باب را حفظ کند.
این مراحل را برای ایجاد سناریو دنبال کنید
- قرارداد توکن را در بلاک چین دپلوی کنید
- قرارداد هوشمند Covered Call را به عنوان آلیس در بلاکچین دپلوی کنید. مطمئن شوید که آدرس Token در دپلوی قرارداد Option استفاده شده است
- آلیس مبلغ دقیق 100 توکن را از قرارداد توکن به قرارداد گزینه های تماس منتقل می کند.
- باب برای خرید قرارداد اقدام می کند
- خرید باب برای اجرای قرارداد را آزمایش کنید و اصول اولیه قرارداد اختیار معامله را بیاموزید.
قرارداد زیر را با استفاده از Remix در شبکه آزمایشی اتریوم مستقر کنید. سعی کنید یک تابع دیگر اضافه کنید یا برخی از پارامترها را تغییر دهید.
pragma solidity ^0.7.6;
//This is a simple ownership contract
contract Owned {
constructor() { owner = msg.sender; }
address payable owner;
modifier onlyOwner {
require(msg.sender == owner, "Only owner can call this function.");
_; /* jumps to code of function using this modifier */
}
}
//the contract Token inherits the Owned contract
//This contract is very basic
//Use ERC-20 standard for creating a token
contract Token is Owned {
mapping(address => uint) public balanceOf;
//use the constructor of the base class
constructor() Owned() {}
//This is an issuance function. Add to the balance you are passing into the function
//OnlyOwner modifier is used to make sure the person calling this function is the owner of the contract
function issue(address recipient, uint amount) public onlyOwner {
balanceOf[recipient] += amount;
}
//Make sure you have enough balance to send
//Decrement and increment the balance of each address
function transfer(address recipient, uint amount) public {
require(balanceOf[msg.sender] >= amount, "Insufficient balance");
balanceOf[msg.sender] -= amount;
balanceOf[recipient] += amount;
}
}
//This is a sample fully collateralized call Option. Covered Call
//Alice creates a contract and transfers 100 tokens to contract
//At any time until expiration bob can send 1 eth to the contract
//After expiration Alice can terminate the contract and reclaim her tokens
//The contract below expands on the contract above.
//It is the same but considers Bob paying Alice the premium for the contract and then the contract is live
contract CallOptionSeller is Owned {
//input bobs address
address buyer;
//Alice funds with 100 tokens
uint quantity; /* 100 */
//Bob can send in 1 ether which is the strikePrice. Lets say it is estimated at $1,000
uint strikePrice; /* 1000000000000000000 (1 ether) */
//Bob has to pay this much for the contract.
uint purchasePrice; /* 100000000000000000 (0.1 ether) */
//Expiration is the new years block number
uint expiry; /* 9577836111 (New Years) */
//indicates if the contract premium was paid
bool wasPurchased; /* refactor as expression (buyer == 0x0)? */
Token token;
constructor(uint _quantity, uint _strikePrice, uint _purchasePrice, uint _expiry, address _tokenAddress) Owned() {
quantity = _quantity;
strikePrice = _strikePrice;
purchasePrice = _purchasePrice;
expiry = _expiry;
token = Token(_tokenAddress);
wasPurchased = false; /* added for clarity, false is default value */
}
//this function allows Bob to purchase the contract and pay the premium
function purchase() public payable {
//was the contract already purchased
require(!wasPurchased, "Option already purchased");
//did Bob send in the correct amount to purchase the contract
require(msg.value == purchasePrice, "Incorrect purchase price");
//ss this Bob trying to make the purchase
buyer = msg.sender;
//set the wasPurchased bool to true
wasPurchased = true;
}
//first check that this contract is valid, then transfer out and clean up
//Bob sends ether into contract to execute a trade
//He is only going to do this if the value of the tokens is worth more then the ether
function execute() public payable {
//make sure the contract was purchased
require(wasPurchased, "Option unpurchased");
//make sure the person trying to execute the contract is Bob
require(msg.sender == buyer, "Unauthorized");
//Contract was funded by Alice
require(token.balanceOf(address(this)) == quantity, "Funding error");
//make sure the amount of eth being payed/sent is == to the strikePrice of 1 ether
require(msg.value == strikePrice, "Payment error");
//make sure the contract has not expired
require(block.timestamp < expiry, "Expired");
//If the above is true send tokens to the buyer
token.transfer(buyer, quantity);
//Clean up the contract and remove from the blockchain
selfdestruct(owner);
}
//refund and send funds back to the owner. Contract is not worth executing
//Can not refund if it was purchased and the contract has expired
function refund() public {
if(wasPurchased) {
require(block.timestamp > expiry, "Not expired");
}
token.transfer(owner, quantity);
selfdestruct(owner);
}
//contract can accept deposits
receive()
external
payable {
}
}

در صورتی که تجربه خاصی در خصوص برنامهنویسی ندارید میتوانید از دورههای رایگان سایت ما “فرازمان“، استفاده کنید. همچنین اگر به دورههای پیشرفتهتری در این خصوص نیاز داشته باشید، ما با آموزش های حرفه ای که در سایتمان قرار دادیم می توانید به سطح دلخواهتان با تلاش و پشتکار برسید.
نقشه راه
راهنما آکادمی فرازمان
برای یادگیری برنامه نویسی بلاکچین…
در این باره بیشتر بخوانید
دیدگاهتان را بنویسید