From 53e5f793e4422468d2f74accb4b36b60c41c748a Mon Sep 17 00:00:00 2001 From: TheChronicMonster Date: Mon, 17 Oct 2022 14:40:47 -0500 Subject: [PATCH] feat: bal abstracted --- examples/simple-nft-minter/index.mjs | 37 ++++++++++++++++------------ 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/examples/simple-nft-minter/index.mjs b/examples/simple-nft-minter/index.mjs index a80a13fc2d..47afbb24d7 100644 --- a/examples/simple-nft-minter/index.mjs +++ b/examples/simple-nft-minter/index.mjs @@ -2,7 +2,6 @@ import { Reach, test } from '@reach-sh/stdlib'; const stdlib = new Reach(process.env, { REACH_NO_WARN: 'Y' }); const startingBalance = stdlib.parseCurrency(100); - const [minter, receiver] = await stdlib.newTestAccounts(2, startingBalance); const gasLimit = 500000; @@ -10,9 +9,9 @@ if (stdlib.connector != 'ALGO') {minter.setGasLimit(gasLimit)}; if (stdlib.connector != 'ALGO') {receiver.setGasLimit(gasLimit)}; const mintAddr = minter.getAddress(); -console.log(`Your address is ${mintAddr}`); +console.log(`Minter's address is ${mintAddr}`); const recAddr = receiver.getAddress(); -console.log(`receiver's address is ${recAddr}`); +console.log(`Receiver's address is ${recAddr}`); const minterAddrFormat = await stdlib.formatAddress(minter); console.log(`The minter's formatted address is ${minterAddrFormat}`); @@ -20,16 +19,26 @@ const receiverAddrFormat = await stdlib.formatAddress(receiver); console.log(`The receiver's formatted address is ${receiverAddrFormat}`); const fmt = (x) => stdlib.formatCurrency(x, 4); -const getBal = async (who) => fmt(await stdlib.balanceOf(who)); -const bal = await getBal(minter); -console.log(`Minter starting balance: ${bal}.`); +const getBal = async (who, tok) => fmt(await stdlib.balanceOf(who, tok)); + +const logBalance = async (acc, tok, accStr) => { + const bal = await getBal(acc, tok, accStr); + const trueBal = await tok ? opts.supply : bal; + const unit = await tok ? 'of the NFT' : stdlib.connector; + accStr = acc.networkAccount.addr.toString(); + console.log(`${accStr} has ${trueBal} ${unit}.`); + + return trueBal; +} + +await logBalance(minter); const name = "JPAlgos"; const symbol = "JPA"; const opts = { supply: 1, - au: "ipfs://bafybeigdyrzt5...", //asset url + url: "ipfs://bafybeigdyrzt5...", //asset url c: null, // clawback f: null, // freeze address defaultFrozen: false, @@ -37,7 +46,7 @@ const opts = { note: Uint8Array[1], }; -const mintNFT = async (minter, name, symbol, opts = {supply: 1, au: "ipfs://bafybeigdyrzt5...", c: null, f: null, defaultFrozen: false, reserve: null, note: Uint8Array[1]}) => { +const mintNFT = async (minter, name, symbol, opts = {supply, url, c: null, f: null, defaultFrozen: false, reserve: null, note}) => { console.log(`Creating the NFT`); const theNFT = await stdlib.launchToken(minter, name, symbol, opts); console.log(theNFT); @@ -45,9 +54,7 @@ const mintNFT = async (minter, name, symbol, opts = {supply: 1, au: "ipfs://bafy } const transferNFT = async (minter, receiver, nftId, supply) => { - - const preAmtNFT = await minter.balanceOf(nftId); - console.log(`Minter has ${preAmtNFT} of the NFT`); + const preAmtNFT = await logBalance(minter, nftId); if (stdlib.connector == 'ALGO' && await receiver.tokenAccept(nftId)) { console.log(`Receiver opted-in to NFT`); @@ -56,15 +63,13 @@ const transferNFT = async (minter, receiver, nftId, supply) => { console.log(`Token accepted`); }; await stdlib.transfer(minter, receiver, supply, nftId); - console.log(`NFT transfer made from minter to receiver`); + console.log(`${supply} ${symbol} transferred from ${minter.networkAccount.addr.toString()} to ${receiver.networkAccount.addr.toString()}`); - const postAmtNFT = await receiver.balanceOf(nftId); - console.log(`Receiver has ${postAmtNFT} of the NFT`); + const postAmtNFT = await logBalance(receiver, nftId); test.chk('NFT AMT', preAmtNFT, postAmtNFT); } const nftId = await mintNFT(minter, name, symbol, opts); await transferNFT(minter, receiver, nftId, opts.supply); -const postBal = await getBal(minter); -console.log(`Minter balance after transfer: ${postBal}.`); \ No newline at end of file +await logBalance(minter);