Truffle 从交易方法返回值

发布于 2025-01-11 02:04:04 字数 1946 浏览 0 评论 0原文

我正在按照本教程为 ERC1155 代币制作不可变的代币元数据。

https://coinsbench.com/complete-decentralized- erc-721-和-erc-1155-nfts-6c229adf9c9b

ERC1155 合约在这里...

https://github.com/Aurelien-Pelissier/Medium/blob/main/Fully%20Decentralized%20ERC-721%20and%20ERC-1155%20NFTs/contracts/ERC1155.sol

创建松露时测试我在上述合同中遇到了这种方法的问题。

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...

https://github.com/Aurelien-Pelissier/Medium/blob/main/Fully%20Decentralized%20ERC-721%20and%20ERC-1155%20NFTs/contracts/ERC1155.sol

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

π浅易 2025-01-18 02:04:04

.call() 仅用于不改变智能合约内部状态的函数,即不修改全局变量。

在您的情况下,您应该在 Solidity 中创建一个新的 view 函数,以便使用 call

function get_last_tokenID() public view returns (uint256) {
    return(_tokenIds.current());
}

您可以在此处查看有关 .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

function get_last_tokenID() public view returns (uint256) {
    return(_tokenIds.current());
}

You can check more details about the .call() method here https://blockheroes.dev/when-to-use-call-solidity-functions-truffle/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文