Skip to content
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
18 changes: 9 additions & 9 deletions src/accountManagement/accountController.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IAccount } from "../accounts/interfaces";
import { Address, BaseController } from "../core";
import { Address, BaseController, BaseControllerInput } from "../core";
import { Transaction } from "../core/transaction";
import { TransactionsFactoryConfig } from "../core/transactionsFactoryConfig";
import { AccountTransactionsFactory } from "./accountTransactionsFactory";
Expand All @@ -18,14 +18,14 @@ export class AccountController extends BaseController {
async createTransactionForSavingKeyValue(
sender: IAccount,
nonce: bigint,
options: SaveKeyValueInput & { guardian?: Address; relayer?: Address },
options: SaveKeyValueInput & BaseControllerInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForSavingKeyValue(sender.address, options);

transaction.guardian = options.guardian ?? Address.empty();
transaction.relayer = options.relayer ?? Address.empty();
transaction.nonce = nonce;
this.addExtraGasLimitIfRequired(transaction);
this.setTransactionGasOptions(transaction, options);
transaction.signature = await sender.signTransaction(transaction);

return transaction;
Expand All @@ -34,14 +34,14 @@ export class AccountController extends BaseController {
async createTransactionForSettingGuardian(
sender: IAccount,
nonce: bigint,
options: SetGuardianInput & { guardian?: Address; relayer?: Address },
options: SetGuardianInput & BaseControllerInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForSettingGuardian(sender.address, options);

transaction.guardian = options.guardian ?? Address.empty();
transaction.relayer = options.relayer ?? Address.empty();
transaction.nonce = nonce;
this.addExtraGasLimitIfRequired(transaction);
this.setTransactionGasOptions(transaction, options);
transaction.signature = await sender.signTransaction(transaction);

return transaction;
Expand All @@ -50,12 +50,12 @@ export class AccountController extends BaseController {
async createTransactionForGuardingAccount(
sender: IAccount,
nonce: bigint,
options: { relayer?: Address },
options: { relayer?: Address; gasPrice?: bigint; gasLimit?: bigint },
): Promise<Transaction> {
const transaction = this.factory.createTransactionForGuardingAccount(sender.address);
transaction.relayer = options.relayer ?? Address.empty();
transaction.nonce = nonce;
this.addExtraGasLimitIfRequired(transaction);
this.setTransactionGasOptions(transaction, options);
transaction.signature = await sender.signTransaction(transaction);

return transaction;
Expand All @@ -64,14 +64,14 @@ export class AccountController extends BaseController {
async createTransactionForUnguardingAccount(
sender: IAccount,
nonce: bigint,
options: { guardian: Address; relayer?: Address },
options: BaseControllerInput,
): Promise<Transaction> {
const transaction = this.factory.createTransactionForUnguardingAccount(sender.address);

transaction.guardian = options.guardian ?? Address.empty();
transaction.relayer = options.relayer ?? Address.empty();
transaction.nonce = nonce;
this.addExtraGasLimitIfRequired(transaction);
this.setTransactionGasOptions(transaction, options);
transaction.signature = await sender.signTransaction(transaction);

return transaction;
Expand Down
23 changes: 21 additions & 2 deletions src/core/baseController.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
import { Transaction } from "../core/transaction";
import { Address } from "./address";
import { EXTRA_GAS_LIMIT_FOR_GUARDED_TRANSACTIONS, EXTRA_GAS_LIMIT_FOR_RELAYED_TRANSACTIONS } from "./constants";
import { Transaction } from "./transaction";

export type BaseControllerInput = {
guardian?: Address;
relayer?: Address;
gasPrice?: bigint;
gasLimit?: bigint;
};

export class BaseController {
addExtraGasLimitIfRequired(transaction: Transaction): void {
protected setTransactionGasOptions(transaction: Transaction, options: { gasLimit?: bigint; gasPrice?: bigint }) {
if (options.gasLimit) {
transaction.gasLimit = options.gasLimit;
} else {
this.addExtraGasLimitIfRequired(transaction);
}
if (options.gasPrice) {
transaction.gasPrice = options.gasPrice;
}
}

protected addExtraGasLimitIfRequired(transaction: Transaction): void {
if (transaction.guardian && !transaction.guardian.isEmpty()) {
transaction.gasLimit += BigInt(EXTRA_GAS_LIMIT_FOR_GUARDED_TRANSACTIONS);
}
Expand Down
Loading
Loading