// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
contract MsgSender {
address public owner;
uint256 public value;
address public caller;
constructor () {
// 在部署合约的时候,设置一个全局唯一的合约所有者,后续可以用于权限控制;
owner = msg.sender;
}
// 1、对与合约而言,msg.sender 是一个可以改变的值,并不一定是合约的创造者;
// 2、任何人调用合约的方法,那么这笔交易中的from就是当前合约中的msg.sender;
function setValue(uint256 input) public {
value = input;
caller = msg.sender;
}
}
contract MsgValue{
mapping(address => uint256) public personToMoney;
function play() public payable {
// 因为这里做转账操作,所以需要使用payable
require(msg.value == 100, "should equal to 100 wei!");
personToMoney[msg.sender] = msg.value;
}
function getBalance() public view returns(uint256){
// 代表合约对象本身,可以通过 address(this) 获取合约地址。
return address(this).balance;
}
}
contract MsgData {
event Data(bytes data, bytes4 sig, address addr, uint256 amt);
function transfer(address addr, uint256 amt) public {
bytes memory data = msg.data;
// msg.sig 表示当前方法函数签名(4字节)
// msg.sig 等价于 this.transfer.selector
emit Data(data, msg.sig, addr, amt);
}
}
contract Payable {
// payable address can receive Ether
address payable public owner;
// payable constructor can receive Ether
constructor() payable {
owner = payable(msg.sender);
}
// function to deposit Ether into this contract
function deposit() public payable {}
// Call this function along wiht some Ether
// The function will throw an error since this function is not payable.
function notPayable() public {}
// function to withdraw all Ether from this contract
function withdraw() public {
// 将合约中的钱存入当前调用合约的地址中
uint amount = address(this).balance;
// owner对象调用transfer会将对象当做第一个参数传给transfer方法
owner.transfer(amount);
}
function transfer(address payable _to, uint _amount) public {
_to.transfer(_amount);
}
}
contract AbiDecode {
struct MyStruct {
string name;
uint[2] nums;
}
function encode(uint x, address addr, uint[] calldata arr, MyStruct calldata myStruct) external pure returns (bytes memory) {
return abi.encode(x, addr, arr, myStruct);
}
function decode(bytes calldata data) external pure returns(uint x, address addr, uint[] memory arr, MyStruct memory myStruct) {
(x, addr, arr, myStruct) = abi.decode(data, (uint, address, uint[], MyStruct));
}
function decodeLess(bytes calldata data) external pure returns(uint x, address addr, uint[] memory arr, MyStruct memory myStruct){
(x) = abi.decode(data, (uint));
}
function encodePacked(
int16 x,
bytes1 y,
uint16 z,
string memory s
) external view returns (bytes memory) {
// encodePacked 不支持struct和mapping
return abi.encodePacked(x, y, z, s);
}
// 可以用encodePacked来拼接字符串
// output string: ipfs://bafybeidmrsvehl4ehipm5qqvgegi33r6/100.json
function encodePackedTest() public pure returns (string memory) {
string memory uri = "ipfs://bafybeidmrsvehl4ehipm5qqvgegi33r6/";
return string(abi.encodePacked(uri, "100", ".json"));
}
}
// web3js编码
// web3.eth.abi.encodeFunctionCall({
// name: 'myMethod',
// type: 'function',
// inputs: [{
// type: 'uint256',
// name: 'myNumber'
// },{
// type: 'string',
// name: 'myString'
// }]
// }, ['2345675643', 'Hello!%']);
// web3js解码
// async function main() {
// let calldata = '' //太大了,删除,去下面线上链接中获取
// let abi = [{ "internalType": "string", "name": "_id", "type": "string" }, { "internalType": "string", "name": "_uniqueId", "type": "string" }, { "internalType": "uint8", "name": "_assetFrom", "type": "uint8" }, { "internalType": "uint8", "name": "_action", "type": "uint8" }, { "internalType": "address", "name": "_srcToken", "type": "address" }, { "internalType": "address", "name": "_dstToken", "type": "address" }, { "internalType": "uint256", "name": "_srcAmount", "type": "uint256" }, { "internalType": "uint256", "name": "_srcFeeAmount", "type": "uint256" }, { "internalType": "bytes", "name": "_data", "type": "bytes" }]
// let res = web3.eth.abi.decodeParameters(abi, calldata)
// console.log('res:', res)
// }
contract Receiver {
event Received(address caller, uint amount, string message);
fallback() external payable {
emit Received(msg.sender, msg.value, "Fallback was called!");
}
function foo(string memory _message, uint _x) public payable returns(uint) {
emit Received(msg.sender, msg.value, _message);
return _x + 1;
}
}
contract ReceiverWithOutFallback {
event Received(address caller, uint amount, string message);
function foo(string memory _message, uint _x) public payable returns (uint) {
emit Received(msg.sender, msg.value, _message);
return _x + 1;
}
}
contract Caller{
event Response(bool success, bytes data);
function testCallFoo(address payable _addr) public payable {
(bool success, bytes memory data) = _addr.call{value: msg.value, gas: 5000}(
abi.encodeWithSignature("foo(string, uint256)", "call foo", 123)
);
emit Response(success, data);
}
// Calling a function that does not exist triggers the fallback function.
function testCallDoesNotExist(address _addr) public {
(bool success, bytes memory data) = _addr.call(abi.encodeWithSignature("doesNotExist()"));
emit Response(success, data);
}
function getProxyAdmin(TransparentUpgradeableProxy proxy) public view virtual returns (address) {
// We need to manually run the static call since the getter cannot be flagged as view
// bytes4(keccak256("admin()")) == 0xf851a440
(bool success, bytes memory returndata) = address(proxy).staticcall(hex"f851a440");
require(success);
return abi.decode(returndata, (address));
}
}
solidity进阶汇总
2023-10-01 05:45 浏览:1165 搜索引擎搜索“手机奥展网”
温馨提示:为防找不到此信息,请务必收藏信息以备急用! 联系我时,请说明是在手机奥展网看到的信息,谢谢。
温馨提示:为防找不到此信息,请务必收藏信息以备急用! 联系我时,请说明是在手机奥展网看到的信息,谢谢。
展开全文+
发布人:eff6**** IP:120.244.15.***
举报/删稿
展会推荐
- AUTO TECH 2026 广州国际汽车内外饰技术展览会
- 广州市2026-11-27 至 2026-11-30
- AUTO TECH 2026 广州国际汽车技术展览会
- 广州市2026-11-27 至 2026-11-30
- 2026第6届上海国际文创展&潮玩展
- 上海市2027-03-18 至 2027-03-20
- HOTELEX 2027第三十五届上海国际酒店及餐饮业博览会
- 上海市2027-03-30 至 2027-04-02
- 2026第十三届广州国际汽车零部件及加工技术、汽车模具展览会
- 广州市2026-11-27 至 2026-11-30
- 2026第六届广州国际新能源汽车产业智能制造技术展览会
- 广州市2026-11-27 至 2026-11-30
- 2026第十届中国(佛山)国际氢能与燃料电池技术及产品展览会
- 南海区2026-10-21 至 2026-10-23
- 2026 年 11 月 5-8 日泰国广告展
- 亚洲2026-11-05 至 2026-11-08
- 2028年美国国际暖通空调制冷楼宇自控净化展AHREXPO
- 国外2028-02-07 至 2028-02-09
- 人生如戏
- 2023-10-01浏览:1805
- 祝愿祖国繁荣昌盛
- 2023-10-01浏览:1178
- 爱的力量,就是一种高维度能量
- 2023-10-01浏览:1295
- 泡脚
- 2023-10-01浏览:1511
- 为什么是劳动创造了人本身?
- 2023-10-01浏览:8003
- 菊缘
- 2023-10-01浏览:4044
- 喜欢,就是会自动屏弃平日里的不喜欢
- 2023-10-01浏览:1413
- 做出正确的努力
- 2023-10-01浏览:1399
- 感谢你特别邀请
- 2023-10-01浏览:2342
- 分别
- 2023-09-30浏览:902
- 天生爱流浪的女人
- 2023-09-30浏览:1196
- 皆得所“月”
- 2023-09-30浏览:1700
- 有关帕金森的科普
- 2023-09-30浏览:1243
- 【爱心小岛·以文会友】‖与人为善,取人为善
- 2023-09-30浏览:2629
- 心系员工 情暖中秋 | 客来福家居给全体员工派发中秋福利
- 2023-09-30浏览:1548
- 梦想与现实
- 2023-09-30浏览:1419
- 教师如何按时下班——《教师的工作力》记录
- 2023-09-30浏览:1887
- 人到中年,宁缺毋滥
- 2023-09-30浏览:893
- 素人改造有感
- 2023-09-30浏览:2215
- 与文字的纠缠是一种执念
- 2023-09-30浏览:1307





