0.1.21 • Published 2 months ago

@initia/gserver.js v0.1.21

Weekly downloads
-
License
ISC
Repository
-
Last release
2 months ago

Initia Game SDK for server

How to install

$ npm install @initia/gserver.js

How to use

import { LCDClient, MnemonicKey, Wallet } from '@initia/initia.js';
import { BroadcastManager } from 'broadcastManager';
import { CoinController, DexController, NftController } from 'controller';

const key = new MnemonicKey({
  mnemonic: '...',
});

const lcd = new LCDClient('https://next-stone-rest.initia.tech/', {
  chainId: 'stone-8',
  gasPrices: '0.15uinit',
  gasAdjustment: '2.0',
});

const wallet = new Wallet(lcd, key);

const broadcastManager = new BroadcastManager(wallet, broadcastCallBack);

async function main() {
  // initiate broadcast worker
  await broadcastManager.init().catch(e => console.log(e));

  const nftModuleName = 'game_nft';

  // init controllers
  const nftController = new NftController(broadcastManager);
  const coinController = new CoinController(broadcastManager);
  const dexController = new DexController(broadcastManager);

  // create coins
  await coinController.createCoin('test_coin_a', 'test coin a', 'TCA', 6);
  await coinController.createCoin('test_coin_b', 'test coin a', 'TCB', 6);
  // mint coins
  await coinController.mintCoin('test_coin_a', 10_000_000_000, key.accAddress); // mint 10,000 coin A to me
  await coinController.mintCoin('test_coin_b', 10_000_000_000, key.accAddress); // mint 10,000 coin B to me
  // create pair
  await dexController.createPair({
    lpModuleName: 'a_b_pair',
    name: 'a_b_pair_lp_token_2',
    symbol: 'uLP',
    swapFeeRate: 0.003,
    coinAStructTag: coinController.coinStructTag('test_coin_a'),
    coinBStructTag: coinController.coinStructTag('test_coin_b'),
    coinAWeight: 0.5,
    coinBWeight: 0.5,
    coinAAmount: 1_000_000_000,
    coinBAmount: 1_000_000_000,
  });

  // create nft collection
  await nftController.publishAndMakeCollection(
    nftModuleName, // module name
    'gmae nft', // nft name
    'GAME', // nft symbol
    'https://game_nft.com', // nft collection uri
    true // isMutable
  );

  // mint new nft
  await nftController.mintNft({
    moduleName: nftModuleName,
    tokenId: 'token:1', // token id
    uri: 'https://game_nft.com/token/1', // token uri
    data: {
      power: 12,
      health: 10,
    }, // metadata
    to: key.accAddress, // recipient in this case send it to me
  });

  // update nft
  await nftController.updateNft(
    nftModuleName,
    'token:1', // token id
    'https://game_nft.com/token/v2/1', // new uri
    {
      power: 100,
      health: 10,
    } // new metadata
  );
}

main();

function broadcastCallBack(
  isSuccess: boolean,
  txHash: string,
  reason?: string
) {
  if (isSuccess) {
    console.log(`Tx(${txHash}) broadcast success`);
  } else {
    console.log(`Tx(${txHash}) broadcast failed due to ${reason}`);
  }
}
0.1.21

2 months ago

0.1.20

2 months ago

0.1.19

4 months ago

0.1.17

4 months ago

0.1.18

4 months ago

0.1.14

5 months ago

0.1.15

5 months ago

0.1.16

5 months ago

0.1.10

6 months ago

0.1.11

6 months ago

0.1.12

6 months ago

0.1.13

6 months ago

0.1.0

7 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.8

6 months ago

0.1.7

6 months ago

0.1.9

6 months ago

0.1.4

6 months ago

0.0.5

8 months ago

0.1.3

6 months ago

0.1.6

6 months ago

0.1.5

6 months ago

0.0.4

11 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago