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

Added error checks in examples #800

Merged
merged 19 commits into from
Oct 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 4 additions & 4 deletions examples/asa/scripts/0-gold-asa.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const crypto = require("crypto");

const { balanceOf } = require("@algo-builder/algob");
const { mkParam } = require("./transfer/common");
const { mkParam, tryExecuteTx } = require("./transfer/common");
/*
Create "gold" Algorand Standard Asset (ASA).
Accounts are loaded from config.
Expand All @@ -24,9 +24,9 @@ async function run(runtimeEnv, deployer) {
// let note = new TextEncoder().encode(message); // note must be Uint8Array

const promises = [
deployer.executeTx(mkParam(masterAccount, goldOwner.addr, 5e6, { note: message })),
deployer.executeTx(mkParam(masterAccount, john.addr, 5e6, { note: message })),
deployer.executeTx(mkParam(masterAccount, bob.addr, 1e6, { note: message })),
tryExecuteTx(deployer, mkParam(masterAccount, goldOwner.addr, 5e6, { note: message })),
tryExecuteTx(deployer, mkParam(masterAccount, john.addr, 5e6, { note: message })),
tryExecuteTx(deployer, mkParam(masterAccount, bob.addr, 1e6, { note: message })),
];
await Promise.all(promises);

Expand Down
8 changes: 5 additions & 3 deletions examples/asa/scripts/1-tesla-asa.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { balanceOf } = require("@algo-builder/algob");
const { mkParam } = require("./transfer/common");
const { mkParam, tryExecuteTx } = require("./transfer/common");

/*
Create "tesla" Algorand Standard Asset (ASA)
Expand All @@ -15,10 +15,12 @@ async function run(runtimeEnv, deployer) {
const john = deployer.accountsByName.get("john");

// activate elon account
await deployer.executeTx(
await tryExecuteTx(
deployer,
mkParam(masterAccount, elon.addr, 40e6, { note: "funding account" })
);
await deployer.executeTx(
await tryExecuteTx(
deployer,
mkParam(masterAccount, john.addr, 40e6, { note: "funding account" })
);

Expand Down
48 changes: 28 additions & 20 deletions examples/asa/scripts/2-gold-asc.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
const { balanceOf, signTransactions } = require("@algo-builder/algob");
const { types, getSuggestedParams } = require("@algo-builder/web");
const { mkParam } = require("./transfer/common");
const { mkParam, tryExecuteTx } = require("./transfer/common");
const { makeAssetTransferTxnWithSuggestedParams } = require("algosdk");

async function run(runtimeEnv, deployer) {
const masterAccount = deployer.accountsByName.get("master-account");
const goldOwner = deployer.accountsByName.get("alice");

await deployer.executeTx(
await tryExecuteTx(
deployer,
mkParam(masterAccount, goldOwner.addr, 200e6, { note: "funding account" })
);

// save Smart Signature by name & fund the account
const ascInfoContract = await deployer.mkContractLsig(
"Gold_C_Lsig",
"2-gold-contract-asc.teal",
{}
);
const ascInfoContract = await deployer
.mkContractLsig("Gold_C_Lsig", "2-gold-contract-asc.teal", {})
.catch((error) => {
throw error;
});
console.log(ascInfoContract);
await deployer.fundLsig("Gold_C_Lsig", { funder: goldOwner, fundingMicroAlgo: 1e6 }, {}); // funding with 1 Algo
// funding with 1 Algo
await deployer
.fundLsig("Gold_C_Lsig", { funder: goldOwner, fundingMicroAlgo: 1e6 }, {})
.catch((error) => {
throw error;
});

const ascInfoAlgoDelegated = await deployer.mkDelegatedLsig(
"Gold_D_Lsig",
"3-gold-delegated-asc.teal",
goldOwner
);
const ascInfoGoldDelegated = await deployer.mkDelegatedLsig(
"Gold_d_asa_lsig",
"4-gold-asa.teal",
goldOwner
);
const ascInfoAlgoDelegated = await deployer
.mkDelegatedLsig("Gold_D_Lsig", "3-gold-delegated-asc.teal", goldOwner)
.catch((error) => {
throw error;
});
const ascInfoGoldDelegated = await deployer
.mkDelegatedLsig("Gold_d_asa_lsig", "4-gold-asa.teal", goldOwner)
.catch((error) => {
throw error;
});

console.log(ascInfoAlgoDelegated);
console.log(ascInfoGoldDelegated);
Expand All @@ -38,7 +44,9 @@ async function run(runtimeEnv, deployer) {
const lsig = await deployer.getLsig("Gold_C_Lsig");
const goldAsset = deployer.asa.get("gold");
const goldAssetID = goldAsset.assetIndex;
await deployer.optInLsigToASA(goldAssetID, lsig, { totalFee: 1000 });
await deployer.optInLsigToASA(goldAssetID, lsig, { totalFee: 1000 }).catch((error) => {
throw error;
});
console.log("Balance: ", await balanceOf(deployer, lsig.address(), goldAssetID));

console.log(`Funding contract ${lsig.address()} with ASA gold`);
Expand All @@ -57,7 +65,7 @@ async function run(runtimeEnv, deployer) {
fromAccount: goldOwner,
};

await deployer.executeTx([{ transaction: tx, sign: sign }]);
await tryExecuteTx(deployer, [{ transaction: tx, sign: sign }]);
await balanceOf(deployer, lsig.address(), goldAssetID);

// To get raw signed transaction you may use `signTransactions` function
Expand Down
56 changes: 33 additions & 23 deletions examples/asa/scripts/3-contract-owned-asa.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,54 @@
* - Create contract account (with app_id embedded/passed as a template param)
* - Deploy ASA using both contracts
*/
const { mkParam } = require("./transfer/common");
const { mkParam, tryExecuteTx } = require("./transfer/common");
const { types } = require("@algo-builder/web");

async function run(runtimeEnv, deployer) {
const masterAccount = deployer.accountsByName.get("master-account");
const alice = deployer.accountsByName.get("alice");

await deployer.executeTx(
await tryExecuteTx(
deployer,
mkParam(masterAccount, alice.addr, 200e6, { note: "funding account" })
);

// Create Application
// Note: An Account can have maximum of 10 Applications.
const appInfo = await deployer.deployApp(
alice,
{
appName: "StatefulASA_App",
metaType: types.MetaType.FILE,
approvalProgramFilename: "5-contract-asa-stateful.py", // approval program
clearProgramFilename: "5-clear.py", // clear program
localInts: 1,
localBytes: 1,
globalInts: 1,
globalBytes: 1,
},
{}
);
const appInfo = await deployer
.deployApp(
alice,
{
appName: "StatefulASA_App",
metaType: types.MetaType.FILE,
approvalProgramFilename: "5-contract-asa-stateful.py", // approval program
clearProgramFilename: "5-clear.py", // clear program
localInts: 1,
localBytes: 1,
globalInts: 1,
globalBytes: 1,
},
{}
)
.catch((error) => {
throw error;
});

console.log(appInfo);

// Get Statless Account Address
await deployer.mkContractLsig("StateLessASALsig", "5-contract-asa-stateless.py", {
APP_ID: appInfo.appID,
});
const statelessAccount = deployer.getLsig("StateLessASALsig");
await deployer
.mkContractLsig("StatelessASALsig", "5-contract-asa-stateless.py", {
APP_ID: appInfo.appID,
})
.catch((error) => {
throw error;
});
const statelessAccount = deployer.getLsig("StatelessASALsig");
console.log("stateless Account Address:", statelessAccount.address());

await deployer.executeTx(
await tryExecuteTx(
deployer,
mkParam(masterAccount, statelessAccount.address(), 200e6, { note: "funding account" })
);

Expand Down Expand Up @@ -77,12 +87,12 @@ async function run(runtimeEnv, deployer) {
},
];

await deployer.executeTx(txGroup);
await tryExecuteTx(deployer, txGroup);

// This should fail because maximum number of asa creation limit is set to 1
try {
txGroup[1].asaName = "alu";
await deployer.executeTx(txGroup);
await tryExecuteTx(deployer, txGroup);
} catch (e) {
console.log(e.response?.error);
}
Expand Down
4 changes: 3 additions & 1 deletion examples/asa/scripts/transfer/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ const { types } = require("@algo-builder/web");

exports.tryExecuteTx = async function (deployer, txnParams) {
try {
await deployer.executeTx(txnParams);
const txnParameters = Array.isArray(txnParams) ? txnParams : [txnParams];
return await deployer.executeTx(txnParameters);
} catch (e) {
console.error("Transaction Failed", e.response ? e.response.error : e);
throw e;
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*/
const { convert } = require("@algo-builder/algob");
const { types } = require("@algo-builder/web");
const { mkParam } = require("../common");
const { mkParam, tryExecuteTx } = require("../common");

async function run(runtimeEnv, deployer) {
const masterAccount = deployer.accountsByName.get("master-account");
const alice = deployer.accountsByName.get("alice");
const bob = deployer.accountsByName.get("bob");

await deployer.executeTx(mkParam(masterAccount, alice.addr, 5e6, { note: "Funding" }));
await tryExecuteTx(deployer, mkParam(masterAccount, alice.addr, 5e6, { note: "Funding" }));

// Get AppInfo from checkpoint.
const appInfo = deployer.getApp("StatefulASA_App");
Expand All @@ -31,7 +31,7 @@ async function run(runtimeEnv, deployer) {
},
];

await deployer.executeTx(tx);
await tryExecuteTx(deployer, tx);
}

module.exports = { default: run };
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
*/
const { types } = require("@algo-builder/web");
const { balanceOf } = require("@algo-builder/algob");
const { executeTx, mkParam } = require("../common");
const { mkParam, tryExecuteTx } = require("../common");

async function run(runtimeEnv, deployer) {
const masterAccount = deployer.accountsByName.get("master-account");
const alice = deployer.accountsByName.get("alice");
const bob = deployer.accountsByName.get("bob");

await deployer.executeTx(mkParam(masterAccount, alice.addr, 5e6, { note: "Funding" }));
await tryExecuteTx(deployer, mkParam(masterAccount, alice.addr, 5e6, { note: "Funding" }));

// Get AppInfo and AssetID from checkpoints.
const appInfo = deployer.getApp("StatefulASA_App");
const lsig = deployer.getLsig("StateLessASALsig");
const lsig = deployer.getLsig("StatelessASALsig");

/* Transfer ASA 'gold' from contract account to user account */
const assetID = deployer.asa.get("platinum").assetIndex;
Expand Down Expand Up @@ -49,21 +49,22 @@ async function run(runtimeEnv, deployer) {
},
];

await deployer.executeTx(txGroup);
await tryExecuteTx(txGroup);

// print assetHolding of alice
console.log("Alice assetHolding balance: ", await balanceOf(deployer, alice.addr, assetID));

try {
// tx FAIL: trying to receive asset from another account
txGroup[0].fromAccount = bob;
await deployer.executeTx(txGroup);
await tryExecuteTx(txGroup);
} catch (e) {
console.error(e);
}

try {
// tx FAIL: trying to send asset directly without calling stateful smart contract
await deployer.executeTx([
await tryExecuteTx(deployer, [
{
type: types.TransactionType.TransferAsset,
sign: types.SignType.LogicSignature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
*/
const { types } = require("@algo-builder/web");
const { balanceOf } = require("@algo-builder/algob");
const { executeTx, mkParam } = require("../common");
const { tryExecuteTx, mkParam } = require("../common");

async function run(runtimeEnv, deployer) {
const masterAccount = deployer.accountsByName.get("master-account");
const alice = deployer.accountsByName.get("alice");
const bob = deployer.accountsByName.get("bob");

await deployer.executeTx(mkParam(masterAccount, bob.addr, 5e6, { note: "Funding" }));
await tryExecuteTx(deployer, mkParam(masterAccount, bob.addr, 5e6, { note: "Funding" }));
// Get AppInfo and AssetID from checkpoints.
const appInfo = deployer.getApp("StatefulASA_App");
const lsig = deployer.getLsig("StateLessASALsig");
const lsig = deployer.getLsig("StatelessASALsig");

/* Transfer ASA 'gold' from contract account to user account */
const assetID = deployer.asa.get("platinum").assetIndex;
Expand Down Expand Up @@ -44,14 +44,14 @@ async function run(runtimeEnv, deployer) {
},
];

await deployer.executeTx(txGroup);
await tryExecuteTx(deployer, txGroup);
// print assetHolding of alice
console.log("Alice assetHolding balance: ", await balanceOf(deployer, alice.addr, assetID));

try {
// tx FAIL: trying to receive asset from initial owner account
txGroup[0].fromAccount = alice;
await deployer.executeTx(txGroup);
await tryExecuteTx(deployer, txGroup);
} catch (e) {
console.error(e);
}
Expand Down
12 changes: 10 additions & 2 deletions examples/asa/scripts/transfer/gold-contract-sc.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ async function run(runtimeEnv, deployer) {
// Transaction FAIL - Gets rejected by logic - As according to .teal logic, amount should be <= 100
const invalidParams = Object.assign({}, algoTxParam);
invalidParams.amountMicroAlgos = 200;
await tryExecuteTx(deployer, invalidParams);
try {
await tryExecuteTx(deployer, invalidParams);
} catch (e) {
console.error(e);
}

/* Transfer ASA 'gold' from contract account to user account */
const assetID = deployer.asa.get("gold").assetIndex;
Expand All @@ -58,7 +62,11 @@ async function run(runtimeEnv, deployer) {
// Transaction FAIL - Gets rejected by logic - As according to .teal logic, amount should be <= 100
const invalidTxParams = Object.assign({}, assetTxParam);
invalidTxParams.amount = 500;
await tryExecuteTx(deployer, invalidTxParams);
try {
await tryExecuteTx(deployer, invalidTxParams);
} catch (e) {
console.error(e);
}
}

module.exports = { default: run };
Loading