Skip to content

Commit

Permalink
Add makeTx function with NetworkProvider interface
Browse files Browse the repository at this point in the history
Related to #2617

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/planetarium/lib9c/issues/2617?shareId=XXXX-XXXX-XXXX-XXXX).
  • Loading branch information
moreal committed Nov 25, 2024
1 parent 4e68199 commit 2a92048
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
17 changes: 17 additions & 0 deletions integrations/javascript/@planetarium/lib9c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,20 @@
This npm package provides functions to build actions equivalent to [Lib9c].

[Lib9c]: https://github.com/planetarium/lib9c

## Usage Example

```typescript
import { HeadlessNetworkProvider } from "@planetarium/9c-headless-network-provider";
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>'),
}));

console.log(unsignedTx);
```
1 change: 1 addition & 0 deletions integrations/javascript/@planetarium/lib9c/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,4 @@ export {
type MigratePledgeToGuildArgs,
} from "./actions/migrate_pledge_to_guild.js";
export { MakeGuild } from "./actions/make_guild.js";
export { makeTx, type NetworkProvider } from "./tx.js";
24 changes: 24 additions & 0 deletions integrations/javascript/@planetarium/lib9c/src/tx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Account, Address } from '@planetarium/account';
import { UnsignedTx } from '@planetarium/tx';
import { PolymorphicAction } from './actions/common.js';

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

export async function makeTx(
account: Account,
provider: NetworkProvider,
action: PolymorphicAction
): Promise<UnsignedTx> {
const nonce = await provider.getNextNonce(account.address);
const genesisHash = await provider.getGenesisHash();

return new UnsignedTx({
nonce,
genesisHash,
signer: account,
actions: [action],
});
}

0 comments on commit 2a92048

Please sign in to comment.