تماس نماینده(Delegate call) برای ارتقای قرارداد هوشمند Solidity
22 تیر 1401
ارسال شده توسط مریم طاهری

از تماس نماینده(Delegate call) برای ارتقاء قرارداد هوشمند Solidity استفاده کنید. یک تابع Delegate call، کد فراخوانی را در محتویات مختلف اجرا می کند (Strorage، msg.sender، msg.value). به خاطر داشته باشید که متغیرهای حالت باید برای تماس گیرنده (Caller) و تماس دهنده (Callee) به یک ترتیب باشند.
چگونه یک تماس نماینده در Solidity کار می کند
قرارداد زیر نمونه ای از نحوه استفاده از تماس نماینده در solidity است. مفهوم این است:
- کاربران شما با قرارداد A تعامل دارند
- قرارداد A قرارداد B را منعقد می کند
- سپس در نقطه ای در آینده می خواهید قرارداد B را ارتقا دهید بنابراین قرارداد C را ایجاد می کنید (ارتقای قرارداد B)
- سپس قرارداد A را برای تعامل با قرارداد C تغییر می دهید
- کاربران شما از تغییر در قراردادها انتزاع شده اند
//to make this work I needed to use a lot of gas in the test environment or it will fail
pragma solidity ^0.6.0;
//Original contract
contract Satellite1 {
//these state variables need to be in the exact same order of contract A when performing a delegate call
uint public num;
address public sender;
uint public value;
constructor() public { owner = msg.sender; }
address payable owner;
//capture the following data and save it in the state variables
function setVars(uint _num) public payable {
num = _num;
sender = msg.sender;
value = msg.value;
}
//send funds back to the owner and destroy the contract
function Destruct() public {
selfdestruct(owner);
}
}
//Now lets say we want to upgrade contract B and we create B2
contract Satellite2 {
//these state variables need to be in the exact same order of contract A when performing a delegate call
uint public num;
address public sender;
uint public value;
constructor() public { owner = msg.sender; }
address payable owner;
//capture the following data and save it in the state variables
function setVars(uint _num) public payable {
//lets multiply the num by 2 so we can see a change
num = 2 * _num;
sender = msg.sender;
value = msg.value;
}
//send funds back to the owner and destroy the contract
function Destruct() public {
selfdestruct(owner);
}
}
contract MainContract {
uint public num;
address public sender;
uint public value;
//this is a delegate call to contract B
//we are going to send ether to contract so we are making it payable
function setVars(address _contract, uint _num) public payable {
//this is to make a delegate call to another contract
//the delegate call will produce 2 outputs. success if there are no errors and the output of the function in bytes
(bool success, bytes memory data) = _contract.delegatecall(
//in abi sig we need to pass in the function signature that we are calling
abi.encodeWithSignature("setVars(uint256)", _num)
);
}
}

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