Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
HinsonSIDAN committed Aug 3, 2024
1 parent 6e1a4aa commit c0fbb2f
Show file tree
Hide file tree
Showing 5 changed files with 288 additions and 38 deletions.
4 changes: 2 additions & 2 deletions apps/playground/src/data/links-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { metaData } from "./links-data";
import { metaProviders } from "./links-providers";
import { metaReact } from "./links-react";
import { metaTransaction } from "./links-transactions";
// import { metaTxbuilder } from "./links-txbuilders";
import { metaTxbuilder } from "./links-txbuilders";
import { metaUtilities } from "./links-utilities";
import { metaWallets } from "./links-wallets";

export const linksApi: MenuItem[] = [
metaWallets,
metaTransaction,
// metaTxbuilder, // todo, work on txbuilder docs
metaTxbuilder, // todo, work on txbuilder docs
metaData,
metaReact,
metaProviders,
Expand Down
2 changes: 2 additions & 0 deletions apps/playground/src/pages/apis/txbuilder/minting/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import TxbuilderMintingCip68 from "./minting-cip68";
import TxbuilderMintingNativeScript from "./minting-native-script";
import TxbuilderMintAsset from "./minting-one-signature";
import TxbuilderMintingPlutusScript from "./minting-plutus-script";
import TxbuilderMintingRoyaltyToken from "./minting-royalty-token";

const ReactPage: NextPage = () => {
const sidebarItems = [
Expand Down Expand Up @@ -42,6 +43,7 @@ const ReactPage: NextPage = () => {
<TxbuilderMintingNativeScript />
<TxbuilderMintingPlutusScript />
<TxbuilderMintingCip68 />
<TxbuilderMintingRoyaltyToken />
</SidebarFullwidth>
</>
);
Expand Down
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>
);
}
2 changes: 1 addition & 1 deletion packages/mesh-common/src/utils/asset-fingerprint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { toBytes } from "../data";
export const AssetFingerprint = CIP14;

export const resolveFingerprint = (policyId: string, assetName: string) => {
return (AssetFingerprint as any).default
return (AssetFingerprint as any).default // todo: check the behaviour
.fromParts(toBytes(policyId), toBytes(assetName))
.fingerprint();
};
Loading

0 comments on commit c0fbb2f

Please sign in to comment.