Skip to content

Commit

Permalink
feat: bal abstracted
Browse files Browse the repository at this point in the history
  • Loading branch information
TheChronicMonster committed Oct 17, 2022
1 parent 7df46a6 commit 53e5f79
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions examples/simple-nft-minter/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,59 @@ 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;
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}`);
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,
reserve: null,
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);
return theNFT.id;
}

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`);
Expand All @@ -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}.`);
await logBalance(minter);

0 comments on commit 53e5f79

Please sign in to comment.