// 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 浏览:217 搜索引擎搜索“手机奥展网”
温馨提示:信息一旦丢失不一定找得到,请务必收藏信息以备急用!本站所有信息均是注册会员发布如遇到侵权请联系文章中的联系方式或客服删除!
联系我时,请说明是在手机奥展网看到的信息,谢谢。
温馨提示:信息一旦丢失不一定找得到,请务必收藏信息以备急用!本站所有信息均是注册会员发布如遇到侵权请联系文章中的联系方式或客服删除!
联系我时,请说明是在手机奥展网看到的信息,谢谢。
展开全文+
发布人:eff6**** IP:120.244.15.***
举报/删稿
展会推荐

- 2025中国上海国际电子化学品及新材料展览会
- 上海市2025-11-05 至 2025-11-07

- 2025中国上海国际智能汽车制造技术、装备及材料展览会
- 上海市2025-08-13 至 2025-08-15

- 2025第七届中国深圳国际电脑产品及配件展览会
- 深圳市2025-06-25 至 2025-06-27

- 2025中国海洋装备博览会
- 福州市2025-10-16 至 2025-10-19

- 2025中国上海国际智能汽车制造技术、装备及材料展览会
- 上海市2025-08-13 至 2025-08-15

- 2025中国信息通信展
- 北京市2025-09-24 至 2025-09-26

- 2025第二届常州国际新能源汽车产业及供应链博览会
- 常州市2025-09-20 至 2025-09-22

- 2025中国上海国际激光加工及焊接切割展览会
- 上海市2025-12-02 至 2025-12-04

- 第九届中国南京国际智慧农业博览会
- 南京市2025-06-20 至 2025-06-22

- 2025中国(北京)国际光学加工技术及设备展览会
- 北京2025-06-30 至 2025-07-02

- 2025年泰国广告展Bangkok AD&SIGN EXPO
- 泰国2025-11-06 至 2025-11-09

- 2025年巴西圣保罗广告展FuturePrint
- 巴西2025-07-16 至 2025-07-19

- 2025年菲律宾第28届广告展Graphic Expo
- 菲律宾2025-07-17 至 2026-07-19

- 2025年厄瓜多尔APPPEXPO ECUADOR广告展
- 厄瓜多尔2025-07-11 至 2025-07-13

- 厄瓜多尔APPPEXPO ECUADOR广告展2025年
- 厄瓜多尔2025-07-11 至 2025-07-13

- 25年菲律宾第28届Graphic Expo广告展
- 菲律宾2025-07-17 至 2025-07-19

- 25年巴西Future Print-沃森会展中国总代项目
- 巴西2025-07-16 至 2025-07-19
- 人生如戏
- 2023-10-01浏览:615
- 祝愿祖国繁荣昌盛
- 2023-10-01浏览:341
- 爱的力量,就是一种高维度能量
- 2023-10-01浏览:721
- 泡脚
- 2023-10-01浏览:681
- 为什么是劳动创造了人本身?
- 2023-10-01浏览:7086
- 菊缘
- 2023-10-01浏览:3201
- 喜欢,就是会自动屏弃平日里的不喜欢
- 2023-10-01浏览:456
- 做出正确的努力
- 2023-10-01浏览:543
- 感谢你特别邀请
- 2023-10-01浏览:1340
- 分别
- 2023-09-30浏览:418
- 天生爱流浪的女人
- 2023-09-30浏览:624
- 皆得所“月”
- 2023-09-30浏览:593
- 有关帕金森的科普
- 2023-09-30浏览:153
- 【爱心小岛·以文会友】‖与人为善,取人为善
- 2023-09-30浏览:1571
- 心系员工 情暖中秋 | 客来福家居给全体员工派发中秋福利
- 2023-09-30浏览:382
- 梦想与现实
- 2023-09-30浏览:373
- 教师如何按时下班——《教师的工作力》记录
- 2023-09-30浏览:223
- 人到中年,宁缺毋滥
- 2023-09-30浏览:153
- 素人改造有感
- 2023-09-30浏览:1302
- 与文字的纠缠是一种执念
- 2023-09-30浏览:627