From d1283ad8ce13f30273c4edef53780fb6c31d07a9 Mon Sep 17 00:00:00 2001 From: "Alex M. (clockwork)" Date: Thu, 19 Oct 2023 17:54:39 -0400 Subject: [PATCH] feat: Complete example --- src/realm-module-example/realm-module.ts | 66 +++++++++++++++++++++++ src/realm-module-example/usage-example.ts | 8 +++ src/wallet/helpers.ts | 10 ++++ src/wallet/wallet.ts | 11 +++- 4 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/realm-module-example/realm-module.ts create mode 100644 src/realm-module-example/usage-example.ts diff --git a/src/realm-module-example/realm-module.ts b/src/realm-module-example/realm-module.ts new file mode 100644 index 0000000..74224dc --- /dev/null +++ b/src/realm-module-example/realm-module.ts @@ -0,0 +1,66 @@ +import { BroadcastTransactionMap, TransactionEndpoint, TxFee } from "@gnolang/tm2-js-client"; +import { GnoWallet } from "../wallet"; +import { parseGnoReturns } from "../wallet/helpers"; + +const realm = "/r/demo/boards"; +const realmTS= "r_demo_boards"; +type GetBoardIDFromNameReturn = [number, boolean] +const queryClient = (wallet: GnoWallet) => { + return { + async GetBoardIDFromName(params: { name: string }, height?: number):Promise { + const result = await wallet.getProvider().evaluateExpression(realm,`GetBoardIDFromName("${name}")`,height); + return parseGnoReturns(result) as GetBoardIDFromNameReturn; + } + } +} +const txClient = (wallet: GnoWallet) => { + return { + async GetBoardIDFromName(params: { name: string }, funds: Map, fee: TxFee):Promise { + + const resp = (await wallet.callMethod( + realm, + "GetBoardIDFromName", + [params.name], + TransactionEndpoint.BROADCAST_TX_COMMIT, + funds, + fee + )); + const result = atob(resp.deliver_tx.ResponseBase.Data as string) + return parseGnoReturns(result) as GetBoardIDFromNameReturn; + }, + } +} +class RealmModule { + public query: ReturnType; + public tx: ReturnType; + + constructor(wallet: GnoWallet) { + this.updateQuery(wallet); + this.updateTX(wallet); + } + updateTX(wallet: GnoWallet) { + const methods = txClient(wallet); + + this.tx = methods; + for (let m in methods) { + this.tx[m as keyof typeof methods] = methods[m as keyof typeof methods].bind(this.tx); + } + } + updateQuery(wallet: GnoWallet) { + const methods = queryClient(wallet); + + this.query = methods; + for (let m in methods) { + this.query[m as keyof typeof methods] = methods[m as keyof typeof methods].bind(this.query); + } + } +}; + +const Realm = (wallet: GnoWallet) => { + return { + realm: { + [realmTS]: new RealmModule(wallet) + } + } +} +export default Realm; \ No newline at end of file diff --git a/src/realm-module-example/usage-example.ts b/src/realm-module-example/usage-example.ts new file mode 100644 index 0000000..6e70edc --- /dev/null +++ b/src/realm-module-example/usage-example.ts @@ -0,0 +1,8 @@ +import { GnoWallet } from '../wallet'; +import Realm from './realm-module'; +const RealmWallet = GnoWallet.addRealm(Realm) +const wallet = new RealmWallet(); + +const test = async () => { + const [boardId, exists] = await wallet.r_demo_boards.query.GetBoardIDFromName({ name: "testboard"}); +} \ No newline at end of file diff --git a/src/wallet/helpers.ts b/src/wallet/helpers.ts index b0e3b5b..638911e 100644 --- a/src/wallet/helpers.ts +++ b/src/wallet/helpers.ts @@ -21,3 +21,13 @@ export type Return = export type RealmInterface = { [key: string]: any } export type Realm = (instance: GnoWallet) => { realm: RealmInterface} + +export const parseGnoReturns = (result: string):Array => { + const ret=[]; + const values = result.split("\n"); + for (let i=0; i { + return this.provider; + }; /** * Initiates a native currency transfer transaction between accounts