Skip to content

Commit

Permalink
Chr wallet check (#65)
Browse files Browse the repository at this point in the history
* add wallet check logic

* add changelog

* clean up with linting

* Update tame-tomatoes-doubt.md

* add receipt tx and attach explorer link

---------

Co-authored-by: Agus <[email protected]>
  • Loading branch information
superoo7 and 0xaguspunk authored Dec 16, 2024
1 parent cdaa58e commit 6134b87
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions typescript/.changeset/tame-tomatoes-doubt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@goat-sdk/wallet-chromia": patch
---

Add wallet check before send
2 changes: 1 addition & 1 deletion typescript/examples/vercel-ai/chromia/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if (!privateKey) {
model: openai("gpt-4o-mini"),
tools: tools,
maxSteps: 5,
prompt: "send 0.0001 CHR to <recipient address>",
prompt: "send 0.0001 CHR to <recipient_address>, and please attach the transaction explorer link",
});

console.log(result.text);
Expand Down
7 changes: 5 additions & 2 deletions typescript/packages/core/src/core-plugins/send-chr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { z } from "zod";
import type { Plugin } from "../plugins";
import type { Chain, ChromiaWalletClient } from "../wallets";

export enum CHROMIA_MAINNET_BRID {
ECONOMY_CHAIN = "15C0CA99BEE60A3B23829968771C50E491BD00D2E3AE448580CD48A8D71E7BBA",
}
export const CHR_ASSET_ID = "5f16d1545a0881f971b164f1601cbbf51c29efd0633b2730da18c403c3b428b5";

export function sendCHR(): Plugin<ChromiaWalletClient> {
Expand Down Expand Up @@ -34,8 +37,8 @@ async function sendCHRMethod(
): Promise<string> {
try {
const { to, amount } = parameters;
await walletClient.sendTransaction({ to, assetId: CHR_ASSET_ID, amount });
return `CHR sent to ${to} with amount ${amount}`;
const { receipt } = await walletClient.sendTransaction({ to, assetId: CHR_ASSET_ID, amount });
return `https://explorer.chromia.com/mainnet/${CHROMIA_MAINNET_BRID.ECONOMY_CHAIN}/transaction/${receipt.transactionRid.toString("hex")}`;
} catch (error) {
return `Error sending CHR: ${error}`;
}
Expand Down
12 changes: 4 additions & 8 deletions typescript/packages/wallets/chromia/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
type Connection,
type KeyStoreInteractor,
Session,
createAmount,
createInMemoryFtKeyStore,
createKeyStoreInteractor,
} from "@chromia/ft4";
import { type Connection, type KeyStoreInteractor, createAmount } from "@chromia/ft4";
import { type DictPair, type IClient, type QueryObject, type RawGtv, SignatureProvider } from "postchain-client";

export enum CHROMIA_MAINNET_BRID {
Expand Down Expand Up @@ -41,6 +34,9 @@ export function chromia({ client, keystoreInteractor, accountAddress, connection
return { signature: "" };
},
async sendTransaction({ to, assetId, amount }: ChromiaTransaction) {
if (!to.match(/^[a-f0-9]{64}$/i)) {
throw new Error("Invalid Address");
}
const accounts = await keystoreInteractor.getAccounts();
const session = await keystoreInteractor.getSession(accounts[0].id);
const asset = await connection.getAssetById(assetId);
Expand Down

0 comments on commit 6134b87

Please sign in to comment.