@initia/gserver.js
v0.1.21
Published
## How to install
Downloads
60
Keywords
Readme
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}`);
}
}