Comparing version 0.0.13 to 0.0.14
@@ -1,2 +0,2 @@ | ||
import algosdk, { ABIValue } from "algosdk"; | ||
import algosdk from "algosdk"; | ||
import { Schema } from "../"; | ||
@@ -9,3 +9,8 @@ import { ApplicationState, AccountState } from "./state"; | ||
export declare type ABIReturnType = object | void | algosdk.ABIValue; | ||
export declare function decodeNamedTuple(v: ABIValue, keys: string[]): object; | ||
export declare function decodeNamedTuple(v: algosdk.ABIValue, keys: string[]): object; | ||
export interface InnerTransaction { | ||
txn: algosdk.Transaction; | ||
createdAsset?: bigint; | ||
createdApp?: bigint; | ||
} | ||
export declare class ABIResult<T extends ABIReturnType> { | ||
@@ -15,7 +20,7 @@ txID: string; | ||
method: algosdk.ABIMethod; | ||
returnValue: ABIValue; | ||
returnValue: algosdk.ABIValue; | ||
decodeError?: Error; | ||
txInfo?: Record<string, any>; | ||
value: T; | ||
inners: Record<string, any>[]; | ||
inners: InnerTransaction[]; | ||
constructor(result: algosdk.ABIResult | undefined, value?: T); | ||
@@ -22,0 +27,0 @@ } |
@@ -63,3 +63,5 @@ "use strict"; | ||
throw Error("Different key length than value length"); | ||
return Object.fromEntries(keys.map(function (key, idx) { return [key, v[idx]]; })); | ||
return Object.fromEntries(keys.map(function (key, idx) { | ||
return [key, v[idx]]; | ||
})); | ||
} | ||
@@ -75,9 +77,14 @@ exports.decodeNamedTuple = decodeNamedTuple; | ||
this.inners = []; | ||
if (result.txInfo !== undefined && 'inner-txns' in result.txInfo) { | ||
var outer_1 = result.txInfo['txn']['txn']; | ||
this.inners = result.txInfo['inner-txns'].map(function (itxn) { | ||
var et = itxn['txn']['txn']; | ||
if (result.txInfo !== undefined && "inner-txns" in result.txInfo) { | ||
// TODO: this only parses 1 level deep | ||
var outer_1 = result.txInfo["txn"]["txn"]; | ||
this.inners = result.txInfo["inner-txns"].map(function (itxn) { | ||
var et = itxn["txn"]["txn"]; | ||
et.gen = outer_1.gen; | ||
et.gh = outer_1.gh; | ||
return algosdk_1.default.Transaction.from_obj_for_encoding(itxn['txn']['txn']); | ||
return { | ||
createdAsset: itxn['asset-index'], | ||
createdApp: itxn['application-index'], | ||
txn: algosdk_1.default.Transaction.from_obj_for_encoding(itxn["txn"]["txn"]), | ||
}; | ||
}); | ||
@@ -358,3 +365,6 @@ } | ||
if (arg instanceof algosdk_1.default.Transaction) { | ||
arg = { txn: arg, signer: this.signer }; | ||
arg = { | ||
txn: arg, | ||
signer: this.signer, | ||
}; | ||
} | ||
@@ -400,5 +410,5 @@ else if (arg instanceof Object) { | ||
appInfo = _a.sent(); | ||
if (!('params' in appInfo) || !('global-state' in appInfo['params'])) | ||
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)]; | ||
return [2 /*return*/, (0, state_1.decodeState)(appInfo["params"]["global-state"], raw)]; | ||
} | ||
@@ -416,8 +426,11 @@ }); | ||
address = this.getSender(); | ||
return [4 /*yield*/, this.client.accountApplicationInformation(address, this.appId).do()]; | ||
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'])) | ||
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)]; | ||
return [2 /*return*/, (0, state_1.decodeState)(acctInfo["app-local-state"]["key-value"], raw)]; | ||
} | ||
@@ -424,0 +437,0 @@ }); |
@@ -47,9 +47,9 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var acct, appClient, _a, appId, appAddr, txId, _b, assetA, assetB, bootstrapResult, _c, _d, _e, _f, poolToken, optInToAsset, _g, _h, fundResult, _j, _k, _l, _m, _o, _p, _q, swapAtoB, _r, _s, _t, _u, swapBtoA, _v, _w, _x, _y, burnResult, _z, _0, _1, _2, _3, aRcv, bRcv; | ||
var _4, _5, _6, _7, _8, _9, _10; | ||
return __generator(this, function (_11) { | ||
switch (_11.label) { | ||
var acct, appClient, assetA, assetB, _a, appId, appAddr, txId, bootstrapResult, _b, _c, _d, _e, poolToken, optInToAsset, _f, _g, fundResult, _h, _j, _k, _l, _m, _o, _p, swapAtoB, _q, _r, _s, _t, swapBtoA, _u, _v, _w, _x, burnResult, _y, _z, _0, _1, _2, aRcv, bRcv; | ||
var _3, _4, _5, _6, _7, _8, _9; | ||
return __generator(this, function (_10) { | ||
switch (_10.label) { | ||
case 0: return [4 /*yield*/, (0, __1.getAccounts)()]; | ||
case 1: | ||
acct = (_11.sent()).pop(); | ||
acct = (_10.sent()).pop(); | ||
appClient = new constantproductamm_client_1.ConstantProductAMM({ | ||
@@ -60,13 +60,16 @@ client: (0, __1.getAlgodClient)(), | ||
}); | ||
return [4 /*yield*/, appClient.create()]; | ||
return [4 /*yield*/, createAsset(appClient.client, acct, "A")]; | ||
case 2: | ||
_a = _11.sent(), appId = _a[0], appAddr = _a[1], txId = _a[2]; | ||
console.log("Created app ".concat(appId, " with address ").concat(appAddr, " in tx ").concat(txId)); | ||
return [4 /*yield*/, createAssets(appClient.client, acct)]; | ||
assetA = _10.sent(); | ||
return [4 /*yield*/, createAsset(appClient.client, acct, "B")]; | ||
case 3: | ||
_b = _11.sent(), assetA = _b[0], assetB = _b[1]; | ||
assetB = _10.sent(); | ||
console.log("created ".concat(assetA, ", ").concat(assetB)); | ||
_d = (_c = appClient).bootstrap; | ||
_f = (_e = algosdk_1.default).makePaymentTxnWithSuggestedParamsFromObject; | ||
_4 = { | ||
return [4 /*yield*/, appClient.create()]; | ||
case 4: | ||
_a = _10.sent(), appId = _a[0], appAddr = _a[1], txId = _a[2]; | ||
console.log("Created app ".concat(appId, " with address ").concat(appAddr, " in tx ").concat(txId)); | ||
_c = (_b = appClient).bootstrap; | ||
_e = (_d = algosdk_1.default).makePaymentTxnWithSuggestedParamsFromObject; | ||
_3 = { | ||
from: acct.addr, | ||
@@ -76,12 +79,14 @@ to: appAddr | ||
return [4 /*yield*/, appClient.client.getTransactionParams().do()]; | ||
case 4: return [4 /*yield*/, _d.apply(_c, [_f.apply(_e, [(_4.suggestedParams = _11.sent(), | ||
_4.amount = BigInt(1e6), | ||
_4)]), | ||
case 5: return [4 /*yield*/, _c.apply(_b, [_e.apply(_d, [(_3.suggestedParams = _10.sent(), | ||
_3.amount = BigInt(1e6), | ||
_3)]), | ||
assetA, | ||
assetB])]; | ||
case 5: | ||
bootstrapResult = _11.sent(); | ||
case 6: | ||
bootstrapResult = _10.sent(); | ||
// Can get the createdAsset from the inners | ||
console.log(bootstrapResult.inners[0].createdAsset); | ||
poolToken = bootstrapResult.value; | ||
_h = (_g = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
_5 = { | ||
_g = (_f = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
_4 = { | ||
from: acct.addr, | ||
@@ -91,7 +96,7 @@ to: acct.addr | ||
return [4 /*yield*/, appClient.client.getTransactionParams().do()]; | ||
case 6: | ||
optInToAsset = _h.apply(_g, [(_5.suggestedParams = _11.sent(), | ||
_5.amount = 0, | ||
_5.assetIndex = Number(poolToken), | ||
_5)]); | ||
case 7: | ||
optInToAsset = _g.apply(_f, [(_4.suggestedParams = _10.sent(), | ||
_4.amount = 0, | ||
_4.assetIndex = Number(poolToken), | ||
_4)]); | ||
appClient.client.sendRawTransaction(optInToAsset.signTxn(acct.privateKey)).do(); | ||
@@ -101,6 +106,17 @@ return [4 /*yield*/, algosdk_1.default.waitForConfirmation(appClient.client, optInToAsset.txID(), 4) | ||
]; | ||
case 7: | ||
_11.sent(); | ||
_k = (_j = appClient).mint; | ||
_m = (_l = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
case 8: | ||
_10.sent(); | ||
_j = (_h = appClient).mint; | ||
_l = (_k = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
_5 = { | ||
from: acct.addr, | ||
to: appAddr | ||
}; | ||
return [4 /*yield*/, appClient.client.getTransactionParams().do()]; | ||
case 9: | ||
_m = [_l.apply(_k, [(_5.suggestedParams = _10.sent(), | ||
_5.amount = BigInt(1e8), | ||
_5.assetIndex = Number(assetA), | ||
_5)])]; | ||
_p = (_o = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
_6 = { | ||
@@ -111,8 +127,14 @@ from: acct.addr, | ||
return [4 /*yield*/, appClient.client.getTransactionParams().do()]; | ||
case 8: | ||
_o = [_m.apply(_l, [(_6.suggestedParams = _11.sent(), | ||
_6.amount = BigInt(1e8), | ||
_6.assetIndex = Number(assetA), | ||
_6)])]; | ||
_q = (_p = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
case 10: return [4 /*yield*/, _j.apply(_h, _m.concat([_p.apply(_o, [(_6.suggestedParams = _10.sent(), | ||
_6.amount = BigInt(1e6), | ||
_6.assetIndex = Number(assetB), | ||
_6)]), | ||
poolToken, | ||
assetA, | ||
assetB]))]; | ||
case 11: | ||
fundResult = _10.sent(); | ||
console.log("Received ".concat(fundResult.inners[0].txn.amount, " pool tokens")); | ||
_r = (_q = appClient).swap; | ||
_t = (_s = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
_7 = { | ||
@@ -123,14 +145,13 @@ from: acct.addr, | ||
return [4 /*yield*/, appClient.client.getTransactionParams().do()]; | ||
case 9: return [4 /*yield*/, _k.apply(_j, _o.concat([_q.apply(_p, [(_7.suggestedParams = _11.sent(), | ||
_7.amount = BigInt(1e6), | ||
_7.assetIndex = Number(assetB), | ||
case 12: return [4 /*yield*/, _r.apply(_q, [_t.apply(_s, [(_7.suggestedParams = _10.sent(), | ||
_7.amount = BigInt(1e3), | ||
_7.assetIndex = Number(assetA), | ||
_7)]), | ||
poolToken, | ||
assetA, | ||
assetB]))]; | ||
case 10: | ||
fundResult = _11.sent(); | ||
console.log("Received ".concat(fundResult.inners[0].aamt, " pool tokens")); | ||
_s = (_r = appClient).swap; | ||
_u = (_t = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
assetB])]; | ||
case 13: | ||
swapAtoB = _10.sent(); | ||
console.log("Received ".concat(swapAtoB.inners[0].txn.amount, " B tokens")); | ||
_v = (_u = appClient).swap; | ||
_x = (_w = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
_8 = { | ||
@@ -141,13 +162,13 @@ from: acct.addr, | ||
return [4 /*yield*/, appClient.client.getTransactionParams().do()]; | ||
case 11: return [4 /*yield*/, _s.apply(_r, [_u.apply(_t, [(_8.suggestedParams = _11.sent(), | ||
case 14: return [4 /*yield*/, _v.apply(_u, [_x.apply(_w, [(_8.suggestedParams = _10.sent(), | ||
_8.amount = BigInt(1e3), | ||
_8.assetIndex = Number(assetA), | ||
_8.assetIndex = Number(assetB), | ||
_8)]), | ||
assetA, | ||
assetB])]; | ||
case 12: | ||
swapAtoB = _11.sent(); | ||
console.log("Received ".concat(swapAtoB.inners[0].aamt, " B tokens")); | ||
_w = (_v = appClient).swap; | ||
_y = (_x = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
case 15: | ||
swapBtoA = _10.sent(); | ||
console.log("Received ".concat(swapBtoA.inners[0].txn.amount, " A tokens")); | ||
_z = (_y = appClient).burn; | ||
_1 = (_0 = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
_9 = { | ||
@@ -158,29 +179,13 @@ from: acct.addr, | ||
return [4 /*yield*/, appClient.client.getTransactionParams().do()]; | ||
case 13: return [4 /*yield*/, _w.apply(_v, [_y.apply(_x, [(_9.suggestedParams = _11.sent(), | ||
_9.amount = BigInt(1e3), | ||
_9.assetIndex = Number(assetB), | ||
case 16: return [4 /*yield*/, _z.apply(_y, [_1.apply(_0, [(_9.suggestedParams = _10.sent(), | ||
_9.amount = BigInt(10), | ||
_9.assetIndex = Number(poolToken), | ||
_9)]), | ||
assetA, | ||
assetB])]; | ||
case 14: | ||
swapBtoA = _11.sent(); | ||
console.log("Received ".concat(swapBtoA.inners[0].aamt, " A tokens")); | ||
_0 = (_z = appClient).burn; | ||
_2 = (_1 = algosdk_1.default).makeAssetTransferTxnWithSuggestedParamsFromObject; | ||
_10 = { | ||
from: acct.addr, | ||
to: appAddr | ||
}; | ||
return [4 /*yield*/, appClient.client.getTransactionParams().do()]; | ||
case 15: return [4 /*yield*/, _0.apply(_z, [_2.apply(_1, [(_10.suggestedParams = _11.sent(), | ||
_10.amount = BigInt(10), | ||
_10.assetIndex = Number(poolToken), | ||
_10)]), | ||
poolToken, | ||
assetA, | ||
assetB])]; | ||
case 16: | ||
burnResult = _11.sent(); | ||
_3 = burnResult.inners, aRcv = _3[0], bRcv = _3[1]; | ||
console.log("Received ".concat(aRcv.aamt, " A tokens and ").concat(bRcv.aamt, " B tokens")); | ||
case 17: | ||
burnResult = _10.sent(); | ||
_2 = burnResult.inners, aRcv = _2[0], bRcv = _2[1]; | ||
console.log("Received ".concat(aRcv.txn.amount, " A tokens and ").concat(bRcv.txn.amount, " B tokens")); | ||
return [2 /*return*/]; | ||
@@ -191,42 +196,29 @@ } | ||
})(); | ||
function createAssets(client, acct) { | ||
function createAsset(client, acct, unitName) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var sp, createA, createB, groupedTxns, signedTxns, aResult, bResult; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, client.getTransactionParams().do()]; | ||
var create, _a, _b, result; | ||
var _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
case 0: | ||
_b = (_a = algosdk_1.default).makeAssetCreateTxnWithSuggestedParamsFromObject; | ||
_c = { | ||
from: acct.addr | ||
}; | ||
return [4 /*yield*/, client.getTransactionParams().do()]; | ||
case 1: | ||
sp = _a.sent(); | ||
createA = algosdk_1.default.makeAssetCreateTxnWithSuggestedParamsFromObject({ | ||
from: acct.addr, | ||
suggestedParams: sp, | ||
total: BigInt(1e10), | ||
decimals: 0, | ||
defaultFrozen: false, | ||
assetName: "Asset A", | ||
unitName: "a", | ||
}); | ||
createB = algosdk_1.default.makeAssetCreateTxnWithSuggestedParamsFromObject({ | ||
from: acct.addr, | ||
suggestedParams: sp, | ||
total: BigInt(1e9), | ||
decimals: 0, | ||
defaultFrozen: false, | ||
assetName: "Asset B", | ||
unitName: "b", | ||
}); | ||
groupedTxns = algosdk_1.default.assignGroupID([createA, createB]); | ||
signedTxns = groupedTxns.map(function (txn) { | ||
return txn.signTxn(acct.privateKey); | ||
}); | ||
return [4 /*yield*/, client.sendRawTransaction(signedTxns).do()]; | ||
create = _b.apply(_a, [(_c.suggestedParams = _d.sent(), | ||
_c.total = BigInt(1e10), | ||
_c.decimals = 0, | ||
_c.defaultFrozen = false, | ||
_c.assetName = "Asset ".concat(unitName), | ||
_c.unitName = unitName, | ||
_c)]); | ||
return [4 /*yield*/, client.sendRawTransaction(create.signTxn(acct.privateKey)).do()]; | ||
case 2: | ||
_a.sent(); | ||
return [4 /*yield*/, algosdk_1.default.waitForConfirmation(client, groupedTxns[0].txID(), 4)]; | ||
_d.sent(); | ||
return [4 /*yield*/, algosdk_1.default.waitForConfirmation(client, create.txID(), 4)]; | ||
case 3: | ||
aResult = _a.sent(); | ||
return [4 /*yield*/, algosdk_1.default.waitForConfirmation(client, groupedTxns[1].txID(), 4)]; | ||
case 4: | ||
bResult = _a.sent(); | ||
return [2 /*return*/, [BigInt(aResult["asset-index"]), BigInt(bResult["asset-index"])]]; | ||
result = _d.sent(); | ||
return [2 /*return*/, BigInt(result["asset-index"])]; | ||
} | ||
@@ -233,0 +225,0 @@ }); |
{ | ||
"name": "beaker-ts", | ||
"version": "0.0.13", | ||
"version": "0.0.14", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -1,22 +0,37 @@ | ||
import algosdk, { ABIValue, EncodedTransaction, SuggestedParams, TransactionParams } from "algosdk"; | ||
import algosdk from "algosdk"; | ||
import { getStateSchema, Schema } from "../"; | ||
import { parseLogicError, LogicError } from "./logic_error"; | ||
import { ApplicationState, AccountState, decodeState } from "./state" | ||
import { ApplicationState, AccountState, decodeState } from "./state"; | ||
export type MethodArg = algosdk.ABIArgument | algosdk.Transaction | object | MethodArg[]; | ||
export type MethodArg = | ||
| algosdk.ABIArgument | ||
| algosdk.Transaction | ||
| object | ||
| MethodArg[]; | ||
export type MethodArgs = { | ||
[key: string]: MethodArg | ||
[key: string]: MethodArg; | ||
}; | ||
export type ABIReturnType = object | void | algosdk.ABIValue | ||
export type ABIReturnType = object | void | algosdk.ABIValue; | ||
export function decodeNamedTuple(v: ABIValue, keys: string[]): object { | ||
if(!Array.isArray(v)) throw Error("Expected array") | ||
if(v.length != keys.length) throw Error("Different key length than value length") | ||
export function decodeNamedTuple(v: algosdk.ABIValue, keys: string[]): object { | ||
if (!Array.isArray(v)) throw Error("Expected array"); | ||
if (v.length != keys.length) | ||
throw Error("Different key length than value length"); | ||
return Object.fromEntries(keys.map((key, idx)=>{ return [key, v[idx]]})) | ||
return Object.fromEntries( | ||
keys.map((key, idx) => { | ||
return [key, v[idx]]; | ||
}) | ||
); | ||
} | ||
export interface InnerTransaction { | ||
txn: algosdk.Transaction; | ||
createdAsset?: bigint; | ||
createdApp?: bigint; | ||
} | ||
export class ABIResult<T extends ABIReturnType> { | ||
@@ -26,3 +41,3 @@ txID: string; | ||
method: algosdk.ABIMethod; | ||
returnValue: ABIValue; | ||
returnValue: algosdk.ABIValue; | ||
decodeError?: Error; | ||
@@ -32,5 +47,5 @@ txInfo?: Record<string, any>; | ||
value: T; | ||
inners: Record<string, any>[]; | ||
inners: InnerTransaction[]; | ||
constructor(result: algosdk.ABIResult | undefined, value?: T){ | ||
constructor(result: algosdk.ABIResult | undefined, value?: T) { | ||
this.txID = result.txID; | ||
@@ -43,20 +58,24 @@ this.rawReturnValue = result.rawReturnValue; | ||
this.inners = []; | ||
if (result.txInfo !== undefined && 'inner-txns' in result.txInfo){ | ||
const outer = result.txInfo['txn']['txn'] as EncodedTransaction | ||
this.inners = result.txInfo['inner-txns'].map((itxn: any)=>{ | ||
const et = itxn['txn']['txn'] as EncodedTransaction | ||
et.gen = outer.gen | ||
et.gh = outer.gh | ||
return algosdk.Transaction.from_obj_for_encoding(itxn['txn']['txn'] as EncodedTransaction) | ||
}) | ||
if (result.txInfo !== undefined && "inner-txns" in result.txInfo) { | ||
// TODO: this only parses 1 level deep | ||
const outer = result.txInfo["txn"]["txn"] as algosdk.EncodedTransaction; | ||
this.inners = result.txInfo["inner-txns"].map((itxn: any) => { | ||
const et = itxn["txn"]["txn"] as algosdk.EncodedTransaction; | ||
et.gen = outer.gen; | ||
et.gh = outer.gh; | ||
return { | ||
createdAsset: itxn['asset-index'] as bigint, | ||
createdApp: itxn['application-index'], | ||
txn: algosdk.Transaction.from_obj_for_encoding( | ||
itxn["txn"]["txn"] as algosdk.EncodedTransaction | ||
), | ||
} as InnerTransaction; | ||
}); | ||
} | ||
this.returnValue = result.returnValue | ||
this.value = value | ||
this.returnValue = result.returnValue; | ||
this.value = value; | ||
} | ||
} | ||
export class ApplicationClient { | ||
@@ -126,3 +145,5 @@ client: algosdk.Algodv2; | ||
async create(txParams?: algosdk.TransactionParams): Promise<[number, string, string]> { | ||
async create( | ||
txParams?: algosdk.TransactionParams | ||
): Promise<[number, string, string]> { | ||
await this.ensurePrograms(); | ||
@@ -185,3 +206,3 @@ | ||
const sp = await this.getSuggestedParams(txParams) | ||
const sp = await this.getSuggestedParams(txParams); | ||
@@ -275,6 +296,5 @@ const atc = new algosdk.AtomicTransactionComposer(); | ||
): Promise<algosdk.ABIResult> { | ||
const atc = new algosdk.AtomicTransactionComposer(); | ||
await this.addMethodCall(atc, method, args, txParams) | ||
await this.addMethodCall(atc, method, args, txParams); | ||
@@ -286,3 +306,2 @@ try { | ||
} | ||
} | ||
@@ -296,3 +315,2 @@ | ||
): Promise<algosdk.AtomicTransactionComposer> { | ||
const sp = await this.getSuggestedParams(txParams); | ||
@@ -302,3 +320,2 @@ | ||
for (const expected_arg of method.args) { | ||
if (!(expected_arg.name in args)) { | ||
@@ -312,5 +329,8 @@ // Error! (or check hints) | ||
if (arg instanceof algosdk.Transaction) { | ||
arg = { txn: arg, signer: this.signer } as algosdk.TransactionWithSigner; | ||
} else if(arg instanceof Object){ | ||
arg = Object.values(arg) | ||
arg = { | ||
txn: arg, | ||
signer: this.signer, | ||
} as algosdk.TransactionWithSigner; | ||
} else if (arg instanceof Object) { | ||
arg = Object.values(arg); | ||
} | ||
@@ -331,3 +351,3 @@ | ||
return atc | ||
return atc; | ||
} | ||
@@ -346,22 +366,39 @@ | ||
async getSuggestedParams(txParams?: algosdk.TransactionParams): Promise<algosdk.SuggestedParams> { | ||
if(txParams !== undefined && txParams.suggestedParams !== undefined) return txParams.suggestedParams | ||
async getSuggestedParams( | ||
txParams?: algosdk.TransactionParams | ||
): Promise<algosdk.SuggestedParams> { | ||
if (txParams !== undefined && txParams.suggestedParams !== undefined) | ||
return txParams.suggestedParams; | ||
return await this.client.getTransactionParams().do(); | ||
} | ||
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 | ||
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 | ||
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 { | ||
@@ -386,3 +423,2 @@ return this.sender; | ||
} | ||
} |
@@ -15,2 +15,7 @@ import algosdk from "algosdk"; | ||
// Create assets for demo that we'll want to use to create a pool | ||
const assetA = await createAsset(appClient.client, acct, "A"); | ||
const assetB = await createAsset(appClient.client, acct, "B"); | ||
console.log(`created ${assetA}, ${assetB}`); | ||
// Deploy app on chain | ||
@@ -20,6 +25,2 @@ const [appId, appAddr, txId] = await appClient.create(); | ||
// Create assets for demo that we'll want to use to create a pool | ||
const [assetA, assetB] = await createAssets(appClient.client, acct); | ||
console.log(`created ${assetA}, ${assetB}`); | ||
// Bootstrap the app with assets we're using | ||
@@ -36,2 +37,4 @@ const bootstrapResult = await appClient.bootstrap( | ||
); | ||
// Can get the createdAsset from the inners | ||
console.log(bootstrapResult.inners[0].createdAsset) | ||
@@ -72,3 +75,3 @@ // The return value is the id of the pool token | ||
) | ||
console.log(`Received ${fundResult.inners[0].aamt} pool tokens`) | ||
console.log(`Received ${fundResult.inners[0].txn.amount} pool tokens`) | ||
@@ -87,3 +90,3 @@ // Try to swap A for B | ||
) | ||
console.log(`Received ${swapAtoB.inners[0].aamt} B tokens`) | ||
console.log(`Received ${swapAtoB.inners[0].txn.amount} B tokens`) | ||
@@ -102,3 +105,3 @@ // Try to swap B for A | ||
) | ||
console.log(`Received ${swapBtoA.inners[0].aamt} A tokens`) | ||
console.log(`Received ${swapBtoA.inners[0].txn.amount} A tokens`) | ||
@@ -120,50 +123,26 @@ // Burn some pool tokens | ||
console.log(`Received ${aRcv.aamt} A tokens and ${bRcv.aamt} B tokens`) | ||
console.log(`Received ${aRcv.txn.amount} A tokens and ${bRcv.txn.amount} B tokens`) | ||
})(); | ||
async function createAssets( | ||
async function createAsset( | ||
client: algosdk.Algodv2, | ||
acct: SandboxAccount | ||
): Promise<[bigint, bigint]> { | ||
const sp = await client.getTransactionParams().do(); | ||
const createA = algosdk.makeAssetCreateTxnWithSuggestedParamsFromObject({ | ||
acct: SandboxAccount, | ||
unitName: string, | ||
): Promise<bigint> { | ||
const create = algosdk.makeAssetCreateTxnWithSuggestedParamsFromObject({ | ||
from: acct.addr, | ||
suggestedParams: sp, | ||
suggestedParams: await client.getTransactionParams().do(), | ||
total: BigInt(1e10), | ||
decimals: 0, | ||
defaultFrozen: false, | ||
assetName: "Asset A", | ||
unitName: "a", | ||
assetName: `Asset ${unitName}`, | ||
unitName: unitName, | ||
}); | ||
await client.sendRawTransaction(create.signTxn(acct.privateKey)).do(); | ||
const createB = algosdk.makeAssetCreateTxnWithSuggestedParamsFromObject({ | ||
from: acct.addr, | ||
suggestedParams: sp, | ||
total: BigInt(1e9), | ||
decimals: 0, | ||
defaultFrozen: false, | ||
assetName: "Asset B", | ||
unitName: "b", | ||
}); | ||
const result = await algosdk.waitForConfirmation( client, create.txID(), 4); | ||
const groupedTxns = algosdk.assignGroupID([createA, createB]); | ||
const signedTxns = groupedTxns.map((txn) => { | ||
return txn.signTxn(acct.privateKey); | ||
}); | ||
return BigInt(result["asset-index"]); | ||
await client.sendRawTransaction(signedTxns).do(); | ||
const aResult = await algosdk.waitForConfirmation( | ||
client, | ||
groupedTxns[0].txID(), | ||
4 | ||
); | ||
const bResult = await algosdk.waitForConfirmation( | ||
client, | ||
groupedTxns[1].txID(), | ||
4 | ||
); | ||
return [BigInt(aResult["asset-index"]), BigInt(bResult["asset-index"])]; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
318983
5055