标签: ether

Soldity:通过地址映射进行迭代

我正在寻找一种方法来迭代 Solidity 中的映射。例如我有这个映射:

mapping (address => uint) private shares;

我想在一个函数中迭代所有地址,并根据它们的份额向它们发送以太币。

就像是:

function giveOutEth() onlyOwner returns (bool success){
for(uint i=0; i < shares.length ; i++){
//get the address and send a value
}
Run Code Online (Sandbox Code Playgroud)

}

我怎样才能实现这个目标?

谢谢

blockchain ethereum solidity smartcontracts ether

4
推荐指数
1
解决办法
1万
查看次数

在智能合约中接受以太

我正在尝试创建一个简单的智能合约来学习可靠性以及以太坊的运作方式.

根据我的理解,使用方法上的修改应付将使其接受一个值.然后我们从发件人中扣除并在其他地方添加,在此代码中我试图将其发送给合同的所有者.

contract  AcceptEth {
    address public owner;
    uint public bal;
    uint public price;
    mapping (address => uint) balance;

    function AcceptEth() {
        // set owner as the address of the one who created the contract
        owner = msg.sender;
        // set the price to 2 ether
        price = 2 ether;
    }

    function accept() payable returns(bool success) {
        // deduct 2 ether from the one person who executed the contract
        balance[msg.sender] -= price;
        // send 2 ether to the owner of this …
Run Code Online (Sandbox Code Playgroud)

solidity smartcontracts ether

3
推荐指数
1
解决办法
3319
查看次数

警告:不推荐使用从地址类型继承的合约成员“余额”。坚固性

警告:不推荐使用从地址类型继承的合约成员“余额”。将合约转换为“address”类型以访问成员,例如使用“address(contract).balance”代替。

我使用 Remix 编辑器在 Solidity 中收到此警告。

这是代码块:

function getSummary() public view returns(
    uint, uint, uint, uint, address
){
    return (
        minimumContribution,
        this.balance, // This is the warning line.
        requests.length,
        approversCount,
        manager
    );
}
Run Code Online (Sandbox Code Playgroud)

我尝试遵循警告的建议:

function getSummary() public view returns(
    uint, uint, uint, uint, address
){
    return (
        minimumContribution,
        address(contract).balance,
        requests.length,
        approversCount,
        manager
    );
}
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

ethereum solidity ether remix

3
推荐指数
1
解决办法
759
查看次数

我们如何在以太坊中生成多个随机数?

我希望我的智能合约在调用合约时返回7或8个唯一的随机数,范围从1到100。获得这种结果的最佳方法是什么?

blockchain ethereum solidity smartcontracts ether

3
推荐指数
1
解决办法
286
查看次数

可靠地获取 ERC20 代币持有者列表

是否可以从另一个 Solidity 合约中获取给定 ERC20 代币的代币持有者列表?

由于“余额”存储在大多数 ERC20 合约的映射中,我认为这是不可能的,因为您无法获得可靠映射的键列表。

有什么我错过的吗?或者这是不可能的?

谢谢!

ethereum solidity smartcontracts ether erc20

3
推荐指数
1
解决办法
2883
查看次数

Context.sol 在 Openzepplin 中的用途是什么

我是 Solidity 新手,并尝试使用 openzepplin 部署 ERC20 令牌。有一件事对我来说没有意义,那就是 context.sol 文件。从评论部分来看,context.sol 的主要功能似乎是实现 GSN 兼容合约,因此您不使用 msg.sender,而是使用 _msgSender()

abstract contract Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}
Run Code Online (Sandbox Code Playgroud)

从我有限的 Solidity 经验来看,它似乎对 msg.sender 做了完全相同的事情。

solidity ether

3
推荐指数
1
解决办法
1800
查看次数

安全帽以太:部署与部署

在下面的代码中:

console.log("Deploying contract....");
const simpleStorage = await simpleStorageFactory.deploy();
await simpleStorage.deployed();
Run Code Online (Sandbox Code Playgroud)

第 2 行部署了合约,我们得到了它。

为什么我们需要在 Line3 中调用已部署的方法?

ethereum ether hardhat

3
推荐指数
1
解决办法
685
查看次数

如何在 Solidity 合约中执行某些操作需要花费 1 以太币

我有一个以 Solidity 定义的合约,我想让它在调用特定函数时,合约的总成本增加 1 个以太币。我对如何ether在实践中使用有点模糊。我会为此使用普通的 int 吗?关键词在哪里ether发挥作用?

solidity ether

2
推荐指数
1
解决办法
3833
查看次数

为什么 BigNumber 和 etherjs 返回 0

我正在使用BigNumberetherjs,并且正在执行这个非常简单的操作:

const cost = BigNumber.from(3)
    .div(BigNumber.from(1000))
    .mul(BigNumber.from(3));

  console.log(cost.toString());
Run Code Online (Sandbox Code Playgroud)

它输出“0”,但应该是 0.09。

为什么?我究竟做错了什么?

javascript ether

1
推荐指数
1
解决办法
5167
查看次数