Skip to content

Commit 9a1538d

Browse files
committed
demo with native token sponsorship
1 parent 3725a72 commit 9a1538d

File tree

2 files changed

+101
-4
lines changed

2 files changed

+101
-4
lines changed

demo/paymaster.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ export function getPaymasterActions(chain: supportedChain): PaymasterActions {
3333
getPaymasterData: async (
3434
parameters: GetPaymasterDataParameters,
3535
): Promise<GetPaymasterDataReturnType> => {
36-
const validAfter = await getBlockTimestamp(chain);
37-
const validUntil = validAfter + 1_000_000n;
36+
const currentBlockTimestamp = await getBlockTimestamp(chain);
37+
const validAfter = currentBlockTimestamp - 1_000_000n;
38+
const validUntil = currentBlockTimestamp + 1_000_000n;
3839

3940
const postVerificationGas =
4041
parameters.paymasterPostOpGasLimit || BigInt(1e5);
@@ -89,9 +90,12 @@ export function getPaymasterActions(chain: supportedChain): PaymasterActions {
8990
parameters: GetPaymasterStubDataParameters,
9091
): Promise<GetPaymasterStubDataReturnType> => {
9192
return {
92-
paymasterAndData: (
93+
paymaster: getAddress(pmAddress),
94+
paymasterData: (
9395
await getPaymasterActions(chain).getPaymasterData(parameters)
94-
).paymasterAndData as Hex,
96+
).paymasterData as Hex,
97+
paymasterPostOpGasLimit: BigInt(1e6),
98+
paymasterVerificationGasLimit: BigInt(1e6),
9599
};
96100
},
97101
};

src/mocks/DemoNativeNFT.sol

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//SPDX-License-Identifier: Unlicense
2+
pragma solidity ^0.8.20;
3+
4+
import {ERC721URIStorage} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
5+
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
6+
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
7+
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
8+
import {AccessControl} from "@openzeppelin/contracts/access/AccessControl.sol";
9+
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
10+
11+
/// @title DemoNativeNFT
12+
/// @notice
13+
/// @dev
14+
contract DemoNativeNFT is Ownable, AccessControl, ERC721URIStorage {
15+
using Strings for uint256;
16+
17+
uint256 public tokenIdCounter;
18+
string public baseTokenURI;
19+
uint256 public mintPrice;
20+
21+
event PaymentTokenSet(address indexed paymentToken);
22+
event MintPriceSet(uint256 indexed mintPrice);
23+
event BaseURISet(string indexed baseURI);
24+
25+
constructor(address _owner, string memory _baseTokenURI, uint256 _mintPrice)
26+
Ownable(_owner)
27+
ERC721("DemoNFT", "DMON")
28+
{
29+
tokenIdCounter = 0;
30+
baseTokenURI = _baseTokenURI;
31+
mintPrice = _mintPrice;
32+
}
33+
34+
////////////////////////////////////////////////////////////////////////
35+
/// Internal functions
36+
////////////////////////////////////////////////////////////////////////
37+
38+
function _baseURI() internal view override returns (string memory) {
39+
return baseTokenURI;
40+
}
41+
42+
////////////////////////////////////////////////////////////////////////
43+
/// Owner actions
44+
////////////////////////////////////////////////////////////////////////
45+
46+
function _transferOwnership(address newOwner) internal virtual override {
47+
super._transferOwnership(newOwner);
48+
_grantRole(DEFAULT_ADMIN_ROLE, newOwner);
49+
}
50+
51+
/// @notice set a new baseTokenURI
52+
/// @param _baseTokenURI the new base token URI
53+
function setBaseURI(string memory _baseTokenURI) public onlyOwner {
54+
baseTokenURI = _baseTokenURI;
55+
emit BaseURISet(_baseTokenURI);
56+
}
57+
58+
function setMintPrice(uint256 _mintPrice) public onlyOwner {
59+
mintPrice = _mintPrice;
60+
emit MintPriceSet(_mintPrice);
61+
}
62+
63+
////////////////////////////////////////////////////////////////////////
64+
/// Minter action
65+
////////////////////////////////////////////////////////////////////////
66+
67+
/// @notice mint a NFT
68+
69+
function mint(string memory _tokenURI) public payable returns (uint256) {
70+
require(msg.value == mintPrice, "Insufficient ETH sent");
71+
72+
unchecked {
73+
++tokenIdCounter;
74+
}
75+
uint256 newItemId = tokenIdCounter;
76+
_safeMint(msg.sender, newItemId);
77+
_setTokenURI(newItemId, _tokenURI);
78+
return newItemId;
79+
}
80+
81+
////////////////////////////////////////////////////////////////////////
82+
/// Interface functions
83+
////////////////////////////////////////////////////////////////////////
84+
85+
function supportsInterface(bytes4 interfaceId)
86+
public
87+
view
88+
override(ERC721URIStorage, AccessControl)
89+
returns (bool)
90+
{
91+
return ERC721URIStorage.supportsInterface(interfaceId) || AccessControl.supportsInterface(interfaceId);
92+
}
93+
}

0 commit comments

Comments
 (0)