All files / ethers.js/src.ts/providers format.ts

94.68% Statements 303/320
80.76% Branches 42/52
92.3% Functions 12/13
94.68% Lines 303/320

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 3211x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 14623x 16261x 11144x 14623x 14623x 1x 1x 2089x 155x 155x 155x 2089x 2089x 1x 1x 1x 1x 1x 1047x 1276x 1276x 25262x 25262x 3363x 3363x 3348x 3348x 3348x 3363x 3363x 25262x 25262x 25262x 25262x 25262x       25262x 1276x 1047x 1047x 1x 1x 19x 19x   19x 19x 19x     1x 1x 1379x 1379x 1379x 1x 1x 3092x 3092x 3092x 1x 1x           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 19x 19x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 103x 103x 13513x 986x 103x 103x 103x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 50x 50x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 61x 61x 1x 1x 1043x 1043x 1043x 1043x     1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1038x 1043x 1043x 1043x 1043x 1043x 1x 1x 1x 1x 1x 1x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 1043x 4x 4x 1043x 1043x 1043x 1043x 1043x     1043x 1043x 1043x 16x 1043x 1027x 1027x 1037x 1037x 1043x 37x 37x 37x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1037x 1043x     1037x 1037x 1037x  
/**
 *  @_ignore
 */
import { getAddress, getCreateAddress } from "../address/index.js";
import { Signature } from "../crypto/index.js"
import { accessListify } from "../transaction/index.js";
import {
    getBigInt, getNumber, hexlify, isHexString, zeroPadValue,
    assert, assertArgument
} from "../utils/index.js";
 
import type {
    BlockParams, LogParams,
    TransactionReceiptParams, TransactionResponseParams,
} from "./formatting.js";
 
 
const BN_0 = BigInt(0);
 
export type FormatFunc = (value: any) => any;
 
export function allowNull(format: FormatFunc, nullValue?: any): FormatFunc {
    return (function(value: any) {
        if (value == null) { return nullValue; }
        return format(value);
    });
}
 
export function arrayOf(format: FormatFunc, allowNull?: boolean): FormatFunc {
    return ((array: any) => {
        if (allowNull && array == null) { return null; }
        if (!Array.isArray(array)) { throw new Error("not an array"); }
        return array.map((i) => format(i));
    });
}
 
// Requires an object which matches a fleet of other formatters
// Any FormatFunc may return `undefined` to have the value omitted
// from the result object. Calls preserve `this`.
export function object(format: Record<string, FormatFunc>, altNames?: Record<string, Array<string>>): FormatFunc {
    return ((value: any) => {
        const result: any = { };
        for (const key in format) {
            let srcKey = key;
            if (altNames && key in altNames && !(srcKey in value)) {
                for (const altKey of altNames[key]) {
                    if (altKey in value) {
                        srcKey = altKey;
                        break;
                    }
                }
            }
 
            try {
                const nv = format[key](value[srcKey]);
                if (nv !== undefined) { result[key] = nv; }
            } catch (error) {
                const message = (error instanceof Error) ? error.message: "not-an-error";
                assert(false, `invalid value for value.${ key } (${ message })`, "BAD_DATA", { value })
            }
        }
        return result;
    });
}
 
export function formatBoolean(value: any): boolean {
    switch (value) {
        case true: case "true":
            return true;
        case false: case "false":
            return false;
    }
    assertArgument(false, `invalid boolean; ${ JSON.stringify(value) }`, "value", value);
}
 
export function formatData(value: string): string {
    assertArgument(isHexString(value, true), "invalid data", "value", value);
    return value;
}
 
export function formatHash(value: any): string {
    assertArgument(isHexString(value, 32), "invalid hash", "value", value);
    return value;
}
 
export function formatUint256(value: any): string {
    if (!isHexString(value)) {
        throw new Error("invalid uint256");
    }
    return zeroPadValue(value, 32);
}
 
const _formatLog = object({
    address: getAddress,
    blockHash: formatHash,
    blockNumber: getNumber,
    data: formatData,
    index: getNumber,
    removed: allowNull(formatBoolean, false),
    topics: arrayOf(formatHash),
    transactionHash: formatHash,
    transactionIndex: getNumber,
}, {
    index: [ "logIndex" ]
});
 
export function formatLog(value: any): LogParams {
    return _formatLog(value);
}
 
const _formatBlock = object({
    hash: allowNull(formatHash),
    parentHash: formatHash,
    parentBeaconBlockRoot: allowNull(formatHash, null),
 
    number: getNumber,
 
    timestamp: getNumber,
    nonce: allowNull(formatData),
    difficulty: getBigInt,
 
    gasLimit: getBigInt,
    gasUsed: getBigInt,
 
    stateRoot: allowNull(formatHash, null),
    receiptsRoot: allowNull(formatHash, null),
 
    blobGasUsed: allowNull(getBigInt, null),
    excessBlobGas: allowNull(getBigInt, null),
 
    miner: allowNull(getAddress),
    prevRandao: allowNull(formatHash, null),
    extraData: formatData,
 
    baseFeePerGas: allowNull(getBigInt)
}, {
    prevRandao: [ "mixHash" ]
});
 
export function formatBlock(value: any): BlockParams {
    const result = _formatBlock(value);
    result.transactions = value.transactions.map((tx: string | TransactionResponseParams) => {
        if (typeof(tx) === "string") { return tx; }
        return formatTransactionResponse(tx);
    });
    return result;
}
 
const _formatReceiptLog = object({
    transactionIndex: getNumber,
    blockNumber: getNumber,
    transactionHash: formatHash,
    address: getAddress,
    topics: arrayOf(formatHash),
    data: formatData,
    index: getNumber,
    blockHash: formatHash,
}, {
    index: [ "logIndex" ]
});
 
export function formatReceiptLog(value: any): LogParams {
    return _formatReceiptLog(value);
}
 
const _formatTransactionReceipt = object({
    to: allowNull(getAddress, null),
    from: allowNull(getAddress, null),
    contractAddress: allowNull(getAddress, null),
    // should be allowNull(hash), but broken-EIP-658 support is handled in receipt
    index: getNumber,
    root: allowNull(hexlify),
    gasUsed: getBigInt,
    blobGasUsed: allowNull(getBigInt, null),
    logsBloom: allowNull(formatData),
    blockHash: formatHash,
    hash: formatHash,
    logs: arrayOf(formatReceiptLog),
    blockNumber: getNumber,
    //confirmations: allowNull(getNumber, null),
    cumulativeGasUsed: getBigInt,
    effectiveGasPrice: allowNull(getBigInt),
    blobGasPrice: allowNull(getBigInt, null),
    status: allowNull(getNumber),
    type: allowNull(getNumber, 0)
}, {
    effectiveGasPrice: [ "gasPrice" ],
    hash: [ "transactionHash" ],
    index: [ "transactionIndex" ],
});
 
export function formatTransactionReceipt(value: any): TransactionReceiptParams {
    return _formatTransactionReceipt(value);
}
 
export function formatTransactionResponse(value: any): TransactionResponseParams {
 
    // Some clients (TestRPC) do strange things like return 0x0 for the
    // 0 address; correct this to be a real address
    if (value.to && getBigInt(value.to) === BN_0) {
        value.to = "0x0000000000000000000000000000000000000000";
    }
 
    const result = object({
        hash: formatHash,
 
        // Some nodes do not return this, usually test nodes (like Ganache)
        index: allowNull(getNumber, undefined),
 
        type: (value: any) => {
            if (value === "0x" || value == null) { return 0; }
            return getNumber(value);
        },
        accessList: allowNull(accessListify, null),
        blobVersionedHashes: allowNull(arrayOf(formatHash, true), null),
 
        authorizationList: allowNull(arrayOf((v: any) => {
            return {
                address: getAddress(v.address),
                chainId: getBigInt(v.chainId),
                nonce: getBigInt(v.nonce),
                signature: Signature.from(v.signature ? v.signature: v)
            };
        }, false), null),
 
        blockHash: allowNull(formatHash, null),
        blockNumber: allowNull(getNumber, null),
        transactionIndex: allowNull(getNumber, null),
 
        from: getAddress,
 
        // either (gasPrice) or (maxPriorityFeePerGas + maxFeePerGas) must be set
        gasPrice: allowNull(getBigInt),
        maxPriorityFeePerGas: allowNull(getBigInt),
        maxFeePerGas: allowNull(getBigInt),
        maxFeePerBlobGas: allowNull(getBigInt, null),
 
        gasLimit: getBigInt,
        to: allowNull(getAddress, null),
        value: getBigInt,
        nonce: getNumber,
        data: formatData,
 
        creates: allowNull(getAddress, null),
 
        chainId: allowNull(getBigInt, null)
    }, {
        data: [ "input" ],
        gasLimit: [ "gas" ],
        index: [ "transactionIndex" ]
    })(value);
 
    // If to and creates are empty, populate the creates from the value
    if (result.to == null && result.creates == null) {
        result.creates = getCreateAddress(result);
    }
 
    // @TODO: Check fee data
 
    // Add an access list to supported transaction types
    if ((value.type === 1 || value.type === 2) && value.accessList == null) {
        result.accessList = [ ];
    }
 
    // Compute the signature
    if (value.signature) {
        result.signature = Signature.from(value.signature);
    } else {
        result.signature = Signature.from(value);
    }
 
    // Some backends omit ChainId on legacy transactions, but we can compute it
    if (result.chainId == null) {
        const chainId = result.signature.legacyChainId;
        if (chainId != null) { result.chainId = chainId; }
    }
 
 
    // @TODO: check chainID
    /*
    if (value.chainId != null) {
        let chainId = value.chainId;
 
        if (isHexString(chainId)) {
            chainId = BigNumber.from(chainId).toNumber();
        }
 
        result.chainId = chainId;
 
    } else {
        let chainId = value.networkId;
 
        // geth-etc returns chainId
        if (chainId == null && result.v == null) {
            chainId = value.chainId;
        }
 
        if (isHexString(chainId)) {
            chainId = BigNumber.from(chainId).toNumber();
        }
 
        if (typeof(chainId) !== "number" && result.v != null) {
            chainId = (result.v - 35) / 2;
            if (chainId < 0) { chainId = 0; }
            chainId = parseInt(chainId);
        }
 
        if (typeof(chainId) !== "number") { chainId = 0; }
 
        result.chainId = chainId;
    }
    */
 
    // 0x0000... should actually be null
    if (result.blockHash && getBigInt(result.blockHash) === BN_0) {
        result.blockHash = null;
    }
 
    return result;
}