有一个简单的 Solidity 合约:
contract SellStuff{
address seller;
string name;
string description;
uint256 price;
function sellStuff(string memory _name, string memory _description, uint256 _price) public{
seller = msg.sender;
name = _name;
description = _description;
price = _price;
}
function getStuff() public view returns (
address _seller,
string memory _name,
string memory _description,
uint256 _price){
return(seller, name, description, price);
}
}
Run Code Online (Sandbox Code Playgroud)
并运行 javascript 测试,如下所示:
var SellStuff= artifacts.require("./SellStuff.sol");
// Testing
contract('SellStuff', function(accounts){
var sellStuffInstance;
var seller = accounts[1];
var stuffName = "stuff 1";
var …Run Code Online (Sandbox Code Playgroud) 描述
获得随机数需要很长时间。执行 getRandomNumber 函数后,几分钟后我就可以与随机数进行交互。
基本上,我单击 getRandomNumber 并必须等待 2-3 分钟,直到随机数显示在 randomResult 变量中。
重现步骤
附加信息
我不确定这种行为是故意的还是我是否需要更改代码。理想情况下,我希望在 getRandomNumber 函数完成执行后获得 randomResult 的值。现在我不知道 randomResult 的值什么时候会出现。
我有一个函数需要调用另一个合约上的传输方法。我希望从原始调用者的地址而不是合约中调用转移方法。是否可以?
这是代码:
function buyGameBundle(string calldata id) external nonReentrant {
structGameBundles memory currentItem = _gameBundles[id];
require(currentItem.exists == true, "bundle does not exists");
require(currentItem.totalSupply > 0, "there are no more bundles left");
if (currentItem.cost > 0) {
erc20.transfer(_feesAccount, currentItem.cost);
}
currentItem.totalSupply = currentItem.totalSupply.sub(1);
_gameBundles[id] = currentItem;
emit BuyGameBundle(_msgSender(), id, currentItem.cost);
}
Run Code Online (Sandbox Code Playgroud) 只是想知道是否有一种方法可以将长字符串拆分为多行?我找不到任何类型的行继续字符,如果您尝试使用这样的两行,则会抛出编译错误。连接字符串似乎也很复杂
string memory s = "This is a very long line of text which I would like to split over
several lines";
Run Code Online (Sandbox Code Playgroud)
连接字符串似乎也很复杂。我只需要将很长的字符串放在很长的线上吗?
想知道是否可以在字符串内存和字符串 calldata 之间进行转换,以便使用仅适用于字符串 calldata 的 string[start : end] 形式的索引。这个功能似乎有效:
function splice(string calldata source, int startPos, int numchars) public pure returns(string memory) {
if (startPos > int(length(sourcestring))) return "";
int start = startPos -1;
int end = startPos + (numchars -1);
string memory retval = string(source[uint(start) : uint(end)]);
return retval;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我将参数更改source为字符串内存,则会出现错误
,因为显然获取子字符串的string memory retval = string(source([uint(start) : uint(end)])
形式适用于字符串而不是字符串,并且没有明显的方法将 a 转换为 a 。sourcestring[start : end]calldatamemorystring memorystring calldata
有什么办法可以做到这一点吗?
我正在尝试将 ExpressJS 中的小型服务器 Web 应用程序连接到 BSC 区块链,但没有成功。
根据我的理解,Web3 库提供了所有必需的内容,我可以通过调用进行连接
new Web3(new Web3.providers.HttpProvider(urlToRpcNode))
按照这个,我将以下内容放入我的函数中,该函数充当 Web3 对象 init:
return new Web3(new Web3.providers.HttpProvider(https://bsc-dataseed1.binance.org));
根据 BscScan,给定的参数是 BSC 的 RPC 节点的 URL https://bscscan.com/apis#rpc。
不幸的是,等待这个调用只会永远卡住并且不会返回任何内容。
我究竟做错了什么?任何帮助表示赞赏。
编辑:
我找到了币安文档的另一个链接,其中概述了连接 BSC 的具体流程。他们是这样做的:
const web3 = new Web3('https://bsc-dataseed1.binance.org:443');
https://docs.binance.org/smart-chain/developer/create-wallet.html#connect-to-bsc-network
不幸的是,这给了我同样的结果。
我有一个 API 端点,它按严格的顺序创建并发送一些事务。因为我不等待这些交易的结果,所以我为每个交易指定一个随机数,以便以正确的顺序执行它们。
该终端节点是使用 AWS Lambda 函数构建的。因此,如果我有许多并发请求,则 lambda 以并发模式运行。在这种情况下,多个并发实例可以获得相同的随机数(我正在使用eth.getTransactionCount方法来获取最新的事务计数)并使用相同的随机数发送一些事务。因此,我收到错误,因为它没有创建新事务,而是尝试替换现有事务。
基本上,我需要一种方法来检查在交易发送之前是否已经获取了随机数,或者以某种方式保留随机数(是否可能?)。
返回错误:处理交易时 VM 异常:恢复 ERC20:转账金额超出限额
代码:
ERC20Tokens[tokenTicker].token.approve(ERC20Tokens[tokenTicker].tokenHolder, 10);
emit tokenOwnerBalance(ERC20Tokens[tokenTicker].token.balanceOf(ERC20Tokens[tokenTicker].tokenHolder));
ERC20Tokens[tokenTicker].token.transferFrom(ERC20Tokens[tokenTicker].tokenHolder, address(this), 1);
Run Code Online (Sandbox Code Playgroud)
日志:
[
{
"from":"0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
"topic":"0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925",
"event":"Approval",
"args":{
"0":"0x7A2946b37399fa3F1C9EF81c7Bcf94AE1099D18F",
"1":"0x95Ba4cF87D6723ad9C0Db21737D862bE80e93911",
"2":"10",
"owner":"0x7A2946b37399fa3F1C9EF81c7Bcf94AE1099D18F",
"spender":"0x95Ba4cF87D6723ad9C0Db21737D862bE80e93911",
"value":"10"
}
},
{
"from":"0x7A2946b37399fa3F1C9EF81c7Bcf94AE1099D18F",
"topic":"0xcff530ae4ada35c1dd7ac314ef643b9eb7ae40665958ad5899e2fbc18865444a",
"event":"tokenOwnerBalance",
"args":{
"0":"31000000"
}
}
]
Run Code Online (Sandbox Code Playgroud) 我想迭代以太坊 ERC-721 合约的所有代币 ID。有些合约有计数 id (0, 1, 2, 3, ...),这很容易,但有些合约有随机 id,例如https://etherscan.io/token/0xf87e31492faf9a91b02ee0deaad50d51d56d5d4d#inventory
遗憾的是 etherscan 只显示最后使用的 10000 个令牌 id,但我想迭代所有 79490 个。有没有办法实现这一点?对我来说,一切都很好。使用一些 API 设置我自己的以太坊节点。
我无法找到一种方法将空字节传递给混音时的 Solidity 函数。这是我的功能
function checkEmptyBytes(bytes calldata _data) external pure returns (string memory){
if (_data.length > 0){
return "NOT_ZERO";
}
return "ZER0";
}
Run Code Online (Sandbox Code Playgroud)
在重新混合时,我必须传递 _data 以使其返回“ZERO”
ethereum ×10
solidity ×6
blockchain ×4
web3js ×2
binance ×1
chainlink ×1
erc20 ×1
javascript ×1
nft ×1
node.js ×1