Skip to content

Commit

Permalink
docs: added technical docs to EvmTransaction datatype
Browse files Browse the repository at this point in the history
  • Loading branch information
sogunshola committed Aug 25, 2022
1 parent d543356 commit 7fa4654
Show file tree
Hide file tree
Showing 3 changed files with 305 additions and 8 deletions.
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
279 changes: 271 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,39 @@ 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);
}

/**
* Internal reference of the transaction.
*/
private _data: EvmTransactionData;

constructor(data: EvmTransacionInput) {
Expand Down Expand Up @@ -43,14 +73,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 +97,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 +125,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
* ```
*/
get to() {
return this._data.to;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

/**
* @returns the transaction block timestamp
* @example
* ```
* transaction.blockTimestamp
* ```
*/
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: transaction.input,
from: "0xd4a3BebD824189481FC45363602b83C9c7e9cbDf",
hash: "0x057Ec652A4F150f7FF94f089A38008f49a0DF88e",
nonce: "326595425",
value: "650000000000000000",
blockHash: transaction.block_hash,
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

0 comments on commit 7fa4654

Please sign in to comment.