javascript - 不能从松露控制台铸币,尽管从测试文件铸币工作
问题描述
我一直在学习本教程以了解 NFT 和以太坊编程。它有点过时了,但一切都很顺利,直到我尝试从松露控制台铸币,这给了我以下错误:
Uncaught Error: Returned error: VM Exception while processing transaction: revert
我正在使用truffle
, ganache
, node
, web3
& openzeppelin
。
这是我得到错误的方式:
truffle console
> global = globalThis
如果我不这样做,我会得到“未定义全局”
> contract = await Color.deployed()
> await contract.mint('#fefefe')
这是我收到错误而不是 TX 收据的地方。
我显然对区块链开发很陌生(也没有对编程进行过如此实验),我尽我所能克服这个问题,但到目前为止是徒劳的。
我不明白为什么minting 从测试文件而不是从控制台工作。我在 ganache 的详细日志中进行了挖掘,但没有找到比错误消息更多的信息(这可能意味着我在网上阅读的很多内容)。
这是智能合约:
// contracts/Color.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.6; // updated from video to match Sol compiler
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
contract Color is ERC721Enumerable { // updated from video to match latest openzeppelin updates
string[] public colors; // array of colors available from outside
mapping(string => bool) _colorExists; // hash of colors with minted status (true/false)
constructor() ERC721("Color", "CLR") {}
function mint(string memory _color) public {
require(!_colorExists[_color]); // color can only be minted once
colors.push(_color);
uint256 _id = colors.length - 1;
_mint(msg.sender, _id); // calling the mint function from openzeppelin contract
_colorExists[_color] = true;
}
}
和测试文件:
const { assert } = require('chai');
/* eslint-disable no-undef */
const Color = artifacts.require('../contracts/Color.sol');
require('chai')
.use(require('chai-as-promised'))
.should();
contract('Color', (accounts) => {
before(async () => {
contract = await Color.deployed();
});
describe('deployment', async () => {
it('deployed successfully', async () => {
const address = contract.address;
console.log('address is:', address);
assert.notEqual(address, '');
assert.notEqual(address, 0x0);
assert.notEqual(address, null);
assert.notEqual(address, undefined);
});
it('has a name', async () => {
const name = await contract.name();
assert.equal(name, 'Color');
});
it('has the right symbol', async () => {
const symbol = await contract.symbol();
assert.equal(symbol, 'CLR');
});
});
describe('minting', async () => {
it('creates a new token', async () => {
const result = await contract.mint('#dc143c');
const totalSupply = await contract.totalSupply();
// SUCCESS
const event = result.logs[0].args;
assert.equal(totalSupply, 1, 'supply is OK');
assert.equal(event.tokenId.toNumber(), totalSupply - 1, 'token id is OK');
assert.equal(
event.from,
'0x0000000000000000000000000000000000000000',
'from address is OK'
);
assert.equal(event.to, accounts[0], 'to address is OK');
// FAILURE : cannot mint same color twice
await contract.mint('#dc143c').should.be.rejected;
});
});
describe('indexing', async () => {
it('lists colors', async () => {
await contract.mint('#6495ed');
await contract.mint('#000000');
await contract.mint('#8fbc8f');
const totalSupply = await contract.totalSupply();
assert.equal(totalSupply, 4, 'total supply is ok');
let result = [];
const expected = ['#dc143c', '#6495ed', '#000000', '#8fbc8f'];
for (let i = 1; i <= totalSupply; i++) {
let color = await contract.colors(i - 1);
result.push(color);
}
assert.equal(
result.join(','),
expected.join(','),
'listing colors is ok'
);
});
});
});
如果有人愿意更深入地挖掘,我会将所有内容都放在回购中。
非常感谢!
解决方案
推荐阅读
- java - 如何解决“/wd/hub/session java.io.IOException:org.openqa.grid.common.exception.GridException:无法从请求中提取功能:”
- flutter - 如何修复颤振中的“SocketException:OS错误:连接超时,errno = 110,地址= 192.168.5.10,端口= 49590”错误
- r - grep(),str_match() 匹配文件中的 [DATA] 字符串并在找到匹配后读取它下面的内容
- swift - 如何使用 Swift5 在 Xcode10.2.1 中添加 alamofire
- php - 尽管只执行一个查询,但命令不同步错误
- php - PHP foreach:从 JSON 解码数组中的多个条件搜索
- awk - 对具有不同列数的行使用 awk
- python - 有没有办法在没有箭袋的情况下绘制箭袋或将箭袋隐藏在 matplotlib 中?
- git - 删除所有已推送的 git 分支
- spring-rest - 有没有办法将所有控制器映射到单个文件