Skip to content

Commit 3bab401

Browse files
committed
refactored program-manager.ts
1 parent 79f8825 commit 3bab401

File tree

1 file changed

+97
-124
lines changed

1 file changed

+97
-124
lines changed

sdk/src/program-manager.ts

Lines changed: 97 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,23 @@ class ProgramManager {
9898
this.recordProvider = recordProvider;
9999
}
100100

101+
/**
102+
* Check if the fee is sufficient to pay for the transaction
103+
*/
104+
async checkFee(address: string, feeAmount: bigint) {
105+
const account_balance =
106+
await this.networkClient?.getProgramMappingValue(
107+
"credits.aleo",
108+
"account",
109+
address,
110+
);
111+
if (feeAmount > BigInt(account_balance)) {
112+
throw Error(
113+
"Public balance is insufficient to execute the transacation.",
114+
);
115+
}
116+
}
117+
101118
/**
102119
* Set the account to use for transaction submission to the Aleo network
103120
*
@@ -327,23 +344,17 @@ class ProgramManager {
327344
let feeAddress;
328345

329346
if (typeof privateKey !== "undefined") {
330-
feeAddress = Address.fromPrivateKey(privateKey);
331-
} else {
347+
feeAddress = Address.from_private_key(privateKey);
348+
} else if (this.account !== undefined) {
332349
feeAddress = this.account?.address();
333-
}
334-
335-
const account_balance =
336-
await this.networkClient?.getProgramMappingValue(
337-
"credits.aleo",
338-
"account",
339-
feeAddress,
350+
} else {
351+
throw Error(
352+
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
340353
);
341-
342-
// Check to make sure the public balance is sufficient to cover the execution fee.
343-
if (tx.feeAmount() > account_balance) {
344-
throw "Public balance is insufficient to execute the transacation.";
345354
}
346355

356+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
357+
347358
return await this.networkClient.submitTransaction(tx);
348359
}
349360

@@ -547,30 +558,16 @@ class ProgramManager {
547558
async execute(options: ExecuteOptions): Promise<string> {
548559
const tx = <Transaction>await this.buildExecutionTransaction(options);
549560

550-
let feeAddress = this.account?.address();
551-
const account_balance =
552-
await this.networkClient?.getProgramMappingValue(
553-
"credits.aleo",
554-
"account",
555-
feeAddress,
561+
let feeAddress;
562+
if (this.account !== undefined) {
563+
feeAddress = this.account?.address();
564+
} else {
565+
throw Error(
566+
"No account set in the ProgramManager. Please set an account before executing a transaction.",
556567
);
557-
558-
// Check to make sure the public balance is sufficient to cover the execution fee.
559-
if (tx.feeAmount() > account_balance) {
560-
throw "Public balance is insufficient to execute the transacation.";
561568
}
562569

563-
// Check to make sure the public balance is sufficient to cover the execution fee.
564-
if (
565-
tx.feeAmount() >
566-
this.networkClient?.getProgramMappingValue(
567-
"credits.aleo",
568-
"account",
569-
this.account?.address(),
570-
)
571-
) {
572-
throw "Public balance is insufficient to execute the transacation.";
573-
}
570+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
574571

575572
return await this.networkClient.submitTransaction(tx);
576573
}
@@ -789,23 +786,17 @@ class ProgramManager {
789786
let feeAddress;
790787

791788
if (typeof privateKey !== "undefined") {
792-
feeAddress = Address.fromPrivateKey(privateKey);
793-
} else {
789+
feeAddress = Address.from_private_key(privateKey);
790+
} else if (this.account !== undefined) {
794791
feeAddress = this.account?.address();
795-
}
796-
797-
const account_balance =
798-
await this.networkClient?.getProgramMappingValue(
799-
"credits.aleo",
800-
"account",
801-
feeAddress,
792+
} else {
793+
throw Error(
794+
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
802795
);
803-
804-
// Check to make sure the public balance is sufficient to cover the execution fee.
805-
if (tx.feeAmount() > account_balance) {
806-
throw "Public balance is insufficient to execute the transacation.";
807796
}
808797

798+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
799+
809800
return await this.networkClient.submitTransaction(tx);
810801
}
811802

@@ -894,23 +885,17 @@ class ProgramManager {
894885
let feeAddress;
895886

896887
if (typeof privateKey !== "undefined") {
897-
feeAddress = Address.fromPrivateKey(privateKey);
898-
} else {
888+
feeAddress = Address.from_private_key(privateKey);
889+
} else if (this.account !== undefined) {
899890
feeAddress = this.account?.address();
900-
}
901-
902-
const account_balance =
903-
await this.networkClient?.getProgramMappingValue(
904-
"credits.aleo",
905-
"account",
906-
feeAddress,
891+
} else {
892+
throw Error(
893+
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
907894
);
908-
909-
// Check to make sure the public balance is sufficient to cover the execution fee.
910-
if (tx.feeAmount() > account_balance) {
911-
throw "Public balance is insufficient to execute the transacation.";
912895
}
913896

897+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
898+
914899
return await this.networkClient.submitTransaction(tx);
915900
}
916901

@@ -1273,23 +1258,17 @@ class ProgramManager {
12731258
let feeAddress;
12741259

12751260
if (typeof privateKey !== "undefined") {
1276-
feeAddress = Address.fromPrivateKey(privateKey);
1277-
} else {
1261+
feeAddress = Address.from_private_key(privateKey);
1262+
} else if (this.account !== undefined) {
12781263
feeAddress = this.account?.address();
1279-
}
1280-
1281-
const account_balance =
1282-
await this.networkClient?.getProgramMappingValue(
1283-
"credits.aleo",
1284-
"account",
1285-
feeAddress,
1264+
} else {
1265+
throw Error(
1266+
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
12861267
);
1287-
1288-
// Check to make sure the public balance is sufficient to cover the execution fee.
1289-
if (tx.feeAmount() > account_balance) {
1290-
throw "Public balance is insufficient to execute the transacation.";
12911268
}
12921269

1270+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
1271+
12931272
return await this.networkClient.submitTransaction(tx);
12941273
}
12951274

@@ -1410,19 +1389,17 @@ class ProgramManager {
14101389
)
14111390
);
14121391

1413-
let feeAddress = this.account?.address();
1414-
const account_balance =
1415-
await this.networkClient?.getProgramMappingValue(
1416-
"credits.aleo",
1417-
"account",
1418-
feeAddress,
1392+
let feeAddress;
1393+
if (this.account !== undefined) {
1394+
feeAddress = this.account?.address();
1395+
} else {
1396+
throw Error(
1397+
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
14191398
);
1420-
1421-
// Check to make sure the public balance is sufficient to cover the execution fee.
1422-
if (tx.feeAmount() > account_balance) {
1423-
throw "Public balance is insufficient to execute the transacation.";
14241399
}
14251400

1401+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
1402+
14261403
return await this.networkClient.submitTransaction(tx);
14271404
}
14281405

@@ -1552,19 +1529,18 @@ class ProgramManager {
15521529
)
15531530
);
15541531

1555-
let feeAddress = this.account?.address();
1556-
const account_balance =
1557-
await this.networkClient?.getProgramMappingValue(
1558-
"credits.aleo",
1559-
"account",
1560-
feeAddress,
1561-
);
1532+
let feeAddress;
15621533

1563-
// Check to make sure the public balance is sufficient to cover the execution fee.
1564-
if (tx.feeAmount() > account_balance) {
1565-
throw "Public balance is insufficient to execute the transacation.";
1534+
if (this.account !== undefined) {
1535+
feeAddress = this.account?.address();
1536+
} else {
1537+
throw Error(
1538+
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
1539+
);
15661540
}
15671541

1542+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
1543+
15681544
return await this.networkClient.submitTransaction(tx);
15691545
}
15701546

@@ -1679,19 +1655,18 @@ class ProgramManager {
16791655
)
16801656
);
16811657

1682-
let feeAddress = this.account?.address();
1683-
const account_balance =
1684-
await this.networkClient?.getProgramMappingValue(
1685-
"credits.aleo",
1686-
"account",
1687-
feeAddress,
1688-
);
1658+
let feeAddress;
16891659

1690-
// Check to make sure the public balance is sufficient to cover the execution fee.
1691-
if (tx.feeAmount() > account_balance) {
1692-
throw "Public balance is insufficient to execute the transacation.";
1660+
if (this.account !== undefined) {
1661+
feeAddress = this.account?.address();
1662+
} else {
1663+
throw Error(
1664+
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
1665+
);
16931666
}
16941667

1668+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
1669+
16951670
return await this.networkClient.submitTransaction(tx);
16961671
}
16971672

@@ -1797,19 +1772,18 @@ class ProgramManager {
17971772
)
17981773
);
17991774

1800-
let feeAddress = this.account?.address();
1801-
const account_balance =
1802-
await this.networkClient?.getProgramMappingValue(
1803-
"credits.aleo",
1804-
"account",
1805-
feeAddress,
1806-
);
1775+
let feeAddress;
18071776

1808-
// Check to make sure the public balance is sufficient to cover the execution fee.
1809-
if (tx.feeAmount() > account_balance) {
1810-
throw "Public balance is insufficient to execute the transacation.";
1777+
if (this.account !== undefined) {
1778+
feeAddress = this.account?.address();
1779+
} else {
1780+
throw Error(
1781+
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
1782+
);
18111783
}
18121784

1785+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
1786+
18131787
return await this.networkClient.submitTransaction(tx);
18141788
}
18151789

@@ -1879,7 +1853,7 @@ class ProgramManager {
18791853
...additionalOptions,
18801854
};
18811855

1882-
return await this.execute(executeOptions);
1856+
return await this.buildExecutionTransaction(executeOptions);
18831857
}
18841858

18851859
/**
@@ -1928,19 +1902,18 @@ class ProgramManager {
19281902
)
19291903
);
19301904

1931-
let feeAddress = this.account?.address();
1932-
const account_balance =
1933-
await this.networkClient?.getProgramMappingValue(
1934-
"credits.aleo",
1935-
"account",
1936-
feeAddress,
1937-
);
1905+
let feeAddress;
19381906

1939-
// Check to make sure the public balance is sufficient to cover the execution fee.
1940-
if (tx.feeAmount() > account_balance) {
1941-
throw "Public balance is insufficient to execute the transacation.";
1907+
if (this.account !== undefined) {
1908+
feeAddress = this.account?.address();
1909+
} else {
1910+
throw Error(
1911+
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
1912+
);
19421913
}
19431914

1915+
this.checkFee(feeAddress.to_string(), tx.feeAmount());
1916+
19441917
return this.networkClient.submitTransaction(tx);
19451918
}
19461919

0 commit comments

Comments
 (0)