Skip to content

Commit

Permalink
Remove INetworkConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
danielailie committed Jan 29, 2025
1 parent 21f3f0b commit aa4953e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 27 deletions.
7 changes: 0 additions & 7 deletions src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,3 @@ export interface IPlainTransactionObject {
guardianSignature?: string;
relayerSignature?: string;
}

export interface INetworkConfig {
MinGasLimit: number;
GasPerDataByte: number;
GasPriceModifier: number;
ChainID: string;
}
28 changes: 14 additions & 14 deletions src/transaction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Buffer } from "buffer";
import { assert } from "chai";
import { Address } from "./address";
import { MIN_TRANSACTION_VERSION_THAT_SUPPORTS_OPTIONS, TRANSACTION_OPTIONS_DEFAULT } from "./constants";
import { NetworkConfig } from "./networkProviders";
import { ProtoSerializer } from "./proto";
import { TestWallet, loadTestWallets } from "./testutils";
import { Transaction } from "./transaction";
Expand All @@ -15,20 +16,19 @@ describe("test transaction", async () => {

const transactionComputer = new TransactionComputer();

const networkConfig = {
MinGasLimit: 50000,
GasPerDataByte: 1500,
GasPriceModifier: 0.01,
ChainID: "D",
};
const networkConfig = new NetworkConfig();
networkConfig.chainID = "D";
networkConfig.minGasLimit = 50000n;
networkConfig.gasPerDataByte = 1500n;
networkConfig.gasPriceModifier = 0.01;

before(async function () {
wallets = await loadTestWallets();
});

it("should serialize transaction for signing (without data)", async () => {
const transaction = new Transaction({
chainID: networkConfig.ChainID,
chainID: networkConfig.chainID,
sender: wallets.alice.address,
receiver: wallets.bob.address,
gasLimit: 50000n,
Expand All @@ -48,7 +48,7 @@ describe("test transaction", async () => {

it("should serialize transaction for signing (with data)", async () => {
const transaction = new Transaction({
chainID: networkConfig.ChainID,
chainID: networkConfig.chainID,
sender: wallets.alice.address,
receiver: wallets.bob.address,
gasLimit: 70000n,
Expand Down Expand Up @@ -143,7 +143,7 @@ describe("test transaction", async () => {

it("should compute hash", async () => {
const transaction = new Transaction({
chainID: networkConfig.ChainID,
chainID: networkConfig.chainID,
sender: wallets.alice.address,
receiver: wallets.alice.address,
gasLimit: 100000n,
Expand All @@ -165,7 +165,7 @@ describe("test transaction", async () => {

it("should compute hash (with usernames)", async () => {
const transaction = new Transaction({
chainID: networkConfig.ChainID,
chainID: networkConfig.chainID,
sender: wallets.alice.address,
receiver: wallets.alice.address,
gasLimit: 100000n,
Expand Down Expand Up @@ -428,7 +428,7 @@ describe("test transaction", async () => {

it("computes fee, but should throw `NotEnoughGas` error", async () => {
const transaction = new Transaction({
chainID: networkConfig.ChainID,
chainID: networkConfig.chainID,
sender: wallets.alice.address,
receiver: wallets.alice.address,
gasLimit: 50000n,
Expand Down Expand Up @@ -458,7 +458,7 @@ describe("test transaction", async () => {

it("computes fee (with data field)", async () => {
const transaction = new Transaction({
chainID: networkConfig.ChainID,
chainID: networkConfig.chainID,
sender: wallets.alice.address,
receiver: wallets.alice.address,
gasLimit: 50000n + 12010n,
Expand Down Expand Up @@ -798,7 +798,7 @@ describe("test transaction", async () => {
});
it("should serialize transaction with relayer", async () => {
const transaction = new Transaction({
chainID: networkConfig.ChainID,
chainID: networkConfig.chainID,
sender: wallets.alice.address,
receiver: wallets.alice.address,
relayer: wallets.bob.address,
Expand All @@ -819,7 +819,7 @@ describe("test transaction", async () => {

it("should test relayed v3", async () => {
const transaction = new Transaction({
chainID: networkConfig.ChainID,
chainID: networkConfig.chainID,
sender: wallets.alice.address,
receiver: wallets.alice.address,
senderUsername: "alice",
Expand Down
5 changes: 3 additions & 2 deletions src/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { BigNumber } from "bignumber.js";
import { Address } from "./address";
import { TRANSACTION_MIN_GAS_PRICE, TRANSACTION_OPTIONS_DEFAULT, TRANSACTION_VERSION_DEFAULT } from "./constants";
import { INetworkConfig, IPlainTransactionObject } from "./interface";
import { IPlainTransactionObject } from "./interface";
import { NetworkConfig } from "./networkProviders";
import { interpretSignatureAsBuffer } from "./signature";
import { TransactionComputer } from "./transactionComputer";

Expand Down Expand Up @@ -444,7 +445,7 @@ export class Transaction {
* Computes the current transaction fee based on the {@link NetworkConfig} and transaction properties
* @param networkConfig {@link NetworkConfig}
*/
computeFee(networkConfig: INetworkConfig): BigNumber {
computeFee(networkConfig: NetworkConfig): BigNumber {
const computer = new TransactionComputer();
const fee = computer.computeTransactionFee(this, networkConfig);
return new BigNumber(fee.toString());
Expand Down
8 changes: 4 additions & 4 deletions src/transactionComputer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TRANSACTION_OPTIONS_TX_HASH_SIGN,
} from "./constants";
import * as errors from "./errors";
import { INetworkConfig } from "./interface";
import { NetworkConfig } from "./networkProviders";
import { ProtoSerializer } from "./proto";
import { Transaction } from "./transaction";

Expand All @@ -22,10 +22,10 @@ export class TransactionComputer {

computeTransactionFee(
transaction: { gasPrice: bigint; gasLimit: bigint; data: Uint8Array },
networkConfig: INetworkConfig,
networkConfig: NetworkConfig,
): bigint {
const moveBalanceGas = BigInt(
networkConfig.MinGasLimit + transaction.data.length * networkConfig.GasPerDataByte,
networkConfig.minGasLimit + BigInt(transaction.data.length) * networkConfig.gasPerDataByte,
);
if (moveBalanceGas > transaction.gasLimit) {
throw new errors.ErrNotEnoughGas(parseInt(transaction.gasLimit.toString(), 10));
Expand All @@ -39,7 +39,7 @@ export class TransactionComputer {

const diff = transaction.gasLimit - moveBalanceGas;
const modifiedGasPrice = BigInt(
new BigNumber(gasPrice.toString()).multipliedBy(new BigNumber(networkConfig.GasPriceModifier)).toFixed(0),
new BigNumber(gasPrice.toString()).multipliedBy(new BigNumber(networkConfig.gasPriceModifier)).toFixed(0),
);
const processingFee = diff * modifiedGasPrice;

Expand Down

0 comments on commit aa4953e

Please sign in to comment.