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

docs: added technical docs to EvmTransaction datatype #614

Merged
merged 5 commits into from
Aug 25, 2022
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
5 changes: 5 additions & 0 deletions .changeset/funny-clocks-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@moralisweb3/evm-utils': minor
---

Added technical documentation for EvmTransaction datatype
276 changes: 268 additions & 8 deletions packages/evmUtils/src/dataTypes/EvmTransaction/EvmTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,36 @@ import { EvmNative } from '../EvmNative';
import { EvmTransactionLog } from '../EvmTransactionLog';
import { EvmTransacionInput, EvmTransactionData } from './types';

/**
* Valid input for a new EvmTransaction instance.
* This can be an existing {@link EvmTransaction} or a valid {@link EvmTransacionInput} object
*/
export type EvmTransactionish = EvmTransacionInput | EvmTransaction;

/**
* The EvmTranaction is a representation of a published transaction.
*
* Use this class any time you work with a transaction.
*
* @category DataType
*/
export class EvmTransaction implements MoralisDataObject {
/**
* Create a new instance of EvmTransaction from any valid transaction input
* @param data - the EvmTransactionish type
* @example
* ```
* const transaction = EvmTransaction.create(data);
*```
*/
static create(data: EvmTransactionish) {
if (data instanceof EvmTransaction) {
return data;
}

return new EvmTransaction(data);
}

private _data: EvmTransactionData;

constructor(data: EvmTransacionInput) {
Expand Down Expand Up @@ -43,14 +70,15 @@ export class EvmTransaction implements MoralisDataObject {
logs: (data.logs ?? []).map((log) => EvmTransactionLog.create(log)),
});

static create(data: EvmTransactionish) {
if (data instanceof EvmTransaction) {
return data;
}

return new EvmTransaction(data);
}

/**
* Check the equality between two Evm transactions
* @param dataA - The first transaction
* @param dataB - The second transaction
* @example
* ```ts
* EvmTransaction.equals(dataA, dataB)
* ```
*/
static equals(dataA: EvmTransactionish, dataB: EvmTransactionish) {
const transactionA = EvmTransaction.create(dataA);
const transactionB = EvmTransaction.create(dataB);
Expand All @@ -66,6 +94,14 @@ export class EvmTransaction implements MoralisDataObject {
return true;
}

/**
* Checks the equality of the current transaction with another evm transaction
* @param data - the transaction to compare with
* @example
* ```ts
* transaction.equals(data)
* ```
*/
equals(data: EvmTransactionish): boolean {
return EvmTransaction.equals(this, data);
}
Expand All @@ -86,14 +122,238 @@ export class EvmTransaction implements MoralisDataObject {
chain: data.chain?.format(),
contractAddress: data.contractAddress?.format(),
logs: data.logs.map((log) => log.toJSON()),
blockTimestamp: data.blockTimestamp.toString(),
};
}

/**
* @returns a JSON represention of the transaction.
* @example
* ```
* transaction.format()
* ```
*/
format() {
return this.toJSON();
}

/**
* @returns the transaction
* @example
* ```
* transaction.result
* ```
*/
get result() {
return this._data;
}

/**
* @returns the transaction to address
* @example
* ```
* transaction.to // EvmAddress
* ```
*/
get to() {
return this._data.to;
}

/**
* @returns the transaction from address
* @example
* ```
* transaction.address // EvmAddress
* ```
*/
get from() {
return this._data.from;
}

/**
* @returns the transaction nonce
* @example
* ```
* transaction.nonce // 326595425
* ```
*/
get nonce() {
return this._data.nonce;
}

/**
* @returns the transaction gas
* @example
* ```
* transaction.gas // 6721975
* ```
*/
get gas() {
return this._data.gas;
}

/**
* @returns the transaction gas price
* @example
* ```
* transaction.gasPrice // 20000000000
* ```
*/
get gasPrice() {
return this._data.gasPrice;
}

/**
* @returns the transaction gas used
* @example
* ```
* transaction.gasUsed // 1340925
* ```
*/
get gasUsed() {
return this._data.gasUsed;
}

/**
* @returns the transaction cumulative gas used
* @example
* ```
* transaction.cumulativeGasUsed // 1340925
* ```
*/
get cumulativeGasUsed() {
return this._data.cumulativeGasUsed;
}

/**
* @returns the transaction block number
* @example
* ```
* transaction.blockNumber // 12526958
* ```
*/
get blockNumber() {
return this._data.blockNumber;
}

/**
* @returns the transaction value
* @example
* ```
* transaction.value // EvmNative
* ```
*/
get value() {
return this._data.value;
}

/**
* @returns the transaction chain
* @example
* ```
* transaction.chain // EvmChain
* ```
*/
get chain() {
return this._data.chain;
}

/**
* @returns the transaction contract address
* @example
* ```
* transaction.contractAddress // EvmAddress
* ```
*/
get contractAddress() {
return this._data.contractAddress;
}

/**
* @returns the transaction logs
* @example
* ```
* transaction.logs // EvmTransactionLog[]
* ```
*/
get logs() {
return this._data.logs;
}

/**
* @returns the transaction receipt root
* @example
* ```
* transaction.receiptRoot // string
* ```
*/
get receiptRoot() {
return this._data.receiptRoot;
}

/**
* @returns the transaction receipt status
* @example
* ```
* transaction.receiptStatus // 1
* ```
*/
get receiptStatus() {
return this._data.receiptStatus;
}

/**
* @returns the transaction data
* @example
* ```
* transaction.data // 0x000000000000000000000000000000000000000000000000000000000000002
* ```
*/
get data() {
return this._data.data;
}

/**
* @returns the transaction hash
* @example
* ```
* transaction.hash // 0x057Ec652A4F150f7FF94f089A38008f49a0DF88e
* ```
*/
get hash() {
return this._data.hash;
}

/**
* @returns the transaction type
* @example
* ```
* transaction.type // 1
* ```
*/
get type() {
return this._data.type;
}

/**
* @returns the transaction black hash
* @example
* ```
* transaction.blockHash // 0x0372c302e3c52e8f2e15d155e2c545e6d802e479236564af052759253b20fd86
* ```
*/
get blockHash() {
return this._data.blockHash;
}

/**
* @returns the transaction block timestamp
* @example
* ```
* transaction.blockTimestamp // Date
* ```
*/
get blockTimestamp() {
return this._data.blockTimestamp;
}
}
29 changes: 29 additions & 0 deletions packages/evmUtils/src/dataTypes/EvmTransaction/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,32 @@ import { EvmChain, EvmChainish } from '../EvmChain';
import { EvmNativeish, EvmNative } from '../EvmNative';
import { EvmTransactionLogish, EvmTransactionLog } from '../EvmTransactionLog';

/**
* This can be any object with valid transaction data.
* @example
* ```
* const transactionInput = {
cumulativeGasUsed: "1340925",
gasPrice: "20000000000",
gasUsed: "1340925",
index: "25",
contractAddress: "0x1d6a4cf64b52f6c73f201839aded7379ce58059c",
receiptRoot: "string",
receiptStatus: "1",
chain: "1",
data: "0x000000000000000000000000000000000000000000000000000000000000002",
from: "0xd4a3BebD824189481FC45363602b83C9c7e9cbDf",
hash: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
nonce: "326595425",
value: "650000000000000000",
blockHash: "0x0372c302e3c52e8f2e15d155e2c545e6d802e479236564af052759253b20fd86",
blockNumber: "12526958",
blockTimestamp: new Date("2021-04-02T10:07:54.000Z"),
gas: "6721975",
to: "0xa71db868318f0a0bae9411347cd4a6fa23d8d4ef",
}
* ```
*/
export interface EvmTransacionInput {
from: EvmAddressish;
to?: null | EvmAddressish;
Expand Down Expand Up @@ -34,6 +60,9 @@ export interface EvmTransacionInput {
logs?: EvmTransactionLogish[];
}

/**
* This is the return type of the processed EVM transaction
*/
export interface EvmTransactionData {
from: EvmAddress;
to?: EvmAddress;
Expand Down