-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6e1a4aa
commit c0fbb2f
Showing
5 changed files
with
288 additions
and
38 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
188 changes: 188 additions & 0 deletions
188
apps/playground/src/pages/apis/txbuilder/minting/minting-royalty-token.tsx
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,188 @@ | ||
import { useState } from "react"; | ||
import Link from "next/link"; | ||
|
||
import { | ||
ForgeScript, | ||
MeshTxBuilder, | ||
Mint, | ||
resolveScriptHash, | ||
RoyaltiesStandard, | ||
Transaction, | ||
} from "@meshsdk/core"; | ||
import { useWallet } from "@meshsdk/react"; | ||
|
||
import Input from "~/components/form/input"; | ||
import InputTable from "~/components/sections/input-table"; | ||
import LiveCodeDemo from "~/components/sections/live-code-demo"; | ||
import TwoColumnsScroll from "~/components/sections/two-columns-scroll"; | ||
import Codeblock from "~/components/text/codeblock"; | ||
import { demoAddresses } from "~/data/cardano"; | ||
|
||
export default function MintingRoyaltyToken() { | ||
return ( | ||
<TwoColumnsScroll | ||
sidebarTo="mintingRoyaltyToken" | ||
title="Minting Royalty Token" | ||
leftSection={Left()} | ||
rightSection={Right()} | ||
/> | ||
); | ||
} | ||
|
||
function Left() { | ||
let codeSnippet = ``; | ||
codeSnippet += `const usedAddress = await wallet.getUsedAddresses();\n`; | ||
codeSnippet += `const address = usedAddress[0];\n`; | ||
codeSnippet += `\n`; | ||
codeSnippet += `// create forgingScript, you can also use native script here\n`; | ||
codeSnippet += `const forgingScript = ForgeScript.withOneSignature(address);\n`; | ||
codeSnippet += `\n`; | ||
codeSnippet += `const tx = new Transaction({ initiator: wallet });\n`; | ||
codeSnippet += `\n`; | ||
codeSnippet += `const _assetMetadata = {\n`; | ||
codeSnippet += ` rate: '0.2',\n`; | ||
codeSnippet += ` addr: '${demoAddresses.testnet}'\n`; | ||
codeSnippet += `};\n`; | ||
codeSnippet += `const asset: Mint = {\n`; | ||
codeSnippet += ` assetName: '',\n`; | ||
codeSnippet += ` assetQuantity: '1',\n`; | ||
codeSnippet += ` metadata: _assetMetadata,\n`; | ||
codeSnippet += ` label: '777',\n`; | ||
codeSnippet += ` recipient: address,\n`; | ||
codeSnippet += `};\n`; | ||
codeSnippet += `\n`; | ||
codeSnippet += `tx.mintAsset(forgingScript, asset);\n`; | ||
codeSnippet += `\n`; | ||
codeSnippet += `const unsignedTx = await tx.build();\n`; | ||
codeSnippet += `const signedTx = await wallet.signTx(unsignedTx);\n`; | ||
codeSnippet += `const txHash = await wallet.submitTx(signedTx);\n`; | ||
|
||
return ( | ||
<> | ||
<p> | ||
Royalty tokens is a special type of token that allows the creator to | ||
collect a royalty fee, this proposed standard will allow for uniform | ||
royalties' distributions across the secondary market space. Read{" "} | ||
<Link | ||
href="https://cips.cardano.org/cips/cip27/" | ||
target="_blank" | ||
rel="noreferrer" | ||
> | ||
CIP-27 | ||
</Link>{" "} | ||
for more information. | ||
</p> | ||
<p> | ||
The implementation of royalty tokens is very simple, minting a token | ||
with <code>777</code> label, with "rate" and "addr" in the metadata. | ||
</p> | ||
|
||
<p>Here is the full code:</p> | ||
<Codeblock data={codeSnippet} isJson={false} /> | ||
</> | ||
); | ||
} | ||
|
||
function Right() { | ||
const { wallet, connected } = useWallet(); | ||
const [userInput, setUserInput] = useState<string>("0.2"); | ||
const [userInput2, setUserInput2] = useState<string>(demoAddresses.testnet); | ||
|
||
async function runDemo() { | ||
const utxos = await wallet.getUtxos(); | ||
const usedAddress = await wallet.getUsedAddresses(); | ||
const address = usedAddress[0]; | ||
|
||
if (address === undefined) { | ||
throw "No address found"; | ||
} | ||
|
||
const forgingScript = ForgeScript.withOneSignature(address); | ||
const policyId = resolveScriptHash(forgingScript); | ||
|
||
const assetMetadata: RoyaltiesStandard = { | ||
rate: userInput, | ||
address: userInput2, | ||
}; | ||
|
||
const metadata = { [policyId]: { [""]: { ...assetMetadata } } }; | ||
|
||
const txBuilder = new MeshTxBuilder(); | ||
|
||
const unsignedTx = await txBuilder | ||
.mint("1", policyId, "") | ||
.mintingScript(forgingScript) | ||
.metadataValue("777", metadata) | ||
.changeAddress(address) | ||
.selectUtxosFrom(utxos) | ||
.complete(); | ||
|
||
const signedTx = await wallet.signTx(unsignedTx); | ||
const txHash = await wallet.submitTx(signedTx); | ||
|
||
return txHash; | ||
} | ||
|
||
let code = ``; | ||
code += `const usedAddress = await wallet.getUsedAddresses();\n`; | ||
code += `const address = usedAddress[0];\n`; | ||
code += `\n`; | ||
code += `const forgingScript = ForgeScript.withOneSignature(address);\n`; | ||
code += `\n`; | ||
code += `const tx = new Transaction({ initiator: wallet });\n`; | ||
code += `\n`; | ||
code += `const _assetMetadata: RoyaltiesStandard = {\n`; | ||
code += ` rate: '${userInput}',\n`; | ||
code += ` address: '${userInput2}',\n`; | ||
code += `};\n`; | ||
code += `const asset: Mint = {\n`; | ||
code += ` assetName: "",\n`; | ||
code += ` assetQuantity: "1",\n`; | ||
code += ` metadata: _assetMetadata,\n`; | ||
code += ` label: "777",\n`; | ||
code += ` recipient: address,\n`; | ||
code += `};\n`; | ||
code += `\n`; | ||
code += `tx.mintAsset(forgingScript, asset);\n`; | ||
code += `\n`; | ||
code += `const unsignedTx = await tx.build();\n`; | ||
code += `const signedTx = await wallet.signTx(unsignedTx);\n`; | ||
code += `const txHash = await wallet.submitTx(signedTx);\n`; | ||
|
||
return ( | ||
<LiveCodeDemo | ||
title="Mint Native Assets" | ||
subtitle="Mint native assets with ForgeScript" | ||
runCodeFunction={runDemo} | ||
disabled={!connected} | ||
runDemoButtonTooltip={ | ||
!connected ? "Connect wallet to run this demo" : undefined | ||
} | ||
runDemoShowBrowseWalletConnect={true} | ||
code={code} | ||
> | ||
<InputTable | ||
listInputs={[ | ||
<Input | ||
value={userInput} | ||
onChange={(e) => setUserInput(e.target.value)} | ||
placeholder="Rate" | ||
label="Rate" | ||
key={0} | ||
/>, | ||
]} | ||
/> | ||
<InputTable | ||
listInputs={[ | ||
<Input | ||
value={userInput2} | ||
onChange={(e) => setUserInput2(e.target.value)} | ||
placeholder="Address" | ||
label="Address" | ||
key={1} | ||
/>, | ||
]} | ||
/> | ||
</LiveCodeDemo> | ||
); | ||
} |
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
Oops, something went wrong.