Some extended examples
For these examples, assume you have the following data and functions available:
Alphanet
At the moment, you can only use the Alphanet API, faucet and explorer:
- JSON RPC URL:
https://api.iota-rebased-alphanet.iota.cafe:443
- Faucet URL:
https://faucet.iota-rebased-alphanet.iota.cafe/gas
- Explorer: https://explorer.iota.cafe/?network=alphanet
// a constant for bullshark's type.
const bullsharkType = `${packageId}::iotafrens::IotaFren<${packageId}::bullshark::Bullshark>`;
// a constant for capy's type.
const capyType = `${packageId}::iotafrens::IotaFren<${packageId}::capy::Capy>`;
// initialize a kioskClient.
const kioskClient = new KioskClient({
client: new IotaClient({
url: getFullnodeUrl('mainnet'),
}),
network: Network.MAINNET,
});
Minting a IotaFren
This example demonstrates how to mint a IotaFren.
async function mintFren(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 txb = new TransactionBlock();
const kioskTx = new KioskTransaction({ transactionBlock: txb, 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 capy here into the kiosk (either a new or an existing one).
txb.moveCall({
target: `${packageId}::iotafrens::mint_app::mint`,
arguments: [kioskTx.getKiosk(), kioskTx.getKioskCap()],
typeArguments: [capyType],
});
// 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 block.
await signAndExecuteTransactionBlock({ tx: txb });
}
Mixing two Iotafrens
This example demonstrates how to use the Kiosk SDK to mix two bullsharks
.
// We're mixing two frens.
async function mixFrens(firstFrenObjectId: string, secondFrenObjectId: string, cap: KioskOwnerCap) {
const txb = new TransactionBlock();
const kioskTx = new KioskTransaction({ transactionBlock: txb, kioskClient, cap });
// borrow both frens.
const [fren1, promise1] = kioskTx.borrow({
itemType: bullsharkType,
itemId: firstFrenObjectId.
});
const [fren2, promise2] = kioskTx.borrow({
itemType: bullsharkType,
itemId: secondFrenObjectId.
});
// Let's call the mix function. We skip any payment related stuff here.
txb.moveCall({
target: `${packageId}::mix_app::mix`,
arguments: [
fren1,
fren2,
kioskTx.getKiosk(),
kioskTx.getKioskCap(),
]
typeArguments: [bullsharkType],
});
kioskTx.return({
itemType: bullsharkType,
item: fren1,
promise: promise1
})
.return({
itemType: bullsharkType,
item: fren2,
promise: promise2
}).finalize();
// sign and execute transaction block.
await signAndExecuteTransactionBlock({ tx: txb });
}