Truffle 从交易方法返回值
我正在按照本教程为 ERC1155 代币制作不可变的代币元数据。
https://coinsbench.com/complete-decentralized- erc-721-和-erc-1155-nfts-6c229adf9c9b
ERC1155 合约在这里...
创建松露时测试我在上述合同中遇到了这种方法的问题。
function mintToken(string memory tokenURI) public returns (uint256){
//Do stuff
return tokenId;
}
如果我将 Truffle 中的函数称为“正常”,我会得到交易收据并且余额是正确的。 (测试 1 - 通过)
如果我使用 call 方法,那么我会得到 tokenId,但是如果我检查帐户余额,我会得到 0。(测试 2 - 失败)
const ERC = artifacts.require("MyNFT_ERC1155");
contract("ERC1155", (accounts) => {
beforeEach(async () => {
erc = await ERC.new();
});
describe("Mint", async () => {
it("Mint and return the reciept", async () => {
let tx = await erc.mintToken("https://web-site.com/something.json", "10");
console.log("Transaction reciept");
console.log(tx);
//First token minted has known id of 0. This is not known in prod.
const balance = await erc.balanceOf(accounts[0], 0);
assert.equal(parseInt(balance), 10);
});
it("Mint and return the token id", async () => {
let tokenId = await erc.mintToken.call(
"https://web-site.com/something.json",
"10"
);
console.log("Token Id");
console.log(tokenId);
const balance = await erc.balanceOf(accounts[0], tokenId);
assert.equal(parseInt(balance), 10);
});
});
});
似乎我需要使用“call”来获取tokenId,但是我如何等待交易完成以便更新余额?
如果我在其他测试中使用此合约,我如何获取 tokenId 并确保令牌已铸造。
问候
I am following this tutorial to make immutable token metadata for ERC1155 tokens.
https://coinsbench.com/fully-decentralized-erc-721-and-erc-1155-nfts-6c229adf9c9b
ERC1155 contract is here...
When creating a Truffle test i am experiencing issues with this method in the above contract.
function mintToken(string memory tokenURI) public returns (uint256){
//Do stuff
return tokenId;
}
If i call the function in Truffle as 'normal', i get the transaction reciept and balance is correct. (TEST 1 - PASSES)
If i use the call method then i get the tokenId back, but if i check the balance of the account i get 0. (TEST 2 - FAILS)
const ERC = artifacts.require("MyNFT_ERC1155");
contract("ERC1155", (accounts) => {
beforeEach(async () => {
erc = await ERC.new();
});
describe("Mint", async () => {
it("Mint and return the reciept", async () => {
let tx = await erc.mintToken("https://web-site.com/something.json", "10");
console.log("Transaction reciept");
console.log(tx);
//First token minted has known id of 0. This is not known in prod.
const balance = await erc.balanceOf(accounts[0], 0);
assert.equal(parseInt(balance), 10);
});
it("Mint and return the token id", async () => {
let tokenId = await erc.mintToken.call(
"https://web-site.com/something.json",
"10"
);
console.log("Token Id");
console.log(tokenId);
const balance = await erc.balanceOf(accounts[0], tokenId);
assert.equal(parseInt(balance), 10);
});
});
});
It seems like i need to use 'call' to get the tokenId, but how do i wait for the transaction to complete so that the balance is updated?
If i use this contract in other tests, how do i get the tokenId AND make sure the token was minted.
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.call() 仅用于不改变智能合约内部状态的函数,即不修改全局变量。
在您的情况下,您应该在 Solidity 中创建一个新的 view 函数,以便使用 call
您可以在此处查看有关 .call() 方法的更多详细信息 https://blockheroes.dev/when-to-use-call-solidity-functions-truffle/
.call() is used only for functions that do not change the internal state of the smart contract i.e. do not modify the global variables.
In your case, you should make a new view function in solidity in order to use call
You can check more details about the .call() method here https://blockheroes.dev/when-to-use-call-solidity-functions-truffle/