All files / ethers.js/src.ts/providers provider-jsonrpc.ts

82.69% Statements 1104/1335
75.71% Branches 159/210
77.08% Functions 37/48
82.69% Lines 1104/1335

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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 13361x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x     8x 8x 8x     8x 8x 8x 8x 8x 21x 21x 8x 8x       1x       1x 75x 75x     1x 1x 1x 1x 1x 29x 29x 29x 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 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 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 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 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 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x         6x 6x 14x 14x 6x 6x 6x     6x 6x 6x 6x 8x 8x 8x 8x 8x 8x               8x 8x 8x 8x 8x 8x 8x 8x 6x 6x 6x 6x 8x 8x 8x 5x 5x 5x 5x 5x 8x 8x 8x 8x 8x 8x 8x 8x 6x 6x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 11x 11x 11x 11x 9x 11x 8x 8x 8x 11x 11x 2x 2x 2x 2x 2x 2x 2x               2x 2x 2x 1x 1x 1x 1x       1x 2x 2x 2x 2x 2x 3x 3x 11x 8x 8x 8x 8x 6x 6x                               6x 6x 6x         6x 6x                             6x 6x       6x 6x 6x         6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 1350x 762x 762x 1350x 1350x 1350x 762x 762x 762x 762x 762x 762x 762x 762x 762x 762x 341x 341x 341x 341x       341x 762x 762x 762x 762x 762x 762x 762x 762x 762x 759x 759x 759x 762x 1098x 1098x       1098x 1098x 1098x 1098x 1098x 1098x               1098x 1098x 1098x 103x 103x 103x 995x 995x 995x 995x 759x 762x 3x 3x 3x 5x 5x 5x 3x 762x 762x 1350x 1350x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x           248x 243x 243x 243x 243x 243x 248x 248x 248x 248x 248x 248x 248x 248x 2767x 2767x 248x 248x 248x 248x 248x 248x 5x 5x 5x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 800x 800x 800x 800x 361x 361x 11x 11x 11x 11x           11x 11x 361x 800x 800x 800x 800x 800x 800x       248x 248x 248x 248x 248x 248x 248x 248x 248x 745x 745x 467x   467x 467x 467x 467x 278x 745x 21x 21x 257x 257x 257x 257x 257x 257x 256x 256x 257x 1x 1x 1x 257x 257x 257x                                                             248x 248x 248x 248x 248x 248x 248x 248x 248x 1103x 247x 247x 247x 247x 247x 247x 247x 247x 5x 5x 5x           5x 247x 247x 247x 247x 247x 248x 248x 248x 248x 248x 248x 248x       248x 248x 248x 248x 248x 248x 248x 248x 248x 29x 29x 29x 29x 29x 2x     2x 2x 27x 27x 27x 29x     27x 27x 27x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 369x 369x 369x 369x 2952x 103x 2952x 103x 369x 369x 369x 369x 1107x 750x 369x 369x 369x 369x     369x 369x       369x 369x                         369x 369x 369x 369x 369x 369x 369x 369x 248x 248x 248x 248x 248x 248x 800x 800x   800x 800x 140x 800x 800x 28x 800x 800x 28x 800x 800x 10x 10x 10x 10x 800x 800x 43x 43x 43x 43x 800x 800x 12x 12x 12x 12x 800x 800x 6x 6x 6x 6x 6x 6x 6x 6x 800x 800x 19x 19x 19x 19x 800x 800x 61x 45x 45x 45x 45x 61x 16x 16x 16x 16x 16x   800x 800x 37x 37x 37x 37x 800x 800x 51x 51x 51x 51x 800x 800x 286x 286x 286x 286x 800x 800x 75x 75x 75x 75x 75x 800x 800x 4x 4x   4x 4x 4x 4x 4x 800x       248x 248x 248x 248x 248x 248x 248x 248x 103x 103x 103x 103x 52x 52x 5x 5x 5x 5x 52x 16x 16x 16x 16x 16x 52x 82x 103x 72x 72x 72x 72x 72x 72x 72x 72x 72x 72x 10x 10x 10x 10x 10x 10x 103x                                 10x 103x 10x 10x 10x         10x 10x 4x 4x 6x 6x 10x 4x 4x 2x 10x         10x 2x 2x 2x 2x     2x 2x 103x         2x 2x 2x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 1103x 1103x 1103x 1103x     1103x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x                                 248x 248x       248x 248x 2347x 2347x 2347x       2347x 2347x 2347x     2347x 2347x 2347x 2347x 2347x 2347x 2347x 248x 1x 1x 1x 1x 1x 1x 1x 1x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 248x 29x 29x 14x 14x 29x 29x 248x 248x 248x 248x 248x 248x                 248x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 248x 248x 248x 248x 248x 248x 248x 3x 248x 245x 245x 248x 248x 248x 762x 762x 248x 248x 1103x 1103x 1103x 1103x 1103x 1103x 1103x 248x 248x 762x 762x 762x 762x 762x 762x 762x 762x 762x 759x 759x 759x 248x 1x 82x 82x 82x 82x 82x 68x 68x 14x 14x 82x 4x 10x 10x 10x 4x 4x 10x 10x 82x 6x 6x 6x 6x 10x 10x 10x 1x 30x 30x 30x 30x 30x 10x 10x 30x 30x 30x 10x 20x 20x 10x 30x 30x 30x 10x 10x 10x 10x 30x 1x 10x 10x 10x 10x 10x  
/**
 *  One of the most common ways to interact with the blockchain is
 *  by a node running a JSON-RPC interface which can be connected to,
 *  based on the transport, using:
 *
 *  - HTTP or HTTPS - [[JsonRpcProvider]]
 *  - WebSocket - [[WebSocketProvider]]
 *  - IPC - [[IpcSocketProvider]]
 *
 * @_section: api/providers/jsonrpc:JSON-RPC Provider  [about-jsonrpcProvider]
 */
 
// @TODO:
// - Add the batching API
 
// https://playground.open-rpc.org/?schemaUrl=https://raw.githubusercontent.com/ethereum/eth1.0-apis/assembled-spec/openrpc.json&uiSchema%5BappBar%5D%5Bui:splitView%5D=true&uiSchema%5BappBar%5D%5Bui:input%5D=false&uiSchema%5BappBar%5D%5Bui:examplesDropdown%5D=false
 
import { AbiCoder } from "../abi/index.js";
import { getAddress, resolveAddress } from "../address/index.js";
import { TypedDataEncoder } from "../hash/index.js";
import { accessListify, authorizationify } from "../transaction/index.js";
import {
    defineProperties, getBigInt, hexlify, isHexString, toQuantity, toUtf8Bytes,
    isError, makeError, assert, assertArgument,
    FetchRequest, resolveProperties
} from "../utils/index.js";
 
import { AbstractProvider, UnmanagedSubscriber } from "./abstract-provider.js";
import { AbstractSigner } from "./abstract-signer.js";
import { Network } from "./network.js";
import { FilterIdEventSubscriber, FilterIdPendingSubscriber } from "./subscriber-filterid.js";
import { PollingEventSubscriber } from "./subscriber-polling.js";
 
import type { TypedDataDomain, TypedDataField } from "../hash/index.js";
import type { TransactionLike } from "../transaction/index.js";
 
import type { PerformActionRequest, Subscriber, Subscription } from "./abstract-provider.js";
import type { Networkish } from "./network.js";
import type { Provider, TransactionRequest, TransactionResponse } from "./provider.js";
import type { Signer } from "./signer.js";
 
type Timer = ReturnType<typeof setTimeout>;
 
const Primitive = "bigint,boolean,function,number,string,symbol".split(/,/g);
//const Methods = "getAddress,then".split(/,/g);
function deepCopy<T = any>(value: T): T {
    if (value == null || Primitive.indexOf(typeof(value)) >= 0) {
        return value;
    }
 
    // Keep any Addressable
    if (typeof((<any>value).getAddress) === "function") {
        return value;
    }
 
    if (Array.isArray(value)) { return <any>(value.map(deepCopy)); }
 
    if (typeof(value) === "object") {
        return Object.keys(value).reduce((accum, key) => {
            accum[key] = (<any>value)[key];
            return accum;
        }, <any>{ });
    }

    throw new Error(`should not happen: ${ value } (${ typeof(value) })`);
}
 
function stall(duration: number): Promise<void> {
    return new Promise((resolve) => { setTimeout(resolve, duration); });
}
 
function getLowerCase(value: string): string {
    if (value) { return value.toLowerCase(); }
    return value;
}
 
interface Pollable {
    pollingInterval: number;
}
 
function isPollable(value: any): value is Pollable {
    return (value && typeof(value.pollingInterval) === "number");
}
 
/**
 *  A JSON-RPC payload, which are sent to a JSON-RPC server.
 */
export type JsonRpcPayload = {
    /**
     *  The JSON-RPC request ID.
     */
    id: number;
 
    /**
     *  The JSON-RPC request method.
     */
    method: string;
 
    /**
     *  The JSON-RPC request parameters.
     */
    params: Array<any> | Record<string, any>;
 
    /**
     *  A required constant in the JSON-RPC specification.
     */
    jsonrpc: "2.0";
};
 
/**
 *  A JSON-RPC result, which are returned on success from a JSON-RPC server.
 */
export type JsonRpcResult = {
    /**
     *  The response ID to match it to the relevant request.
     */
    id: number;
 
    /**
     *  The response result.
     */
    result: any;
};
 
/**
 *  A JSON-RPC error, which are returned on failure from a JSON-RPC server.
 */
export type JsonRpcError = {
    /**
     *  The response ID to match it to the relevant request.
     */
    id: number;
 
    /**
     *  The response error.
     */
    error: {
        code: number;
        message?: string;
        data?: any;
    }
};
 
/**
 *  When subscribing to the ``"debug"`` event, the [[Listener]] will
 *  receive this object as the first parameter.
 */
export type DebugEventJsonRpcApiProvider = {
    action: "sendRpcPayload",
    payload: JsonRpcPayload | Array<JsonRpcPayload>
} | {
    action: "receiveRpcResult",
    result: Array<JsonRpcResult | JsonRpcError>
} | {
    action: "receiveRpcError",
    error: Error
};
 
/**
 *  Options for configuring a [[JsonRpcApiProvider]]. Much of this
 *  is targetted towards sub-classes, which often will not expose
 *  any of these options to their consumers.
 *
 *  **``polling``** - use the polling strategy is used immediately
 *  for events; otherwise, attempt to use filters and fall back onto
 *  polling (default: ``false``)
 *
 *  **``staticNetwork``** - do not request chain ID on requests to
 *  validate the underlying chain has not changed (default: ``null``)
 *
 *  This should **ONLY** be used if it is **certain** that the network
 *  cannot change, such as when using INFURA (since the URL dictates the
 *  network). If the network is assumed static and it does change, this
 *  can have tragic consequences. For example, this **CANNOT** be used
 *  with MetaMask, since the user can select a new network from the
 *  drop-down at any time.
 *
 *  **``batchStallTime``** - how long (ms) to aggregate requests into a
 *  single batch. ``0`` indicates batching will only encompass the current
 *  event loop. If ``batchMaxCount = 1``, this is ignored. (default: ``10``)
 *
 *  **``batchMaxSize``** - target maximum size (bytes) to allow per batch
 *  request (default: 1Mb)
 *
 *  **``batchMaxCount``** - maximum number of requests to allow in a batch.
 *  If ``batchMaxCount = 1``, then batching is disabled. (default: ``100``)
 *
 *  **``cacheTimeout``** - passed as [[AbstractProviderOptions]].
 */
export type JsonRpcApiProviderOptions = {
    polling?: boolean;
    staticNetwork?: null | boolean | Network;
    batchStallTime?: number;
    batchMaxSize?: number;
    batchMaxCount?: number;
 
    cacheTimeout?: number;
    pollingInterval?: number;
};
 
const defaultOptions = {
    polling: false,
    staticNetwork: null,
 
    batchStallTime: 10,      // 10ms
    batchMaxSize: (1 << 20), // 1Mb
    batchMaxCount: 100,      // 100 requests
 
    cacheTimeout: 250,
    pollingInterval: 4000
}
 
/**
 *  A **JsonRpcTransactionRequest** is formatted as needed by the JSON-RPC
 *  Ethereum API specification.
 */
export interface JsonRpcTransactionRequest {
     /**
      *  The sender address to use when signing.
      */
     from?: string;
 
     /**
      *  The target address.
      */
     to?: string;
 
     /**
      *  The transaction data.
      */
     data?: string;
 
     /**
      *  The chain ID the transaction is valid on.
      */
     chainId?: string;
 
     /**
      *  The [[link-eip-2718]] transaction type.
      */
     type?: string;
 
     /**
      *  The maximum amount of gas to allow a transaction to consume.
      *
      *  In most other places in ethers, this is called ``gasLimit`` which
      *  differs from the JSON-RPC Ethereum API specification.
      */
     gas?: string;
 
     /**
      *  The gas price per wei for transactions prior to [[link-eip-1559]].
      */
     gasPrice?: string;
 
     /**
      *  The maximum fee per gas for [[link-eip-1559]] transactions.
      */
     maxFeePerGas?: string;
 
     /**
      *  The maximum priority fee per gas for [[link-eip-1559]] transactions.
      */
     maxPriorityFeePerGas?: string;
 
     /**
      *  The nonce for the transaction.
      */
     nonce?: string;
 
     /**
      *  The transaction value (in wei).
      */
     value?: string;
 
     /**
      *  The transaction access list.
      */
     accessList?: Array<{ address: string, storageKeys: Array<string> }>;
 
     /**
      *  The transaction authorization list.
      */
     authorizationList?: Array<{
         address: string, nonce: string, chainId: string,
         yParity: string, r: string, s: string
     }>;
}
 
// @TODO: Unchecked Signers
 
export class JsonRpcSigner extends AbstractSigner<JsonRpcApiProvider> {
    address!: string;
 
    constructor(provider: JsonRpcApiProvider, address: string) {
        super(provider);
        address = getAddress(address);
        defineProperties<JsonRpcSigner>(this, { address });
    }
 
    connect(provider: null | Provider): Signer {
        assert(false, "cannot reconnect JsonRpcSigner", "UNSUPPORTED_OPERATION", {
            operation: "signer.connect"
        });
    }
 
    async getAddress(): Promise<string> {
        return this.address;
    }
 
    // JSON-RPC will automatially fill in nonce, etc. so we just check from
    async populateTransaction(tx: TransactionRequest): Promise<TransactionLike<string>> {
        return await this.populateCall(tx);
    }
 
    // Returns just the hash of the transaction after sent, which is what
    // the bare JSON-RPC API does;
    async sendUncheckedTransaction(_tx: TransactionRequest): Promise<string> {
        const tx = deepCopy(_tx);
 
        const promises: Array<Promise<void>> = [];
 
        // Make sure the from matches the sender
        if (tx.from) {
            const _from = tx.from;
            promises.push((async () => {
                const from = await resolveAddress(_from, this.provider);
                assertArgument(from != null && from.toLowerCase() === this.address.toLowerCase(),
                    "from address mismatch", "transaction", _tx);
                tx.from = from;
            })());
        } else {
            tx.from = this.address;
        }
 
        // The JSON-RPC for eth_sendTransaction uses 90000 gas; if the user
        // wishes to use this, it is easy to specify explicitly, otherwise
        // we look it up for them.
        if (tx.gasLimit == null) {
            promises.push((async () => {
                tx.gasLimit = await this.provider.estimateGas({ ...tx, from: this.address});
            })());
        }
 
        // The address may be an ENS name or Addressable
        if (tx.to != null) {
            const _to = tx.to;
            promises.push((async () => {
                tx.to = await resolveAddress(_to, this.provider);
            })());
        }
 
        // Wait until all of our properties are filled in
        if (promises.length) { await Promise.all(promises); }
 
        const hexTx = this.provider.getRpcTransaction(tx);
 
        return this.provider.send("eth_sendTransaction", [ hexTx ]);
    }
 
    async sendTransaction(tx: TransactionRequest): Promise<TransactionResponse> {
        // This cannot be mined any earlier than any recent block
        const blockNumber = await this.provider.getBlockNumber();
 
        // Send the transaction
        const hash = await this.sendUncheckedTransaction(tx);
 
        // Unfortunately, JSON-RPC only provides and opaque transaction hash
        // for a response, and we need the actual transaction, so we poll
        // for it; it should show up very quickly
        return await (new Promise((resolve, reject) => {
            const timeouts = [ 1000, 100 ];
            let invalids = 0;
 
            const checkTx = async () => {
 
                try {
                    // Try getting the transaction
                    const tx = await this.provider.getTransaction(hash);
 
                    if (tx != null) {
                        resolve(tx.replaceableTransaction(blockNumber));
                        return;
                    }
 
                } catch (error) {
 
                    // If we were cancelled: stop polling.
                    // If the data is bad: the node returns bad transactions
                    // If the network changed: calling again will also fail
                    // If unsupported: likely destroyed
                    if (isError(error, "CANCELLED") || isError(error, "BAD_DATA") ||
                        isError(error, "NETWORK_ERROR") || isError(error, "UNSUPPORTED_OPERATION")) {

                        if (error.info == null) { error.info = { }; }
                        error.info.sendTransactionHash = hash;

                        reject(error);
                        return;
                    }
 
                    // Stop-gap for misbehaving backends; see #4513
                    if (isError(error, "INVALID_ARGUMENT")) {
                        invalids++;
                        if (error.info == null) { error.info = { }; }
                        error.info.sendTransactionHash = hash;
                        if (invalids > 10) {
                            reject(error);
                            return;
                        }
                    }
 
                    // Notify anyone that cares; but we will try again, since
                    // it is likely an intermittent service error
                    this.provider.emit("error", makeError("failed to fetch transation after sending (will try again)", "UNKNOWN_ERROR", { error }));
                }
 
                // Wait another 4 seconds
                this.provider._setTimeout(() => { checkTx(); }, timeouts.pop() || 4000);
            };
            checkTx();
        }));
    }
 
    async signTransaction(_tx: TransactionRequest): Promise<string> {
        const tx = deepCopy(_tx);

        // Make sure the from matches the sender
        if (tx.from) {
            const from = await resolveAddress(tx.from, this.provider);
            assertArgument(from != null && from.toLowerCase() === this.address.toLowerCase(),
                "from address mismatch", "transaction", _tx);
            tx.from = from;
        } else {
            tx.from = this.address;
        }

        const hexTx = this.provider.getRpcTransaction(tx);
        return await this.provider.send("eth_signTransaction", [ hexTx ]);
    }
 
 
    async signMessage(_message: string | Uint8Array): Promise<string> {
        const message = ((typeof(_message) === "string") ? toUtf8Bytes(_message): _message);
        return await this.provider.send("personal_sign", [
            hexlify(message), this.address.toLowerCase() ]);
    }
 
    async signTypedData(domain: TypedDataDomain, types: Record<string, Array<TypedDataField>>, _value: Record<string, any>): Promise<string> {
        const value = deepCopy(_value);

        // Populate any ENS names (in-place)
        const populated = await TypedDataEncoder.resolveNames(domain, types, value, async (value: string) => {
            const address = await resolveAddress(value);
            assertArgument(address != null, "TypedData does not support null address", "value", value);
            return address;
        });

        return await this.provider.send("eth_signTypedData_v4", [
            this.address.toLowerCase(),
            JSON.stringify(TypedDataEncoder.getPayload(populated.domain, types, populated.value))
        ]);
    }
 
    async unlock(password: string): Promise<boolean> {
        return this.provider.send("personal_unlockAccount", [
            this.address.toLowerCase(), password, null ]);
    }
 
    // https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign
    async _legacySignMessage(_message: string | Uint8Array): Promise<string> {
        const message = ((typeof(_message) === "string") ? toUtf8Bytes(_message): _message);
        return await this.provider.send("eth_sign", [
            this.address.toLowerCase(), hexlify(message) ]);
    }
}
 
type ResolveFunc = (result: JsonRpcResult) => void;
type RejectFunc = (error: Error) => void;
 
type Payload = { payload: JsonRpcPayload, resolve: ResolveFunc, reject: RejectFunc };
 
/**
 *  The JsonRpcApiProvider is an abstract class and **MUST** be
 *  sub-classed.
 *
 *  It provides the base for all JSON-RPC-based Provider interaction.
 *
 *  Sub-classing Notes:
 *  - a sub-class MUST override _send
 *  - a sub-class MUST call the `_start()` method once connected
 */
export abstract class JsonRpcApiProvider extends AbstractProvider {
 
    #options: Required<JsonRpcApiProviderOptions>;
 
    // The next ID to use for the JSON-RPC ID field
    #nextId: number;
 
    // Payloads are queued and triggered in batches using the drainTimer
    #payloads: Array<Payload>;
    #drainTimer: null | Timer;
 
    #notReady: null | {
        promise: Promise<void>,
        resolve: null | ((v: void) => void)
    };
 
    #network: null | Network;
    #pendingDetectNetwork: null | Promise<Network>;
 
    #scheduleDrain(): void {
        if (this.#drainTimer) { return; }
 
        // If we aren't using batching, no harm in sending it immediately
        const stallTime = (this._getOption("batchMaxCount") === 1) ? 0: this._getOption("batchStallTime");
 
        this.#drainTimer = setTimeout(() => {
            this.#drainTimer = null;
 
            const payloads = this.#payloads;
            this.#payloads = [ ];
 
            while (payloads.length) {
 
                // Create payload batches that satisfy our batch constraints
                const batch = [ <Payload>(payloads.shift()) ];
                while (payloads.length) {
                    if (batch.length === this.#options.batchMaxCount) { break; }
                    batch.push(<Payload>(payloads.shift()));
                    const bytes = JSON.stringify(batch.map((p) => p.payload));
                    if (bytes.length > this.#options.batchMaxSize) {
                        payloads.unshift(<Payload>(batch.pop()));
                        break;
                    }
                }
 
                // Process the result to each payload
                (async () => {
                    const payload = ((batch.length === 1) ? batch[0].payload: batch.map((p) => p.payload));
 
                    this.emit("debug", { action: "sendRpcPayload", payload });
 
                    try {
                        const result = await this._send(payload);
                        this.emit("debug", { action: "receiveRpcResult", result });
 
                        // Process results in batch order
                        for (const { resolve, reject, payload } of batch) {
 
                            if (this.destroyed) {
                                reject(makeError("provider destroyed; cancelled request", "UNSUPPORTED_OPERATION", { operation: payload.method }));
                                continue;
                            }
 
                            // Find the matching result
                            const resp = result.filter((r) => (r.id === payload.id))[0];
 
                            // No result; the node failed us in unexpected ways
                            if (resp == null) {
                                const error = makeError("missing response for request", "BAD_DATA", {
                                    value: result, info: { payload }
                                });
                                this.emit("error", error);
                                reject(error);
                                continue;
                            }
 
                            // The response is an error
                            if ("error" in resp) {
                                reject(this.getRpcError(payload, resp));
                                continue;
                            }
 
                            // All good; send the result
                            resolve(resp.result);
                        }
 
                    } catch (error: any) {
                        this.emit("debug", { action: "receiveRpcError", error });
 
                        for (const { reject } of batch) {
                            // @TODO: augment the error with the payload
                            reject(error);
                        }
                    }
                })();
            }
        }, stallTime);
    }
 
    constructor(network?: Networkish, options?: JsonRpcApiProviderOptions) {
        super(network, options);
 
        this.#nextId = 1;
        this.#options = Object.assign({ }, defaultOptions, options || { });
 
        this.#payloads = [ ];
        this.#drainTimer = null;
 
        this.#network = null;
        this.#pendingDetectNetwork = null;
 
        {
            let resolve: null | ((value: void) => void) = null;
            const promise = new Promise((_resolve: (value: void) => void) => {
                resolve = _resolve;
            });
            this.#notReady = { promise, resolve };
        }
 
        const staticNetwork = this._getOption("staticNetwork");
        if (typeof(staticNetwork) === "boolean") {
            assertArgument(!staticNetwork || network !== "any", "staticNetwork cannot be used on special network 'any'", "options", options);
            if (staticNetwork && network != null) {
                this.#network = Network.from(network);
            }

        } else if (staticNetwork) {
            // Make sure any static network is compatbile with the provided netwrok
            assertArgument(network == null || staticNetwork.matches(network),
                "staticNetwork MUST match network object", "options", options);
            this.#network = staticNetwork;
        }
    }
 
    /**
     *  Returns the value associated with the option %%key%%.
     *
     *  Sub-classes can use this to inquire about configuration options.
     */
    _getOption<K extends keyof JsonRpcApiProviderOptions>(key: K): JsonRpcApiProviderOptions[K] {
        return this.#options[key];
    }
 
    /**
     *  Gets the [[Network]] this provider has committed to. On each call, the network
     *  is detected, and if it has changed, the call will reject.
     */
    get _network(): Network {
        assert (this.#network, "network is not available yet", "NETWORK_ERROR");
        return this.#network;
    }
 
    /**
     *  Sends a JSON-RPC %%payload%% (or a batch) to the underlying channel.
     *
     *  Sub-classes **MUST** override this.
     */
    abstract _send(payload: JsonRpcPayload | Array<JsonRpcPayload>): Promise<Array<JsonRpcResult | JsonRpcError>>;
 
 
    /**
     *  Resolves to the non-normalized value by performing %%req%%.
     *
     *  Sub-classes may override this to modify behavior of actions,
     *  and should generally call ``super._perform`` as a fallback.
     */
    async _perform(req: PerformActionRequest): Promise<any> {
 
        // Legacy networks do not like the type field being passed along (which
        // is fair), so we delete type if it is 0 and a non-EIP-1559 network
        if (req.method === "call" || req.method === "estimateGas") {
            let tx = req.transaction;
            if (tx && tx.type != null && getBigInt(tx.type)) {
                // If there are no EIP-1559 or newer properties, it might be pre-EIP-1559
                if (tx.maxFeePerGas == null && tx.maxPriorityFeePerGas == null) {
                    const feeData = await this.getFeeData();
                    if (feeData.maxFeePerGas == null && feeData.maxPriorityFeePerGas == null) {
                        // Network doesn't know about EIP-1559 (and hence type)
                        req = Object.assign({ }, req, {
                            transaction: Object.assign({ }, tx, { type: undefined })
                        });
                    }
                }
            }
        }
 
        const request = this.getRpcRequest(req);
 
        if (request != null) {
            return await this.send(request.method, request.args);
        }

        return super._perform(req);
    }
 
    /**
     *  Sub-classes may override this; it detects the *actual* network that
     *  we are **currently** connected to.
     *
     *  Keep in mind that [[send]] may only be used once [[ready]], otherwise the
     *  _send primitive must be used instead.
     */
    async _detectNetwork(): Promise<Network> {
        const network = this._getOption("staticNetwork");
        if (network) {
            if (network === true) {
                if (this.#network) { return this.#network; }
            } else {
                return network;
            }
        }
 
        if (this.#pendingDetectNetwork) {
            return await this.#pendingDetectNetwork;
        }
 
        // If we are ready, use ``send``, which enabled requests to be batched
        if (this.ready) {
            this.#pendingDetectNetwork = (async () => {
                try {
                    const result = Network.from(getBigInt(await this.send("eth_chainId", [ ])));
                    this.#pendingDetectNetwork = null;
                    return result;
                } catch (error) {
                    this.#pendingDetectNetwork = null;
                    throw error;
                }
            })();
            return await this.#pendingDetectNetwork;
        }

        // We are not ready yet; use the primitive _send
        this.#pendingDetectNetwork = (async () => {
            const payload: JsonRpcPayload = {
                id: this.#nextId++, method: "eth_chainId", params: [ ], jsonrpc: "2.0"
            };

            this.emit("debug", { action: "sendRpcPayload", payload });

            let result: JsonRpcResult | JsonRpcError;
            try {
                result = (await this._send(payload))[0];
                this.#pendingDetectNetwork = null;
            } catch (error) {
                this.#pendingDetectNetwork = null;
                this.emit("debug", { action: "receiveRpcError", error });
                throw error;
            }

            this.emit("debug", { action: "receiveRpcResult", result });

            if ("result" in result) {
                return Network.from(getBigInt(result.result));
            }

            throw this.getRpcError(payload, result);
        })();

        return await this.#pendingDetectNetwork;
    }
 
    /**
     *  Sub-classes **MUST** call this. Until [[_start]] has been called, no calls
     *  will be passed to [[_send]] from [[send]]. If it is overridden, then
     *  ``super._start()`` **MUST** be called.
     *
     *  Calling it multiple times is safe and has no effect.
     */
    _start(): void {
        if (this.#notReady == null || this.#notReady.resolve == null) { return; }
 
        this.#notReady.resolve();
        this.#notReady = null;
 
        (async () => {
 
            // Bootstrap the network
            while (this.#network == null && !this.destroyed) {
                try {
                    this.#network = await this._detectNetwork();
                } catch (error) {
                    if (this.destroyed) { break; }
                    console.log("JsonRpcProvider failed to detect network and cannot start up; retry in 1s (perhaps the URL is wrong or the node is not started)");
                    this.emit("error", makeError("failed to bootstrap network detection", "NETWORK_ERROR", { event: "initial-network-discovery", info: { error } }));
                    await stall(1000);
                }
            }
 
            // Start dispatching requests
            this.#scheduleDrain();
        })();
    }
 
    /**
     *  Resolves once the [[_start]] has been called. This can be used in
     *  sub-classes to defer sending data until the connection has been
     *  established.
     */
    async _waitUntilReady(): Promise<void> {
        if (this.#notReady == null) { return; }
        return await this.#notReady.promise;
    }
 
 
    /**
     *  Return a Subscriber that will manage the %%sub%%.
     *
     *  Sub-classes may override this to modify the behavior of
     *  subscription management.
     */
    _getSubscriber(sub: Subscription): Subscriber {
 
        // Pending Filters aren't availble via polling
        if (sub.type === "pending") { return new FilterIdPendingSubscriber(this); }
 
        if (sub.type === "event") {
            if (this._getOption("polling")) {
                return new PollingEventSubscriber(this, sub.filter);
            }
            return new FilterIdEventSubscriber(this, sub.filter);
        }
 
        // Orphaned Logs are handled automatically, by the filter, since
        // logs with removed are emitted by it
        if (sub.type === "orphan" && sub.filter.orphan === "drop-log") {
            return new UnmanagedSubscriber("orphan");
        }
 
        return super._getSubscriber(sub);
    }
 
    /**
     *  Returns true only if the [[_start]] has been called.
     */
    get ready(): boolean { return this.#notReady == null; }
 
    /**
     *  Returns %%tx%% as a normalized JSON-RPC transaction request,
     *  which has all values hexlified and any numeric values converted
     *  to Quantity values.
     */
    getRpcTransaction(tx: TransactionRequest): JsonRpcTransactionRequest {
        const result: JsonRpcTransactionRequest = {};
 
        // JSON-RPC now requires numeric values to be "quantity" values
        ["chainId", "gasLimit", "gasPrice", "type", "maxFeePerGas", "maxPriorityFeePerGas", "nonce", "value"].forEach((key) => {
            if ((<any>tx)[key] == null) { return; }
            let dstKey = key;
            if (key === "gasLimit") { dstKey = "gas"; }
            (<any>result)[dstKey] = toQuantity(getBigInt((<any>tx)[key], `tx.${ key }`));
        });
 
        // Make sure addresses and data are lowercase
        ["from", "to", "data"].forEach((key) => {
            if ((<any>tx)[key] == null) { return; }
            (<any>result)[key] = hexlify((<any>tx)[key]);
        });
 
        // Normalize the access list object
        if (tx.accessList) {
            result["accessList"] = accessListify(tx.accessList);
        }
 
        if (tx.blobVersionedHashes) {
            // @TODO: Remove this <any> case once EIP-4844 added to prepared tx
            (<any>result)["blobVersionedHashes"] = tx.blobVersionedHashes.map(h => h.toLowerCase());
        }
 
        if (tx.authorizationList) {
            result["authorizationList"] = tx.authorizationList.map((_a) => {
                const a = authorizationify(_a);
                return {
                    address: a.address,
                    nonce: toQuantity(a.nonce),
                    chainId: toQuantity(a.chainId),
                    yParity: toQuantity(a.signature.yParity),
                    r: a.signature.r,
                    s: a.signature.s,
                }
            });
        }
 
        // @TODO: blobs should probably also be copied over, optionally
        // accounting for the kzg property to backfill blobVersionedHashes
        // using the commitment. Or should that be left as an exercise to
        // the caller?
 
        return result;
    }
 
    /**
     *  Returns the request method and arguments required to perform
     *  %%req%%.
     */
    getRpcRequest(req: PerformActionRequest): null | { method: string, args: Array<any> } {
        switch (req.method) {
            case "chainId":
                return { method: "eth_chainId", args: [ ] };
 
            case "getBlockNumber":
                return { method: "eth_blockNumber", args: [ ] };
 
            case "getGasPrice":
                return { method: "eth_gasPrice", args: [] };
 
            case "getPriorityFee":
                return { method: "eth_maxPriorityFeePerGas", args: [ ] };
 
            case "getBalance":
                return {
                    method: "eth_getBalance",
                    args: [ getLowerCase(req.address), req.blockTag ]
                };
 
            case "getTransactionCount":
                return {
                    method: "eth_getTransactionCount",
                    args: [ getLowerCase(req.address), req.blockTag ]
                };
 
            case "getCode":
                return {
                    method: "eth_getCode",
                    args: [ getLowerCase(req.address), req.blockTag ]
                };
 
            case "getStorage":
                return {
                    method: "eth_getStorageAt",
                    args: [
                        getLowerCase(req.address),
                        ("0x" + req.position.toString(16)),
                        req.blockTag
                    ]
                };
 
            case "broadcastTransaction":
                return {
                    method: "eth_sendRawTransaction",
                    args: [ req.signedTransaction ]
                };
 
            case "getBlock":
                if ("blockTag" in req) {
                    return {
                        method: "eth_getBlockByNumber",
                        args: [ req.blockTag, !!req.includeTransactions ]
                    };
                } else if ("blockHash" in req) {
                    return {
                        method: "eth_getBlockByHash",
                        args: [ req.blockHash, !!req.includeTransactions ]
                    };
                }
                break;
 
            case "getTransaction":
                return {
                    method: "eth_getTransactionByHash",
                    args: [ req.hash ]
                };
 
            case "getTransactionReceipt":
                return {
                    method: "eth_getTransactionReceipt",
                    args: [ req.hash ]
                };
 
            case "call":
                return {
                    method: "eth_call",
                    args: [ this.getRpcTransaction(req.transaction), req.blockTag ]
                };
 
            case "estimateGas": {
                return {
                    method: "eth_estimateGas",
                    args: [ this.getRpcTransaction(req.transaction) ]
                };
            }
 
            case "getLogs":
                if (req.filter && req.filter.address != null) {
                    if (Array.isArray(req.filter.address)) {
                        req.filter.address = req.filter.address.map(getLowerCase);
                    } else {
                        req.filter.address = getLowerCase(req.filter.address);
                    }
                }
                return { method: "eth_getLogs", args: [ req.filter ] };
        }

        return null;
    }
 
    /**
     *  Returns an ethers-style Error for the given JSON-RPC error
     *  %%payload%%, coalescing the various strings and error shapes
     *  that different nodes return, coercing them into a machine-readable
     *  standardized error.
     */
    getRpcError(payload: JsonRpcPayload, _error: JsonRpcError): Error {
        const { method } = payload;
        const { error } = _error;
 
        if (method === "eth_estimateGas" && error.message) {
            const msg = error.message;
            if (!msg.match(/revert/i) && msg.match(/insufficient funds/i)) {
                return makeError("insufficient funds", "INSUFFICIENT_FUNDS", {
                    transaction: ((<any>payload).params[0]),
                    info: { payload, error }
                });
            } else if (msg.match(/nonce/i) && msg.match(/too low/i)) {
                return makeError("nonce has already been used", "NONCE_EXPIRED", {
                    transaction: ((<any>payload).params[0]),
                    info: { payload, error }
                });
            }
        }
 
        if (method === "eth_call" || method === "eth_estimateGas") {
            const result = spelunkData(error);
 
            const e = AbiCoder.getBuiltinCallException(
                (method === "eth_call") ? "call": "estimateGas",
                ((<any>payload).params[0]),
                (result ? result.data: null)
            );
            e.info = { error, payload };
            return e;
        }
 
        // Only estimateGas and call can return arbitrary contract-defined text, so now we
        // we can process text safely.
 
        const message = JSON.stringify(spelunkMessage(error));
 
        if (typeof(error.message) === "string" && error.message.match(/user denied|ethers-user-denied/i)) {
            const actionMap: Record<string, "requestAccess" | "sendTransaction" | "signMessage" | "signTransaction" | "signTypedData"> = {
                eth_sign: "signMessage",
                personal_sign: "signMessage",
                eth_signTypedData_v4: "signTypedData",
                eth_signTransaction: "signTransaction",
                eth_sendTransaction: "sendTransaction",
                eth_requestAccounts: "requestAccess",
                wallet_requestAccounts: "requestAccess",
            };

            return makeError(`user rejected action`, "ACTION_REJECTED", {
                action: (actionMap[method] || "unknown") ,
                reason: "rejected",
                info: { payload, error }
            });
        }
 
        if (method === "eth_sendRawTransaction" || method === "eth_sendTransaction") {
            const transaction = <TransactionLike<string>>((<any>payload).params[0]);
 
            if (message.match(/insufficient funds|base fee exceeds gas limit/i)) {
                return makeError("insufficient funds for intrinsic transaction cost", "INSUFFICIENT_FUNDS", {
                    transaction, info: { error }
                });
            }
 
            if (message.match(/nonce/i) && message.match(/too low/i)) {
                return makeError("nonce has already been used", "NONCE_EXPIRED", { transaction, info: { error } });
            }
 
            // "replacement transaction underpriced"
            if (message.match(/replacement transaction/i) && message.match(/underpriced/i)) {
                return makeError("replacement fee too low", "REPLACEMENT_UNDERPRICED", { transaction, info: { error } });
            }
 
            if (message.match(/only replay-protected/i)) {
                return makeError("legacy pre-eip-155 transactions not supported", "UNSUPPORTED_OPERATION", {
                    operation: method, info: { transaction, info: { error } }
                });
            }
        }
 
        let unsupported = !!message.match(/the method .* does not exist/i);
        if (!unsupported) {
            if (error && (<any>error).details && (<any>error).details.startsWith("Unauthorized method:")) {
                unsupported = true;
            }
        }
 
        if (unsupported) {
            return makeError("unsupported operation", "UNSUPPORTED_OPERATION", {
                operation: payload.method, info: { error, payload }
            });
        }
 
        return makeError("could not coalesce error", "UNKNOWN_ERROR", { error, payload });
    }
 
 
    /**
     *  Requests the %%method%% with %%params%% via the JSON-RPC protocol
     *  over the underlying channel. This can be used to call methods
     *  on the backend that do not have a high-level API within the Provider
     *  API.
     *
     *  This method queues requests according to the batch constraints
     *  in the options, assigns the request a unique ID.
     *
     *  **Do NOT override** this method in sub-classes; instead
     *  override [[_send]] or force the options values in the
     *  call to the constructor to modify this method's behavior.
     */
    send(method: string, params: Array<any> | Record<string, any>): Promise<any> {
        // @TODO: cache chainId?? purge on switch_networks
 
        // We have been destroyed; no operations are supported anymore
        if (this.destroyed) {
            return Promise.reject(makeError("provider destroyed; cancelled request", "UNSUPPORTED_OPERATION", { operation: method }));
        }
 
        const id = this.#nextId++;
        const promise = new Promise((resolve, reject) => {
            this.#payloads.push({
                resolve, reject,
                payload: { method, params, id, jsonrpc: "2.0" }
            });
        });
 
        // If there is not a pending drainTimer, set one
        this.#scheduleDrain();
 
        return <Promise<JsonRpcResult>>promise;
    }
 
    /**
     *  Resolves to the [[Signer]] account for  %%address%% managed by
     *  the client.
     *
     *  If the %%address%% is a number, it is used as an index in the
     *  the accounts from [[listAccounts]].
     *
     *  This can only be used on clients which manage accounts (such as
     *  Geth with imported account or MetaMask).
     *
     *  Throws if the account doesn't exist.
     */
    async getSigner(address?: number | string): Promise<JsonRpcSigner> {
        if (address == null) { address = 0; }
 
        const accountsPromise = this.send("eth_accounts", [ ]);
 
        // Account index
        if (typeof(address) === "number") {
            const accounts = <Array<string>>(await accountsPromise);
            if (address >= accounts.length) { throw new Error("no such account"); }
            return new JsonRpcSigner(this, accounts[address]);
        }

        const { accounts } = await resolveProperties({
            network: this.getNetwork(),
            accounts: accountsPromise
        });

        // Account address
        address = getAddress(address);
        for (const account of accounts) {
            if (getAddress(account) === address) {
                return new JsonRpcSigner(this, address);
            }
        }

        throw new Error("invalid account");
    }
 
    async listAccounts(): Promise<Array<JsonRpcSigner>> {
        const accounts: Array<string> = await this.send("eth_accounts", [ ]);
        return accounts.map((a) => new JsonRpcSigner(this, a));
    }
 
    destroy(): void {
 
        // Stop processing requests
        if (this.#drainTimer) {
            clearTimeout(this.#drainTimer);
            this.#drainTimer = null;
        }
 
        // Cancel all pending requests
        for (const { payload, reject } of this.#payloads) {
            reject(makeError("provider destroyed; cancelled request", "UNSUPPORTED_OPERATION", { operation: payload.method }));
        }
 
        this.#payloads = [ ];
 
        // Parent clean-up
        super.destroy();
 
    }
}
 
// @TODO: remove this in v7, it is not exported because this functionality
// is exposed in the JsonRpcApiProvider by setting polling to true. It should
// be safe to remove regardless, because it isn't reachable, but just in case.
/**
 *  @_ignore:
 */
export abstract class JsonRpcApiPollingProvider extends JsonRpcApiProvider {
    #pollingInterval: number;
    constructor(network?: Networkish, options?: JsonRpcApiProviderOptions) {
        super(network, options);
 
        let pollingInterval = this._getOption("pollingInterval");
        if (pollingInterval == null) { pollingInterval = defaultOptions.pollingInterval; }
 
        this.#pollingInterval = pollingInterval;
    }
 
    _getSubscriber(sub: Subscription): Subscriber {
        const subscriber = super._getSubscriber(sub);
        if (isPollable(subscriber)) {
            subscriber.pollingInterval = this.#pollingInterval;
        }
        return subscriber;
    }
 
    /**
     *  The polling interval (default: 4000 ms)
     */
    get pollingInterval(): number { return this.#pollingInterval; }
    set pollingInterval(value: number) {
        if (!Number.isInteger(value) || value < 0) { throw new Error("invalid interval"); }
        this.#pollingInterval = value;
        this._forEachSubscriber((sub) => {
            if (isPollable(sub)) {
                sub.pollingInterval = this.#pollingInterval;
            }
        });
    }
}
 
/**
 *  The JsonRpcProvider is one of the most common Providers,
 *  which performs all operations over HTTP (or HTTPS) requests.
 *
 *  Events are processed by polling the backend for the current block
 *  number; when it advances, all block-base events are then checked
 *  for updates.
 */
export class JsonRpcProvider extends JsonRpcApiPollingProvider {
    #connect: FetchRequest;
 
    constructor(url?: string | FetchRequest, network?: Networkish, options?: JsonRpcApiProviderOptions) {
        if (url == null) { url = "http:/\/localhost:8545"; }
        super(network, options);
 
        if (typeof(url) === "string") {
            this.#connect = new FetchRequest(url);
        } else {
            this.#connect = url.clone();
        }
    }
 
    _getConnection(): FetchRequest {
        return this.#connect.clone();
    }
 
    async send(method: string, params: Array<any> | Record<string, any>): Promise<any> {
        // All requests are over HTTP, so we can just start handling requests
        // We do this here rather than the constructor so that we don't send any
        // requests to the network (i.e. eth_chainId) until we absolutely have to.
        await this._start();
 
        return await super.send(method, params);
    }
 
    async _send(payload: JsonRpcPayload | Array<JsonRpcPayload>): Promise<Array<JsonRpcResult>> {
        // Configure a POST connection for the requested method
        const request = this._getConnection();
        request.body = JSON.stringify(payload);
        request.setHeader("content-type", "application/json");
        const response = await request.send();
        response.assertOk();
 
        let resp = response.bodyJson;
        if (!Array.isArray(resp)) { resp = [ resp ]; }
 
        return resp;
    }
}
 
function spelunkData(value: any): null | { message: string, data: string } {
    if (value == null) { return null; }
 
    // These *are* the droids we're looking for.
    if (typeof(value.message) === "string" && value.message.match(/revert/i) && isHexString(value.data)) {
        return { message: value.message, data: value.data };
    }
 
    // Spelunk further...
    if (typeof(value) === "object") {
        for (const key in value) {
            const result = spelunkData(value[key]);
            if (result) { return result; }
        }
        return null;
    }
 
    // Might be a JSON string we can further descend...
    if (typeof(value) === "string") {
        try {
            return spelunkData(JSON.parse(value));
        } catch (error) { }
    }
 
    return null;
}
 
function _spelunkMessage(value: any, result: Array<string>): void {
    if (value == null) { return; }
 
    // These *are* the droids we're looking for.
    if (typeof(value.message) === "string") {
        result.push(value.message);
    }
 
    // Spelunk further...
    if (typeof(value) === "object") {
        for (const key in value) {
            _spelunkMessage(value[key], result);
        }
    }
 
    // Might be a JSON string we can further descend...
    if (typeof(value) === "string") {
        try {
            return _spelunkMessage(JSON.parse(value), result);
        } catch (error) { }
    }
}
 
function spelunkMessage(value: any): Array<string> {
    const result: Array<string> = [ ];
    _spelunkMessage(value, result);
    return result;
}