-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add NFT minting functionality and NFT list component
- Loading branch information
1 parent
683a738
commit 5e9167b
Showing
8 changed files
with
225 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing"; | ||
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"; | ||
import { Tendermint37Client } from "@cosmjs/tendermint-rpc"; | ||
import { GasPrice } from "@cosmjs/stargate"; | ||
import { fromHex } from "@cosmjs/encoding"; | ||
import { createHash } from "crypto"; | ||
|
||
const rpcEndpoint = "https://rpc.xion-testnet-1.burnt.com:443"; | ||
const key = process.env.PRIVATE_KEY; | ||
const contractAddress = | ||
"xion1rcdjfs8f0dqrfyep28m6rgfuw5ue2y788lk5jsj0ll5f2jekh6yqm95y32"; | ||
|
||
export default async function handler(req, res) { | ||
if (req.method !== "POST") { | ||
return res.status(405).json({ error: "Method not allowed" }); | ||
} | ||
|
||
const { address } = req.body; | ||
if (!address) { | ||
return res.status(400).json({ error: "Address is required" }); | ||
} else if (!key) { | ||
return res.status(400).json({ error: "Private key is required" }); | ||
} | ||
|
||
try { | ||
const tendermint = await Tendermint37Client.connect(rpcEndpoint); | ||
const signer = await DirectSecp256k1Wallet.fromKey(fromHex(key), "xion"); | ||
const [accountData] = await signer.getAccounts(); | ||
|
||
const client = await SigningCosmWasmClient.createWithSigner( | ||
tendermint, | ||
signer, | ||
{ | ||
gasPrice: GasPrice.fromString("0.001uxion"), | ||
}, | ||
); | ||
|
||
const uniqueString = `${Date.now()}-${Math.random()}`; | ||
const tokenId = createHash("sha256").update(uniqueString).digest("hex"); | ||
|
||
const msg = { | ||
mint: { | ||
token_id: tokenId, | ||
owner: address, | ||
token_uri: null, | ||
extension: null, | ||
}, | ||
}; | ||
|
||
const fee = "auto"; | ||
|
||
const result = await client.execute( | ||
accountData.address, | ||
contractAddress, | ||
msg, | ||
fee, | ||
); | ||
return res.status(200).json({ txHash: result.transactionHash, tokenId }); | ||
} catch (error) { | ||
console.error("Error executing transaction:", error); | ||
return res.status(500).json({ error: "Failed to execute transaction" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
export default function NftList({ nfts }) { | ||
if (!nfts || nfts.length === 0) { | ||
return <p>No NFTs found.</p>; | ||
} | ||
|
||
return ( | ||
<> | ||
<h2>NFT Token IDs:</h2> | ||
<ul> | ||
{nfts.map((tokenId) => ( | ||
<li key={tokenId}>{tokenId}</li> | ||
))} | ||
</ul> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { SigningCosmWasmClient } from "@cosmjs/cosmwasm-stargate"; | ||
import { GasPrice } from "@cosmjs/stargate"; | ||
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing"; | ||
import { Tendermint37Client } from "@cosmjs/tendermint-rpc"; | ||
import { fromHex } from "@cosmjs/encoding"; | ||
|
||
import dotenv from "dotenv"; | ||
|
||
dotenv.config(); | ||
|
||
const key = process.env.PRIVATE_KEY; | ||
if (!key) { | ||
console.error("Private key is missing. Please check your .env file."); | ||
process.exit(1); | ||
} | ||
|
||
const rpcEndpoint = "https://rpc.xion-testnet-1.burnt.com:443"; | ||
const contractCodeId = 1674; // Testnet Code ID | ||
|
||
async function instantiateContract() { | ||
const tendermint = await Tendermint37Client.connect(rpcEndpoint); | ||
const signer = await DirectSecp256k1Wallet.fromKey(fromHex(key), "xion"); | ||
|
||
const [accountData] = await signer.getAccounts(); | ||
const client = await SigningCosmWasmClient.createWithSigner( | ||
tendermint, | ||
signer, | ||
{ | ||
gasPrice: GasPrice.fromString("0.001uxion"), | ||
}, | ||
); | ||
|
||
const msg = { | ||
name: "Soulbound Token", | ||
symbol: "SBT", | ||
minter: accountData.address, | ||
}; | ||
|
||
const result = await client.instantiate( | ||
accountData.address, | ||
contractCodeId, | ||
msg, | ||
"Soulbound Token - Instantiation", | ||
"auto", | ||
{ admin: accountData.address }, | ||
); | ||
console.log("Contract instantiated at address:", result.contractAddress); | ||
} | ||
|
||
instantiateContract().catch(console.error); |