@reyaxyz/sdk 0.36.0 → 0.37.0
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.
- package/dist/config/index.js +91 -0
- package/dist/config/index.js.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/services/executeTransaction.js +54 -6
- package/dist/services/executeTransaction.js.map +1 -1
- package/dist/services/margin-accounts/account.js +1 -1
- package/dist/services/margin-accounts/account.js.map +1 -1
- package/dist/services/orders/order.js +2 -2
- package/dist/services/orders/order.js.map +1 -1
- package/dist/types/config/index.d.ts +12 -0
- package/dist/types/config/index.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/services/executeTransaction.d.ts +4 -0
- package/dist/types/services/executeTransaction.d.ts.map +1 -1
- package/dist/types/services/margin-accounts/account.d.ts.map +1 -1
- package/dist/types/services/orders/order.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/config/index.ts +42 -0
- package/src/index.ts +1 -0
- package/src/services/executeTransaction.ts +51 -1
- package/src/services/margin-accounts/account.ts +3 -2
- package/src/services/orders/order.ts +5 -3
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
+
function step(op) {
|
|
31
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
34
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
+
switch (op[0]) {
|
|
36
|
+
case 0: case 1: t = op; break;
|
|
37
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
+
default:
|
|
41
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
+
if (t[2]) _.ops.pop();
|
|
46
|
+
_.trys.pop(); continue;
|
|
47
|
+
}
|
|
48
|
+
op = body.call(thisArg, _);
|
|
49
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
54
|
+
exports.getTransactionModuleClient = exports.configureSDK = exports.TransactionModule = void 0;
|
|
55
|
+
var common_1 = require("@reyaxyz/common");
|
|
56
|
+
var TransactionModule = /** @class */ (function (_super) {
|
|
57
|
+
__extends(TransactionModule, _super);
|
|
58
|
+
function TransactionModule() {
|
|
59
|
+
return _super !== null && _super.apply(this, arguments) || this;
|
|
60
|
+
}
|
|
61
|
+
TransactionModule.prototype.executeTransaction = function (signedTx, contractType, metadata) {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
63
|
+
var uri;
|
|
64
|
+
return __generator(this, function (_a) {
|
|
65
|
+
uri = "/api/transaction/execute";
|
|
66
|
+
return [2 /*return*/, this.post(uri, {}, {
|
|
67
|
+
signedTx: signedTx,
|
|
68
|
+
contractType: contractType,
|
|
69
|
+
metadata: metadata,
|
|
70
|
+
})];
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
};
|
|
74
|
+
return TransactionModule;
|
|
75
|
+
}(common_1.RestClient));
|
|
76
|
+
exports.TransactionModule = TransactionModule;
|
|
77
|
+
// Internal state
|
|
78
|
+
var transactionModule = new TransactionModule(common_1.API_CLIENT_CONFIGS['test'].apiEndpoint);
|
|
79
|
+
// Function to configure the SDK
|
|
80
|
+
var configureSDK = function (environment) {
|
|
81
|
+
var config = common_1.API_CLIENT_CONFIGS[environment] || common_1.API_CLIENT_CONFIGS['test'];
|
|
82
|
+
transactionModule = new TransactionModule(config.apiEndpoint);
|
|
83
|
+
};
|
|
84
|
+
exports.configureSDK = configureSDK;
|
|
85
|
+
var getTransactionModuleClient = function () {
|
|
86
|
+
if (!transactionModule)
|
|
87
|
+
throw new Error('SDK is not configured! Call configureSDK()');
|
|
88
|
+
return transactionModule;
|
|
89
|
+
};
|
|
90
|
+
exports.getTransactionModuleClient = getTransactionModuleClient;
|
|
91
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["config/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,0CAAgF;AAKhF;IAAuC,qCAAU;IAAjD;;IAiBA,CAAC;IAhBO,8CAAkB,GAAxB,UACE,QAAgB,EAChB,YAAoB,EACpB,QAAsC;;;;gBAEhC,GAAG,GAAG,0BAA0B,CAAC;gBACvC,sBAAO,IAAI,CAAC,IAAI,CACd,GAAG,EACH,EAAE,EACF;wBACE,QAAQ,UAAA;wBACR,YAAY,cAAA;wBACZ,QAAQ,UAAA;qBACT,CACF,EAAC;;;KACH;IACH,wBAAC;AAAD,CAAC,AAjBD,CAAuC,mBAAU,GAiBhD;AAjBY,8CAAiB;AAmB9B,iBAAiB;AACjB,IAAI,iBAAiB,GAAsB,IAAI,iBAAiB,CAC9D,2BAAkB,CAAC,MAAM,CAAC,CAAC,WAAW,CACvC,CAAC;AAEF,gCAAgC;AACzB,IAAM,YAAY,GAAG,UAC1B,WAAyC;IAEzC,IAAM,MAAM,GAAG,2BAAkB,CAAC,WAAW,CAAC,IAAI,2BAAkB,CAAC,MAAM,CAAC,CAAC;IAC7E,iBAAiB,GAAG,IAAI,iBAAiB,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;AAChE,CAAC,CAAC;AALW,QAAA,YAAY,gBAKvB;AAEK,IAAM,0BAA0B,GAAG;IACxC,IAAI,CAAC,iBAAiB;QACpB,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;IAChE,OAAO,iBAAiB,CAAC;AAC3B,CAAC,CAAC;AAJW,QAAA,0BAA0B,8BAIrC","sourcesContent":["import { API_CLIENT_CONFIGS, RestClient, ServiceConfig } from '@reyaxyz/common';\n\nexport type BroadcastTransactionMetadata = {\n accountName?: string;\n};\nexport class TransactionModule extends RestClient {\n async executeTransaction(\n signedTx: string,\n contractType: string,\n metadata: BroadcastTransactionMetadata,\n ) {\n const uri = `/api/transaction/execute`;\n return this.post<{ txHash: string }>(\n uri,\n {},\n {\n signedTx,\n contractType,\n metadata,\n },\n );\n }\n}\n\n// Internal state\nlet transactionModule: TransactionModule = new TransactionModule(\n API_CLIENT_CONFIGS['test'].apiEndpoint,\n);\n\n// Function to configure the SDK\nexport const configureSDK = (\n environment: ServiceConfig['environment'],\n): void => {\n const config = API_CLIENT_CONFIGS[environment] || API_CLIENT_CONFIGS['test'];\n transactionModule = new TransactionModule(config.apiEndpoint);\n};\n\nexport const getTransactionModuleClient = (): TransactionModule => {\n if (!transactionModule)\n throw new Error('SDK is not configured! Call configureSDK()');\n return transactionModule;\n};\n"]}
|
package/dist/index.js
CHANGED
|
@@ -14,9 +14,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.configureSDK = void 0;
|
|
17
18
|
__exportStar(require("./services/lp"), exports);
|
|
18
19
|
__exportStar(require("./services/margin-accounts"), exports);
|
|
19
20
|
__exportStar(require("./services/money-flows"), exports);
|
|
20
21
|
__exportStar(require("./services/orders"), exports);
|
|
21
22
|
__exportStar(require("./services/token"), exports);
|
|
23
|
+
var config_1 = require("./config");
|
|
24
|
+
Object.defineProperty(exports, "configureSDK", { enumerable: true, get: function () { return config_1.configureSDK; } });
|
|
22
25
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,gDAA8B;AAC9B,6DAA2C;AAC3C,yDAAuC;AACvC,oDAAkC;AAClC,mDAAiC;AACjC,mCAAwC;AAA/B,sGAAA,YAAY,OAAA","sourcesContent":["export * from './services/lp';\nexport * from './services/margin-accounts';\nexport * from './services/money-flows';\nexport * from './services/orders';\nexport * from './services/token';\nexport { configureSDK } from './config';\n"]}
|
|
@@ -47,21 +47,40 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
50
|
-
exports.executeTransaction = exports.estimateGas = void 0;
|
|
50
|
+
exports.signAndBroadcastTransaction = exports.executeTransaction = exports.estimateGas = void 0;
|
|
51
51
|
var contractAddresses_1 = require("../utils/contractAddresses");
|
|
52
|
+
var txHelpers_1 = require("../utils/txHelpers");
|
|
53
|
+
var config_1 = require("../config");
|
|
52
54
|
function estimateGas(signer, data, value, chainId, targetContract) {
|
|
53
55
|
return __awaiter(this, void 0, void 0, function () {
|
|
54
|
-
var accountAddress, contractAddress, tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas;
|
|
56
|
+
var accountAddress, nonce, contractAddress, tx, gasLimit, gasEstimate, error_1, maxPriorityFeePerGas, maxFeePerGas;
|
|
55
57
|
return __generator(this, function (_a) {
|
|
56
58
|
switch (_a.label) {
|
|
57
59
|
case 0: return [4 /*yield*/, signer.getAddress()];
|
|
58
60
|
case 1:
|
|
59
61
|
accountAddress = _a.sent();
|
|
62
|
+
return [4 /*yield*/, signer.getNonce()];
|
|
63
|
+
case 2:
|
|
64
|
+
nonce = _a.sent();
|
|
60
65
|
contractAddress = Object.values(contractAddresses_1.ContractType).includes(targetContract)
|
|
61
66
|
? (0, contractAddresses_1.getAddress)(chainId, targetContract)
|
|
62
67
|
: targetContract;
|
|
63
|
-
tx = __assign({ from: accountAddress, to: contractAddress, data: data }, (value && value !== '0' ? { value: value } : {}));
|
|
68
|
+
tx = __assign(__assign({ from: accountAddress, to: contractAddress, data: data }, (value && value !== '0' ? { value: value } : {})), { chainId: chainId, nonce: nonce });
|
|
64
69
|
gasLimit = BigInt('5000000');
|
|
70
|
+
_a.label = 3;
|
|
71
|
+
case 3:
|
|
72
|
+
_a.trys.push([3, 5, , 6]);
|
|
73
|
+
return [4 /*yield*/, signer.estimateGas(tx)];
|
|
74
|
+
case 4:
|
|
75
|
+
gasEstimate = _a.sent();
|
|
76
|
+
gasLimit = (0, txHelpers_1.getGasBuffer)(gasEstimate);
|
|
77
|
+
return [3 /*break*/, 6];
|
|
78
|
+
case 5:
|
|
79
|
+
error_1 = _a.sent();
|
|
80
|
+
// sentry error & throw
|
|
81
|
+
console.warn(error_1);
|
|
82
|
+
return [3 /*break*/, 6];
|
|
83
|
+
case 6:
|
|
65
84
|
maxPriorityFeePerGas = BigInt('0');
|
|
66
85
|
maxFeePerGas = BigInt('0');
|
|
67
86
|
return [2 /*return*/, __assign(__assign({}, tx), { gasLimit: gasLimit, maxPriorityFeePerGas: maxPriorityFeePerGas, maxFeePerGas: maxFeePerGas })];
|
|
@@ -72,7 +91,7 @@ function estimateGas(signer, data, value, chainId, targetContract) {
|
|
|
72
91
|
exports.estimateGas = estimateGas;
|
|
73
92
|
function executeTransaction(signer, data, value, chainId, targetContract) {
|
|
74
93
|
return __awaiter(this, void 0, void 0, function () {
|
|
75
|
-
var txData, txResponse, txReceipt,
|
|
94
|
+
var txData, txResponse, txReceipt, error_2;
|
|
76
95
|
return __generator(this, function (_a) {
|
|
77
96
|
switch (_a.label) {
|
|
78
97
|
case 0: return [4 /*yield*/, estimateGas(signer, data, value, chainId, targetContract)];
|
|
@@ -89,8 +108,8 @@ function executeTransaction(signer, data, value, chainId, targetContract) {
|
|
|
89
108
|
txReceipt = _a.sent();
|
|
90
109
|
return [2 /*return*/, txReceipt];
|
|
91
110
|
case 5:
|
|
92
|
-
|
|
93
|
-
console.warn(
|
|
111
|
+
error_2 = _a.sent();
|
|
112
|
+
console.warn(error_2);
|
|
94
113
|
throw new Error('Transaction Execution Error');
|
|
95
114
|
case 6: return [2 /*return*/];
|
|
96
115
|
}
|
|
@@ -98,4 +117,33 @@ function executeTransaction(signer, data, value, chainId, targetContract) {
|
|
|
98
117
|
});
|
|
99
118
|
}
|
|
100
119
|
exports.executeTransaction = executeTransaction;
|
|
120
|
+
function signAndBroadcastTransaction(signer, data, value, chainId, targetContract, metadata) {
|
|
121
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
122
|
+
var txData, signedTx, transactionModuleClient, txHash, error_3;
|
|
123
|
+
return __generator(this, function (_a) {
|
|
124
|
+
switch (_a.label) {
|
|
125
|
+
case 0: return [4 /*yield*/, estimateGas(signer, data, value, chainId, targetContract)];
|
|
126
|
+
case 1:
|
|
127
|
+
txData = _a.sent();
|
|
128
|
+
_a.label = 2;
|
|
129
|
+
case 2:
|
|
130
|
+
_a.trys.push([2, 5, , 6]);
|
|
131
|
+
return [4 /*yield*/, signer.signTransaction(txData)];
|
|
132
|
+
case 3:
|
|
133
|
+
signedTx = _a.sent();
|
|
134
|
+
transactionModuleClient = (0, config_1.getTransactionModuleClient)();
|
|
135
|
+
return [4 /*yield*/, transactionModuleClient.executeTransaction(signedTx, targetContract, metadata)];
|
|
136
|
+
case 4:
|
|
137
|
+
txHash = (_a.sent()).txHash;
|
|
138
|
+
return [2 /*return*/, { hash: txHash }];
|
|
139
|
+
case 5:
|
|
140
|
+
error_3 = _a.sent();
|
|
141
|
+
console.warn(error_3);
|
|
142
|
+
throw new Error('Transaction Execution Error');
|
|
143
|
+
case 6: return [2 /*return*/];
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
exports.signAndBroadcastTransaction = signAndBroadcastTransaction;
|
|
101
149
|
//# sourceMappingURL=executeTransaction.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executeTransaction.js","sourceRoot":"/","sources":["services/executeTransaction.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,gEAAsE;
|
|
1
|
+
{"version":3,"file":"executeTransaction.js","sourceRoot":"/","sources":["services/executeTransaction.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,gEAAsE;AACtE,gDAAkD;AAClD,oCAGmB;AASnB,SAAsB,WAAW,CAC/B,MAA8B,EAC9B,IAAY,EACZ,KAAa,EACb,OAAe,EACf,cAAqC;;;;;wBAQd,qBAAM,MAAM,CAAC,UAAU,EAAE,EAAA;;oBAA1C,cAAc,GAAG,SAAyB;oBAClC,qBAAM,MAAM,CAAC,QAAQ,EAAE,EAAA;;oBAA/B,KAAK,GAAG,SAAuB;oBAE/B,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,gCAAY,CAAC,CAAC,QAAQ,CAC1D,cAA8B,CAC/B;wBACC,CAAC,CAAC,IAAA,8BAAU,EAAC,OAAO,EAAE,cAA8B,CAAC;wBACrD,CAAC,CAAC,cAAc,CAAC;oBACb,EAAE,uBACN,IAAI,EAAE,cAAc,EACpB,EAAE,EAAE,eAAe,EACnB,IAAI,MAAA,IACD,CAAC,KAAK,IAAI,KAAK,KAAK,GAAG,CAAC,CAAC,CAAC,EA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M,WAAW,CAC9B,MAAM,EACN,IAAI,EACJ,KAAK,EACL,OAAO,EACP,cAAc,CACf,EAAA;;oBANK,MAAM,GAAG,SAMd;;;;oBAGkB,qBAAM,MAAM,CAAC,eAAe,CAAC,MAAM,CAAC,EAAA;;oBAA/C,QAAQ,GAAG,SAAoC;oBAE/C,uBAAuB,GAAG,IAAA,mCAA0B,GAAE,CAAC;oBAC1C,qBAAM,uBAAuB,CAAC,kBAAkB,CACjE,QAAQ,EACR,cAAc,EACd,QAAQ,CACT,EAAA;;oBAJO,MAAM,GAAK,CAAA,SAIlB,CAAA,OAJa;oBAKd,sBAAO,EAAE,IAAI,EAAE,MAAM,EAAE,EAAC;;;oBAExB,OAAO,CAAC,IAAI,CAAC,OAAK,CAAC,CAAC;oBACpB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;;;;;CAElD;AA9BD,kEA8BC","sourcesContent":["import { JsonRpcSigner, Signer } from 'ethers';\nimport { ContractType, getAddress } from '../utils/contractAddresses';\nimport { getGasBuffer } from '../utils/txHelpers';\nimport {\n BroadcastTransactionMetadata,\n getTransactionModuleClient,\n} from '../config';\n\nexport type Transaction = {\n from: string;\n to: string;\n data: string;\n value?: string;\n};\n\nexport async function estimateGas(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n): Promise<\n Transaction & {\n gasLimit: bigint;\n maxPriorityFeePerGas: bigint;\n maxFeePerGas: bigint;\n }\n> {\n const accountAddress = await signer.getAddress();\n const nonce = await signer.getNonce();\n\n const contractAddress = Object.values(ContractType).includes(\n targetContract as ContractType,\n )\n ? getAddress(chainId, targetContract as ContractType)\n : targetContract;\n const tx = {\n from: accountAddress,\n to: contractAddress,\n data,\n ...(value && value !== '0' ? { value: value } : {}),\n chainId: chainId,\n nonce,\n };\n\n let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate\n\n try {\n const gasEstimate = await signer.estimateGas(tx);\n gasLimit = getGasBuffer(gasEstimate);\n } catch (error) {\n // sentry error & throw\n console.warn(error);\n }\n\n const maxPriorityFeePerGas: bigint = BigInt('0');\n const maxFeePerGas: bigint = BigInt('0');\n return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };\n}\n\nexport async function executeTransaction(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n) {\n const txData = await estimateGas(\n signer,\n data,\n value,\n chainId,\n targetContract,\n );\n\n try {\n const txResponse = await signer.sendTransaction(txData);\n const txReceipt = await txResponse.wait();\n return txReceipt;\n } catch (error) {\n console.warn(error);\n throw new Error('Transaction Execution Error');\n }\n}\n\nexport async function signAndBroadcastTransaction(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n metadata: BroadcastTransactionMetadata,\n): Promise<{ hash: string }> {\n const txData = await estimateGas(\n signer,\n data,\n value,\n chainId,\n targetContract,\n );\n\n try {\n const signedTx = await signer.signTransaction(txData);\n\n const transactionModuleClient = getTransactionModuleClient();\n const { txHash } = await transactionModuleClient.executeTransaction(\n signedTx,\n targetContract,\n metadata,\n );\n return { hash: txHash };\n } catch (error) {\n console.warn(error);\n throw new Error('Transaction Execution Error');\n }\n}\n"]}
|
|
@@ -51,7 +51,7 @@ var createAccount = function (params) { return __awaiter(void 0, void 0, void 0,
|
|
|
51
51
|
case 1:
|
|
52
52
|
network = _c.sent();
|
|
53
53
|
chainId = Number(network === null || network === void 0 ? void 0 : network.chainId);
|
|
54
|
-
return [4 /*yield*/, (0, executeTransaction_1.
|
|
54
|
+
return [4 /*yield*/, (0, executeTransaction_1.signAndBroadcastTransaction)(params.signer, data, value, chainId, contractAddresses_1.ContractType.CORE_PROXY, { accountName: params.name })];
|
|
55
55
|
case 2:
|
|
56
56
|
result = _c.sent();
|
|
57
57
|
return [2 /*return*/, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"account.js","sourceRoot":"/","sources":["services/margin-accounts/account.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,
|
|
1
|
+
{"version":3,"file":"account.js","sourceRoot":"/","sources":["services/margin-accounts/account.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,4DAAoE;AACpE,mCAAmD;AAEnD,mEAA6D;AAEtD,IAAM,aAAa,GAAG,UAC3B,MAA2B;;;;;;gBAErB,KAA4B,IAAA,gCAAuB,EACvD,MAAM,CAAC,YAAY,CACpB,EAFiB,IAAI,cAAA,EAAE,KAAK,WAAA,CAE3B;gBAEc,qBAAM,CAAA,MAAA,MAAM,CAAC,MAAM,CAAC,QAAQ,0CAAE,UAAU,EAAE,CAAA,EAAA;;gBAApD,OAAO,GAAG,SAA0C;gBACpD,OAAO,GAAG,MAAM,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAAC;gBAE1B,qBAAM,IAAA,gDAA2B,EAC9C,MAAM,CAAC,MAAM,EACb,IAAI,EACJ,KAAK,EACL,OAAO,EACP,gCAAY,CAAC,UAAU,EACvB,EAAE,WAAW,EAAE,MAAM,CAAC,IAAI,EAAE,CAC7B,EAAA;;gBAPK,MAAM,GAAG,SAOd;gBACD,sBAAO;wBACL,eAAe,EAAE,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,KAAI,IAAI;qBACtC,EAAC;;;KACH,CAAC;AArBW,QAAA,aAAa,iBAqBxB","sourcesContent":["import { signAndBroadcastTransaction } from '../executeTransaction';\nimport { encodeCreateAccountCall } from './encode';\nimport { CreateAccountParams, CreateAccountResult } from './types';\nimport { ContractType } from '../../utils/contractAddresses';\n\nexport const createAccount = async (\n params: CreateAccountParams,\n): Promise<CreateAccountResult> => {\n const { calldata: data, value } = encodeCreateAccountCall(\n params.ownerAddress,\n );\n\n const network = await params.signer.provider?.getNetwork();\n const chainId = Number(network?.chainId);\n\n const result = await signAndBroadcastTransaction(\n params.signer,\n data,\n value,\n chainId,\n ContractType.CORE_PROXY,\n { accountName: params.name },\n );\n return {\n transactionHash: result?.hash || null,\n };\n};\n"]}
|
|
@@ -62,7 +62,7 @@ var matchOrder = function (params) { return __awaiter(void 0, void 0, void 0, fu
|
|
|
62
62
|
case 1:
|
|
63
63
|
network = _c.sent();
|
|
64
64
|
chainId = Number(network === null || network === void 0 ? void 0 : network.chainId);
|
|
65
|
-
return [4 /*yield*/, (0, executeTransaction_1.
|
|
65
|
+
return [4 /*yield*/, (0, executeTransaction_1.signAndBroadcastTransaction)(params.signer, data, value, chainId, contractAddresses_1.ContractType.CORE_PROXY, {})];
|
|
66
66
|
case 2:
|
|
67
67
|
result = _c.sent();
|
|
68
68
|
return [2 /*return*/, {
|
|
@@ -88,7 +88,7 @@ var closeOrder = function (params) { return __awaiter(void 0, void 0, void 0, fu
|
|
|
88
88
|
case 1:
|
|
89
89
|
network = _c.sent();
|
|
90
90
|
chainId = Number(network === null || network === void 0 ? void 0 : network.chainId);
|
|
91
|
-
return [4 /*yield*/, (0, executeTransaction_1.
|
|
91
|
+
return [4 /*yield*/, (0, executeTransaction_1.signAndBroadcastTransaction)(params.signer, data, value, chainId, contractAddresses_1.ContractType.CORE_PROXY, {})];
|
|
92
92
|
case 2:
|
|
93
93
|
result = _c.sent();
|
|
94
94
|
return [2 /*return*/, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order.js","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,mCAA4C;AAC5C,
|
|
1
|
+
{"version":3,"file":"order.js","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,mCAA4C;AAC5C,4DAAoE;AACpE,2CAAgE;AAChE,8DAAqC;AACrC,mEAA6D;AA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AAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;gBAChD,CAAC;gBAEK,IAAI,GAAG,IAAA,cAAK,EAAC,EAAE,CAAC,CAAC,IAAA,sBAAS,EAAC,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,CAAC;gBACnE,eAAe,GAAG,IAAA,mCAA2B,EACjD,MAAM,CAAC,MAAM,CAAC,YAAY,EAC1B,IAAA,sBAAS,EAAC,MAAM,CAAC,SAAS,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CACjD,CAAC;gBAEI,KAA4B,IAAA,yBAAgB,EAChD,MAAM,CAAC,eAAe,EACtB,MAAM,CAAC,IAAI,CAAC,EACZ,eAAe,EACf,MAAM,CAAC,MAAM,CAAC,sBAAsB,EACpC,MAAM,CAAC,MAAM,CAAC,EAAE,EAChB,MAAM,CAAC,MAAM,CAAC,UAAU,CACzB,EAPiB,IAAI,cAAA,EAAE,KAAK,WAAA,CAO3B;gBAEc,qBAAM,CAAA,MAAA,MAAM,CAAC,MAAM,CAAC,QAAQ,0CAAE,UAAU,EAAE,CAAA,EAAA;;gBAApD,OAAO,GAAG,SAA0C;gBACpD,OAAO,GAAG,MAAM,CAAC,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,CAAC,CAAC;gBAE1B,qBAAM,IAAA,gDAA2B,EAC9C,MAAM,CAAC,MAAM,EACb,IAAI,EACJ,KAAK,EACL,OAAO,EACP,gCAAY,CAAC,UAAU,EACvB,EAAE,CACH,EAAA;;gBAPK,MAAM,GAAG,SAOd;gBAED,sBAAO;wBACL,eAAe,EAAE,CAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,IAAI,KAAI,IAAI;qBACtC,EAAC;;;KACH,CAAC;AArCW,QAAA,UAAU,cAqCrB","sourcesContent":["import {\n CloseOrderParams,\n CloseOrderResult,\n MatchOrderParams,\n MatchOrderResult,\n} from './types';\nimport { encodeMatchOrder } from './encode';\nimport { signAndBroadcastTransaction } from '../executeTransaction';\nimport { calculatePriceLimitForTrade } from '../../utils/trade';\nimport BigNumber from 'bignumber.js';\nimport { ContractType } from '../../utils/contractAddresses';\nimport { scale } from '@reyaxyz/common';\n\nexport const matchOrder = async (\n params: MatchOrderParams,\n): Promise<MatchOrderResult> => {\n if (params.orderBase === 0) {\n throw new Error('Position base can not be 0');\n }\n const orderPriceLimit = calculatePriceLimitForTrade(\n params.market.currentPrice,\n params.orderBase,\n );\n\n const orderBase = scale(18)(params.orderBase);\n\n const { calldata: data, value } = encodeMatchOrder(\n params.marginAccountId,\n orderBase,\n orderPriceLimit,\n params.market.counterpartyAccountIds,\n params.market.id,\n params.market.exchangeId,\n );\n\n const network = await params.signer.provider?.getNetwork();\n const chainId = Number(network?.chainId);\n\n const result = await signAndBroadcastTransaction(\n params.signer,\n data,\n value,\n chainId,\n ContractType.CORE_PROXY,\n {},\n );\n\n return {\n transactionHash: result?.hash || null,\n };\n};\n\nexport const closeOrder = async (\n params: CloseOrderParams,\n): Promise<CloseOrderResult> => {\n if (params.orderBase === 0) {\n throw new Error('Position base can not be 0');\n }\n\n const base = scale(18)(BigNumber(params.orderBase).negated().toNumber());\n const orderPriceLimit = calculatePriceLimitForTrade(\n params.market.currentPrice,\n BigNumber(params.orderBase).negated().toNumber(), // used just because of sign\n );\n\n const { calldata: data, value } = encodeMatchOrder(\n params.marginAccountId,\n BigInt(base),\n orderPriceLimit,\n params.market.counterpartyAccountIds,\n params.market.id,\n params.market.exchangeId,\n );\n\n const network = await params.signer.provider?.getNetwork();\n const chainId = Number(network?.chainId);\n\n const result = await signAndBroadcastTransaction(\n params.signer,\n data,\n value,\n chainId,\n ContractType.CORE_PROXY,\n {},\n );\n\n return {\n transactionHash: result?.hash || null,\n };\n};\n"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { RestClient, ServiceConfig } from '@reyaxyz/common';
|
|
2
|
+
export type BroadcastTransactionMetadata = {
|
|
3
|
+
accountName?: string;
|
|
4
|
+
};
|
|
5
|
+
export declare class TransactionModule extends RestClient {
|
|
6
|
+
executeTransaction(signedTx: string, contractType: string, metadata: BroadcastTransactionMetadata): Promise<{
|
|
7
|
+
txHash: string;
|
|
8
|
+
}>;
|
|
9
|
+
}
|
|
10
|
+
export declare const configureSDK: (environment: ServiceConfig['environment']) => void;
|
|
11
|
+
export declare const getTransactionModuleClient: () => TransactionModule;
|
|
12
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["config/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,UAAU,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAEhF,MAAM,MAAM,4BAA4B,GAAG;IACzC,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC;AACF,qBAAa,iBAAkB,SAAQ,UAAU;IACzC,kBAAkB,CACtB,QAAQ,EAAE,MAAM,EAChB,YAAY,EAAE,MAAM,EACpB,QAAQ,EAAE,4BAA4B;gBAGX,MAAM;;CAUpC;AAQD,eAAO,MAAM,YAAY,gBACV,aAAa,CAAC,aAAa,CAAC,KACxC,IAGF,CAAC;AAEF,eAAO,MAAM,0BAA0B,QAAO,iBAI7C,CAAC"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"/","sources":["index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,wBAAwB,CAAC;AACvC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,OAAO,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { JsonRpcSigner, Signer } from 'ethers';
|
|
2
2
|
import { ContractType } from '../utils/contractAddresses';
|
|
3
|
+
import { BroadcastTransactionMetadata } from '../config';
|
|
3
4
|
export type Transaction = {
|
|
4
5
|
from: string;
|
|
5
6
|
to: string;
|
|
@@ -12,4 +13,7 @@ export declare function estimateGas(signer: Signer | JsonRpcSigner, data: string
|
|
|
12
13
|
maxFeePerGas: bigint;
|
|
13
14
|
}>;
|
|
14
15
|
export declare function executeTransaction(signer: Signer | JsonRpcSigner, data: string, value: string, chainId: number, targetContract: ContractType | string): Promise<import("ethers").TransactionReceipt | null>;
|
|
16
|
+
export declare function signAndBroadcastTransaction(signer: Signer | JsonRpcSigner, data: string, value: string, chainId: number, targetContract: ContractType | string, metadata: BroadcastTransactionMetadata): Promise<{
|
|
17
|
+
hash: string;
|
|
18
|
+
}>;
|
|
15
19
|
//# sourceMappingURL=executeTransaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executeTransaction.d.ts","sourceRoot":"/","sources":["services/executeTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAc,MAAM,4BAA4B,CAAC;AAEtE,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,wBAAsB,WAAW,CAC/B,MAAM,EAAE,MAAM,GAAG,aAAa,EAC9B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,EACf,cAAc,EAAE,YAAY,GAAG,MA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rC,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC,CAuB3B"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"account.d.ts","sourceRoot":"/","sources":["services/margin-accounts/account.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAGnE,eAAO,MAAM,aAAa,WAChB,mBAAmB,KAC1B,QAAQ,mBAAmB,
|
|
1
|
+
{"version":3,"file":"account.d.ts","sourceRoot":"/","sources":["services/margin-accounts/account.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAGnE,eAAO,MAAM,aAAa,WAChB,mBAAmB,KAC1B,QAAQ,mBAAmB,CAmB7B,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order.d.ts","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAQjB,eAAO,MAAM,UAAU,WACb,gBAAgB,KACvB,QAAQ,gBAAgB,
|
|
1
|
+
{"version":3,"file":"order.d.ts","sourceRoot":"/","sources":["services/orders/order.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EAChB,gBAAgB,EACjB,MAAM,SAAS,CAAC;AAQjB,eAAO,MAAM,UAAU,WACb,gBAAgB,KACvB,QAAQ,gBAAgB,CAmC1B,CAAC;AAEF,eAAO,MAAM,UAAU,WACb,gBAAgB,KACvB,QAAQ,gBAAgB,CAmC1B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.37.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -34,5 +34,5 @@
|
|
|
34
34
|
"ethers": "6.9.0"
|
|
35
35
|
},
|
|
36
36
|
"packageManager": "pnpm@8.10.4",
|
|
37
|
-
"gitHead": "
|
|
37
|
+
"gitHead": "08b91b59581c5c96bc77b2f490f9596b9371bb39"
|
|
38
38
|
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { API_CLIENT_CONFIGS, RestClient, ServiceConfig } from '@reyaxyz/common';
|
|
2
|
+
|
|
3
|
+
export type BroadcastTransactionMetadata = {
|
|
4
|
+
accountName?: string;
|
|
5
|
+
};
|
|
6
|
+
export class TransactionModule extends RestClient {
|
|
7
|
+
async executeTransaction(
|
|
8
|
+
signedTx: string,
|
|
9
|
+
contractType: string,
|
|
10
|
+
metadata: BroadcastTransactionMetadata,
|
|
11
|
+
) {
|
|
12
|
+
const uri = `/api/transaction/execute`;
|
|
13
|
+
return this.post<{ txHash: string }>(
|
|
14
|
+
uri,
|
|
15
|
+
{},
|
|
16
|
+
{
|
|
17
|
+
signedTx,
|
|
18
|
+
contractType,
|
|
19
|
+
metadata,
|
|
20
|
+
},
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Internal state
|
|
26
|
+
let transactionModule: TransactionModule = new TransactionModule(
|
|
27
|
+
API_CLIENT_CONFIGS['test'].apiEndpoint,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
// Function to configure the SDK
|
|
31
|
+
export const configureSDK = (
|
|
32
|
+
environment: ServiceConfig['environment'],
|
|
33
|
+
): void => {
|
|
34
|
+
const config = API_CLIENT_CONFIGS[environment] || API_CLIENT_CONFIGS['test'];
|
|
35
|
+
transactionModule = new TransactionModule(config.apiEndpoint);
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const getTransactionModuleClient = (): TransactionModule => {
|
|
39
|
+
if (!transactionModule)
|
|
40
|
+
throw new Error('SDK is not configured! Call configureSDK()');
|
|
41
|
+
return transactionModule;
|
|
42
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { JsonRpcSigner, Signer } from 'ethers';
|
|
2
2
|
import { ContractType, getAddress } from '../utils/contractAddresses';
|
|
3
|
+
import { getGasBuffer } from '../utils/txHelpers';
|
|
4
|
+
import {
|
|
5
|
+
BroadcastTransactionMetadata,
|
|
6
|
+
getTransactionModuleClient,
|
|
7
|
+
} from '../config';
|
|
3
8
|
|
|
4
9
|
export type Transaction = {
|
|
5
10
|
from: string;
|
|
@@ -22,6 +27,8 @@ export async function estimateGas(
|
|
|
22
27
|
}
|
|
23
28
|
> {
|
|
24
29
|
const accountAddress = await signer.getAddress();
|
|
30
|
+
const nonce = await signer.getNonce();
|
|
31
|
+
|
|
25
32
|
const contractAddress = Object.values(ContractType).includes(
|
|
26
33
|
targetContract as ContractType,
|
|
27
34
|
)
|
|
@@ -32,9 +39,20 @@ export async function estimateGas(
|
|
|
32
39
|
to: contractAddress,
|
|
33
40
|
data,
|
|
34
41
|
...(value && value !== '0' ? { value: value } : {}),
|
|
42
|
+
chainId: chainId,
|
|
43
|
+
nonce,
|
|
35
44
|
};
|
|
36
45
|
|
|
37
|
-
|
|
46
|
+
let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
const gasEstimate = await signer.estimateGas(tx);
|
|
50
|
+
gasLimit = getGasBuffer(gasEstimate);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
// sentry error & throw
|
|
53
|
+
console.warn(error);
|
|
54
|
+
}
|
|
55
|
+
|
|
38
56
|
const maxPriorityFeePerGas: bigint = BigInt('0');
|
|
39
57
|
const maxFeePerGas: bigint = BigInt('0');
|
|
40
58
|
return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };
|
|
@@ -64,3 +82,35 @@ export async function executeTransaction(
|
|
|
64
82
|
throw new Error('Transaction Execution Error');
|
|
65
83
|
}
|
|
66
84
|
}
|
|
85
|
+
|
|
86
|
+
export async function signAndBroadcastTransaction(
|
|
87
|
+
signer: Signer | JsonRpcSigner,
|
|
88
|
+
data: string,
|
|
89
|
+
value: string,
|
|
90
|
+
chainId: number,
|
|
91
|
+
targetContract: ContractType | string,
|
|
92
|
+
metadata: BroadcastTransactionMetadata,
|
|
93
|
+
): Promise<{ hash: string }> {
|
|
94
|
+
const txData = await estimateGas(
|
|
95
|
+
signer,
|
|
96
|
+
data,
|
|
97
|
+
value,
|
|
98
|
+
chainId,
|
|
99
|
+
targetContract,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const signedTx = await signer.signTransaction(txData);
|
|
104
|
+
|
|
105
|
+
const transactionModuleClient = getTransactionModuleClient();
|
|
106
|
+
const { txHash } = await transactionModuleClient.executeTransaction(
|
|
107
|
+
signedTx,
|
|
108
|
+
targetContract,
|
|
109
|
+
metadata,
|
|
110
|
+
);
|
|
111
|
+
return { hash: txHash };
|
|
112
|
+
} catch (error) {
|
|
113
|
+
console.warn(error);
|
|
114
|
+
throw new Error('Transaction Execution Error');
|
|
115
|
+
}
|
|
116
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { signAndBroadcastTransaction } from '../executeTransaction';
|
|
2
2
|
import { encodeCreateAccountCall } from './encode';
|
|
3
3
|
import { CreateAccountParams, CreateAccountResult } from './types';
|
|
4
4
|
import { ContractType } from '../../utils/contractAddresses';
|
|
@@ -13,12 +13,13 @@ export const createAccount = async (
|
|
|
13
13
|
const network = await params.signer.provider?.getNetwork();
|
|
14
14
|
const chainId = Number(network?.chainId);
|
|
15
15
|
|
|
16
|
-
const result = await
|
|
16
|
+
const result = await signAndBroadcastTransaction(
|
|
17
17
|
params.signer,
|
|
18
18
|
data,
|
|
19
19
|
value,
|
|
20
20
|
chainId,
|
|
21
21
|
ContractType.CORE_PROXY,
|
|
22
|
+
{ accountName: params.name },
|
|
22
23
|
);
|
|
23
24
|
return {
|
|
24
25
|
transactionHash: result?.hash || null,
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
MatchOrderResult,
|
|
6
6
|
} from './types';
|
|
7
7
|
import { encodeMatchOrder } from './encode';
|
|
8
|
-
import {
|
|
8
|
+
import { signAndBroadcastTransaction } from '../executeTransaction';
|
|
9
9
|
import { calculatePriceLimitForTrade } from '../../utils/trade';
|
|
10
10
|
import BigNumber from 'bignumber.js';
|
|
11
11
|
import { ContractType } from '../../utils/contractAddresses';
|
|
@@ -36,12 +36,13 @@ export const matchOrder = async (
|
|
|
36
36
|
const network = await params.signer.provider?.getNetwork();
|
|
37
37
|
const chainId = Number(network?.chainId);
|
|
38
38
|
|
|
39
|
-
const result = await
|
|
39
|
+
const result = await signAndBroadcastTransaction(
|
|
40
40
|
params.signer,
|
|
41
41
|
data,
|
|
42
42
|
value,
|
|
43
43
|
chainId,
|
|
44
44
|
ContractType.CORE_PROXY,
|
|
45
|
+
{},
|
|
45
46
|
);
|
|
46
47
|
|
|
47
48
|
return {
|
|
@@ -74,12 +75,13 @@ export const closeOrder = async (
|
|
|
74
75
|
const network = await params.signer.provider?.getNetwork();
|
|
75
76
|
const chainId = Number(network?.chainId);
|
|
76
77
|
|
|
77
|
-
const result = await
|
|
78
|
+
const result = await signAndBroadcastTransaction(
|
|
78
79
|
params.signer,
|
|
79
80
|
data,
|
|
80
81
|
value,
|
|
81
82
|
chainId,
|
|
82
83
|
ContractType.CORE_PROXY,
|
|
84
|
+
{},
|
|
83
85
|
);
|
|
84
86
|
|
|
85
87
|
return {
|