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

Implemented the Transaction class #366

Merged
merged 6 commits into from
Jan 5, 2024
Merged

Implemented the Transaction class #366

merged 6 commits into from
Jan 5, 2024

Conversation

popenta
Copy link
Contributor

@popenta popenta commented Dec 22, 2023

Implemented the new Transaction class as described in the specs.

@popenta popenta self-assigned this Dec 22, 2023
CiprianDraghici
CiprianDraghici previously approved these changes Jan 3, 2024
tx.applySignature(transaction.signature);
}

if (transaction.guardianSignature) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checks for transaction.signature and transaction.guardianSignature are not symmetric.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, they were not. Fixed.

@@ -458,3 +492,215 @@ export class TransactionHash extends Hash {
}
}

/**
* An abstraction for creating, signing and broadcasting transactions.
* Will replace the {@link Transaction} class in the future.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, this is the one according to sdk-specs 👍

this.gasLimit = gasLimit;
this.data = data || new Uint8Array();
this.chainID = chainID;
this.version = version || TRANSACTION_OPTIONS_DEFAULT;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Version vs. options.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

receiver: Address.fromBech32(transaction.receiver),
gasLimit: new BigNumber(transaction.gasLimit).toNumber(),
chainID: transaction.chainID,
value: new BigNumber(transaction.value).toFixed(0),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct 👍

constructor() {}

computeTransactionFee(transaction: ITransactionNext, networkConfig: INetworkConfig): BigNumber{
let moveBalanceGas = new BigNumber(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When possible, we should use const instead of let (optional, can also be done in other PR).

Copy link
Contributor Author

@popenta popenta Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replaced with const where possible.

.update(buffer)
.digest("hex");

return new Uint8Array(Buffer.from(hash, "hex"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally speaking, the following:

return new Uint8Array(Buffer.from(hash, "hex"))

Can be replaced by:

return Buffer.from(hash, "hex")

(optional, shorter)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, replaced it.

chainID: transaction.chainID,
version: transaction.version,
options: transaction.options == 0 ? undefined : transaction.options,
guardian: transaction.guardian == "" ? undefined : transaction.guardian,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check can also be: transaction.guardian ? undefined : transaction.guardian, so that it handles null values, as well. CC: @CiprianDraghici, makes sense? I think for options, as well. Since setters are public.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check is now:

transaction.guardian ? transaction.guardian : undefined

Same for options.

receiverUsername: transaction.receiverUsername ? Buffer.from(transaction.receiverUsername).toString("base64") : undefined,
gasPrice: Number(transaction.gasPrice),
gasLimit: Number(transaction.gasLimit),
data: transaction.data.length == 0 ? undefined : Buffer.from(transaction.data).toString("base64"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the setter is public, here we can also protect against a null / undefined data field.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Comment on lines +8 to +12
MinGasLimit: number;
GasPerDataByte: number;
GasPriceModifier: number;
ChainID: string;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now I see that at some point we have to have consistent naming for public fields. Here, uppercase. For TransactionNext, the first letter is lowercase.

All right for now, no need to rename the fields of NetworkConfig in v13.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really sure what you mean. The TransactionComputer uses the same INetworkConfig interface as the Transaction class where the first letter is uppercase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I do agree that we need to stay consistent with the naming.

@popenta popenta merged commit e059d13 into feat/next Jan 5, 2024
1 check passed
@popenta popenta deleted the transaction-next branch January 5, 2024 09:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants