Skip to content

Commit

Permalink
Merge pull request #119 from bcnmy/fix/gaslessmint
Browse files Browse the repository at this point in the history
Update gaslessmint.md
  • Loading branch information
Rahat-ch authored Nov 30, 2023
2 parents 15cad99 + 97021f1 commit a5cce67
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions docs/tutorials/nodejs/gaslessmint.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ TypeScript that allows user to mint an NFT without paying for any Gas.
:::info This tutorial has a two other steps in the previous sections:
[Environment Setup](environmentsetup) and
[Initializing Account](initializeaccount)
:::
:::

This tutorial will be done on the Polygon Mumbai Network and the smart contract
for the NFT mint is available
Expand All @@ -27,12 +27,18 @@ async function mintNFT() {}
```

All of the code from this point forward will be code we add to the mintNFT
function defined above. Let's start with creating an interface for our NFT
contract.
function defined above. Let's start with instantiate the Smart Account and retrieve its address.

```typescript
const smartAccount = await createAccount();
const address = await smartAccount.getAccountAddress();
```

Then we need to create an interface for our NFT contract.

```typescript
const nftInterface = new ethers.utils.Interface([
"function safeMint(address _to)",
"function safeMint(address _to)",
]);
```

Expand All @@ -52,21 +58,22 @@ first part of our transaction:
const nftAddress = "0x1758f42Af7026fBbB559Dc60EcE0De3ef81f665e";

const transaction = {
to: nftAddress,
data: data,
to: nftAddress,
data: data,
};
```

We will now start building out the partial userOp for this transaction:

```typescript
let partialUserOp = await smartAccount.buildUserOp([transaction], {
paymasterServiceData: {
mode: PaymasterMode.SPONSORED,
},
});
paymasterServiceData: {
mode: PaymasterMode.SPONSORED,
},
});
```
Since we want to have this become a sponsored transaction we will need to ensure that we pass the `paymasterServiceData` object with a `SPONSORED` paymaster mode.

Since we want to have this become a sponsored transaction we will need to ensure that we pass the `paymasterServiceData` object with a `SPONSORED` paymaster mode.

## Paymaster Service Data response

Expand All @@ -76,22 +83,18 @@ the following variable

```typescript
const biconomyPaymaster =
smartAccount.paymaster as IHybridPaymaster<SponsorUserOperationDto>;
smartAccount.paymaster as IHybridPaymaster<SponsorUserOperationDto>;
```

With this done let's get the paymaster and Data response from our paymaster and
add it to our userOp.

```typescript
try {
const paymasterAndDataResponse =
await biconomyPaymaster.getPaymasterAndData(
partialUserOp,
paymasterServiceData
);
partialUserOp.paymasterAndData = paymasterAndDataResponse.paymasterAndData;
const paymasterAndDataResponse = await biconomyPaymaster.getPaymasterAndData(partialUserOp);
partialUserOp.paymasterAndData = paymasterAndDataResponse.paymasterAndData;
} catch (e) {
console.log("error received ", e);
console.log("error received ", e);
}
```

Expand All @@ -101,16 +104,16 @@ Finally let's go ahead and mint this NFT!

```typescript
try {
const userOpResponse = await smartAccount.sendUserOp(partialUserOp);
const transactionDetails = await userOpResponse.wait();
console.log(
`transactionDetails: https://mumbai.polygonscan.com/tx/${transactionDetails.receipt.transactionHash}`
);
console.log(
`view minted nfts for smart account: https://testnets.opensea.io/${address}`
);
const userOpResponse = await smartAccount.sendUserOp(partialUserOp);
const transactionDetails = await userOpResponse.wait();
console.log(
`transactionDetails: https://mumbai.polygonscan.com/tx/${transactionDetails.receipt.transactionHash}`,
);
console.log(
`view minted nfts for smart account: https://testnets.opensea.io/${address}`,
);
} catch (e) {
console.log("error received ", e);
console.log("error received ", e);
}
```

Expand All @@ -122,7 +125,6 @@ link to quickly view NFT's minted by our smart account.
<summary> Click to view final code </summary>

```typescript

import { config } from "dotenv";
import { IBundler, Bundler } from "@biconomy/bundler";
import { ChainId } from "@biconomy/core-types";
Expand All @@ -146,18 +148,20 @@ import {
config();

const provider = new ethers.providers.JsonRpcProvider(
"https://rpc.ankr.com/polygon_mumbai"
"https://rpc.ankr.com/polygon_mumbai",
);
const wallet = new ethers.Wallet(process.env.PRIVATE_KEY || "", provider);

const bundler: IBundler = new Bundler({
bundlerUrl: "https://bundler.biconomy.io/api/v2/80001/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44",
bundlerUrl:
"https://bundler.biconomy.io/api/v2/80001/nJPK7B3ru.dd7f7861-190d-41bd-af80-6877f74b8f44",
chainId: ChainId.POLYGON_MUMBAI,
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS,
});

const paymaster: IPaymaster = new BiconomyPaymaster({
paymasterUrl: "https://paymaster.biconomy.io/api/v1/80001/Tpk8nuCUd.70bd3a7f-a368-4e5a-af14-80c7f1fcda1a",
paymasterUrl:
"https://paymaster.biconomy.io/api/v1/80001/Tpk8nuCUd.70bd3a7f-a368-4e5a-af14-80c7f1fcda1a",
});

async function createAccount() {
Expand Down Expand Up @@ -207,18 +211,17 @@ async function mintNFT() {
const userOpResponse = await smartAccount.sendUserOp(partialUserOp);
const transactionDetails = await userOpResponse.wait();
console.log(
`transactionDetails: https://mumbai.polygonscan.com/tx/${transactionDetails.receipt.transactionHash}`
`transactionDetails: https://mumbai.polygonscan.com/tx/${transactionDetails.receipt.transactionHash}`,
);
console.log(
`view minted nfts for smart account: https://testnets.opensea.io/${address}`
`view minted nfts for smart account: https://testnets.opensea.io/${address}`,
);
} catch (e) {
console.log("error received ", e);
}
}

mintNFT();

```

</details>
Expand Down

0 comments on commit a5cce67

Please sign in to comment.