diff --git a/src/realm-module-example/realm-module.ts b/src/realm-module-example/realm-module.ts new file mode 100644 index 0000000..a81f284 --- /dev/null +++ b/src/realm-module-example/realm-module.ts @@ -0,0 +1,50 @@ +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.tx = txClient(wallet); + this.query = queryClient(wallet); + } +}; + +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 new file mode 100644 index 0000000..638911e --- /dev/null +++ b/src/wallet/helpers.ts @@ -0,0 +1,33 @@ +import { GnoWallet } from "./wallet"; + +export type Constructor = new (...args: any[]) => T; + +export type AnyFunction = (...args: any) => any; + +export type UnionToIntersection = + (Union extends any + ? (argument: Union) => void + : never + ) extends (argument: infer Intersection) => void + ? Intersection + : never; + +export type Return = + T extends AnyFunction + ? ReturnType + : T extends AnyFunction[] + ? UnionToIntersection> + : never + +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 { + const realmInstance = realm(this); + Object.assign(this, realmInstance.realm); + }); } + static addRealm(realms: T) { + const currentRealms = this.realms; + class AugmentedWallet extends this { + static realms = currentRealms.concat(realms); + } + + if (Array.isArray(realms)) { + type Extension = UnionToIntersection['realm']> + return AugmentedWallet as typeof GnoWallet & Constructor; + } + + type Extension = Return['realm'] + return AugmentedWallet as typeof GnoWallet & Constructor; + } /** * Generates a private key-based wallet, using a random seed * @param {AccountWalletOption} options the account options @@ -86,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