Have a contract thats extending ERC721, and has added minting functionality. I'm having issues testing this, because it seems like _owners gets refreshed between transactions somehow.
This is the contract itself (relevant parts). LoanManager doesn't override any interesting functions.
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "./LoanManager.sol";
contract LoanToken is LoanManager, ERC721URIStorage {
modifier onlyApprovedOrOwner(uint256 id) {
require(
_isApprovedOrOwner(msg.sender, id),
"LoanToken: Caller must have access to this token"
);
_;
}
constructor(address manager)
LoanManager(manager)
ERC721("PeriodicLoanToken", "PLT")
{}
/**
* @dev Mint a new loan token with given information
* @param maturity The maturity of the loan
* @param period The period of the loan
* @param totalBalance The total of service payments to the loan
*/
function mintLoan(
uint256 maturity,
uint256 period,
uint256 totalBalance
) external returns (uint256) {
require(
period >= 900,
"LoanToken: Period must be at least 900 seconds"
);
require(
maturity - block.timestamp >= period,
"LoanToken: Maturity must be at least one period after current block timestamp"
);
require(
totalBalance > 0,
"LoanToken: Total balance must be greater than 0"
);
uint256 id = _createLoan(maturity, period, totalBalance);
_mint(msg.sender, id);
require(_exists(id), "LoanToken: failed to mint loan");
return id;
}
function ownerOf(uint256 id) public view override returns (address) {
require(id == 0, "Only checking 0 for now, sorry");
require(_exists(id), "Doesn't exist");
return address(this);
}
}
Aaaand this is the test I'm writing for it:
const LoanToken = artifacts.require('LoanToken');
const truffleAssert = require('truffle-assertions');
const TIME_UNIT = {
DAY: 86400,
WEEK: 604800,
MONTH: 2592000,
};
contract('LoanToken', accounts => {
let instance;
const ownerAccount = accounts[0];
before(async () => {
instance = await LoanToken.deployed();
});
it('should allow minting loans', async () => {
let id = await instance.mintLoan.call(Date.now() - TIME_UNIT.WEEK, TIME_UNIT.DAY, 100);
assert.equal(id, 0);
// let l = await instance.loans(id);
// console.log(l);
const owner = await debug(instance.ownerOf.call(0));
assert.equal(owner, ownerAccount, 'Loan owner was not set to creator by default');
});
This test fails, with Doesn't Exist as the revert error. After doing some debugging with truffle breakpoints, I realized that _owners does get set during the minting process, but is back to being just Map(0) {}
(an empty map) as soon as second call begins.
Black magic. Why isn't this being persisted? Am I testing this wrong?
Environment
Using truffle