@zill-protocol/client 4.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/dist/index.d.ts +2 -0
  3. package/dist/index.d.ts.map +1 -0
  4. package/dist/index.js +18 -0
  5. package/dist/index.js.map +1 -0
  6. package/dist/package.json +58 -0
  7. package/dist/src/NocturneClient.d.ts +68 -0
  8. package/dist/src/NocturneClient.d.ts.map +1 -0
  9. package/dist/src/NocturneClient.js +264 -0
  10. package/dist/src/NocturneClient.js.map +1 -0
  11. package/dist/src/NocturneDB.d.ts +100 -0
  12. package/dist/src/NocturneDB.d.ts.map +1 -0
  13. package/dist/src/NocturneDB.js +525 -0
  14. package/dist/src/NocturneDB.js.map +1 -0
  15. package/dist/src/OpTracker.d.ts +13 -0
  16. package/dist/src/OpTracker.d.ts.map +1 -0
  17. package/dist/src/OpTracker.js +34 -0
  18. package/dist/src/OpTracker.js.map +1 -0
  19. package/dist/src/conversion/converter.d.ts +5 -0
  20. package/dist/src/conversion/converter.d.ts.map +1 -0
  21. package/dist/src/conversion/converter.js +15 -0
  22. package/dist/src/conversion/converter.js.map +1 -0
  23. package/dist/src/conversion/index.d.ts +3 -0
  24. package/dist/src/conversion/index.d.ts.map +1 -0
  25. package/dist/src/conversion/index.js +21 -0
  26. package/dist/src/conversion/index.js.map +1 -0
  27. package/dist/src/conversion/mock.d.ts +6 -0
  28. package/dist/src/conversion/mock.d.ts.map +1 -0
  29. package/dist/src/conversion/mock.js +14 -0
  30. package/dist/src/conversion/mock.js.map +1 -0
  31. package/dist/src/index.d.ts +14 -0
  32. package/dist/src/index.d.ts.map +1 -0
  33. package/dist/src/index.js +39 -0
  34. package/dist/src/index.js.map +1 -0
  35. package/dist/src/opRequestGas.d.ts +20 -0
  36. package/dist/src/opRequestGas.d.ts.map +1 -0
  37. package/dist/src/opRequestGas.js +321 -0
  38. package/dist/src/opRequestGas.js.map +1 -0
  39. package/dist/src/operationRequest/builder.d.ts +40 -0
  40. package/dist/src/operationRequest/builder.d.ts.map +1 -0
  41. package/dist/src/operationRequest/builder.js +192 -0
  42. package/dist/src/operationRequest/builder.js.map +1 -0
  43. package/dist/src/operationRequest/index.d.ts +3 -0
  44. package/dist/src/operationRequest/index.d.ts.map +1 -0
  45. package/dist/src/operationRequest/index.js +6 -0
  46. package/dist/src/operationRequest/index.js.map +1 -0
  47. package/dist/src/operationRequest/operationRequest.d.ts +50 -0
  48. package/dist/src/operationRequest/operationRequest.d.ts.map +1 -0
  49. package/dist/src/operationRequest/operationRequest.js +16 -0
  50. package/dist/src/operationRequest/operationRequest.js.map +1 -0
  51. package/dist/src/prepareOperation.d.ts +21 -0
  52. package/dist/src/prepareOperation.d.ts.map +1 -0
  53. package/dist/src/prepareOperation.js +256 -0
  54. package/dist/src/prepareOperation.js.map +1 -0
  55. package/dist/src/proveOperation.d.ts +7 -0
  56. package/dist/src/proveOperation.d.ts.map +1 -0
  57. package/dist/src/proveOperation.js +79 -0
  58. package/dist/src/proveOperation.js.map +1 -0
  59. package/dist/src/signOperation.d.ts +3 -0
  60. package/dist/src/signOperation.d.ts.map +1 -0
  61. package/dist/src/signOperation.js +61 -0
  62. package/dist/src/signOperation.js.map +1 -0
  63. package/dist/src/snapJsonRpc.d.ts +55 -0
  64. package/dist/src/snapJsonRpc.d.ts.map +1 -0
  65. package/dist/src/snapJsonRpc.js +63 -0
  66. package/dist/src/snapJsonRpc.js.map +1 -0
  67. package/dist/src/syncSDK.d.ts +17 -0
  68. package/dist/src/syncSDK.d.ts.map +1 -0
  69. package/dist/src/syncSDK.js +188 -0
  70. package/dist/src/syncSDK.js.map +1 -0
  71. package/dist/src/types.d.ts +60 -0
  72. package/dist/src/types.d.ts.map +1 -0
  73. package/dist/src/types.js +3 -0
  74. package/dist/src/types.js.map +1 -0
  75. package/dist/src/utils/constants.d.ts +3 -0
  76. package/dist/src/utils/constants.d.ts.map +1 -0
  77. package/dist/src/utils/constants.js +20 -0
  78. package/dist/src/utils/constants.js.map +1 -0
  79. package/dist/src/utils/index.d.ts +3 -0
  80. package/dist/src/utils/index.d.ts.map +1 -0
  81. package/dist/src/utils/index.js +19 -0
  82. package/dist/src/utils/index.js.map +1 -0
  83. package/dist/src/utils/misc.d.ts +13 -0
  84. package/dist/src/utils/misc.d.ts.map +1 -0
  85. package/dist/src/utils/misc.js +77 -0
  86. package/dist/src/utils/misc.js.map +1 -0
  87. package/dist/tsconfig.tsbuildinfo +1 -0
  88. package/package.json +58 -0
  89. package/src/NocturneClient.ts +415 -0
  90. package/src/NocturneDB.ts +761 -0
  91. package/src/OpTracker.ts +44 -0
  92. package/src/conversion/converter.ts +22 -0
  93. package/src/conversion/index.ts +2 -0
  94. package/src/conversion/mock.ts +11 -0
  95. package/src/index.ts +14 -0
  96. package/src/opRequestGas.ts +487 -0
  97. package/src/operationRequest/builder.ts +359 -0
  98. package/src/operationRequest/index.ts +16 -0
  99. package/src/operationRequest/operationRequest.ts +87 -0
  100. package/src/prepareOperation.ts +420 -0
  101. package/src/proveOperation.ts +124 -0
  102. package/src/signOperation.ts +116 -0
  103. package/src/snapJsonRpc.ts +109 -0
  104. package/src/syncSDK.ts +285 -0
  105. package/src/types.ts +83 -0
  106. package/src/utils/constants.ts +16 -0
  107. package/src/utils/index.ts +2 -0
  108. package/src/utils/misc.ts +107 -0
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.MockEthToTokenConverter = void 0;
18
+ __exportStar(require("./converter"), exports);
19
+ var mock_1 = require("./mock");
20
+ Object.defineProperty(exports, "MockEthToTokenConverter", { enumerable: true, get: function () { return mock_1.MockEthToTokenConverter; } });
21
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/conversion/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,8CAA4B;AAC5B,+BAAiD;AAAxC,+GAAA,uBAAuB,OAAA"}
@@ -0,0 +1,6 @@
1
+ import { EthToTokenConverter } from "./converter";
2
+ export declare class MockEthToTokenConverter extends EthToTokenConverter {
3
+ constructor();
4
+ weiToTargetErc20(amountWei: bigint, _targetTicker: string): Promise<bigint>;
5
+ }
6
+ //# sourceMappingURL=mock.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock.d.ts","sourceRoot":"","sources":["../../../src/conversion/mock.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,qBAAa,uBAAwB,SAAQ,mBAAmB;;IAK9D,gBAAgB,CAAC,SAAS,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;CAG5E"}
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MockEthToTokenConverter = void 0;
4
+ const converter_1 = require("./converter");
5
+ class MockEthToTokenConverter extends converter_1.EthToTokenConverter {
6
+ constructor() {
7
+ super();
8
+ }
9
+ weiToTargetErc20(amountWei, _targetTicker) {
10
+ return Promise.resolve(amountWei);
11
+ }
12
+ }
13
+ exports.MockEthToTokenConverter = MockEthToTokenConverter;
14
+ //# sourceMappingURL=mock.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mock.js","sourceRoot":"","sources":["../../../src/conversion/mock.ts"],"names":[],"mappings":";;;AAAA,2CAAkD;AAElD,MAAa,uBAAwB,SAAQ,+BAAmB;IAC9D;QACE,KAAK,EAAE,CAAC;IACV,CAAC;IAED,gBAAgB,CAAC,SAAiB,EAAE,aAAqB;QACvD,OAAO,OAAO,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC;IACpC,CAAC;CACF;AARD,0DAQC"}
@@ -0,0 +1,14 @@
1
+ export * from "./types";
2
+ export * from "./conversion";
3
+ export * from "./operationRequest";
4
+ export * from "./snapJsonRpc";
5
+ export { NocturneClient } from "./NocturneClient";
6
+ export { NocturneDB, GetNotesOpts } from "./NocturneDB";
7
+ export { SyncOpts } from "./syncSDK";
8
+ export { BundlerOpTracker } from "./OpTracker";
9
+ export { NotEnoughGasTokensError } from "./opRequestGas";
10
+ export { NotEnoughFundsError } from "./prepareOperation";
11
+ export { signOperation } from "./signOperation";
12
+ export { proveOperation } from "./proveOperation";
13
+ export { isTerminalOpStatus, isFailedOpStatus } from "./utils";
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,oBAAoB,CAAC;AACnC,cAAc,eAAe,CAAC;AAE9B,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAC;AACrC,OAAO,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,EAAE,uBAAuB,EAAE,MAAM,gBAAgB,CAAC;AACzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAC"}
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.isFailedOpStatus = exports.isTerminalOpStatus = exports.proveOperation = exports.signOperation = exports.NotEnoughFundsError = exports.NotEnoughGasTokensError = exports.BundlerOpTracker = exports.NocturneDB = exports.NocturneClient = void 0;
18
+ __exportStar(require("./types"), exports);
19
+ __exportStar(require("./conversion"), exports);
20
+ __exportStar(require("./operationRequest"), exports);
21
+ __exportStar(require("./snapJsonRpc"), exports);
22
+ var NocturneClient_1 = require("./NocturneClient");
23
+ Object.defineProperty(exports, "NocturneClient", { enumerable: true, get: function () { return NocturneClient_1.NocturneClient; } });
24
+ var NocturneDB_1 = require("./NocturneDB");
25
+ Object.defineProperty(exports, "NocturneDB", { enumerable: true, get: function () { return NocturneDB_1.NocturneDB; } });
26
+ var OpTracker_1 = require("./OpTracker");
27
+ Object.defineProperty(exports, "BundlerOpTracker", { enumerable: true, get: function () { return OpTracker_1.BundlerOpTracker; } });
28
+ var opRequestGas_1 = require("./opRequestGas");
29
+ Object.defineProperty(exports, "NotEnoughGasTokensError", { enumerable: true, get: function () { return opRequestGas_1.NotEnoughGasTokensError; } });
30
+ var prepareOperation_1 = require("./prepareOperation");
31
+ Object.defineProperty(exports, "NotEnoughFundsError", { enumerable: true, get: function () { return prepareOperation_1.NotEnoughFundsError; } });
32
+ var signOperation_1 = require("./signOperation");
33
+ Object.defineProperty(exports, "signOperation", { enumerable: true, get: function () { return signOperation_1.signOperation; } });
34
+ var proveOperation_1 = require("./proveOperation");
35
+ Object.defineProperty(exports, "proveOperation", { enumerable: true, get: function () { return proveOperation_1.proveOperation; } });
36
+ var utils_1 = require("./utils");
37
+ Object.defineProperty(exports, "isTerminalOpStatus", { enumerable: true, get: function () { return utils_1.isTerminalOpStatus; } });
38
+ Object.defineProperty(exports, "isFailedOpStatus", { enumerable: true, get: function () { return utils_1.isFailedOpStatus; } });
39
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,0CAAwB;AACxB,+CAA6B;AAC7B,qDAAmC;AACnC,gDAA8B;AAE9B,mDAAkD;AAAzC,gHAAA,cAAc,OAAA;AACvB,2CAAwD;AAA/C,wGAAA,UAAU,OAAA;AAEnB,yCAA+C;AAAtC,6GAAA,gBAAgB,OAAA;AACzB,+CAAyD;AAAhD,uHAAA,uBAAuB,OAAA;AAChC,uDAAyD;AAAhD,uHAAA,mBAAmB,OAAA;AAC5B,iDAAgD;AAAvC,8GAAA,aAAa,OAAA;AACtB,mDAAkD;AAAzC,gHAAA,cAAc,OAAA;AACvB,iCAA+D;AAAtD,2GAAA,kBAAkB,OAAA;AAAE,yGAAA,gBAAgB,OAAA"}
@@ -0,0 +1,20 @@
1
+ import { Handler } from "@zill-protocol/contracts";
2
+ import { Asset, SparseMerkleProver } from "@zill-protocol/core";
3
+ import { NocturneDB } from "./NocturneDB";
4
+ import { EthToTokenConverter } from "./conversion";
5
+ import { GasAccountedOperationRequest, OperationRequest } from "./operationRequest/operationRequest";
6
+ export interface HandleOpRequestGasDeps {
7
+ db: NocturneDB;
8
+ handlerContract: Handler;
9
+ gasAssets: Map<string, Asset>;
10
+ tokenConverter: EthToTokenConverter;
11
+ merkle: SparseMerkleProver;
12
+ }
13
+ export declare class NotEnoughGasTokensError extends Error {
14
+ readonly gasAssets: Asset[];
15
+ readonly gasEstimates: bigint[];
16
+ readonly gasAssetBalances: bigint[];
17
+ constructor(gasAssets: Asset[], gasEstimates: bigint[], gasAssetBalances: bigint[]);
18
+ }
19
+ export declare function handleGasForOperationRequest(deps: HandleOpRequestGasDeps, opRequest: OperationRequest, gasMultiplier: number): Promise<GasAccountedOperationRequest>;
20
+ //# sourceMappingURL=opRequestGas.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opRequestGas.d.ts","sourceRoot":"","sources":["../../src/opRequestGas.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAC;AACnD,OAAO,EACL,KAAK,EAaL,kBAAkB,EAKnB,MAAM,qBAAqB,CAAC;AAG7B,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,EACL,4BAA4B,EAE5B,gBAAgB,EACjB,MAAM,qCAAqC,CAAC;AAoB7C,MAAM,WAAW,sBAAsB;IACrC,EAAE,EAAE,UAAU,CAAC;IACf,eAAe,EAAE,OAAO,CAAC;IACzB,SAAS,EAAE,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IAC9B,cAAc,EAAE,mBAAmB,CAAC;IACpC,MAAM,EAAE,kBAAkB,CAAC;CAC5B;AAwBD,qBAAa,uBAAwB,SAAQ,KAAK;aAE9B,SAAS,EAAE,KAAK,EAAE;aAClB,YAAY,EAAE,MAAM,EAAE;aACtB,gBAAgB,EAAE,MAAM,EAAE;gBAF1B,SAAS,EAAE,KAAK,EAAE,EAClB,YAAY,EAAE,MAAM,EAAE,EACtB,gBAAgB,EAAE,MAAM,EAAE;CAK7C;AAED,wBAAsB,4BAA4B,CAChD,IAAI,EAAE,sBAAsB,EAC5B,SAAS,EAAE,gBAAgB,EAC3B,aAAa,EAAE,MAAM,GACpB,OAAO,CAAC,4BAA4B,CAAC,CAmDvC"}
@@ -0,0 +1,321 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.NotEnoughGasTokensError = void 0;
37
+ exports.handleGasForOperationRequest = handleGasForOperationRequest;
38
+ const core_1 = require("@zill-protocol/core");
39
+ const crypto_1 = require("@zill-protocol/crypto");
40
+ const JSON = __importStar(require("bigint-json-serialization"));
41
+ const prepareOperation_1 = require("./prepareOperation");
42
+ const utils_1 = require("./utils");
43
+ // If gas asset refund is less than this amount * gasPrice denominated in the gas asset, refund will
44
+ // not be processed and funds will be sent to bundler. This is because cost of processing would
45
+ // outweight value of note.
46
+ const DEFAULT_GAS_ASSET_REFUND_THRESHOLD_GAS = 600000n;
47
+ const DUMMY_GAS_ASSET = {
48
+ assetType: core_1.AssetType.ERC20,
49
+ assetAddr: "0x0000000000000000000000000000000000000000",
50
+ id: core_1.ERC20_ID,
51
+ };
52
+ // VK corresponding to SK of 1 with minimum valid nonce
53
+ const DUMMY_VIEWER = new crypto_1.NocturneViewer(655374300543486358510310527362452574140738137308572239595396943710924175576n, 3n);
54
+ const DUMMY_REFUND_ADDR = DUMMY_VIEWER.generateRandomStealthAddress();
55
+ class NotEnoughGasTokensError extends Error {
56
+ constructor(gasAssets, gasEstimates, gasAssetBalances) {
57
+ super("Not enough gas to execute operation");
58
+ this.gasAssets = gasAssets;
59
+ this.gasEstimates = gasEstimates;
60
+ this.gasAssetBalances = gasAssetBalances;
61
+ this.name = "NotEnoughGasTokensError";
62
+ }
63
+ }
64
+ exports.NotEnoughGasTokensError = NotEnoughGasTokensError;
65
+ async function handleGasForOperationRequest(deps, opRequest, gasMultiplier) {
66
+ // estimate gas params for opRequest
67
+ console.log("estimating gas for op request");
68
+ const { totalGasLimit, executionGasLimit, gasPrice, usedNotes } = await getOperationRequestTrace(deps, opRequest, gasMultiplier);
69
+ const gasEstimatedOpRequest = {
70
+ ...opRequest,
71
+ executionGasLimit,
72
+ gasPrice,
73
+ };
74
+ if (opRequest?.gasPrice == 0n) {
75
+ // If gasPrice = 0, override dummy gas asset and don't further modify opRequest
76
+ console.log("returning dummy gas asset");
77
+ return {
78
+ ...gasEstimatedOpRequest,
79
+ gasPrice: 0n,
80
+ gasAsset: DUMMY_GAS_ASSET,
81
+ gasAssetRefundThreshold: 0n,
82
+ totalGasLimit,
83
+ };
84
+ }
85
+ else {
86
+ console.log(`total gas limit pre gas update: ${totalGasLimit}`);
87
+ // attempt to update the joinSplitRequests with gas compensation
88
+ // gasAsset will be `undefined` if the user's too broke to pay for gas
89
+ const [joinSplitRequests, gasAssetAndTicker] = await tryUpdateJoinSplitRequestsForGasEstimate(deps.gasAssets, deps.db, gasEstimatedOpRequest.joinSplitRequests, totalGasLimit, gasPrice, deps.tokenConverter, usedNotes);
90
+ const gasAssetRefundThreshold = await deps.tokenConverter.weiToTargetErc20(DEFAULT_GAS_ASSET_REFUND_THRESHOLD_GAS * gasPrice, gasAssetAndTicker.ticker);
91
+ return {
92
+ ...gasEstimatedOpRequest,
93
+ gasAssetRefundThreshold,
94
+ joinSplitRequests,
95
+ gasAsset: gasAssetAndTicker.asset,
96
+ totalGasLimit,
97
+ };
98
+ }
99
+ }
100
+ // update the joinSplitRequests to include any additional gas compensation, if needed
101
+ // returns the updated JoinSplitRequests and the gas asset used to pay for gas if the user can afford gas
102
+ // if the user can't afford gas, returns an empty array and undefined.
103
+ async function tryUpdateJoinSplitRequestsForGasEstimate(gasAssets, db, joinSplitRequests, gasUnitsEstimate, gasPrice, tokenConverter, usedNotes) {
104
+ // group joinSplitRequests by asset address
105
+ const joinSplitRequestsByAsset = (0, core_1.groupByMap)(joinSplitRequests, (request) => request.asset.assetAddr);
106
+ const gasCompJoinSplitRequestsByAsset = (0, core_1.groupByMap)(joinSplitRequests.filter((request) => request.allowGasCompensation !== false), (request) => request.asset.assetAddr);
107
+ const gasEstimateWei = gasPrice * gasUnitsEstimate;
108
+ const gasEstimatesInGasAssets = await tokenConverter.gasEstimatesInGasAssets(gasEstimateWei, Array.from(gasAssets.keys()));
109
+ const [matchingGasAssets, nonMatchingGasAssets] = (0, core_1.partition)(Array.from(gasAssets.entries()), ([_, gasAsset]) => gasCompJoinSplitRequestsByAsset.has(gasAsset.assetAddr));
110
+ const failedGasEstimates = [];
111
+ const failedGasAssets = [];
112
+ const failedGasAssetBalances = [];
113
+ // attempt to find matching gas asset with enough balance
114
+ for (const [ticker, gasAsset] of matchingGasAssets) {
115
+ const totalOwnedGasAsset = await db.getBalanceForAsset(gasAsset);
116
+ const matchingJoinSplitRequests = gasCompJoinSplitRequestsByAsset.get(gasAsset.assetAddr);
117
+ if (!matchingJoinSplitRequests || matchingJoinSplitRequests.length === 0) {
118
+ continue;
119
+ }
120
+ const totalAmountInMatchingJoinSplitRequests = matchingJoinSplitRequests.reduce((acc, request) => {
121
+ return acc + (0, utils_1.getJoinSplitRequestTotalValue)(request);
122
+ }, 0n);
123
+ // if they have enough for request + gas, modify one of the requests to include the gas, and
124
+ // we're done
125
+ const estimateInGasAsset = gasEstimatesInGasAssets.get(ticker);
126
+ if (totalOwnedGasAsset >=
127
+ estimateInGasAsset + totalAmountInMatchingJoinSplitRequests) {
128
+ const usedNotesForGasAsset = usedNotes.get(gasAsset) ??
129
+ Array.from(usedNotes.entries()).find(([asset]) => core_1.AssetTrait.isSameAsset(asset, gasAsset))?.[1] ??
130
+ [];
131
+ const usedMerkleIndicesForGasAsset = new Set(usedNotesForGasAsset.map((note) => note.merkleIndex));
132
+ const totalValueInExistingJoinSplits = usedNotesForGasAsset.reduce((acc, note) => acc + note.value, 0n);
133
+ const remainingValueNeededInNewJoinSplits = totalAmountInMatchingJoinSplitRequests +
134
+ estimateInGasAsset -
135
+ totalValueInExistingJoinSplits;
136
+ let extraJoinSplitsGas;
137
+ let numExtraJoinSplits;
138
+ if (remainingValueNeededInNewJoinSplits > 0n) {
139
+ // Add enough to cover gas needed for existing joinsplits + gas for an extra joinsplits
140
+ const extraNotes = await (0, prepareOperation_1.gatherNotes)(db, remainingValueNeededInNewJoinSplits, gasAsset, usedMerkleIndicesForGasAsset);
141
+ console.log(`usedNotes: ${JSON.stringify(usedNotes)}`);
142
+ console.log(`need ${extraNotes.length} extra notes for gas comp`);
143
+ numExtraJoinSplits =
144
+ Math.ceil((usedNotesForGasAsset.length + extraNotes.length) / 2) -
145
+ Math.ceil(usedNotesForGasAsset.length / 2);
146
+ extraJoinSplitsGas =
147
+ BigInt(numExtraJoinSplits) *
148
+ core_1.MAX_GAS_FOR_ADDITIONAL_JOINSPLIT *
149
+ gasPrice;
150
+ const estimateInGasAssetIncludingNewJoinSplits = estimateInGasAsset + extraJoinSplitsGas;
151
+ if (totalOwnedGasAsset < estimateInGasAssetIncludingNewJoinSplits) {
152
+ // Don't have enough for new joinsplits gas overhead, try new asset
153
+ failedGasAssets.push(gasAsset);
154
+ failedGasEstimates.push(estimateInGasAssetIncludingNewJoinSplits);
155
+ failedGasAssetBalances.push(totalOwnedGasAsset);
156
+ continue;
157
+ }
158
+ }
159
+ else {
160
+ // otherwise we don't need any extra joinsplits because existing notes cover gas cost
161
+ extraJoinSplitsGas = 0n;
162
+ numExtraJoinSplits = 0;
163
+ }
164
+ console.log(`adding ${extraJoinSplitsGas} tokens for ${numExtraJoinSplits} extra joinsplits for gas compensation`);
165
+ matchingJoinSplitRequests[0].unwrapValue +=
166
+ estimateInGasAsset + extraJoinSplitsGas;
167
+ joinSplitRequestsByAsset.set(gasAsset.assetAddr, matchingJoinSplitRequests);
168
+ console.log(`amount to add for gas asset by modifying existing joinsplit for gas asset with ticker ${ticker}: ${estimateInGasAsset}}`, { gasAssetTicker: ticker, gasAsset, estimateInGasAsset });
169
+ return [
170
+ Array.from(joinSplitRequestsByAsset.values()).flat(),
171
+ { asset: gasAsset, ticker },
172
+ ];
173
+ }
174
+ else {
175
+ failedGasAssets.push(gasAsset);
176
+ failedGasEstimates.push(estimateInGasAsset);
177
+ failedGasAssetBalances.push(totalOwnedGasAsset);
178
+ }
179
+ }
180
+ // if we couldn't find an existing joinsplit request with a supported gas asset,
181
+ // attempt to make a new joinsplit request to include the gas comp
182
+ // iterate through each gas asset
183
+ for (const [ticker, gasAsset] of nonMatchingGasAssets) {
184
+ const estimateInGasAsset = gasEstimatesInGasAssets.get(ticker);
185
+ const totalOwnedGasAsset = await db.getBalanceForAsset(gasAsset);
186
+ if (totalOwnedGasAsset >= estimateInGasAsset) {
187
+ // Add enough to cover gas needed for existing joinsplits + gas for an extra joinsplits
188
+ const extraNotes = await (0, prepareOperation_1.gatherNotes)(db, estimateInGasAsset, gasAsset);
189
+ const numExtraJoinSplits = Math.ceil(extraNotes.length / 2);
190
+ const additionaJoinSplitGas = BigInt(numExtraJoinSplits) *
191
+ core_1.MAX_GAS_FOR_ADDITIONAL_JOINSPLIT *
192
+ gasPrice;
193
+ const estimateInGasAssetIncludingNewJoinSplits = estimateInGasAsset + additionaJoinSplitGas;
194
+ if (totalOwnedGasAsset < estimateInGasAssetIncludingNewJoinSplits) {
195
+ // Don't have enough for new joinsplits gas overhead, try new asset
196
+ failedGasAssets.push(gasAsset);
197
+ failedGasEstimates.push(estimateInGasAssetIncludingNewJoinSplits);
198
+ failedGasAssetBalances.push(totalOwnedGasAsset);
199
+ continue;
200
+ }
201
+ const modifiedJoinSplitRequests = joinSplitRequests.concat({
202
+ asset: gasAsset,
203
+ unwrapValue: estimateInGasAssetIncludingNewJoinSplits,
204
+ });
205
+ console.log(`amount to add for gas asset by adding a new joinsplit for gas asset with ticker ${ticker}: ${estimateInGasAssetIncludingNewJoinSplits}`, {
206
+ gasAssetTicker: ticker,
207
+ gasAsset,
208
+ estimateInGasAssetIncludingNewJoinSplits,
209
+ });
210
+ return [modifiedJoinSplitRequests, { asset: gasAsset, ticker }];
211
+ }
212
+ else {
213
+ failedGasAssets.push(gasAsset);
214
+ failedGasEstimates.push(estimateInGasAsset);
215
+ failedGasAssetBalances.push(totalOwnedGasAsset);
216
+ }
217
+ }
218
+ // if we get here, the user can't afford the gas
219
+ throw new NotEnoughGasTokensError(failedGasAssets, failedGasEstimates, failedGasAssetBalances);
220
+ }
221
+ // estimate gas params for opRequest
222
+ async function getOperationRequestTrace({ handlerContract, ...deps }, opRequest, gasMultiplier) {
223
+ let { executionGasLimit, gasPrice } = opRequest;
224
+ // Simulate operation to get number of joinSplits
225
+ const dummyOpRequest = {
226
+ ...opRequest,
227
+ gasAssetRefundThreshold: 0n,
228
+ executionGasLimit: core_1.BLOCK_GAS_LIMIT,
229
+ refundAddr: DUMMY_REFUND_ADDR,
230
+ // Use 0 gas price and dummy asset for simulation
231
+ gasPrice: 0n,
232
+ gasAsset: DUMMY_GAS_ASSET,
233
+ totalGasLimit: core_1.BLOCK_GAS_LIMIT,
234
+ };
235
+ // prepare the request into an operation using a dummy viewer
236
+ const preparedOp = await (0, prepareOperation_1.prepareOperation)({ viewer: DUMMY_VIEWER, ...deps }, dummyOpRequest);
237
+ const usedNotes = (0, utils_1.getIncludedNotesFromOp)(preparedOp);
238
+ // simulate the operation
239
+ if (!executionGasLimit) {
240
+ console.log("simulating operation");
241
+ const result = await simulateOperation(handlerContract, preparedOp);
242
+ if (!result.opProcessed) {
243
+ throw Error("cannot estimate gas with Error: " + result.failureReason);
244
+ }
245
+ // set executionGasLimit with 20% buffer above the simulation result
246
+ executionGasLimit = (result.executionGas * 12n) / 10n;
247
+ }
248
+ preparedOp.executionGasLimit = executionGasLimit;
249
+ const totalGasLimit = (0, core_1.maxGasForOperation)(preparedOp);
250
+ const scale = 100;
251
+ const gasMultiplierScaled = BigInt(Math.floor(gasMultiplier * scale));
252
+ // if gasPrice is not specified, get it from RPC node
253
+ // NOTE: gasPrice returned in wei
254
+ gasPrice =
255
+ gasPrice ??
256
+ ((await handlerContract.provider.getGasPrice()).toBigInt() *
257
+ gasMultiplierScaled) /
258
+ BigInt(Math.floor(scale));
259
+ return {
260
+ totalGasLimit,
261
+ executionGasLimit,
262
+ gasPrice,
263
+ usedNotes,
264
+ };
265
+ }
266
+ async function simulateOperation(handlerContract, op) {
267
+ // We need to do staticCall, which fails if wallet is connected to a signer
268
+ // https://github.com/ethers-io/ethers.js/discussions/3327#discussioncomment-3539505
269
+ // Switching to a regular provider underlying the signer
270
+ if (handlerContract.signer) {
271
+ handlerContract = handlerContract.connect(handlerContract.provider);
272
+ }
273
+ // Fill-in the some fake proof
274
+ const opWithFakeProofs = fakeProvenOperation(op);
275
+ // Set gasPrice to 0 so that gas payment does not interfere with amount of
276
+ // assets unwrapped pre gas estimation
277
+ // ?: does this actually do anything if it's after `fakeProvenOperation` dummy provenOp?
278
+ op.gasPrice = 0n;
279
+ // Set dummy parameters which should not affect operation simulation
280
+ const verificationGasForOp = 0n;
281
+ const bundler = handlerContract.address;
282
+ const tellerAddress = await handlerContract._teller();
283
+ const result = await handlerContract.callStatic.handleOperation(
284
+ // TODO: fix after contract changes
285
+ //@ts-ignore
286
+ opWithFakeProofs, verificationGasForOp, bundler, {
287
+ from: tellerAddress,
288
+ });
289
+ const { opProcessed, failureReason, callSuccesses, callResults, verificationGas, executionGas, numRefunds, } = result;
290
+ return {
291
+ opProcessed,
292
+ failureReason,
293
+ callSuccesses,
294
+ callResults,
295
+ verificationGas: verificationGas.toBigInt(),
296
+ executionGas: executionGas.toBigInt(),
297
+ numRefunds: numRefunds.toBigInt(),
298
+ };
299
+ }
300
+ function fakeProvenOperation(op) {
301
+ const provenJoinSplits = op.joinSplits.map((js) => {
302
+ return {
303
+ ...js,
304
+ proof: [0n, 0n, 0n, 0n, 0n, 0n, 0n, 0n],
305
+ };
306
+ });
307
+ return core_1.OperationTrait.toSubmittable({
308
+ networkInfo: op.networkInfo,
309
+ joinSplits: provenJoinSplits,
310
+ refundAddr: op.refundAddr,
311
+ actions: op.actions,
312
+ refunds: op.refunds,
313
+ encodedGasAsset: op.encodedGasAsset,
314
+ gasAssetRefundThreshold: op.gasAssetRefundThreshold,
315
+ executionGasLimit: op.executionGasLimit,
316
+ gasPrice: op.gasPrice,
317
+ deadline: op.deadline,
318
+ atomicActions: op.atomicActions,
319
+ });
320
+ }
321
+ //# sourceMappingURL=opRequestGas.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opRequestGas.js","sourceRoot":"","sources":["../../src/opRequestGas.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;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C,iBAAiB;QACvC,QAAQ,EAAE,EAAE,CAAC,QAAQ;QACrB,QAAQ,EAAE,EAAE,CAAC,QAAQ;QACrB,aAAa,EAAE,EAAE,CAAC,aAAa;KAChC,CAAC,CAAC;AACL,CAAC"}
@@ -0,0 +1,40 @@
1
+ import { NocturneConfig } from "@zill-protocol/config";
2
+ import { CanonAddress, StealthAddress } from "@zill-protocol/crypto";
3
+ import { ConfidentialPayment, OperationGasParams, OperationRequestWithMetadata, OperationRequest, UnwrapRequest, ConfidentialPaymentRequest, RefundRequest } from "./operationRequest";
4
+ import { Action, Address, Asset } from "@zill-protocol/core";
5
+ import { ethers } from "ethers";
6
+ import { OperationMetadataItem } from "../types";
7
+ export type OpRequestBuilder = OpRequestBuilderExt<BaseOpRequestBuilder>;
8
+ export interface BuilderItemToProcess {
9
+ unwraps: UnwrapRequest[];
10
+ confidentialPayments: ConfidentialPaymentRequest[];
11
+ refunds: RefundRequest[];
12
+ actions: Action[];
13
+ metadatas: OperationMetadataItem[];
14
+ }
15
+ export type OpRequestBuilderExt<E extends BaseOpRequestBuilder> = E & {
16
+ use<E2 extends E>(plugin: OpRequestBuilderPlugin<E, E2>): OpRequestBuilderExt<E2>;
17
+ };
18
+ export interface BaseOpRequestBuilder {
19
+ provider: ethers.providers.JsonRpcProvider;
20
+ config: NocturneConfig;
21
+ _op: OperationRequest;
22
+ _builderItemsToProcess: Promise<BuilderItemToProcess>[];
23
+ build(): Promise<OperationRequestWithMetadata>;
24
+ pluginFn(pluginPromise: Promise<BuilderItemToProcess>): this;
25
+ __action(contractAddress: Address, encodedFunction: string): this;
26
+ __unwrap(asset: Asset, amountUnits: bigint): this;
27
+ confidentialPayment(asset: Asset, amountUnits: bigint, receiver: CanonAddress): this;
28
+ __refund(refund: RefundRequest): this;
29
+ refundAddr(addr: StealthAddress): this;
30
+ deadline(deadline: bigint): this;
31
+ gas(gasParams: OperationGasParams): this;
32
+ gasPrice(gasPrice: bigint): this;
33
+ }
34
+ export type OpRequestBuilderPlugin<E extends BaseOpRequestBuilder, E2 extends E> = (inner: OpRequestBuilderExt<E>) => OpRequestBuilderExt<E2>;
35
+ export type UnwrapAmountAndPaymentsForAsset = {
36
+ unwrapAmount: bigint;
37
+ payments: ConfidentialPayment[];
38
+ };
39
+ export declare function newOpRequestBuilder(provider: ethers.providers.JsonRpcProvider, chainId: bigint, config?: NocturneConfig): OpRequestBuilderExt<BaseOpRequestBuilder>;
40
+ //# sourceMappingURL=builder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"builder.d.ts","sourceRoot":"","sources":["../../../src/operationRequest/builder.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,EAEf,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AACrE,OAAO,EACL,mBAAmB,EACnB,kBAAkB,EAClB,4BAA4B,EAC5B,gBAAgB,EAChB,aAAa,EACb,0BAA0B,EAC1B,aAAa,EACd,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,MAAM,EACN,OAAO,EACP,KAAK,EAGN,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAqB,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAGpE,MAAM,MAAM,gBAAgB,GAAG,mBAAmB,CAAC,oBAAoB,CAAC,CAAC;AAEzE,MAAM,WAAW,oBAAoB;IACnC,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB,oBAAoB,EAAE,0BAA0B,EAAE,CAAC;IACnD,OAAO,EAAE,aAAa,EAAE,CAAC;IACzB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,SAAS,EAAE,qBAAqB,EAAE,CAAC;CACpC;AAGD,MAAM,MAAM,mBAAmB,CAAC,CAAC,SAAS,oBAAoB,IAAI,CAAC,GAAG;IAEpE,GAAG,CAAC,EAAE,SAAS,CAAC,EACd,MAAM,EAAE,sB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