You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
e.g. Our goal is to make '04/01/2024 00:00:00' in the date parameter into a Coordinated Universal Time (UTC) of Unix Timestamp, that is, number of seconds that have elapsed since 1970-01-01T00:00:00Z UTC
The DateTimeOffset.ToUnixTimeSeconds function will assume variable date is local time and auto convert it to UTC (-8hr from HK). It becomes '03/01/2024 16:00:00' which is not what we want
To correct it, we need to find out the adjustment: ts = TimeZoneInfo.Local.GetUtcOffset(date)
Then, we add back the adjustment (ts.Hours * 3600) because 1 Hr has 3600 seconds.
The "image" is not a png file but an URL that dynamically draw the graphics of ID and booking date string
The 'baseURI' is obtained from appsettings.json, "imageURI", which is "https://xxxx.yyyy.com/"
Hence, in the 'Mint' function:
Note that the ID is unknown now
string tokenURI = $"{baseURI}Metadata?date={date.ToString("yyyy-MM-dd")}(UTC)&ID=";
var f_mint = contract.GetFunction("mintMEET");
var gas = await f_mint.EstimateGasAsync(account.Address, null, null, account.Address, unixDateAdj, tokenURI).ConfigureAwait(false);
var receipt = await f_mint.SendTransactionAndWaitForReceiptAsync(account.Address, gas, null, null, null, account.Address, unixDateAdj, tokenURI).ConfigureAwait(false);
The corresponding function in MeetingRoom.sol
abi.encodePacked(a,b) is to concat strings a & b
This is to append the NFT ID just created to the URI
_setTokenURI is to set the unchangeable URI to this NFT
function mintMEET(address recipient, uint256 bookingDate, string memory tokenURI_) external returns (uint256) {
require((_bookingDates[bookingDate] == 0), "Booking date already exists");
require(bookingDate >= (block.timestamp - (block.timestamp % 1 days)), "Booking date must not be in the past");
_tokenIds.increment();
uint256 newTokenId = _tokenIds.current();
_nfts[newTokenId] = NFT(bookingDate, true);
_bookingDates[bookingDate] = newTokenId;
_safeMint(recipient, newTokenId);
string memory tokenURI = string(abi.encodePacked(tokenURI_, Strings.toString(newTokenId)));
_setTokenURI(newTokenId, tokenURI);
emit Mint(recipient, newTokenId, bookingDate);
return newTokenId;
}
For unit testing without a wallet, all these can be ignored for the time being
Unit Test Design
date0: the latest booking date. The unit test will use dates after date0 and hence can be rerun many times
Tid1, Tid2: date0 + 1 & date0 + 2. Mint two NFTs for all unit testings.
Tid3: Get the earliest booking date. If already expired, continue running. Otherwise, stop
First time execution: Completed with a message that the 'burn' function cannot be tested until tomorrow.
Get Expired NFT 1
List NFT Id = 1
NFT: Id=1, Owner=0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1, BookingDate=05-01-2024 UTC, Occupied=occupied
NFT 1 not expired yet. Wait for tomorrow for the following tests to run.
When executed the next day, all tests are successful