Skip to content

Commit 767f6fe

Browse files
committed
refactored join method to avoid duplicate private key checks
1 parent f305c0a commit 767f6fe

File tree

1 file changed

+14
-36
lines changed

1 file changed

+14
-36
lines changed

sdk/src/program-manager.ts

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -710,13 +710,18 @@ class ProgramManager {
710710
privateKey?: PrivateKey,
711711
offlineQuery?: OfflineQuery,
712712
): Promise<string> {
713-
// Get the private key from the account if it is not provided in the parameters
713+
// Get the private key from the account if it is not provided in the parameters and assign the fee address
714714
let executionPrivateKey = privateKey;
715+
let feeAddress;
715716
if (
716717
typeof privateKey === "undefined" &&
717718
typeof this.account !== "undefined"
718719
) {
719720
executionPrivateKey = this.account.privateKey();
721+
feeAddress = this.account?.address();
722+
}
723+
else {
724+
feeAddress = Address.from_private_key(privateKey);
720725
}
721726

722727
if (typeof executionPrivateKey === "undefined") {
@@ -788,18 +793,6 @@ class ProgramManager {
788793
offlineQuery,
789794
);
790795

791-
let feeAddress;
792-
793-
if (typeof privateKey !== "undefined") {
794-
feeAddress = Address.from_private_key(privateKey);
795-
} else if (this.account !== undefined) {
796-
feeAddress = this.account?.address();
797-
} else {
798-
throw Error(
799-
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
800-
);
801-
}
802-
803796
// Check if the account has sufficient credits to pay for the transaction
804797
this.checkFee(feeAddress.to_string(), tx.feeAmount());
805798

@@ -841,14 +834,14 @@ class ProgramManager {
841834
privateKey?: PrivateKey,
842835
offlineQuery?: OfflineQuery,
843836
): Promise<string> {
844-
// Get the private key from the account if it is not provided in the parameters
845-
let executionPrivateKey = privateKey;
846-
if (
847-
typeof executionPrivateKey === "undefined" &&
848-
typeof this.account !== "undefined"
849-
) {
850-
executionPrivateKey = this.account.privateKey();
851-
}
837+
// Get the private key from the account if it is not provided in the parameters
838+
let executionPrivateKey = privateKey;
839+
if (
840+
typeof privateKey === "undefined" &&
841+
typeof this.account !== "undefined"
842+
) {
843+
executionPrivateKey = this.account.privateKey();
844+
}
852845

853846
if (typeof executionPrivateKey === "undefined") {
854847
throw "No private key provided and no private key set in the ProgramManager";
@@ -888,21 +881,6 @@ class ProgramManager {
888881
offlineQuery,
889882
);
890883

891-
let feeAddress;
892-
893-
if (typeof privateKey !== "undefined") {
894-
feeAddress = Address.from_private_key(privateKey);
895-
} else if (this.account !== undefined) {
896-
feeAddress = this.account?.address();
897-
} else {
898-
throw Error(
899-
"No private key provided and no private key set in the ProgramManager. Please set an account or provide a private key.",
900-
);
901-
}
902-
903-
// Check if the account has sufficient credits to pay for the transaction
904-
this.checkFee(feeAddress.to_string(), tx.feeAmount());
905-
906884
return await this.networkClient.submitTransaction(tx);
907885
}
908886

0 commit comments

Comments
 (0)