Skip to content

Commit

Permalink
feat: Complete example
Browse files Browse the repository at this point in the history
  • Loading branch information
clockworkgr committed Oct 19, 2023
1 parent 59ec5c3 commit d1283ad
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 1 deletion.
66 changes: 66 additions & 0 deletions src/realm-module-example/realm-module.ts
Original file line number Diff line number Diff line change
@@ -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<GetBoardIDFromNameReturn> {
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<string,number>, fee: TxFee):Promise<GetBoardIDFromNameReturn> {

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<typeof queryClient>;
public tx: ReturnType<typeof txClient>;

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;
8 changes: 8 additions & 0 deletions src/realm-module-example/usage-example.ts
Original file line number Diff line number Diff line change
@@ -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"});
}
10 changes: 10 additions & 0 deletions src/wallet/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,13 @@ export type Return<T> =

export type RealmInterface = { [key: string]: any }
export type Realm = (instance: GnoWallet) => { realm: RealmInterface}

export const parseGnoReturns = (result: string):Array<unknown> => {
const ret=[];
const values = result.split("\n");
for (let i=0; i<values.length; i++) {
let value=JSON.parse(values[i].substring(1).split(" ").slice(0,-1).join(" "));
ret.push(value);
}
return ret;
}
11 changes: 10 additions & 1 deletion src/wallet/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
AccountWalletOption,
BroadcastTransactionMap,
CreateWalletOptions,
Provider,
Tx,
TxFee,
Wallet,
Expand All @@ -12,13 +13,14 @@ import { MemPackage, MsgAddPackage, MsgCall, MsgSend } from '../proto';
import { MsgEndpoint } from './endpoints';
import { LedgerConnector } from '@cosmjs/ledger-amino';
import { Constructor, Realm, Return, UnionToIntersection } from './helpers';
import { GnoProvider } from '../provider';

/**
* GnoWallet is an extension of the TM2 wallet with
* specific functionality for Gno chains
*/
export class GnoWallet extends Wallet {

protected provider:GnoProvider
static realms: Realm[] = [];
constructor() {
super();
Expand Down Expand Up @@ -108,6 +110,13 @@ export class GnoWallet extends Wallet {

return gnoWallet;
};
/**
* Returns the connected provider, if any
* (Here to ensure correct GnoProvider inference)
*/
getProvider = (): GnoProvider => {
return this.provider;
};

/**
* Initiates a native currency transfer transaction between accounts
Expand Down

0 comments on commit d1283ad

Please sign in to comment.