Some extended examples
For these examples, assume you have the following data and functions available:
// a constant for my type.
const myType = `${packageId}::my_module::MyStruct<${packageId}::my_coin_module::MyCoin>`;
// a constant for another type.
const otherType = `${packageId}::other_module::OtherStruct<${packageId}::other_coin_module::OtherCoin>`;
// initialize a kioskClient.
const kioskClient = new KioskClient({
client: new IotaClient({
url: getFullnodeUrl(Network.Testnet),
}),
network: Network.Testnet,
});
Minting a MyCoin
This example demonstrates how to mint a MyCoin.
async function mintMyCoin(address: string) {
const { kioskOwnerCaps } = await kioskClient.getOwnedKiosks({ address });
// Choose the first kiosk for simplicity. We could have extra logic here (e.g. let the user choose, pick a personal one, etc).
const cap = kioskOwnerCaps[0];
const tx = new Transaction();
const kioskTx = new KioskTransaction({ transaction: tx, kioskClient, cap });
// We're mixing the logic here. If the cap is undefined, we create a new kiosk.
if (!cap) kioskTx.create();
// Let's mint a MyCoin here into the kiosk (either a new or an existing one).
tx.moveCall({
target: `${packageId}::my_module::mint_app::mint`,
arguments: [kioskTx.getKiosk(), kioskTx.getKioskCap()],
typeArguments: [myType],
});
// If we don't have a cap, that means we create a new kiosk for the user in this flow.
if (!cap) kioskTx.shareAndTransferCap(address);
kioskTx.finalize();
// sign and execute transaction.
await signAndExecuteTransaction({ tx });
}
Mixing two MyCoins
This example demonstrates how to use the Kiosk SDK to mix two MyCoins
.
// We're mixing two coins.
async function mixMyCoins(firstCoinObjectId: string, secondCoinObjectId: string, cap: KioskOwnerCap) {
const tx = new Transaction();
const kioskTx = new KioskTransaction({ transaction: tx, kioskClient, cap });
// borrow both coins.
const [coin1, promise1] = kioskTx.borrow({
itemType: myType,
itemId: firstCoinObjectId.
});
const [coin2, promise2] = kioskTx.borrow({
itemType: myType,
itemId: secondCoinObjectId.
});
// Let's call the mix function. We skip any payment related stuff here.
tx.moveCall({
target: `${packageId}::mix_app::mix`,
arguments: [
coin1,
coin2,
kioskTx.getKiosk(),
kioskTx.getKioskCap(),
]
typeArguments: [myType],
});
kioskTx.return({
itemType: myType,
item: coin1,
promise: promise1
})
.return({
itemType: myType,
item: coin2,
promise: promise2
}).finalize();
// sign and execute transaction.
await signAndExecuteTransaction({ tx });
}
Feedback Form