/* eslint no-console:"off" */

/**
 * This script demonstrates how to create and update asset
 *
 * Run script with: `DEBUG=@ocap/client node examples/asset.js`
 */

const { fromRandom } = require('@ocap/wallet');
const GraphQLClient = require('../lib/node');

const endpoint = process.env.OCAP_API_HOST || 'http://127.0.0.1:4000'; // testnet

const client = new GraphQLClient(`${endpoint}/api`);
// const sleep = (timeout) => new Promise((resolve) => {setTimeout(resolve, timeout);});

(async () => {
  try {
    const owner = fromRandom();
    console.log({ owner: owner.address });

    // 1. declare owner
    const hash = await client.declare({ moniker: 'owner', wallet: owner });
    console.log('declare.owner.result', hash);
    console.log('view owner account', `${endpoint}/explorer/accounts/${owner.address}`);

    const createResult = await client.createToken({
      name: 'demo token',
      description: 'demo token',
      symbol: `DT_${Date.now()}`,
      decimal: 10,
      unit: 'D',
      totalSupply: 1000000000000,
      wallet: owner,
    });

    console.log('create result', createResult);
  } catch (err) {
    console.error(err);
    console.log(JSON.stringify(err.errors));
  }
})();
