前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Solidity:在合约中创建其它合约

Solidity:在合约中创建其它合约

作者头像
孟斯特
发布于 2024-05-30 06:36:27
发布于 2024-05-30 06:36:27
17700
代码可运行
举报
文章被收录于专栏:code人生code人生
运行总次数:0
代码可运行

在Solidity中,new关键字用于创建一个新的智能合约实例。当你使用new关键字创建一个新的合约实例时,Solidity会在区块链上部署一个新的合约,并返回新合约的地址。自0.8.0版本开始,new关键字通过指定salt选项支持create2特性。

以下是使用new关键字创建新的合约实例的基本语法:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
ContractName variableName = new ContractName(arguments);

在这里,ContractName是你要创建的合约的名称,variableName是你要给新创建的合约实例的变量名,arguments是传递给新合约构造函数的参数(如果有的话)。

例如,假设你有一个名为MyContract的合约,它有一个接受一个uint类型参数的构造函数,你可以使用以下代码创建一个新的MyContract实例:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
MyContract myContract = new MyContract(123);

在这个例子中,new MyContract(123)会在区块链上部署一个新的MyContract合约,并将构造函数的参数设置为123。然后,它会返回新合约的地址,并将这个地址赋值给myContract变量。

需要注意的是,使用new关键字创建新的合约实例会消耗gas,因为它涉及到在区块链上部署新的合约。因此,你需要确保你有足够的gas来完成这个操作。此外,新创建的合约的代码和数据将被永久存储在区块链上,因此,你需要谨慎地管理你的合约代码和数据,以避免浪费存储空间。

示例

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

contract Car {
    address public owner;
    string public color;

    constructor(address _owner, string memory _color) {
        owner = _owner;
        color = _color;
    }

    function getOwner() public view returns (address) {
        return owner;
    }

    function getColor() public view returns (string memory) {
        return color;
    }
}

contract CarStore {
    Car[] public cars;

    function create(address _owner, string memory _color) public {
        Car car = new Car(_owner, _color);
        cars.push(car);
    }

    function createWithSalt(
        address _owner,
        string memory _color,
        bytes32 _salt
    ) public {
        Car car = (new Car){salt: _salt}(_owner, _color);
        cars.push(car);
    }

    function getCar(uint256 index)
        public
        view
        returns (address, string memory)
    {
        Car car = cars[index];
        // 即使变量被声明为public,我们也不能在合约外部直接访问它们。只能通过调用自动生成的getter函数来访问这些变量。
        // return (car.owner,car.color);    // 会报错
        // return (car.owner(),car.color());
        return (car.getOwner(), car.getColor());
    }
}

上面的示例中包含两个合约:CarCarStore

Car合约代表一辆汽车,它有两个状态变量:ownercolor,分别表示汽车的所有者和颜色。这两个状态变量都被声明为public,因此Solidity会自动为它们生成getter函数。此外,Car合约还有两个自定义的getter函数:getOwnergetColor,它们分别返回汽车的所有者和颜色。•CarStore合约代表一个汽车商店,它有一个状态变量cars,用于存储商店中的所有汽车。cars变量是一个Car合约的数组,每个元素都是一个Car合约的实例。•create函数:创建一个新的Car合约实例,并将其添加到cars数组中。这个函数接受两个参数:汽车的所有者和颜色。•createWithSalt函数:与create函数类似,但它使用create2特性创建新的Car合约实例。create2特性允许你使用一个salt值来影响新合约的地址。这个函数接受三个参数:汽车的所有者、颜色和salt值。•getCar函数:返回cars数组中指定索引的汽车的所有者和颜色。这个函数接受一个参数:汽车的索引。

声明:本作品采用署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)[1]进行许可,使用时请注明出处。 Author: mengbin[2] blog: mengbin[3] Github: mengbin92[4] cnblogs: 恋水无意[5] 腾讯云开发者社区:孟斯特[6]


References

[1] 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0): https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh [2] mengbin: mengbin1992@outlook.com [3] mengbin: https://mengbin.top [4] mengbin92: https://mengbin92.github.io/ [5] 恋水无意: https://www.cnblogs.com/lianshuiwuyi/ [6] 孟斯特: https://cloud.tencent.com/developer/user/6649301

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-05-29,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 孟斯特 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
MySQL5.7版本sql_mode=only_full_group_by问题解决办法
原因分析:MySQL5.7版本默认设置了 mysql sql_mode = only_full_group_by 属性,导致报错。
全栈程序员站长
2022/08/24
6550
MySQL5.7版本sql_mode=only_full_group_by问题解决办法
MySQL5.7之group by语法问题
使用group by 进行分组查询时,提示异常: SELECT list is not in GROUP BY clause and contains nonaggregated column ‘XXX’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode =only_full_group_by
执笔记忆的空白
2020/12/24
9040
mysql5.7 group by语法 1055
1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'information_schema.ENGINES.ENGINE' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
陈灬大灬海
2019/07/03
1.4K0
mysql5.7 group by语法 1055
MySQL报错1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated colu
1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'fbjs.mscc.ContactTime' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by, Time: 0.000000s
JaneYork
2023/10/11
5830
MySQL报错1055 - Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated colu
MySQL中使用group by出现1055错误的解决办法
解释:ONLY_FULL_GROUP_BY: 对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么将认为这个SQL是不合法的,因为列不在GROUP BY从句中
悟空宇
2024/03/11
9020
MySQL中使用group by出现1055错误的解决办法
[MySQL] 5.7版本以上group by语句报1055错误问题
1. 在5.7版本以上mysql中使用group by语句进行分组时, 如果select的字段 , 不是完全对应的group by后面的字段 , 有其他字段 , 那么就会报这个错误
唯一Chat
2019/09/10
6800
[MySQL] 5.7版本以上group by语句报1055错误问题
MySQL:解决MySQL5.7以上使用GROUP BY语句时报错
使用GROUP BY 语句违背了sql_mode=only_full_group_by。因为mysql版本5.7之后默认的模式是ONLY_FULL_GROUP_BY。官网文档的原话:
Jensen_97
2023/07/20
4.5K0
MySQL:解决MySQL5.7以上使用GROUP BY语句时报错
ERROR 1055 (42000): Expression #1 of SELECT list is not in
字面意思理解是sql_model=only_full_group_by限制了,导致在以往MYSQL版本中能正常查询的SQL,在5.7不能用了
码农编程进阶笔记
2021/07/20
1.5K0
MySQL报错1055 – Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated 解决方法
今天在进行数据迁移时,使用Navicat连接数据库进行连接时,由于 SQL语句中使用了 group by分组函数,结果报了如下错误:
Java架构师必看
2021/03/22
2.4K0
MySQL报错1055 – Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated 解决方法
MySQL 排错-解决MySQL非聚合列未包含在GROUP BY子句报错问题
SELECT id, name, count(*) AS cnt FROM case_table GROUP BY name
授客
2019/09/29
1.8K0
已解决:mysql报错:1055 - this is incompatible with sql_mode=only_full_group_by
[Err] 1055 - Expression #2 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'tase1.ai.home_url' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
陈哈哈
2020/07/06
5.1K0
MySQL遇见SELECT list is not in GROUP BY clause and contains nonaggre的问题
方法一 使用命令行或者数据库客户端执行SQL 1.SQL语句,select @@global.sql_mode查询
流柯
2020/08/28
2.5K0
Java中ONLY_FULL_GROUP_BY错误
将数据从旧的数据库(MySQL5.1)迁移到新的数据库(MySQL5.7)中时,查询语句不变的情况下,报如下错;
村雨遥
2019/11/06
9990
Java中ONLY_FULL_GROUP_BY错误
MySQL使用group by分组时报错
第一项默认开启ONLY_FULL_GROUP_BY了,导致出现了错误,需要把它的默认关掉。
Autooooooo
2020/11/09
1.8K0
MySQL使用group by分组时报错
mysql5.7在使用group by的注意事项
这是因为在mysql5.7中开启了sql_mode中的“only_full_group_by”,而这个在执行以往版本中带有group by的语句时就会报错。
别团等shy哥发育
2023/02/25
7330
mysql5.7在使用group by的注意事项
mysql5.7.5版本的sql_mode=only_full_group_by问题
mysql5.7.5版本默认设置了sql_mode=only_full_group_by,造成了之前使用的sql语句有些会出现sql_mode=only_full_group_by错误,接下来为解决此类问题的方法。
余生大大
2022/10/25
4530
MySQL的sql_mode模式说明及设置
sql_mode是个很容易被忽视的变量,默认值是空值,在这种设置下是可以允许一些非法操作的,比如允许一些非法数据的插入。在生产环境必须将这个值设置为严格模式,所以开发、测试环境的数据库也必须要设置,这样在开发测试阶段就可以发现问题。
星哥玩云
2022/08/18
2.1K0
mysql 5.7 sql_mode设置问题
在mysql较低版本中,对SQL语句并没有严格的限制检查,在5.7及以上版本开启严格模式,在插入数据的时候,如果字段没有设置默认值,则会报类似于这样的错误:“Field ‘title’ doesn’t have a default value”。
参谋带个长
2022/04/28
1.3K0
使用GROUP BY 发生错误 SELECT list is not in GROUP BY clause .......... 解决
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'test.w.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
友儿
2022/09/11
2.2K0
MySQL only_full_group_by 1055 报错的三种解决方案,临时关闭有影响吗?
本文首发:MySQL only_full_group_by 1055报错的三种解决方案,临时关闭有影响吗?
蒋川
2021/12/02
6.3K0
MySQL only_full_group_by 1055 报错的三种解决方案,临时关闭有影响吗?
推荐阅读
相关推荐
MySQL5.7版本sql_mode=only_full_group_by问题解决办法
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档