planetarium / lib9c

Lib9c is a library that contains key implementations of Nine Chronicles, a decentralized RPG developed with Libplanet. Lib9c includes Nine Chronicle's key features like in-game decisions and data models, which can be used to implement game core capabilities.
https://nine-chronicles.com/
GNU General Public License v3.0
22 stars 39 forks source link

@planetarium/lib9c: provide helper `makeTx` transaction to construct tx easily #2617

Open moreal opened 2 weeks ago

moreal commented 2 weeks ago

It suggests to provide helper method, makeTx with NetworkProvider interface. It expects to implement NineChronicles.Headless implement NetworkProvider.

import { Account, Address } from '@planetarium/account';
import { UnsignedTx } from '@planetarium/tx';

interface NetworkProvider {
  getNextNonce(address: Address): Promise<bigint>,
  getGenesisHash(): Promise<string>,
}

function makeTx(account: Account, provider: NetworkProvider, action: PolymorphicAction): Promise<UnsignedTx>;

Expected code:

import { HeadlessNetworkProvider } from "@planetarium/headless-network-provider"; // Temporary name
import { RawPrivateKey, Address } from "@planetarium/account";
import { makeTx, ClaimStakeReward } from "@planetarium/lib9c";

const networkProvider = new HeadlessNetworkProvider("https://9c-main-full-state.nine-chronicles.com/graphql");
const account = RawPrivateKey.generate();  // Temporary private key key.

const unsignedTx = await makeTx(account, networkProvider, new ClaimStakeReward({
  avatarAddress: Address.fromHex('<ADDRESS>'),
}));

unsignedTx