Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boxes for application calls in static array #573

Merged
merged 25 commits into from
Jun 30, 2022
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
f7ec88d
Initial work to define box types in appl calls
algochoi May 26, 2022
6a68e65
More changes to boxes field
algochoi May 26, 2022
3996d72
Change boxes to array types, fix transactions, and tests
algochoi May 27, 2022
67dcb16
Write box translation tests
algochoi May 31, 2022
720ba89
Add cucumber test steps
algochoi May 31, 2022
7ba6913
Revert pretty print
algochoi Jun 1, 2022
5aa0264
Fix decoding for txn boxes
algochoi Jun 1, 2022
a2addcb
Revise tests to make box args similar to app args
algochoi Jun 2, 2022
18fbd8b
Clean up some code and add a blank test case
algochoi Jun 9, 2022
ec24770
Merge branch 'develop' into box-reference
algochoi Jun 9, 2022
0c3c4e1
Fix err case
algochoi Jun 10, 2022
7b15d3a
Fix box ref catch
algochoi Jun 10, 2022
90b21c1
Delete box name field for length 0 array
algochoi Jun 10, 2022
57836dc
Revise guard
algochoi Jun 15, 2022
d88dff6
Merge branch 'develop' into box-reference
algochoi Jun 21, 2022
65551db
Merge branch 'feature/box-storage' into box-reference
algochoi Jun 22, 2022
9443518
Address some PR feedback
algochoi Jun 22, 2022
b9a4e8c
Fix box translation condition from index to id
algochoi Jun 22, 2022
645f48f
Alias own reference condition
algochoi Jun 22, 2022
52a6549
Fix unit tests and add more comments
algochoi Jun 22, 2022
6b32fd3
Add guard and change reference condition for better readability
algochoi Jun 23, 2022
4bae434
Refactor box reference condition
algochoi Jun 23, 2022
027ddbc
Export box reference type in index.ts and change SDK testing branch
algochoi Jun 23, 2022
6d38a33
Revert back test branch to older version
algochoi Jun 23, 2022
d6366dd
Remove redundant null check on array
algochoi Jun 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/boxStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { EncodedBoxReference } from './types';
import { BoxReference } from './types/transactions/base';

function translateBoxReference(
reference: BoxReference,
foreignApps: number[],
appIndex: number
): EncodedBoxReference {
const referenceId = reference.appIndex;
const referenceName = reference.name;
const isOwnReference = referenceId === 0 || referenceId === appIndex;
let index = 0;

if (foreignApps != null) {
// Foreign apps start from index 1; index 0 is its own app ID.
index = foreignApps.indexOf(referenceId) + 1;
}
// Check if the app referenced is itself after checking the foreign apps array.
// If index is zero, then the app ID was not found in the foreign apps array
// or the foreign apps array was null.
if (index === 0 && !isOwnReference) {
// Error if the app is trying to reference a foreign app that was not in
// its own foreign apps array.
throw new Error(`Box ref with appId ${referenceId} not in foreign-apps`);
}
return { i: index, n: referenceName };
}

/**
* translateBoxReferences translates an array of BoxReferences with app IDs
* into an array of EncodedBoxReferences with foreign indices.
*/
export function translateBoxReferences(
references: BoxReference[],
foreignApps: number[],
appIndex: number
): EncodedBoxReference[] {
if (references == null || !Array.isArray(references)) return [];
return references.map((bx) =>
translateBoxReference(bx, foreignApps, appIndex)
);
}
7 changes: 7 additions & 0 deletions src/composer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
isTransactionWithSigner,
} from './signer';
import {
BoxReference,
OnApplicationComplete,
SuggestedParams,
} from './types/transactions/base';
Expand Down Expand Up @@ -201,6 +202,7 @@ export class AtomicTransactionComposer {
numLocalInts,
numLocalByteSlices,
extraPages,
boxes,
note,
lease,
rekeyTo,
Expand Down Expand Up @@ -232,6 +234,8 @@ export class AtomicTransactionComposer {
numLocalByteSlices?: number;
/** The number of extra pages to allocate for the application's programs. Only set this if this is an application creation call. If omitted, defaults to 0. */
extraPages?: number;
/** The box references for this application call */
boxes?: BoxReference[];
/** The note value for this application call */
note?: Uint8Array;
/** The lease value for this application call */
Expand Down Expand Up @@ -317,6 +321,8 @@ export class AtomicTransactionComposer {
const refArgTypes: ABIReferenceType[] = [];
const refArgValues: ABIValue[] = [];
const refArgIndexToBasicArgIndex: Map<number, number> = new Map();
// TODO: Box encoding for ABI
const boxReferences: BoxReference[] = !boxes ? [] : boxes;

for (let i = 0; i < methodArgs.length; i++) {
let argType = method.args[i].type;
Expand Down Expand Up @@ -437,6 +443,7 @@ export class AtomicTransactionComposer {
accounts: foreignAccounts,
foreignApps,
foreignAssets,
boxes: boxReferences,
onComplete:
onComplete == null ? OnApplicationComplete.NoOpOC : onComplete,
approvalProgram,
Expand Down
37 changes: 37 additions & 0 deletions src/makeTxn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ export function makeAssetTransferTxnWithSuggestedParamsFromObject(
* @param accounts - Array of Address strings, any additional accounts to supply to the application
* @param foreignApps - Array of int, any other apps used by the application, identified by index
* @param foreignAssets - Array of int, any assets used by the application, identified by index
* @param boxes - Array of BoxReference, app ID and name of box to be accessed
* @param note - Arbitrary data for sender to store
* @param lease - Lease a transaction
* @param rekeyTo - String representation of the Algorand address that will be used to authorize all future transactions
Expand All @@ -1119,6 +1120,7 @@ export function makeApplicationCreateTxn(
accounts?: AppCreateTxn['appAccounts'],
foreignApps?: AppCreateTxn['appForeignApps'],
foreignAssets?: AppCreateTxn['appForeignAssets'],
boxes?: AppCreateTxn['boxes'],
note?: AppCreateTxn['note'],
lease?: AppCreateTxn['lease'],
rekeyTo?: AppCreateTxn['reKeyTo'],
Expand All @@ -1140,6 +1142,7 @@ export function makeApplicationCreateTxn(
appAccounts: accounts,
appForeignApps: foreignApps,
appForeignAssets: foreignAssets,
boxes,
note,
lease,
reKeyTo: rekeyTo,
Expand Down Expand Up @@ -1181,6 +1184,7 @@ export function makeApplicationCreateTxnFromObject(
| 'accounts'
| 'foreignApps'
| 'foreignAssets'
| 'boxes'
| 'note'
| 'lease'
| 'rekeyTo'
Expand All @@ -1202,6 +1206,7 @@ export function makeApplicationCreateTxnFromObject(
o.accounts,
o.foreignApps,
o.foreignAssets,
o.boxes,
o.note,
o.lease,
o.rekeyTo,
Expand All @@ -1227,6 +1232,7 @@ export function makeApplicationCreateTxnFromObject(
* @param accounts - Array of Address strings, any additional accounts to supply to the application
* @param foreignApps - Array of int, any other apps used by the application, identified by index
* @param foreignAssets - Array of int, any assets used by the application, identified by index
* @param boxes - Array of BoxReference, app ID and name of box to be accessed
* @param note - Arbitrary data for sender to store
* @param lease - Lease a transaction
* @param rekeyTo - String representation of the Algorand address that will be used to authorize all future transactions
Expand All @@ -1241,6 +1247,7 @@ export function makeApplicationUpdateTxn(
accounts?: AppUpdateTxn['appAccounts'],
foreignApps?: AppUpdateTxn['appForeignApps'],
foreignAssets?: AppUpdateTxn['appForeignAssets'],
boxes?: AppUpdateTxn['boxes'],
note?: AppUpdateTxn['note'],
lease?: AppUpdateTxn['lease'],
rekeyTo?: AppUpdateTxn['reKeyTo']
Expand All @@ -1257,6 +1264,7 @@ export function makeApplicationUpdateTxn(
appAccounts: accounts,
appForeignApps: foreignApps,
appForeignAssets: foreignAssets,
boxes,
note,
lease,
reKeyTo: rekeyTo,
Expand Down Expand Up @@ -1288,6 +1296,7 @@ export function makeApplicationUpdateTxnFromObject(
| 'accounts'
| 'foreignApps'
| 'foreignAssets'
| 'boxes'
| 'note'
| 'lease'
| 'rekeyTo'
Expand All @@ -1304,6 +1313,7 @@ export function makeApplicationUpdateTxnFromObject(
o.accounts,
o.foreignApps,
o.foreignAssets,
o.boxes,
o.note,
o.lease,
o.rekeyTo
Expand All @@ -1326,6 +1336,7 @@ export function makeApplicationUpdateTxnFromObject(
* @param accounts - Array of Address strings, any additional accounts to supply to the application
* @param foreignApps - Array of int, any other apps used by the application, identified by index
* @param foreignAssets - Array of int, any assets used by the application, identified by index
* @param boxes - Array of BoxReference, app ID and name of box to be accessed
* @param note - Arbitrary data for sender to store
* @param lease - Lease a transaction
* @param rekeyTo - String representation of the Algorand address that will be used to authorize all future transactions
Expand All @@ -1338,6 +1349,7 @@ export function makeApplicationDeleteTxn(
accounts?: AppDeleteTxn['appAccounts'],
foreignApps?: AppDeleteTxn['appForeignApps'],
foreignAssets?: AppDeleteTxn['appForeignAssets'],
boxes?: AppDeleteTxn['boxes'],
note?: AppDeleteTxn['note'],
lease?: AppDeleteTxn['lease'],
rekeyTo?: AppDeleteTxn['reKeyTo']
Expand All @@ -1352,6 +1364,7 @@ export function makeApplicationDeleteTxn(
appAccounts: accounts,
appForeignApps: foreignApps,
appForeignAssets: foreignAssets,
boxes,
note,
lease,
reKeyTo: rekeyTo,
Expand Down Expand Up @@ -1379,6 +1392,7 @@ export function makeApplicationDeleteTxnFromObject(
| 'accounts'
| 'foreignApps'
| 'foreignAssets'
| 'boxes'
| 'note'
| 'lease'
| 'rekeyTo'
Expand All @@ -1393,6 +1407,7 @@ export function makeApplicationDeleteTxnFromObject(
o.accounts,
o.foreignApps,
o.foreignAssets,
o.boxes,
o.note,
o.lease,
o.rekeyTo
Expand All @@ -1415,6 +1430,7 @@ export function makeApplicationDeleteTxnFromObject(
* @param accounts - Array of Address strings, any additional accounts to supply to the application
* @param foreignApps - Array of int, any other apps used by the application, identified by index
* @param foreignAssets - Array of int, any assets used by the application, identified by index
* @param boxes - Array of BoxReference, app ID and name of box to be accessed
* @param note - Arbitrary data for sender to store
* @param lease - Lease a transaction
* @param rekeyTo - String representation of the Algorand address that will be used to authorize all future transactions
Expand All @@ -1427,6 +1443,7 @@ export function makeApplicationOptInTxn(
accounts?: AppOptInTxn['appAccounts'],
foreignApps?: AppOptInTxn['appForeignApps'],
foreignAssets?: AppOptInTxn['appForeignAssets'],
boxes?: AppOptInTxn['boxes'],
note?: AppOptInTxn['note'],
lease?: AppOptInTxn['lease'],
rekeyTo?: AppOptInTxn['reKeyTo']
Expand All @@ -1441,6 +1458,7 @@ export function makeApplicationOptInTxn(
appAccounts: accounts,
appForeignApps: foreignApps,
appForeignAssets: foreignAssets,
boxes,
note,
lease,
reKeyTo: rekeyTo,
Expand Down Expand Up @@ -1468,6 +1486,7 @@ export function makeApplicationOptInTxnFromObject(
| 'accounts'
| 'foreignApps'
| 'foreignAssets'
| 'boxes'
| 'note'
| 'lease'
| 'rekeyTo'
Expand All @@ -1482,6 +1501,7 @@ export function makeApplicationOptInTxnFromObject(
o.accounts,
o.foreignApps,
o.foreignAssets,
o.boxes,
o.note,
o.lease,
o.rekeyTo
Expand All @@ -1504,6 +1524,7 @@ export function makeApplicationOptInTxnFromObject(
* @param accounts - Array of Address strings, any additional accounts to supply to the application
* @param foreignApps - Array of int, any other apps used by the application, identified by index
* @param foreignAssets - Array of int, any assets used by the application, identified by index
* @param boxes - Array of BoxReference, app ID and name of box to be accessed
* @param note - Arbitrary data for sender to store
* @param lease - Lease a transaction
* @param rekeyTo - String representation of the Algorand address that will be used to authorize all future transactions
Expand All @@ -1516,6 +1537,7 @@ export function makeApplicationCloseOutTxn(
accounts?: AppCloseOutTxn['appAccounts'],
foreignApps?: AppCloseOutTxn['appForeignApps'],
foreignAssets?: AppCloseOutTxn['appForeignAssets'],
boxes?: AppCloseOutTxn['boxes'],
note?: AppCloseOutTxn['note'],
lease?: AppCloseOutTxn['lease'],
rekeyTo?: AppCloseOutTxn['reKeyTo']
Expand All @@ -1530,6 +1552,7 @@ export function makeApplicationCloseOutTxn(
appAccounts: accounts,
appForeignApps: foreignApps,
appForeignAssets: foreignAssets,
boxes,
note,
lease,
reKeyTo: rekeyTo,
Expand Down Expand Up @@ -1557,6 +1580,7 @@ export function makeApplicationCloseOutTxnFromObject(
| 'accounts'
| 'foreignApps'
| 'foreignAssets'
| 'boxes'
| 'note'
| 'lease'
| 'rekeyTo'
Expand All @@ -1571,6 +1595,7 @@ export function makeApplicationCloseOutTxnFromObject(
o.accounts,
o.foreignApps,
o.foreignAssets,
o.boxes,
o.note,
o.lease,
o.rekeyTo
Expand All @@ -1593,6 +1618,7 @@ export function makeApplicationCloseOutTxnFromObject(
* @param accounts - Array of Address strings, any additional accounts to supply to the application
* @param foreignApps - Array of int, any other apps used by the application, identified by index
* @param foreignAssets - Array of int, any assets used by the application, identified by index
* @param boxes - Array of BoxReference, app ID and name of box to be accessed
* @param note - Arbitrary data for sender to store
* @param lease - Lease a transaction
* @param rekeyTo - String representation of the Algorand address that will be used to authorize all future transactions
Expand All @@ -1605,6 +1631,7 @@ export function makeApplicationClearStateTxn(
accounts?: AppClearStateTxn['appAccounts'],
foreignApps?: AppClearStateTxn['appForeignApps'],
foreignAssets?: AppClearStateTxn['appForeignAssets'],
boxes?: AppClearStateTxn['boxes'],
note?: AppClearStateTxn['note'],
lease?: AppClearStateTxn['lease'],
rekeyTo?: AppClearStateTxn['reKeyTo']
Expand All @@ -1619,6 +1646,7 @@ export function makeApplicationClearStateTxn(
appAccounts: accounts,
appForeignApps: foreignApps,
appForeignAssets: foreignAssets,
boxes,
note,
lease,
reKeyTo: rekeyTo,
Expand Down Expand Up @@ -1646,6 +1674,7 @@ export function makeApplicationClearStateTxnFromObject(
| 'accounts'
| 'foreignApps'
| 'foreignAssets'
| 'boxes'
| 'note'
| 'lease'
| 'rekeyTo'
Expand All @@ -1660,6 +1689,7 @@ export function makeApplicationClearStateTxnFromObject(
o.accounts,
o.foreignApps,
o.foreignAssets,
o.boxes,
o.note,
o.lease,
o.rekeyTo
Expand All @@ -1682,6 +1712,7 @@ export function makeApplicationClearStateTxnFromObject(
* @param accounts - Array of Address strings, any additional accounts to supply to the application
* @param foreignApps - Array of int, any other apps used by the application, identified by index
* @param foreignAssets - Array of int, any assets used by the application, identified by index
* @param boxes - Array of BoxReference, app ID and name of box to be accessed
* @param note - Arbitrary data for sender to store
* @param lease - Lease a transaction
* @param rekeyTo - String representation of the Algorand address that will be used to authorize all future transactions
Expand All @@ -1694,6 +1725,7 @@ export function makeApplicationNoOpTxn(
accounts?: AppNoOpTxn['appAccounts'],
foreignApps?: AppNoOpTxn['appForeignApps'],
foreignAssets?: AppNoOpTxn['appForeignAssets'],
boxes?: AppNoOpTxn['boxes'],
note?: AppNoOpTxn['note'],
lease?: AppNoOpTxn['lease'],
rekeyTo?: AppNoOpTxn['reKeyTo']
Expand All @@ -1708,6 +1740,7 @@ export function makeApplicationNoOpTxn(
appAccounts: accounts,
appForeignApps: foreignApps,
appForeignAssets: foreignAssets,
boxes,
note,
lease,
reKeyTo: rekeyTo,
Expand Down Expand Up @@ -1735,6 +1768,7 @@ export function makeApplicationNoOpTxnFromObject(
| 'accounts'
| 'foreignApps'
| 'foreignAssets'
| 'boxes'
| 'note'
| 'lease'
| 'rekeyTo'
Expand All @@ -1749,6 +1783,7 @@ export function makeApplicationNoOpTxnFromObject(
o.accounts,
o.foreignApps,
o.foreignAssets,
o.boxes,
o.note,
o.lease,
o.rekeyTo
Expand Down Expand Up @@ -1781,6 +1816,7 @@ export function makeApplicationCallTxnFromObject(
| 'accounts'
| 'foreignApps'
| 'foreignAssets'
| 'boxes'
| 'note'
| 'lease'
| 'rekeyTo'
Expand Down Expand Up @@ -1825,6 +1861,7 @@ export function makeApplicationCallTxnFromObject(
appAccounts: options.accounts,
appForeignApps: options.foreignApps,
appForeignAssets: options.foreignAssets,
boxes: options.boxes,
note: options.note,
lease: options.lease,
reKeyTo: options.rekeyTo,
Expand Down
Loading