Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup contract references in auth-server and nft-quest #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 1 addition & 22 deletions examples/nft-quest-contracts/deploy/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { formatEther, parseEther } from "ethers";
import fs from "fs";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import path from "path";

import { deployContract, getProvider, getWallet } from "./utils";

export default async function (hre: HardhatRuntimeEnvironment) {
export default async function () {
const provider = getProvider();

const baseTokenURI = "https://nft.zksync.dev/nft/metadata.json";
Expand All @@ -16,24 +13,6 @@ export default async function (hre: HardhatRuntimeEnvironment) {
console.log("NFT CONTRACT: ", await nftContract.getAddress());
console.log("PAYMASTER CONTRACT: ", await paymasterContract.getAddress());

if (hre.network.config.ethNetwork.includes("localhost")) {
// Update the .env.local file with the contract addresses for NFT Quest app
const envFilePath = path.join(__dirname, "../../nft-quest/.env.local");

// Check if the .env.local file exists, if not, create it
if (!fs.existsSync(envFilePath)) {
fs.writeFileSync(envFilePath, "", { encoding: "utf8" });
console.log(`.env.local file has been created at ${envFilePath}`);
}
const nftContractAddress = await nftContract.getAddress();
const paymasterContractAddress = await paymasterContract.getAddress();

const envContent = `NUXT_PUBLIC_CONTRACTS_NFT=${nftContractAddress}\nNUXT_PUBLIC_CONTRACTS_PAYMASTER=${paymasterContractAddress}\n`;

fs.writeFileSync(envFilePath, envContent, { encoding: "utf8" });
console.log(`.env.local file has been updated at ${envFilePath}`);
}

// fund the paymaster contract with enough ETH to pay for transactions
const wallet = getWallet();
await (
Expand Down
10 changes: 3 additions & 7 deletions examples/nft-quest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ pnpm nx deploy contracts
pnpm nx deploy:local nft-quest-contracts
```

The contract addresses for the NFT Quest app will be set in `.env.local`. This
.env file will override the values set in the `runtimeConfig` in
`nuxt.config.ts`.

You may also need to update the contract addresses for the Auth Server in
`/packages/auth-server/stores/client.ts` under the
`contractsByChain[zksyncInMemoryNode.id]`
Contract addresses are defined in the `/packages/auth-server/abi` directory. For
local development using In Memory Node, edit the contracts' `addressByChain` for
`260`.

```sh
# Start the website and Auth Server
Expand Down
2 changes: 2 additions & 0 deletions examples/nft-quest/abi/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as Nft from "./nft";
export * as Paymaster from "./paymaster";
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export const ZeekNftQuestAbi = [
import type { AddressByChain } from "./types";

export const addressByChain: AddressByChain = {
300: "0x4D533d3B20b50b57268f189F93bFaf8B39c36AB6",
260: "0x111C3E89Ce80e62EE88318C2804920D4c96f92bb",
};

export const Abi = [
{
inputs: [
{
Expand Down
6 changes: 6 additions & 0 deletions examples/nft-quest/abi/paymaster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { AddressByChain } from "./types";

export const addressByChain: AddressByChain = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be better to store addresses in the same way as we do in auth-server. Less files, everything is in one place and less repetitive code

300: "0x60eef092977DF2738480a6986e2aCD10236b1FA7",
260: "0x4B5DF730c2e6b28E17013A1485E5d9BC41Efe021",
};
5 changes: 5 additions & 0 deletions examples/nft-quest/abi/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type { Address } from "viem";

export type AddressByChain = {
[k in SupportedChainId]: Address;
};
11 changes: 8 additions & 3 deletions examples/nft-quest/composables/useMintNft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,25 @@ import { waitForTransactionReceipt, writeContract } from "@wagmi/core";
import type { Address } from "viem";
import { getGeneralPaymasterInput } from "viem/zksync";

import { Nft, Paymaster } from "~/abi";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think abi folder should just store contract abi's, not other information.

import type { SupportedChainId } from "~/stores/connector";

export const useMintNft = async (_address: MaybeRef<Address>) => {
const address = toRef(_address);

return await useAsyncData("mintZeek", async () => {
const runtimeConfig = useRuntimeConfig();
const { wagmiConfig } = storeToRefs(useConnectorStore());
const chainId = runtimeConfig.public.defaultChainId as SupportedChainId;

const mintingForAddress = address.value;

const transactionHash = await writeContract(wagmiConfig.value, {
address: runtimeConfig.public.contracts.nft as Address,
abi: nftAbi,
address: Nft.addressByChain[chainId] as Address,
abi: Nft.Abi,
functionName: "mint",
args: [mintingForAddress],
paymaster: runtimeConfig.public.contracts.paymaster as Address,
paymaster: Paymaster.addressByChain[chainId] as Address,
paymasterInput: getGeneralPaymasterInput({ innerInput: "0x" }),
});

Expand Down
8 changes: 1 addition & 7 deletions examples/nft-quest/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import type { Chain } from "viem";

declare module "nuxt/schema" {
interface PublicRuntimeConfig {
chain: Chain;
contracts: {
nft: `0x${string}`;
paymaster: `0x${string}`;
};
defaultChainId: number;
baseUrl: string;
authServerUrl: string;
explorerUrl: string;
Expand Down
12 changes: 2 additions & 10 deletions examples/nft-quest/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ export default defineNuxtConfig({
$production: {
runtimeConfig: {
public: {
chain: zksyncSepoliaTestnet,
contracts: {
nft: "0x4D533d3B20b50b57268f189F93bFaf8B39c36AB6",
paymaster: "0x60eef092977DF2738480a6986e2aCD10236b1FA7",
},
defaultChainId: zksyncSepoliaTestnet.id,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just do same as in auth server, use default chain env by default or testnet if the env is missing. I thought that was the idea of this PR, but env isn't used

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Runtimeconfig vars will automatically get replaced if it follows the naming scheme: "NUXT_PUBLIC_{public runtime config prop}". Writing it in is redundant and ignored if the env var is defined.

baseUrl: "https://nft.zksync.dev",
authServerUrl: "https://auth-test.zksync.dev/confirm",
explorerUrl: "https://sepolia.explorer.zksync.io",
Expand Down Expand Up @@ -57,11 +53,7 @@ export default defineNuxtConfig({
},
runtimeConfig: {
public: {
chain: zksyncInMemoryNode,
contracts: {
nft: "0x111C3E89Ce80e62EE88318C2804920D4c96f92bb",
paymaster: "0x4B5DF730c2e6b28E17013A1485E5d9BC41Efe021",
},
defaultChainId: zksyncInMemoryNode.id,
baseUrl: "http://localhost:3006",
authServerUrl: "http://localhost:3002/confirm",
explorerUrl: "http://localhost:3010",
Expand Down
28 changes: 16 additions & 12 deletions examples/nft-quest/stores/connector.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
import { connect, createConfig, type CreateConnectorFn, disconnect, getAccount, http, reconnect, watchAccount } from "@wagmi/core";
import { zksyncInMemoryNode, zksyncLocalNode, zksyncSepoliaTestnet } from "@wagmi/core/chains";
import { type Address, type Hash, parseEther } from "viem";
import { zksyncInMemoryNode, zksyncSepoliaTestnet } from "@wagmi/core/chains";
import { type Address, parseEther } from "viem";
import { callPolicy, zksyncSsoConnector } from "zksync-sso/connector";

import { ZeekNftQuestAbi } from "@/abi/ZeekNFTQuest";
import { Nft } from "~/abi";

export type SupportedChainId = (typeof supportedChains)[number]["id"];

const supportedChains = [
zksyncSepoliaTestnet,
zksyncInMemoryNode,
] as const;

export const useConnectorStore = defineStore("connector", () => {
const runtimeConfig = useRuntimeConfig();
const supportedChains = [
zksyncSepoliaTestnet,
zksyncInMemoryNode,
zksyncLocalNode,
] as const;
const chain = supportedChains.filter((x) => x.id == runtimeConfig.public.chain.id)[0];

const chainId = runtimeConfig.public.defaultChainId;
const chain = supportedChains.filter((x) => x.id == chainId)[0];
type SupportedChainId = (typeof supportedChains)[number]["id"];
if (!chain) throw new Error(`Chain with id ${runtimeConfig.public.chain.id} was not found in supported chains list`);
if (!chain) throw new Error(`Chain with id ${chainId} was not found in supported chains list`);

const connector = zksyncSsoConnector({
metadata: {
Expand All @@ -25,8 +29,8 @@ export const useConnectorStore = defineStore("connector", () => {
feeLimit: parseEther("0.001"),
contractCalls: [
callPolicy({
address: runtimeConfig.public.contracts.nft as Hash,
abi: ZeekNftQuestAbi,
address: Nft.addressByChain[chainId],
abi: Nft.Abi,
functionName: "mint",
}),
],
Expand Down
21 changes: 0 additions & 21 deletions examples/nft-quest/utils/abi.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/auth-server/abi/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as NFTQuest from "./nft-quest";
export * as SSO from "./sso";
2 changes: 2 additions & 0 deletions packages/auth-server/abi/nft-quest/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as Nft from "./nft";
export * as Paymaster from "./paymaster";
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
export const ZeekNftQuestAbi = [
import type { AddressByChain } from "../types";

export const addressByChain: AddressByChain = {
300: "0x4D533d3B20b50b57268f189F93bFaf8B39c36AB6",
260: "0x111C3E89Ce80e62EE88318C2804920D4c96f92bb",
};

export const Abi = [
{
inputs: [
{
Expand Down
Loading
Loading