'Truffle return value from transaction method [duplicate]
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
Solution 1:[1]
.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/
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Aurélien Pélissier |
