Skip to content

Commit 5714a98

Browse files
committed
refactor: replace algosdk with algokit-utils
Remove algosdk dependency and use @algorandfoundation/algokit-utils instead. - Use encodeSignedTransactionToJson for wire-format JSON encoding - Import types and utilities from algokit-utils subpaths - Simplify displaySignedTxnJson to re-export from utils
1 parent 26f496c commit 5714a98

File tree

8 files changed

+3399
-1880
lines changed

8 files changed

+3399
-1880
lines changed

package-lock.json

Lines changed: 3218 additions & 1612 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,23 +69,22 @@
6969
"pre-commit": "npm run lint && npm run typecheck && npm run format"
7070
},
7171
"dependencies": {
72+
"@algorandfoundation/algokit-utils": "file:../algokit-utils-ts/algorandfoundation-algokit-utils-0.1.0.tgz",
7273
"@vscode/debugadapter": "^1.64.0",
73-
"algosdk": "^3.0.0",
7474
"await-notify": "^1.0.1"
7575
},
7676
"devDependencies": {
77-
"semantic-release": "^24.1.2",
7877
"@istanbuljs/nyc-config-typescript": "^1.0.2",
7978
"@types/glob": "^7.2.0",
8079
"@types/lodash": "^4.14.196",
8180
"@types/mocha": "^9.1.0",
8281
"@types/node": "^14.14.37",
8382
"@types/vscode": "^1.66.0",
84-
"conventional-changelog-conventionalcommits": "8.0.0",
8583
"@typescript-eslint/eslint-plugin": "^5.62.0",
8684
"@typescript-eslint/parser": "^5.62.0",
8785
"@vscode/debugadapter-testsupport": "^1.64.0",
8886
"@vscode/vsce": "^2.22.0",
87+
"conventional-changelog-conventionalcommits": "8.0.0",
8988
"esbuild": "^0.14.29",
9089
"eslint": "^8.52.0",
9190
"eslint-config-prettier": "^9.0.0",
@@ -94,6 +93,7 @@
9493
"mocha": "^10.2.0",
9594
"nyc": "^15.1.0",
9695
"prettier": "^3.0.3",
96+
"semantic-release": "^24.1.2",
9797
"shx": "^0.3.4",
9898
"ts-mocha": "^10.0.0",
9999
"typescript": "^4.6.3",

src/common/appState.ts

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,31 @@
1-
import * as algosdk from 'algosdk';
1+
import type { AvmValue, AvmKeyValue, ApplicationInitialStates } from '@algorandfoundation/algokit-utils/algod-client';
2+
import { hexToBytes } from '@algorandfoundation/algokit-utils/common';
23
import { ByteArrayMap } from './utils';
34

45
export class AppState {
5-
globalState: ByteArrayMap<algosdk.modelsv2.AvmValue>;
6-
localState: Map<string, ByteArrayMap<algosdk.modelsv2.AvmValue>>;
7-
boxState: ByteArrayMap<algosdk.modelsv2.AvmValue>;
6+
globalState: ByteArrayMap<AvmValue>;
7+
localState: Map<string, ByteArrayMap<AvmValue>>;
8+
boxState: ByteArrayMap<AvmValue>;
89

910
constructor() {
10-
this.globalState = new ByteArrayMap<algosdk.modelsv2.AvmValue>();
11-
this.localState = new Map<
12-
string,
13-
ByteArrayMap<algosdk.modelsv2.AvmValue>
14-
>();
15-
this.boxState = new ByteArrayMap<algosdk.modelsv2.AvmValue>();
11+
this.globalState = new ByteArrayMap<AvmValue>();
12+
this.localState = new Map<string, ByteArrayMap<AvmValue>>();
13+
this.boxState = new ByteArrayMap<AvmValue>();
1614
}
1715

18-
public globalStateArray(): algosdk.modelsv2.AvmKeyValue[] {
16+
public globalStateArray(): AvmKeyValue[] {
1917
return createAvmKvArray(this.globalState);
2018
}
2119

22-
public localStateArray(account: string): algosdk.modelsv2.AvmKeyValue[] {
20+
public localStateArray(account: string): AvmKeyValue[] {
2321
const map = this.localState.get(account);
2422
if (!map) {
2523
return [];
2624
}
2725
return createAvmKvArray(map);
2826
}
2927

30-
public boxStateArray(): algosdk.modelsv2.AvmKeyValue[] {
28+
public boxStateArray(): AvmKeyValue[] {
3129
return createAvmKvArray(this.boxState);
3230
}
3331

@@ -45,7 +43,7 @@ export class AppState {
4543
}
4644

4745
public static fromAppInitialState(
48-
initialState: algosdk.modelsv2.ApplicationInitialStates,
46+
initialState: ApplicationInitialStates,
4947
): AppState {
5048
const state = new AppState();
5149

@@ -56,7 +54,7 @@ export class AppState {
5654
}
5755

5856
for (const appLocal of initialState.appLocals || []) {
59-
const map = new ByteArrayMap<algosdk.modelsv2.AvmValue>();
57+
const map = new ByteArrayMap<AvmValue>();
6058
for (const { key, value } of appLocal.kvs) {
6159
map.set(key, value);
6260
}
@@ -73,16 +71,11 @@ export class AppState {
7371
}
7472
}
7573

76-
function createAvmKvArray(
77-
map: ByteArrayMap<algosdk.modelsv2.AvmValue>,
78-
): algosdk.modelsv2.AvmKeyValue[] {
74+
function createAvmKvArray(map: ByteArrayMap<AvmValue>): AvmKeyValue[] {
7975
return Array.from(map.entriesHex())
8076
.sort()
81-
.map(
82-
([key, value]) =>
83-
new algosdk.modelsv2.AvmKeyValue({
84-
key: algosdk.hexToBytes(key),
85-
value,
86-
}),
87-
);
77+
.map(([key, value]) => ({
78+
key: hexToBytes(key),
79+
value,
80+
}));
8881
}

src/common/debugSession.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import {
1616
import { DebugProtocol } from '@vscode/debugprotocol';
1717
import { AvmRuntime, IRuntimeBreakpoint } from './runtime';
1818
import { Subject } from 'await-notify';
19-
import * as algosdk from 'algosdk';
19+
import type { AvmValue, AvmKeyValue } from '@algorandfoundation/algokit-utils/algod-client';
20+
import { bytesToHex, bytesToBase64, encodeAddress, hexToBytes, decodeAddress } from '@algorandfoundation/algokit-utils/common';
2021
import { FileAccessor } from './fileAccessor';
2122
import {
2223
AvmDebuggingAssets,
@@ -541,11 +542,11 @@ export class AvmDebugSession extends DebugSession {
541542
);
542543
}
543544
} else if (v.specificState === 'scratch') {
544-
const expandedScratch: algosdk.modelsv2.AvmValue[] = [];
545+
const expandedScratch: AvmValue[] = [];
545546
for (let i = 0; i < 256; i++) {
546547
expandedScratch.push(
547548
programState.scratch.get(i) ||
548-
new algosdk.modelsv2.AvmValue({ type: 2 }),
549+
({ type: 2 }),
549550
);
550551
}
551552
if (args.filter !== 'named') {
@@ -652,15 +653,15 @@ export class AvmDebugSession extends DebugSession {
652653
v.scope instanceof PuyaScope
653654
) {
654655
const state = this.getProgramState(v.scope.frameIndex);
655-
let toExpand: algosdk.modelsv2.AvmValue | undefined;
656+
let toExpand: AvmValue | undefined;
656657

657658
if (v.scope instanceof ProgramStateScope) {
658659
if (v.scope.specificState === 'stack') {
659660
toExpand = state.stack[v.key as number];
660661
} else if (v.scope.specificState === 'scratch') {
661662
toExpand =
662663
state.scratch.get(v.key as number) ||
663-
new algosdk.modelsv2.AvmValue({ type: 2 });
664+
({ type: 2 });
664665
}
665666
} else if (v.scope instanceof PuyaScope) {
666667
const variable = state.variables.find(([name]) => name === v.key);
@@ -677,14 +678,14 @@ export class AvmDebugSession extends DebugSession {
677678
typeof v.key === 'string' &&
678679
v.key.startsWith('0x')
679680
) {
680-
let toExpand: algosdk.modelsv2.AvmKeyValue;
681+
let toExpand: AvmKeyValue;
681682
const state = this._runtime.getAppState(v.scope.appID);
682683
const keyHex = v.key.slice(2);
683684
if (v.scope.scope === 'global') {
684685
const value = state.globalState.getHex(keyHex);
685686
if (value) {
686-
toExpand = new algosdk.modelsv2.AvmKeyValue({
687-
key: algosdk.hexToBytes(keyHex),
687+
toExpand = ({
688+
key: hexToBytes(keyHex),
688689
value,
689690
});
690691
} else {
@@ -706,16 +707,16 @@ export class AvmDebugSession extends DebugSession {
706707
`key "${v.key}" not found in local state for account "${v.scope.account}"`,
707708
);
708709
}
709-
toExpand = new algosdk.modelsv2.AvmKeyValue({
710-
key: algosdk.hexToBytes(keyHex),
710+
toExpand = ({
711+
key: hexToBytes(keyHex),
711712
value,
712713
});
713714
}
714715
} else if (v.scope.scope === 'box') {
715716
const value = state.boxState.getHex(keyHex);
716717
if (value) {
717-
toExpand = new algosdk.modelsv2.AvmKeyValue({
718-
key: algosdk.hexToBytes(keyHex),
718+
toExpand = ({
719+
key: hexToBytes(keyHex),
719720
value,
720721
});
721722
} else {
@@ -857,7 +858,7 @@ export class AvmDebugSession extends DebugSession {
857858
rv = this.convertAvmValue(
858859
scopeWithFrame,
859860
state.scratch.get(index) ||
860-
new algosdk.modelsv2.AvmValue({ type: 2 }),
861+
({ type: 2 }),
861862
index,
862863
);
863864
} else {
@@ -874,8 +875,8 @@ export class AvmDebugSession extends DebugSession {
874875
const keyHex = key.slice(2);
875876
const value = state.globalState.getHex(keyHex);
876877
if (value) {
877-
const kv = new algosdk.modelsv2.AvmKeyValue({
878-
key: algosdk.hexToBytes(keyHex),
878+
const kv = ({
879+
key: hexToBytes(keyHex),
879880
value,
880881
});
881882
rv = this.convertAvmKeyValue(scope, kv);
@@ -906,8 +907,8 @@ export class AvmDebugSession extends DebugSession {
906907
const keyHex = key.slice(2);
907908
const value = accountState.getHex(keyHex);
908909
if (value) {
909-
const kv = new algosdk.modelsv2.AvmKeyValue({
910-
key: algosdk.hexToBytes(keyHex),
910+
const kv = ({
911+
key: hexToBytes(keyHex),
911912
value,
912913
});
913914
rv = this.convertAvmKeyValue(scope, kv);
@@ -922,8 +923,8 @@ export class AvmDebugSession extends DebugSession {
922923
const keyHex = key.slice(2);
923924
const value = state.boxState.getHex(keyHex);
924925
if (value) {
925-
const kv = new algosdk.modelsv2.AvmKeyValue({
926-
key: algosdk.hexToBytes(keyHex),
926+
const kv = ({
927+
key: hexToBytes(keyHex),
927928
value,
928929
});
929930
rv = this.convertAvmKeyValue(scope, kv);
@@ -1064,7 +1065,7 @@ export class AvmDebugSession extends DebugSession {
10641065

10651066
private convertAvmValue(
10661067
scope: AvmValueScope | PuyaScope,
1067-
avmValue: algosdk.modelsv2.AvmValue,
1068+
avmValue: AvmValue,
10681069
key: number | string,
10691070
overrideVariableReference?: boolean,
10701071
): DebugProtocol.Variable {
@@ -1118,7 +1119,7 @@ export class AvmDebugSession extends DebugSession {
11181119
}
11191120

11201121
private expandAvmValue(
1121-
avmValue: algosdk.modelsv2.AvmValue,
1122+
avmValue: AvmValue,
11221123
filter?: DebugProtocol.VariablesArguments['filter'],
11231124
): DebugProtocol.Variable[] {
11241125
// uint64 has no expanded variables
@@ -1133,14 +1134,14 @@ export class AvmDebugSession extends DebugSession {
11331134
values.push({
11341135
name: 'hex',
11351136
type: 'string',
1136-
value: algosdk.bytesToHex(bytes),
1137+
value: bytesToHex(bytes),
11371138
variablesReference: 0,
11381139
});
11391140

11401141
values.push({
11411142
name: 'base64',
11421143
type: 'string',
1143-
value: algosdk.bytesToBase64(bytes),
1144+
value: bytesToBase64(bytes),
11441145
variablesReference: 0,
11451146
});
11461147

@@ -1158,7 +1159,7 @@ export class AvmDebugSession extends DebugSession {
11581159
values.push({
11591160
name: 'address',
11601161
type: 'string',
1161-
value: algosdk.encodeAddress(bytes),
1162+
value: encodeAddress(bytes),
11621163
variablesReference: 0,
11631164
});
11641165
}
@@ -1187,10 +1188,10 @@ export class AvmDebugSession extends DebugSession {
11871188

11881189
private convertAvmKeyValue(
11891190
scope: AvmValueScope,
1190-
avmKeyValue: algosdk.modelsv2.AvmKeyValue,
1191+
avmKeyValue: AvmKeyValue,
11911192
): DebugProtocol.Variable {
11921193
const keyString =
1193-
'0x' + algosdk.bytesToHex(avmKeyValue.key || new Uint8Array());
1194+
'0x' + bytesToHex(avmKeyValue.key || new Uint8Array());
11941195
const value = this.convertAvmValue(
11951196
scope,
11961197
avmKeyValue.value,
@@ -1204,15 +1205,15 @@ export class AvmDebugSession extends DebugSession {
12041205

12051206
private expandAvmKeyValue(
12061207
scope: AppSpecificStateScope,
1207-
avmKeyValue: algosdk.modelsv2.AvmKeyValue,
1208+
avmKeyValue: AvmKeyValue,
12081209
filter?: DebugProtocol.VariablesArguments['filter'],
12091210
): DebugProtocol.Variable[] {
12101211
if (typeof scope.property === 'undefined') {
12111212
if (filter === 'indexed') {
12121213
return [];
12131214
}
12141215
const keyString =
1215-
'0x' + algosdk.bytesToHex(avmKeyValue.key || new Uint8Array());
1216+
'0x' + bytesToHex(avmKeyValue.key || new Uint8Array());
12161217
const keyScope = new AppSpecificStateScope({
12171218
scope: scope.scope,
12181219
appID: scope.appID,
@@ -1227,7 +1228,7 @@ export class AvmDebugSession extends DebugSession {
12271228
});
12281229
const keyVariable = this.convertAvmValue(
12291230
keyScope,
1230-
new algosdk.modelsv2.AvmValue({ type: 1, bytes: avmKeyValue.key }),
1231+
({ type: 1, bytes: avmKeyValue.key }),
12311232
'',
12321233
false,
12331234
);
@@ -1270,7 +1271,7 @@ export class AvmDebugSession extends DebugSession {
12701271
}
12711272

12721273
if (scope.property === 'key') {
1273-
const avmKey = new algosdk.modelsv2.AvmValue({
1274+
const avmKey = ({
12741275
type: 1,
12751276
bytes: avmKeyValue.key,
12761277
});
@@ -1280,11 +1281,11 @@ export class AvmDebugSession extends DebugSession {
12801281
return this.expandAvmValue(avmKeyValue.value, filter);
12811282
}
12821283

1283-
private avmValueToString(avmValue: algosdk.modelsv2.AvmValue): string {
1284+
private avmValueToString(avmValue: AvmValue): string {
12841285
if (avmValue.type === 1) {
12851286
// byte array
12861287
const bytes = avmValue.bytes || new Uint8Array();
1287-
return '0x' + algosdk.bytesToHex(bytes);
1288+
return '0x' + bytesToHex(bytes);
12881289
}
12891290
// uint64
12901291
const uint = avmValue.uint || 0;
@@ -1436,7 +1437,7 @@ function evaluateNameToScope(name: string): [AvmValueScope, number | string] {
14361437
throw new Error(`Unexpected app property: ${property}`);
14371438
}
14381439
try {
1439-
algosdk.decodeAddress(appLocalMatches[2]); // ensure valid address
1440+
decodeAddress(appLocalMatches[2]); // ensure valid address
14401441
} catch {
14411442
throw new Error(`Invalid address: ${appLocalMatches[2]}:`);
14421443
}

0 commit comments

Comments
 (0)