Comparing version 0.0.4 to 0.0.5
import algosdk, { ABIValue } from "algosdk"; | ||
import { Schema } from "../"; | ||
import { ApplicationState, AccountState } from "./state"; | ||
export declare type MethodArg = algosdk.ABIArgument | algosdk.Transaction | object | MethodArg[]; | ||
@@ -71,2 +72,4 @@ export declare type MethodArgs = { | ||
getSuggestedParams(txParams?: algosdk.TransactionParams): Promise<algosdk.SuggestedParams>; | ||
getApplicationState(raw?: boolean): Promise<ApplicationState>; | ||
getAccountState(address?: string, raw?: boolean): Promise<AccountState>; | ||
private getSender; | ||
@@ -73,0 +76,0 @@ private getLocalSchema; |
@@ -57,2 +57,3 @@ "use strict"; | ||
var logic_error_1 = require("./logic_error"); | ||
var state_1 = require("./state"); | ||
function decodeNamedTuple(v, keys) { | ||
@@ -379,2 +380,35 @@ if (!Array.isArray(v)) | ||
}; | ||
ApplicationClient.prototype.getApplicationState = function (raw) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var appInfo; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, this.client.getApplicationByID(this.appId).do()]; | ||
case 1: | ||
appInfo = _a.sent(); | ||
if (!('params' in appInfo) || !('global-state' in appInfo['params'])) | ||
throw Error("No global state found"); | ||
return [2 /*return*/, (0, state_1.decodeState)(appInfo['params']['global-state'], raw)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
ApplicationClient.prototype.getAccountState = function (address, raw) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var acctInfo; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
if (address === undefined) | ||
address = this.getSender(); | ||
return [4 /*yield*/, this.client.accountApplicationInformation(address, this.appId).do()]; | ||
case 1: | ||
acctInfo = _a.sent(); | ||
if (!('app-local-state' in acctInfo) || !('key-value' in acctInfo['app-local-state'])) | ||
throw Error("No global state found"); | ||
return [2 /*return*/, (0, state_1.decodeState)(acctInfo['app-local-state']['key-value'], raw)]; | ||
} | ||
}); | ||
}); | ||
}; | ||
ApplicationClient.prototype.getSender = function () { | ||
@@ -381,0 +415,0 @@ return this.sender; |
@@ -39,2 +39,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var types_1 = require("util/types"); | ||
var __1 = require("../../"); | ||
@@ -44,3 +45,3 @@ var structer_client_1 = require("./structer_client"); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var acct, appClient, _a, appId, appAddr, txId, result, result2; | ||
var acct, appClient, _a, appId, appAddr, txId, result, result2, state, k, val; | ||
return __generator(this, function (_b) { | ||
@@ -71,2 +72,12 @@ switch (_b.label) { | ||
console.log("Result: ", result2.value); | ||
return [4 /*yield*/, appClient.getAccountState(acct.addr, true)]; | ||
case 6: | ||
state = _b.sent(); | ||
for (k in state) { | ||
val = state[k]; | ||
// appease ts | ||
if (!(0, types_1.isUint8Array)(val)) | ||
continue; | ||
console.log(structer_client_1.Order.decodeBytes(val)); | ||
} | ||
return [2 /*return*/]; | ||
@@ -73,0 +84,0 @@ } |
import algosdk from "algosdk"; | ||
import { ApplicationClient, ABIResult, Schema } from "../.."; | ||
export declare type Order = { | ||
export declare class Order { | ||
item: string; | ||
quantity: number; | ||
}; | ||
static codec: algosdk.ABIType; | ||
static decodeResult(val: algosdk.ABIValue): Order; | ||
static decodeBytes(val: Uint8Array): Order; | ||
} | ||
export declare class Structer extends ApplicationClient { | ||
@@ -8,0 +11,0 @@ desc: string; |
@@ -57,5 +57,18 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Structer = void 0; | ||
exports.Structer = exports.Order = void 0; | ||
var algosdk_1 = __importDefault(require("algosdk")); | ||
var __1 = require("../.."); | ||
var Order = /** @class */ (function () { | ||
function Order() { | ||
} | ||
Order.decodeResult = function (val) { | ||
return (0, __1.decodeNamedTuple)(val, ["item", "quantity"]); | ||
}; | ||
Order.decodeBytes = function (val) { | ||
return (0, __1.decodeNamedTuple)(Order.codec.decode(val), ["item", "quantity"]); | ||
}; | ||
Order.codec = algosdk_1.default.ABIType.from("(string,uint16)"); | ||
return Order; | ||
}()); | ||
exports.Order = Order; | ||
var Structer = /** @class */ (function (_super) { | ||
@@ -62,0 +75,0 @@ __extends(Structer, _super); |
@@ -41,2 +41,3 @@ "use strict"; | ||
var CLIENT_NAME = "ApplicationClient"; | ||
// TODO: only import if we _need_ them | ||
var CLIENT_IMPORTS = "{".concat(CLIENT_NAME, ", ABIResult, decodeNamedTuple, Schema, AVMType}"); | ||
@@ -204,7 +205,20 @@ var CLIENT_PATH = "beaker-ts"; | ||
var members = []; | ||
var tupleTypes = []; | ||
var tupleNames = []; | ||
for (var _i = 0, _a = s.elements; _i < _a.length; _i++) { | ||
var elem = _a[_i]; | ||
members.push(typescript_1.factory.createPropertySignature(undefined, typescript_1.factory.createIdentifier(elem[0]), undefined, tsTypeFromAbiType(elem[1]))); | ||
tupleNames.push(elem[0]); | ||
tupleTypes.push(elem[1]); | ||
members.push(typescript_1.factory.createPropertyDeclaration(undefined, undefined, typescript_1.factory.createIdentifier(elem[0]), undefined, tsTypeFromAbiType(elem[1]), undefined)); | ||
} | ||
return typescript_1.factory.createTypeAliasDeclaration(undefined, [typescript_1.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], typescript_1.factory.createIdentifier(s.name), undefined, typescript_1.factory.createTypeLiteralNode(members)); | ||
members.push(typescript_1.factory.createPropertyDeclaration(undefined, [typescript_1.factory.createModifier(typescript_1.default.SyntaxKind.StaticKeyword)], typescript_1.factory.createIdentifier("codec"), undefined, typescript_1.factory.createTypeReferenceNode(typescript_1.factory.createQualifiedName(typescript_1.factory.createIdentifier("algosdk"), typescript_1.factory.createIdentifier("ABIType")), undefined), typescript_1.factory.createCallExpression(typescript_1.factory.createPropertyAccessExpression(typescript_1.factory.createPropertyAccessExpression(typescript_1.factory.createIdentifier("algosdk"), typescript_1.factory.createIdentifier("ABIType")), typescript_1.factory.createIdentifier("from")), undefined, [typescript_1.factory.createStringLiteral("(".concat(tupleTypes.join(","), ")"))]))); | ||
members.push(typescript_1.factory.createMethodDeclaration(undefined, [typescript_1.factory.createModifier(typescript_1.default.SyntaxKind.StaticKeyword)], undefined, typescript_1.factory.createIdentifier("decodeResult"), undefined, undefined, [typescript_1.factory.createParameterDeclaration(undefined, undefined, undefined, typescript_1.factory.createIdentifier("val"), undefined, typescript_1.factory.createTypeReferenceNode(typescript_1.factory.createQualifiedName(typescript_1.factory.createIdentifier("algosdk"), typescript_1.factory.createIdentifier("ABIValue")), undefined), undefined)], typescript_1.factory.createTypeReferenceNode(typescript_1.factory.createIdentifier(s.name), undefined), typescript_1.factory.createBlock([typescript_1.factory.createReturnStatement(typescript_1.factory.createAsExpression(typescript_1.factory.createCallExpression(typescript_1.factory.createIdentifier("decodeNamedTuple"), undefined, [ | ||
typescript_1.factory.createIdentifier("val"), | ||
typescript_1.factory.createArrayLiteralExpression(tupleNames.map(function (name) { return typescript_1.factory.createStringLiteral(name); }), false) | ||
]), typescript_1.factory.createTypeReferenceNode(typescript_1.factory.createIdentifier(s.name), undefined)))], true))); | ||
members.push(typescript_1.factory.createMethodDeclaration(undefined, [typescript_1.factory.createModifier(typescript_1.default.SyntaxKind.StaticKeyword)], undefined, typescript_1.factory.createIdentifier("decodeBytes"), undefined, undefined, [typescript_1.factory.createParameterDeclaration(undefined, undefined, undefined, typescript_1.factory.createIdentifier("val"), undefined, typescript_1.factory.createTypeReferenceNode(typescript_1.factory.createIdentifier("Uint8Array"), undefined), undefined)], typescript_1.factory.createTypeReferenceNode(typescript_1.factory.createIdentifier(s.name), undefined), typescript_1.factory.createBlock([typescript_1.factory.createReturnStatement(typescript_1.factory.createAsExpression(typescript_1.factory.createCallExpression(typescript_1.factory.createIdentifier("decodeNamedTuple"), undefined, [ | ||
typescript_1.factory.createCallExpression(typescript_1.factory.createPropertyAccessExpression(typescript_1.factory.createPropertyAccessExpression(typescript_1.factory.createIdentifier(s.name), typescript_1.factory.createIdentifier("codec")), typescript_1.factory.createIdentifier("decode")), undefined, [typescript_1.factory.createIdentifier("val")]), | ||
typescript_1.factory.createArrayLiteralExpression(tupleNames.map(function (name) { return typescript_1.factory.createStringLiteral(name); }), false) | ||
]), typescript_1.factory.createTypeReferenceNode(typescript_1.factory.createIdentifier(s.name), undefined)))], true))); | ||
return typescript_1.factory.createClassDeclaration(undefined, [typescript_1.factory.createModifier(typescript_1.default.SyntaxKind.ExportKeyword)], typescript_1.factory.createIdentifier(s.name), undefined, undefined, members); | ||
} | ||
@@ -211,0 +225,0 @@ function generateContractProperties(spec) { |
{ | ||
"name": "beaker-ts", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "", | ||
@@ -14,3 +14,3 @@ "main": "./lib/index.js", | ||
"local": "npm install -g && beaker-generate-client example_specs/hello.json src/" | ||
"local": "npm install -g && beaker generate src/examples/hello/hello.json src/examples/hello/" | ||
}, | ||
@@ -17,0 +17,0 @@ "bin": { |
@@ -5,18 +5,8 @@ # Beaker Typescript Client Generator | ||
> :warning: **Barely works, zero tests, hardcoded paths** | ||
> :warning: **zero tests, please report issues** | ||
## Example | ||
## Generate a client Spec | ||
git clone this repo and cd into it | ||
Use [beaker-pyteal](https://github.com/algorand-devrel/beaker) to create an Application and write the app spec to json | ||
```sh | ||
rm src/examples/hello/hellobeaker_client.ts | ||
npm run gen src/examples/hello/hello.json src/examples/hello | ||
npm run hello | ||
``` | ||
## Generate a client | ||
To generate a client, use [beaker-pyteal](https://github.com/algorand-devrel/beaker) to create an Application and write the app spec to json | ||
```py | ||
@@ -38,6 +28,21 @@ import json | ||
## Generate a TypeScript Client | ||
In a node project directory install beaker-ts | ||
```sh | ||
npm run gen $PATH_TO_APP_SPEC_JSON $PATH_TO_WHERE_CLIENT_SHOULD_BE_WRITTEN | ||
npm install beaker-ts | ||
``` | ||
Generate the client using the beaker command | ||
```sh | ||
npx beaker generate $PATH_TO_APP_SPEC_JSON $PATH_TO_WHERE_CLIENT_SHOULD_BE_WRITTEN | ||
``` | ||
This should create a new file called `${NAME_OF_APP}_client.ts` | ||
The client can now be imported and used to create or call the app! | ||
See the [examples directory](./src/examples/) for usage examples. | ||
Please report issues (bound to be lots) |
@@ -5,2 +5,3 @@ import algosdk, { ABIValue, SuggestedParams, TransactionParams } from "algosdk"; | ||
import { parseLogicError, LogicError } from "./logic_error"; | ||
import { ApplicationState, AccountState, decodeState } from "./state" | ||
@@ -328,2 +329,17 @@ export type MethodArg = algosdk.ABIArgument | algosdk.Transaction | object | MethodArg[]; | ||
async getApplicationState(raw?: boolean): Promise<ApplicationState> { | ||
const appInfo = await this.client.getApplicationByID(this.appId).do() | ||
if (!('params' in appInfo) || !('global-state' in appInfo['params'])) throw Error("No global state found") | ||
return decodeState(appInfo['params']['global-state'], raw) as ApplicationState | ||
} | ||
async getAccountState(address?: string, raw?: boolean): Promise<AccountState> { | ||
if(address === undefined) address = this.getSender() | ||
const acctInfo = await this.client.accountApplicationInformation(address, this.appId).do() | ||
if (!('app-local-state' in acctInfo) || !('key-value' in acctInfo['app-local-state'])) throw Error("No global state found") | ||
return decodeState(acctInfo['app-local-state']['key-value'], raw) as ApplicationState | ||
} | ||
private getSender(): string { | ||
@@ -330,0 +346,0 @@ return this.sender; |
@@ -0,1 +1,2 @@ | ||
import { isUint8Array } from 'util/types'; | ||
import { getAccounts, getAlgodClient } from "../../"; | ||
@@ -23,2 +24,11 @@ import {Order, Structer} from "./structer_client"; | ||
console.log("Result: ", result2.value); | ||
const state = await appClient.getAccountState(acct.addr, true) | ||
for(const k in state){ | ||
const val = state[k] | ||
// appease ts | ||
if (!isUint8Array(val)) continue; | ||
console.log(Order.decodeBytes(val)); | ||
} | ||
})(); |
import algosdk from "algosdk"; | ||
import {ApplicationClient, ABIResult, decodeNamedTuple, Schema, AVMType} from "../.."; | ||
export type Order = { | ||
export class Order { | ||
item: string; | ||
quantity: number; | ||
}; | ||
static codec: algosdk.ABIType = algosdk.ABIType.from("(string,uint16)"); | ||
static decodeResult(val: algosdk.ABIValue): Order { | ||
return decodeNamedTuple(val, ["item", "quantity"]) as Order; | ||
} | ||
static decodeBytes(val: Uint8Array): Order { | ||
return decodeNamedTuple(Order.codec.decode(val), ["item", "quantity"]) as Order; | ||
} | ||
} | ||
export class Structer extends ApplicationClient { | ||
@@ -8,0 +15,0 @@ desc: string = ""; |
@@ -21,2 +21,3 @@ import { | ||
const CLIENT_NAME = "ApplicationClient"; | ||
// TODO: only import if we _need_ them | ||
const CLIENT_IMPORTS = `{${CLIENT_NAME}, ABIResult, decodeNamedTuple, Schema, AVMType}` | ||
@@ -378,12 +379,19 @@ const CLIENT_PATH = "beaker-ts"; | ||
function generateStruct(s: Struct): ts.TypeAliasDeclaration { | ||
const members: ts.TypeElement[] = [] | ||
function generateStruct(s: Struct): ts.ClassDeclaration { | ||
const members: ts.ClassElement[] = [] | ||
const tupleTypes: string[] = []; | ||
const tupleNames: string[] = []; | ||
for(const elem of s.elements){ | ||
tupleNames.push(elem[0]) | ||
tupleTypes.push(elem[1]) | ||
members.push( | ||
factory.createPropertySignature( | ||
factory.createPropertyDeclaration( | ||
undefined, | ||
undefined, | ||
factory.createIdentifier(elem[0]), | ||
undefined, | ||
tsTypeFromAbiType(elem[1]) | ||
tsTypeFromAbiType(elem[1]), | ||
undefined, | ||
) | ||
@@ -393,3 +401,136 @@ ) | ||
return factory.createTypeAliasDeclaration( | ||
members.push( | ||
factory.createPropertyDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
factory.createIdentifier("codec"), | ||
undefined, | ||
factory.createTypeReferenceNode( | ||
factory.createQualifiedName( | ||
factory.createIdentifier("algosdk"), | ||
factory.createIdentifier("ABIType") | ||
), | ||
undefined | ||
), | ||
factory.createCallExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier("algosdk"), | ||
factory.createIdentifier("ABIType") | ||
), | ||
factory.createIdentifier("from") | ||
), | ||
undefined, | ||
[factory.createStringLiteral(`(${tupleTypes.join(",")})`)] | ||
) | ||
) | ||
) | ||
members.push( | ||
factory.createMethodDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
undefined, | ||
factory.createIdentifier("decodeResult"), | ||
undefined, | ||
undefined, | ||
[factory.createParameterDeclaration( | ||
undefined, | ||
undefined, | ||
undefined, | ||
factory.createIdentifier("val"), | ||
undefined, | ||
factory.createTypeReferenceNode( | ||
factory.createQualifiedName( | ||
factory.createIdentifier("algosdk"), | ||
factory.createIdentifier("ABIValue") | ||
), | ||
undefined | ||
), | ||
undefined | ||
)], | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
), | ||
factory.createBlock( | ||
[factory.createReturnStatement(factory.createAsExpression( | ||
factory.createCallExpression( | ||
factory.createIdentifier("decodeNamedTuple"), | ||
undefined, | ||
[ | ||
factory.createIdentifier("val"), | ||
factory.createArrayLiteralExpression( | ||
tupleNames.map((name)=>{ return factory.createStringLiteral(name) }), | ||
false | ||
) | ||
] | ||
), | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
) | ||
))], | ||
true | ||
) | ||
) | ||
) | ||
members.push( | ||
factory.createMethodDeclaration( | ||
undefined, | ||
[factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
undefined, | ||
factory.createIdentifier("decodeBytes"), | ||
undefined, undefined, | ||
[factory.createParameterDeclaration( | ||
undefined, undefined, undefined, | ||
factory.createIdentifier("val"), | ||
undefined, | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier("Uint8Array"), | ||
undefined | ||
), | ||
undefined | ||
)], | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
), | ||
factory.createBlock( | ||
[factory.createReturnStatement(factory.createAsExpression( | ||
factory.createCallExpression( | ||
factory.createIdentifier("decodeNamedTuple"), | ||
undefined, | ||
[ | ||
factory.createCallExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createPropertyAccessExpression( | ||
factory.createIdentifier(s.name), | ||
factory.createIdentifier("codec") | ||
), | ||
factory.createIdentifier("decode") | ||
), | ||
undefined, | ||
[factory.createIdentifier("val")] | ||
), | ||
factory.createArrayLiteralExpression( | ||
tupleNames.map((name)=>{ return factory.createStringLiteral(name) }), | ||
false | ||
) | ||
] | ||
), | ||
factory.createTypeReferenceNode( | ||
factory.createIdentifier(s.name), | ||
undefined | ||
) | ||
))], | ||
true | ||
) | ||
) | ||
) | ||
return factory.createClassDeclaration( | ||
undefined, | ||
@@ -399,3 +540,4 @@ [factory.createModifier(ts.SyntaxKind.ExportKeyword)], | ||
undefined, | ||
factory.createTypeLiteralNode(members) | ||
undefined, | ||
members | ||
) | ||
@@ -402,0 +544,0 @@ |
236202
71
4142
46