Skip to content

Commit

Permalink
Add makeTx function with NetworkProvider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
moreal committed Nov 26, 2024
1 parent 4e68199 commit 8262d28
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
22 changes: 22 additions & 0 deletions integrations/javascript/@planetarium/lib9c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,25 @@
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 { signTx } from "@planetarium/tx";
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);

const signedTx = await signTx(unsignedTx, account);

console.log(signedTx);
```
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";
40 changes: 40 additions & 0 deletions integrations/javascript/@planetarium/lib9c/src/tx.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Buffer } from "buffer";
import { type Account, Address } from "@planetarium/account";
import type { UnsignedTx } from "@planetarium/tx";
import type { PolymorphicAction } from "./actions/common.js";
import { TransferAsset } from "./actions/transfer_asset.js";
import { TransferAssets } from "./actions/transfer_assets.js";
import { MEAD, fav } from "./models/currencies.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 publicKey = await account.getPublicKey();
const signer = Address.deriveFrom(publicKey);
const nonce = await provider.getNextNonce(signer);
const genesisHash = await provider.getGenesisHash();

const gasLimit =
action instanceof TransferAsset || action instanceof TransferAssets
? 4n
: 1n;

return {
nonce,
genesisHash: Buffer.from(genesisHash, "hex"),
signer: signer.toBytes(),
updatedAddresses: new Set(),
actions: [action.bencode()],
publicKey: publicKey.toBytes("uncompressed"),
timestamp: new Date(),
gasLimit,
maxGasPrice: fav(MEAD, 1n),
};
}

0 comments on commit 8262d28

Please sign in to comment.