genlayer-js 0.6.2 → 0.6.3
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/CHANGELOG.md +2 -0
- package/dist/chains/index.cjs +2 -2
- package/dist/chains/index.d.cts +2 -2
- package/dist/chains/index.d.ts +2 -2
- package/dist/chains/index.js +1 -1
- package/dist/chains-C5PI_Nr_.d.cts +13 -0
- package/dist/chains-C5PI_Nr_.d.ts +13 -0
- package/dist/{chunk-3TYB36DW.js → chunk-O3T2C57T.js} +4 -1
- package/dist/{chunk-IINRDYKF.cjs → chunk-XAQMWSP4.cjs} +4 -1
- package/dist/{index-SfwloK-R.d.cts → index-CHQn8ZME.d.cts} +1 -1
- package/dist/{index-B5TpaPQ6.d.ts → index-CioK5j0r.d.ts} +1 -1
- package/dist/{index-CIFGsVKF.d.cts → index-FMf4Unm0.d.cts} +4 -3
- package/dist/{index-B1ZR98OZ.d.ts → index-vnyqXaRU.d.ts} +4 -3
- package/dist/index.cjs +89 -61
- package/dist/index.d.cts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +84 -56
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.ts +2 -2
- package/package.json +1 -1
- package/src/chains/actions.ts +40 -0
- package/src/chains/simulator.ts +3 -0
- package/src/client/client.ts +47 -42
- package/src/contracts/actions.ts +75 -77
- package/src/types/chains.ts +9 -1
- package/src/types/clients.ts +6 -4
- package/dist/chains-BV4Glo-M.d.cts +0 -5
- package/dist/chains-BV4Glo-M.d.ts +0 -5
package/CHANGELOG.md
CHANGED
package/dist/chains/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var _chunkXAQMWSP4cjs = require('../chunk-XAQMWSP4.cjs');
|
|
4
4
|
require('../chunk-75ZPJI57.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
|
-
exports.simulator =
|
|
7
|
+
exports.simulator = _chunkXAQMWSP4cjs.simulator;
|
package/dist/chains/index.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { s as simulator } from '../index-
|
|
2
|
-
import '../chains-
|
|
1
|
+
export { s as simulator } from '../index-CHQn8ZME.cjs';
|
|
2
|
+
import '../chains-C5PI_Nr_.cjs';
|
|
3
3
|
import 'viem';
|
package/dist/chains/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { s as simulator } from '../index-
|
|
2
|
-
import '../chains-
|
|
1
|
+
export { s as simulator } from '../index-CioK5j0r.js';
|
|
2
|
+
import '../chains-C5PI_Nr_.js';
|
|
3
3
|
import 'viem';
|
package/dist/chains/index.js
CHANGED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Chain } from 'viem';
|
|
2
|
+
|
|
3
|
+
type SimulatorChain = Chain & {
|
|
4
|
+
consensusMainContract: {
|
|
5
|
+
address: string;
|
|
6
|
+
abi: any[];
|
|
7
|
+
bytecode: string;
|
|
8
|
+
} | null;
|
|
9
|
+
defaultNumberOfInitialValidators: number;
|
|
10
|
+
defaultConsensusMaxRotations: number;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type { SimulatorChain as S };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Chain } from 'viem';
|
|
2
|
+
|
|
3
|
+
type SimulatorChain = Chain & {
|
|
4
|
+
consensusMainContract: {
|
|
5
|
+
address: string;
|
|
6
|
+
abi: any[];
|
|
7
|
+
bytecode: string;
|
|
8
|
+
} | null;
|
|
9
|
+
defaultNumberOfInitialValidators: number;
|
|
10
|
+
defaultConsensusMaxRotations: number;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export type { SimulatorChain as S };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as SimulatorChain } from './chains-
|
|
2
|
-
import { Client, Transport, PublicActions, Account } from 'viem';
|
|
1
|
+
import { S as SimulatorChain } from './chains-C5PI_Nr_.cjs';
|
|
2
|
+
import { Client, Transport, WalletActions, PublicActions, Account } from 'viem';
|
|
3
3
|
|
|
4
4
|
type Address = `0x${string}` & {
|
|
5
5
|
length: 42;
|
|
@@ -116,7 +116,7 @@ type GenLayerMethod = {
|
|
|
116
116
|
method: "eth_getTransactionCount";
|
|
117
117
|
params: [address: string, block: string];
|
|
118
118
|
};
|
|
119
|
-
type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transport, TSimulatorChain>, "transport" | "getTransaction" | "readContract"> & Omit<PublicActions<Transport, TSimulatorChain>, "readContract" | "getTransaction" | "waitForTransactionReceipt"> & {
|
|
119
|
+
type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transport, TSimulatorChain>, "transport" | "getTransaction" | "readContract"> & Omit<WalletActions<TSimulatorChain>, "deployContract" | "writeContract"> & Omit<PublicActions<Transport, TSimulatorChain>, "readContract" | "getTransaction" | "waitForTransactionReceipt"> & {
|
|
120
120
|
request: Client<Transport, TSimulatorChain>["request"] & {
|
|
121
121
|
<TMethod extends GenLayerMethod>(args: Extract<GenLayerMethod, {
|
|
122
122
|
method: TMethod["method"];
|
|
@@ -167,6 +167,7 @@ type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transp
|
|
|
167
167
|
}) => Promise<GenLayerTransaction>;
|
|
168
168
|
getContractSchema: (address: string) => Promise<ContractSchema>;
|
|
169
169
|
getContractSchemaForCode: (contractCode: string | Uint8Array) => Promise<ContractSchema>;
|
|
170
|
+
initializeConsensusSmartContract: (forceReset?: boolean) => Promise<void>;
|
|
170
171
|
};
|
|
171
172
|
|
|
172
173
|
export { type Address as A, type CalldataEncodable as C, type GenLayerClient as G, type MethodDescription as M, type TransactionDataElement$1 as T, CalldataAddress as a, type GenLayerMethod as b, type ContractParamsArraySchemaElement as c, type ContractParamsSchema as d, type ContractMethodBase as e, type ContractMethod as f, type ContractSchema as g, type TransactionHash as h, TransactionStatus as i, type GenLayerTransaction as j, type TransactionDataElement as k };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as SimulatorChain } from './chains-
|
|
2
|
-
import { Client, Transport, PublicActions, Account } from 'viem';
|
|
1
|
+
import { S as SimulatorChain } from './chains-C5PI_Nr_.js';
|
|
2
|
+
import { Client, Transport, WalletActions, PublicActions, Account } from 'viem';
|
|
3
3
|
|
|
4
4
|
type Address = `0x${string}` & {
|
|
5
5
|
length: 42;
|
|
@@ -116,7 +116,7 @@ type GenLayerMethod = {
|
|
|
116
116
|
method: "eth_getTransactionCount";
|
|
117
117
|
params: [address: string, block: string];
|
|
118
118
|
};
|
|
119
|
-
type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transport, TSimulatorChain>, "transport" | "getTransaction" | "readContract"> & Omit<PublicActions<Transport, TSimulatorChain>, "readContract" | "getTransaction" | "waitForTransactionReceipt"> & {
|
|
119
|
+
type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transport, TSimulatorChain>, "transport" | "getTransaction" | "readContract"> & Omit<WalletActions<TSimulatorChain>, "deployContract" | "writeContract"> & Omit<PublicActions<Transport, TSimulatorChain>, "readContract" | "getTransaction" | "waitForTransactionReceipt"> & {
|
|
120
120
|
request: Client<Transport, TSimulatorChain>["request"] & {
|
|
121
121
|
<TMethod extends GenLayerMethod>(args: Extract<GenLayerMethod, {
|
|
122
122
|
method: TMethod["method"];
|
|
@@ -167,6 +167,7 @@ type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<Client<Transp
|
|
|
167
167
|
}) => Promise<GenLayerTransaction>;
|
|
168
168
|
getContractSchema: (address: string) => Promise<ContractSchema>;
|
|
169
169
|
getContractSchemaForCode: (contractCode: string | Uint8Array) => Promise<ContractSchema>;
|
|
170
|
+
initializeConsensusSmartContract: (forceReset?: boolean) => Promise<void>;
|
|
170
171
|
};
|
|
171
172
|
|
|
172
173
|
export { type Address as A, type CalldataEncodable as C, type GenLayerClient as G, type MethodDescription as M, type TransactionDataElement$1 as T, CalldataAddress as a, type GenLayerMethod as b, type ContractParamsArraySchemaElement as c, type ContractParamsSchema as d, type ContractMethodBase as e, type ContractMethod as f, type ContractSchema as g, type TransactionHash as h, TransactionStatus as i, type GenLayerTransaction as j, type TransactionDataElement as k };
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var
|
|
4
|
+
var _chunkXAQMWSP4cjs = require('./chunk-XAQMWSP4.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
var _chunk52HYC24Lcjs = require('./chunk-52HYC24L.cjs');
|
|
@@ -16,7 +16,7 @@ var _viem = require('viem');
|
|
|
16
16
|
function accountActions(client) {
|
|
17
17
|
return {
|
|
18
18
|
fundAccount: async ({ address, amount }) => {
|
|
19
|
-
if (_optionalChain([client, 'access', _ => _.chain, 'optionalAccess', _2 => _2.id]) !==
|
|
19
|
+
if (_optionalChain([client, 'access', _ => _.chain, 'optionalAccess', _2 => _2.id]) !== _chunkXAQMWSP4cjs.simulator.id) {
|
|
20
20
|
throw new Error("Client is not connected to the simulator");
|
|
21
21
|
}
|
|
22
22
|
return client.request({
|
|
@@ -412,7 +412,14 @@ var contractActions = (client) => {
|
|
|
412
412
|
};
|
|
413
413
|
var overrideContractActions = (client) => {
|
|
414
414
|
client.readContract = async (args) => {
|
|
415
|
-
const {
|
|
415
|
+
const {
|
|
416
|
+
account,
|
|
417
|
+
address,
|
|
418
|
+
functionName,
|
|
419
|
+
args: callArgs,
|
|
420
|
+
kwargs,
|
|
421
|
+
stateStatus = "ACCEPTED" /* ACCEPTED */
|
|
422
|
+
} = args;
|
|
416
423
|
const encodedData = encode(makeCalldataObject(functionName, callArgs, kwargs));
|
|
417
424
|
const serializedData = serializeOne(encodedData);
|
|
418
425
|
const senderAddress = _nullishCoalesce(_optionalChain([account, 'optionalAccess', _5 => _5.address]), () => ( _optionalChain([client, 'access', _6 => _6.account, 'optionalAccess', _7 => _7.address])));
|
|
@@ -435,75 +442,62 @@ var overrideContractActions = (client) => {
|
|
|
435
442
|
const { account, address, functionName, args: callArgs, kwargs, value = 0n, leaderOnly = false } = args;
|
|
436
443
|
const data = [encode(makeCalldataObject(functionName, callArgs, kwargs)), leaderOnly];
|
|
437
444
|
const serializedData = serialize(data);
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
method: "eth_sendTransaction",
|
|
448
|
-
params: [transaction]
|
|
449
|
-
});
|
|
450
|
-
}
|
|
445
|
+
return _sendTransaction(address, serializedData, account || client.account, value);
|
|
446
|
+
};
|
|
447
|
+
client.deployContract = async (args) => {
|
|
448
|
+
const { account, code, args: constructorArgs, kwargs, leaderOnly = false } = args;
|
|
449
|
+
const data = [code, encode(makeCalldataObject(void 0, constructorArgs, kwargs)), leaderOnly];
|
|
450
|
+
const serializedData = serialize(data);
|
|
451
|
+
return _sendTransaction(_viem.zeroAddress, serializedData, account || client.account);
|
|
452
|
+
};
|
|
453
|
+
const _sendTransaction = async (recipient, data, senderAccount, value) => {
|
|
451
454
|
if (!senderAccount) {
|
|
452
455
|
throw new Error(
|
|
453
456
|
"No account set. Configure the client with an account or pass an account to this function."
|
|
454
457
|
);
|
|
455
458
|
}
|
|
456
|
-
if (!_optionalChain([
|
|
457
|
-
throw new Error(
|
|
459
|
+
if (!_optionalChain([client, 'access', _8 => _8.chain, 'access', _9 => _9.consensusMainContract, 'optionalAccess', _10 => _10.address])) {
|
|
460
|
+
throw new Error(
|
|
461
|
+
"Consensus main contract not initialized. Please ensure client is properly initialized."
|
|
462
|
+
);
|
|
458
463
|
}
|
|
464
|
+
const encodedData = _viem.encodeFunctionData.call(void 0, {
|
|
465
|
+
abi: _optionalChain([client, 'access', _11 => _11.chain, 'access', _12 => _12.consensusMainContract, 'optionalAccess', _13 => _13.abi]),
|
|
466
|
+
functionName: "addTransaction",
|
|
467
|
+
args: [
|
|
468
|
+
senderAccount.address,
|
|
469
|
+
recipient,
|
|
470
|
+
client.chain.defaultNumberOfInitialValidators,
|
|
471
|
+
client.chain.defaultConsensusMaxRotations,
|
|
472
|
+
data
|
|
473
|
+
]
|
|
474
|
+
});
|
|
459
475
|
const nonce = await client.getCurrentNonce({ address: senderAccount.address });
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
to: address,
|
|
463
|
-
|
|
476
|
+
const transactionRequest = await client.prepareTransactionRequest({
|
|
477
|
+
account: senderAccount,
|
|
478
|
+
to: _optionalChain([client, 'access', _14 => _14.chain, 'access', _15 => _15.consensusMainContract, 'optionalAccess', _16 => _16.address]),
|
|
479
|
+
data: encodedData,
|
|
464
480
|
type: "legacy",
|
|
465
|
-
nonce
|
|
481
|
+
nonce,
|
|
482
|
+
value: _nullishCoalesce(value, () => ( 0n))
|
|
466
483
|
});
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
const { account, code, args: constructorArgs, kwargs, leaderOnly = false } = args;
|
|
474
|
-
const data = [code, encode(makeCalldataObject(void 0, constructorArgs, kwargs)), leaderOnly];
|
|
475
|
-
const serializedData = serialize(data);
|
|
476
|
-
const senderAccount = account || client.account;
|
|
477
|
-
if (_optionalChain([senderAccount, 'optionalAccess', _11 => _11.type]) !== "local") {
|
|
478
|
-
const transaction = {
|
|
479
|
-
from: _optionalChain([senderAccount, 'optionalAccess', _12 => _12.address]),
|
|
480
|
-
to: null,
|
|
481
|
-
data: serializedData,
|
|
482
|
-
value: "0x0"
|
|
484
|
+
if (_optionalChain([senderAccount, 'optionalAccess', _17 => _17.type]) !== "local") {
|
|
485
|
+
const formattedRequest = {
|
|
486
|
+
from: transactionRequest.from,
|
|
487
|
+
to: transactionRequest.to,
|
|
488
|
+
data: encodedData,
|
|
489
|
+
value: transactionRequest.value ? `0x${transactionRequest.value.toString(16)}` : "0x0"
|
|
483
490
|
};
|
|
484
491
|
return await client.request({
|
|
485
492
|
method: "eth_sendTransaction",
|
|
486
|
-
params: [
|
|
493
|
+
params: [formattedRequest]
|
|
487
494
|
});
|
|
488
495
|
}
|
|
489
|
-
if (!senderAccount) {
|
|
490
|
-
throw new Error(
|
|
491
|
-
"No account set. Configure the client with an account or pass an account to this function."
|
|
492
|
-
);
|
|
493
|
-
}
|
|
494
|
-
if (!_optionalChain([senderAccount, 'optionalAccess', _13 => _13.signTransaction])) {
|
|
496
|
+
if (!_optionalChain([senderAccount, 'optionalAccess', _18 => _18.signTransaction])) {
|
|
495
497
|
throw new Error("Account does not support signTransaction");
|
|
496
498
|
}
|
|
497
|
-
const
|
|
498
|
-
|
|
499
|
-
data: serializedData,
|
|
500
|
-
type: "legacy",
|
|
501
|
-
nonce
|
|
502
|
-
});
|
|
503
|
-
return await client.request({
|
|
504
|
-
method: "eth_sendRawTransaction",
|
|
505
|
-
params: [signedTransaction]
|
|
506
|
-
});
|
|
499
|
+
const serializedTransaction = await senderAccount.signTransaction(transactionRequest);
|
|
500
|
+
return client.sendRawTransaction({ serializedTransaction });
|
|
507
501
|
};
|
|
508
502
|
return client;
|
|
509
503
|
};
|
|
@@ -547,16 +541,47 @@ var transactionActions = (client) => ({
|
|
|
547
541
|
}
|
|
548
542
|
});
|
|
549
543
|
|
|
544
|
+
// src/chains/actions.ts
|
|
545
|
+
function chainActions(client) {
|
|
546
|
+
return {
|
|
547
|
+
initializeConsensusSmartContract: async (forceReset = false) => {
|
|
548
|
+
if (_optionalChain([client, 'access', _19 => _19.chain, 'optionalAccess', _20 => _20.id]) !== _chunkXAQMWSP4cjs.simulator.id) {
|
|
549
|
+
throw new Error("Client is not connected to the simulator");
|
|
550
|
+
}
|
|
551
|
+
if (!forceReset && _optionalChain([client, 'access', _21 => _21.chain, 'access', _22 => _22.consensusMainContract, 'optionalAccess', _23 => _23.address]) && _optionalChain([client, 'access', _24 => _24.chain, 'access', _25 => _25.consensusMainContract, 'optionalAccess', _26 => _26.abi])) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
const contractsResponse = await fetch(client.chain.rpcUrls.default.http[0], {
|
|
555
|
+
method: "POST",
|
|
556
|
+
headers: {
|
|
557
|
+
"Content-Type": "application/json"
|
|
558
|
+
},
|
|
559
|
+
body: JSON.stringify({
|
|
560
|
+
jsonrpc: "2.0",
|
|
561
|
+
id: Date.now(),
|
|
562
|
+
method: "sim_getConsensusContract",
|
|
563
|
+
params: ["ConsensusMain"]
|
|
564
|
+
})
|
|
565
|
+
});
|
|
566
|
+
if (!contractsResponse.ok) {
|
|
567
|
+
throw new Error("Failed to fetch ConsensusMain contract");
|
|
568
|
+
}
|
|
569
|
+
const consensusMainContract = await contractsResponse.json();
|
|
570
|
+
client.chain.consensusMainContract = consensusMainContract.result;
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
550
575
|
// src/client/client.ts
|
|
551
|
-
var createClient = (config = { chain:
|
|
552
|
-
const chainConfig = config.chain ||
|
|
576
|
+
var createClient = (config = { chain: _chunkXAQMWSP4cjs.simulator }) => {
|
|
577
|
+
const chainConfig = config.chain || _chunkXAQMWSP4cjs.simulator;
|
|
553
578
|
const rpcUrl = config.endpoint || chainConfig.rpcUrls.default.http[0];
|
|
554
579
|
const isAddress = typeof config.account !== "object";
|
|
555
580
|
const customTransport = {
|
|
556
581
|
async request({ method, params }) {
|
|
557
582
|
if (method.startsWith("eth_") && isAddress) {
|
|
558
583
|
try {
|
|
559
|
-
return await _optionalChain([window, 'access',
|
|
584
|
+
return await _optionalChain([window, 'access', _27 => _27.ethereum, 'optionalAccess', _28 => _28.request, 'call', _29 => _29({ method, params })]);
|
|
560
585
|
} catch (err) {
|
|
561
586
|
console.warn(`Error using window.ethereum for method ${method}:`, err);
|
|
562
587
|
throw err;
|
|
@@ -591,7 +616,10 @@ var createClient = (config = { chain: _chunkIINRDYKFcjs.simulator }) => {
|
|
|
591
616
|
chain: chainConfig,
|
|
592
617
|
transport: _viem.custom.call(void 0, customTransport),
|
|
593
618
|
...config.account ? { account: config.account } : {}
|
|
594
|
-
}).extend(_viem.publicActions).extend((client) => accountActions(client)).extend((client) => transactionActions(client)).extend((client) => contractActions(client));
|
|
619
|
+
}).extend(_viem.publicActions).extend(_viem.walletActions).extend((client) => accountActions(client)).extend((client) => transactionActions(client)).extend((client) => contractActions(client)).extend((client) => chainActions(client));
|
|
620
|
+
baseClient.initializeConsensusSmartContract().catch((error) => {
|
|
621
|
+
console.error("Failed to initialize consensus smart contract:", error);
|
|
622
|
+
});
|
|
595
623
|
return overrideContractActions(baseClient);
|
|
596
624
|
};
|
|
597
625
|
|
|
@@ -618,4 +646,4 @@ var transactions = transactions_exports;
|
|
|
618
646
|
|
|
619
647
|
|
|
620
648
|
|
|
621
|
-
exports.abi = abi_exports; exports.chains =
|
|
649
|
+
exports.abi = abi_exports; exports.chains = _chunkXAQMWSP4cjs.chains_exports; exports.createAccount = createAccount; exports.createClient = createClient; exports.generatePrivateKey = generatePrivateKey;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as viem from 'viem';
|
|
2
2
|
import { Account, Address } from 'viem';
|
|
3
|
-
import { S as SimulatorChain } from './chains-
|
|
4
|
-
import { G as GenLayerClient, C as CalldataEncodable, T as TransactionDataElement } from './index-
|
|
3
|
+
import { S as SimulatorChain } from './chains-C5PI_Nr_.cjs';
|
|
4
|
+
import { G as GenLayerClient, C as CalldataEncodable, T as TransactionDataElement } from './index-FMf4Unm0.cjs';
|
|
5
5
|
import * as abitype from 'abitype';
|
|
6
6
|
import * as viem__types_experimental_eip7702_types_authorization from 'viem/_types/experimental/eip7702/types/authorization';
|
|
7
7
|
import * as viem_accounts from 'viem/accounts';
|
|
8
|
-
export { i as chains } from './index-
|
|
8
|
+
export { i as chains } from './index-CHQn8ZME.cjs';
|
|
9
9
|
|
|
10
10
|
interface ClientConfig {
|
|
11
11
|
chain?: {
|
package/dist/index.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import * as viem from 'viem';
|
|
2
2
|
import { Account, Address } from 'viem';
|
|
3
|
-
import { S as SimulatorChain } from './chains-
|
|
4
|
-
import { G as GenLayerClient, C as CalldataEncodable, T as TransactionDataElement } from './index-
|
|
3
|
+
import { S as SimulatorChain } from './chains-C5PI_Nr_.js';
|
|
4
|
+
import { G as GenLayerClient, C as CalldataEncodable, T as TransactionDataElement } from './index-vnyqXaRU.js';
|
|
5
5
|
import * as abitype from 'abitype';
|
|
6
6
|
import * as viem__types_experimental_eip7702_types_authorization from 'viem/_types/experimental/eip7702/types/authorization';
|
|
7
7
|
import * as viem_accounts from 'viem/accounts';
|
|
8
|
-
export { i as chains } from './index-
|
|
8
|
+
export { i as chains } from './index-CioK5j0r.js';
|
|
9
9
|
|
|
10
10
|
interface ClientConfig {
|
|
11
11
|
chain?: {
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
chains_exports,
|
|
3
3
|
simulator
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-O3T2C57T.js";
|
|
5
5
|
import {
|
|
6
6
|
CalldataAddress
|
|
7
7
|
} from "./chunk-2EJVFGKU.js";
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
} from "./chunk-MLKGABMK.js";
|
|
11
11
|
|
|
12
12
|
// src/client/client.ts
|
|
13
|
-
import { createClient as createViemClient, publicActions, custom } from "viem";
|
|
13
|
+
import { createClient as createViemClient, publicActions, custom, walletActions } from "viem";
|
|
14
14
|
|
|
15
15
|
// src/accounts/actions.ts
|
|
16
16
|
function accountActions(client) {
|
|
@@ -365,7 +365,7 @@ function serialize(data) {
|
|
|
365
365
|
}
|
|
366
366
|
|
|
367
367
|
// src/contracts/actions.ts
|
|
368
|
-
import { fromHex, toHex as toHex2 } from "viem";
|
|
368
|
+
import { fromHex, toHex as toHex2, zeroAddress, encodeFunctionData } from "viem";
|
|
369
369
|
function makeCalldataObject(method, args, kwargs) {
|
|
370
370
|
let ret = {};
|
|
371
371
|
if (method) {
|
|
@@ -412,7 +412,14 @@ var contractActions = (client) => {
|
|
|
412
412
|
};
|
|
413
413
|
var overrideContractActions = (client) => {
|
|
414
414
|
client.readContract = async (args) => {
|
|
415
|
-
const {
|
|
415
|
+
const {
|
|
416
|
+
account,
|
|
417
|
+
address,
|
|
418
|
+
functionName,
|
|
419
|
+
args: callArgs,
|
|
420
|
+
kwargs,
|
|
421
|
+
stateStatus = "ACCEPTED" /* ACCEPTED */
|
|
422
|
+
} = args;
|
|
416
423
|
const encodedData = encode(makeCalldataObject(functionName, callArgs, kwargs));
|
|
417
424
|
const serializedData = serializeOne(encodedData);
|
|
418
425
|
const senderAddress = account?.address ?? client.account?.address;
|
|
@@ -435,75 +442,62 @@ var overrideContractActions = (client) => {
|
|
|
435
442
|
const { account, address, functionName, args: callArgs, kwargs, value = 0n, leaderOnly = false } = args;
|
|
436
443
|
const data = [encode(makeCalldataObject(functionName, callArgs, kwargs)), leaderOnly];
|
|
437
444
|
const serializedData = serialize(data);
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
method: "eth_sendTransaction",
|
|
448
|
-
params: [transaction]
|
|
449
|
-
});
|
|
450
|
-
}
|
|
445
|
+
return _sendTransaction(address, serializedData, account || client.account, value);
|
|
446
|
+
};
|
|
447
|
+
client.deployContract = async (args) => {
|
|
448
|
+
const { account, code, args: constructorArgs, kwargs, leaderOnly = false } = args;
|
|
449
|
+
const data = [code, encode(makeCalldataObject(void 0, constructorArgs, kwargs)), leaderOnly];
|
|
450
|
+
const serializedData = serialize(data);
|
|
451
|
+
return _sendTransaction(zeroAddress, serializedData, account || client.account);
|
|
452
|
+
};
|
|
453
|
+
const _sendTransaction = async (recipient, data, senderAccount, value) => {
|
|
451
454
|
if (!senderAccount) {
|
|
452
455
|
throw new Error(
|
|
453
456
|
"No account set. Configure the client with an account or pass an account to this function."
|
|
454
457
|
);
|
|
455
458
|
}
|
|
456
|
-
if (!
|
|
457
|
-
throw new Error(
|
|
459
|
+
if (!client.chain.consensusMainContract?.address) {
|
|
460
|
+
throw new Error(
|
|
461
|
+
"Consensus main contract not initialized. Please ensure client is properly initialized."
|
|
462
|
+
);
|
|
458
463
|
}
|
|
464
|
+
const encodedData = encodeFunctionData({
|
|
465
|
+
abi: client.chain.consensusMainContract?.abi,
|
|
466
|
+
functionName: "addTransaction",
|
|
467
|
+
args: [
|
|
468
|
+
senderAccount.address,
|
|
469
|
+
recipient,
|
|
470
|
+
client.chain.defaultNumberOfInitialValidators,
|
|
471
|
+
client.chain.defaultConsensusMaxRotations,
|
|
472
|
+
data
|
|
473
|
+
]
|
|
474
|
+
});
|
|
459
475
|
const nonce = await client.getCurrentNonce({ address: senderAccount.address });
|
|
460
|
-
const
|
|
461
|
-
|
|
462
|
-
to: address,
|
|
463
|
-
|
|
476
|
+
const transactionRequest = await client.prepareTransactionRequest({
|
|
477
|
+
account: senderAccount,
|
|
478
|
+
to: client.chain.consensusMainContract?.address,
|
|
479
|
+
data: encodedData,
|
|
464
480
|
type: "legacy",
|
|
465
|
-
nonce
|
|
466
|
-
|
|
467
|
-
return await client.request({
|
|
468
|
-
method: "eth_sendRawTransaction",
|
|
469
|
-
params: [signedTransaction]
|
|
481
|
+
nonce,
|
|
482
|
+
value: value ?? 0n
|
|
470
483
|
});
|
|
471
|
-
};
|
|
472
|
-
client.deployContract = async (args) => {
|
|
473
|
-
const { account, code, args: constructorArgs, kwargs, leaderOnly = false } = args;
|
|
474
|
-
const data = [code, encode(makeCalldataObject(void 0, constructorArgs, kwargs)), leaderOnly];
|
|
475
|
-
const serializedData = serialize(data);
|
|
476
|
-
const senderAccount = account || client.account;
|
|
477
484
|
if (senderAccount?.type !== "local") {
|
|
478
|
-
const
|
|
479
|
-
from:
|
|
480
|
-
to:
|
|
481
|
-
data:
|
|
482
|
-
value: "0x0"
|
|
485
|
+
const formattedRequest = {
|
|
486
|
+
from: transactionRequest.from,
|
|
487
|
+
to: transactionRequest.to,
|
|
488
|
+
data: encodedData,
|
|
489
|
+
value: transactionRequest.value ? `0x${transactionRequest.value.toString(16)}` : "0x0"
|
|
483
490
|
};
|
|
484
491
|
return await client.request({
|
|
485
492
|
method: "eth_sendTransaction",
|
|
486
|
-
params: [
|
|
493
|
+
params: [formattedRequest]
|
|
487
494
|
});
|
|
488
495
|
}
|
|
489
|
-
if (!senderAccount) {
|
|
490
|
-
throw new Error(
|
|
491
|
-
"No account set. Configure the client with an account or pass an account to this function."
|
|
492
|
-
);
|
|
493
|
-
}
|
|
494
496
|
if (!senderAccount?.signTransaction) {
|
|
495
497
|
throw new Error("Account does not support signTransaction");
|
|
496
498
|
}
|
|
497
|
-
const
|
|
498
|
-
|
|
499
|
-
data: serializedData,
|
|
500
|
-
type: "legacy",
|
|
501
|
-
nonce
|
|
502
|
-
});
|
|
503
|
-
return await client.request({
|
|
504
|
-
method: "eth_sendRawTransaction",
|
|
505
|
-
params: [signedTransaction]
|
|
506
|
-
});
|
|
499
|
+
const serializedTransaction = await senderAccount.signTransaction(transactionRequest);
|
|
500
|
+
return client.sendRawTransaction({ serializedTransaction });
|
|
507
501
|
};
|
|
508
502
|
return client;
|
|
509
503
|
};
|
|
@@ -547,6 +541,37 @@ var transactionActions = (client) => ({
|
|
|
547
541
|
}
|
|
548
542
|
});
|
|
549
543
|
|
|
544
|
+
// src/chains/actions.ts
|
|
545
|
+
function chainActions(client) {
|
|
546
|
+
return {
|
|
547
|
+
initializeConsensusSmartContract: async (forceReset = false) => {
|
|
548
|
+
if (client.chain?.id !== simulator.id) {
|
|
549
|
+
throw new Error("Client is not connected to the simulator");
|
|
550
|
+
}
|
|
551
|
+
if (!forceReset && client.chain.consensusMainContract?.address && client.chain.consensusMainContract?.abi) {
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
const contractsResponse = await fetch(client.chain.rpcUrls.default.http[0], {
|
|
555
|
+
method: "POST",
|
|
556
|
+
headers: {
|
|
557
|
+
"Content-Type": "application/json"
|
|
558
|
+
},
|
|
559
|
+
body: JSON.stringify({
|
|
560
|
+
jsonrpc: "2.0",
|
|
561
|
+
id: Date.now(),
|
|
562
|
+
method: "sim_getConsensusContract",
|
|
563
|
+
params: ["ConsensusMain"]
|
|
564
|
+
})
|
|
565
|
+
});
|
|
566
|
+
if (!contractsResponse.ok) {
|
|
567
|
+
throw new Error("Failed to fetch ConsensusMain contract");
|
|
568
|
+
}
|
|
569
|
+
const consensusMainContract = await contractsResponse.json();
|
|
570
|
+
client.chain.consensusMainContract = consensusMainContract.result;
|
|
571
|
+
}
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
550
575
|
// src/client/client.ts
|
|
551
576
|
var createClient = (config = { chain: simulator }) => {
|
|
552
577
|
const chainConfig = config.chain || simulator;
|
|
@@ -591,7 +616,10 @@ var createClient = (config = { chain: simulator }) => {
|
|
|
591
616
|
chain: chainConfig,
|
|
592
617
|
transport: custom(customTransport),
|
|
593
618
|
...config.account ? { account: config.account } : {}
|
|
594
|
-
}).extend(publicActions).extend((client) => accountActions(client)).extend((client) => transactionActions(client)).extend((client) => contractActions(client));
|
|
619
|
+
}).extend(publicActions).extend(walletActions).extend((client) => accountActions(client)).extend((client) => transactionActions(client)).extend((client) => contractActions(client)).extend((client) => chainActions(client));
|
|
620
|
+
baseClient.initializeConsensusSmartContract().catch((error) => {
|
|
621
|
+
console.error("Failed to initialize consensus smart contract:", error);
|
|
622
|
+
});
|
|
595
623
|
return overrideContractActions(baseClient);
|
|
596
624
|
};
|
|
597
625
|
|
package/dist/types/index.d.cts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { A as Address, a as CalldataAddress, C as CalldataEncodable, f as ContractMethod, e as ContractMethodBase, c as ContractParamsArraySchemaElement, d as ContractParamsSchema, g as ContractSchema, G as GenLayerClient, b as GenLayerMethod, j as GenLayerTransaction, M as MethodDescription, k as TransactionDataElement, h as TransactionHash, i as TransactionStatus } from '../index-
|
|
2
|
-
export { S as SimulatorChain } from '../chains-
|
|
1
|
+
export { A as Address, a as CalldataAddress, C as CalldataEncodable, f as ContractMethod, e as ContractMethodBase, c as ContractParamsArraySchemaElement, d as ContractParamsSchema, g as ContractSchema, G as GenLayerClient, b as GenLayerMethod, j as GenLayerTransaction, M as MethodDescription, k as TransactionDataElement, h as TransactionHash, i as TransactionStatus } from '../index-FMf4Unm0.cjs';
|
|
2
|
+
export { S as SimulatorChain } from '../chains-C5PI_Nr_.cjs';
|
|
3
3
|
export { Account } from 'viem';
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { A as Address, a as CalldataAddress, C as CalldataEncodable, f as ContractMethod, e as ContractMethodBase, c as ContractParamsArraySchemaElement, d as ContractParamsSchema, g as ContractSchema, G as GenLayerClient, b as GenLayerMethod, j as GenLayerTransaction, M as MethodDescription, k as TransactionDataElement, h as TransactionHash, i as TransactionStatus } from '../index-
|
|
2
|
-
export { S as SimulatorChain } from '../chains-
|
|
1
|
+
export { A as Address, a as CalldataAddress, C as CalldataEncodable, f as ContractMethod, e as ContractMethodBase, c as ContractParamsArraySchemaElement, d as ContractParamsSchema, g as ContractSchema, G as GenLayerClient, b as GenLayerMethod, j as GenLayerTransaction, M as MethodDescription, k as TransactionDataElement, h as TransactionHash, i as TransactionStatus } from '../index-vnyqXaRU.js';
|
|
2
|
+
export { S as SimulatorChain } from '../chains-C5PI_Nr_.js';
|
|
3
3
|
export { Account } from 'viem';
|
package/package.json
CHANGED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import {GenLayerClient, SimulatorChain} from "@/types";
|
|
2
|
+
import {simulator} from "@/chains";
|
|
3
|
+
|
|
4
|
+
export function chainActions(client: GenLayerClient<SimulatorChain>) {
|
|
5
|
+
return {
|
|
6
|
+
initializeConsensusSmartContract: async (forceReset: boolean = false): Promise<void> => {
|
|
7
|
+
if (client.chain?.id !== simulator.id) {
|
|
8
|
+
throw new Error("Client is not connected to the simulator");
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (
|
|
12
|
+
!forceReset &&
|
|
13
|
+
client.chain.consensusMainContract?.address &&
|
|
14
|
+
client.chain.consensusMainContract?.abi
|
|
15
|
+
) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const contractsResponse = await fetch(client.chain.rpcUrls.default.http[0], {
|
|
20
|
+
method: "POST",
|
|
21
|
+
headers: {
|
|
22
|
+
"Content-Type": "application/json",
|
|
23
|
+
},
|
|
24
|
+
body: JSON.stringify({
|
|
25
|
+
jsonrpc: "2.0",
|
|
26
|
+
id: Date.now(),
|
|
27
|
+
method: "sim_getConsensusContract",
|
|
28
|
+
params: ["ConsensusMain"],
|
|
29
|
+
}),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
if (!contractsResponse.ok) {
|
|
33
|
+
throw new Error("Failed to fetch ConsensusMain contract");
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const consensusMainContract = await contractsResponse.json();
|
|
37
|
+
client.chain.consensusMainContract = consensusMainContract.result;
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
}
|
package/src/chains/simulator.ts
CHANGED
package/src/client/client.ts
CHANGED
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
import {Account, createClient as createViemClient, publicActions, custom, Address} from "viem";
|
|
1
|
+
import {Account, createClient as createViemClient, publicActions, custom, Address, walletActions} from "viem";
|
|
2
2
|
import {simulator} from "../chains/simulator";
|
|
3
3
|
import {accountActions} from "../accounts/actions";
|
|
4
4
|
import {contractActions, overrideContractActions} from "../contracts/actions";
|
|
5
5
|
import {transactionActions} from "../transactions/actions";
|
|
6
6
|
import {GenLayerClient, SimulatorChain} from "@/types";
|
|
7
|
+
import {chainActions} from "@/chains/actions";
|
|
7
8
|
|
|
8
9
|
// Define the configuration interface for the client
|
|
9
10
|
interface ClientConfig {
|
|
10
11
|
chain?: {
|
|
11
12
|
id: number;
|
|
12
13
|
name: string;
|
|
13
|
-
rpcUrls: {
|
|
14
|
-
nativeCurrency: {
|
|
15
|
-
blockExplorers?: {
|
|
14
|
+
rpcUrls: {default: {http: readonly string[]}};
|
|
15
|
+
nativeCurrency: {name: string; symbol: string; decimals: number};
|
|
16
|
+
blockExplorers?: {default: {name: string; url: string}};
|
|
16
17
|
};
|
|
17
18
|
endpoint?: string; // Custom RPC endpoint
|
|
18
19
|
account?: Account | Address;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
|
-
export const createClient = (config: ClientConfig = {
|
|
22
|
+
export const createClient = (config: ClientConfig = {chain: simulator}) => {
|
|
22
23
|
const chainConfig = config.chain || simulator;
|
|
23
24
|
const rpcUrl = config.endpoint || chainConfig.rpcUrls.default.http[0];
|
|
24
25
|
const isAddress = typeof config.account !== "object";
|
|
25
26
|
|
|
26
27
|
const customTransport = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
const data = await response.json();
|
|
28
|
+
async request({method, params}: {method: string; params: any[]}) {
|
|
29
|
+
if (method.startsWith("eth_") && isAddress) {
|
|
30
|
+
try {
|
|
31
|
+
return await window.ethereum?.request({method, params});
|
|
32
|
+
} catch (err) {
|
|
33
|
+
console.warn(`Error using window.ethereum for method ${method}:`, err);
|
|
34
|
+
throw err;
|
|
35
|
+
}
|
|
36
|
+
} else {
|
|
37
|
+
try {
|
|
38
|
+
const response = await fetch(rpcUrl, {
|
|
39
|
+
method: "POST",
|
|
40
|
+
headers: {
|
|
41
|
+
"Content-Type": "application/json",
|
|
42
|
+
},
|
|
43
|
+
body: JSON.stringify({
|
|
44
|
+
jsonrpc: "2.0",
|
|
45
|
+
id: Date.now(),
|
|
46
|
+
method,
|
|
47
|
+
params,
|
|
48
|
+
}),
|
|
49
|
+
});
|
|
51
50
|
|
|
52
|
-
|
|
53
|
-
throw new Error(data.error.message);
|
|
54
|
-
}
|
|
51
|
+
const data = await response.json();
|
|
55
52
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
console.error(`Error fetching ${method} from GenLayer RPC:`, err);
|
|
59
|
-
throw err;
|
|
53
|
+
if (data.error) {
|
|
54
|
+
throw new Error(data.error.message);
|
|
60
55
|
}
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
}
|
|
64
56
|
|
|
57
|
+
return data.result;
|
|
58
|
+
} catch (err) {
|
|
59
|
+
console.error(`Error fetching ${method} from GenLayer RPC:`, err);
|
|
60
|
+
throw err;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
65
|
|
|
66
66
|
const baseClient = createViemClient({
|
|
67
67
|
chain: chainConfig,
|
|
@@ -69,11 +69,16 @@ export const createClient = (config: ClientConfig = { chain: simulator }) => {
|
|
|
69
69
|
...(config.account ? {account: config.account} : {}),
|
|
70
70
|
})
|
|
71
71
|
.extend(publicActions)
|
|
72
|
+
.extend(walletActions)
|
|
72
73
|
.extend(client => accountActions(client as unknown as GenLayerClient<SimulatorChain>))
|
|
73
74
|
.extend(client => transactionActions(client as unknown as GenLayerClient<SimulatorChain>))
|
|
74
|
-
.extend(client => contractActions(client as unknown as GenLayerClient<SimulatorChain>))
|
|
75
|
+
.extend(client => contractActions(client as unknown as GenLayerClient<SimulatorChain>))
|
|
76
|
+
.extend(client => chainActions(client as unknown as GenLayerClient<SimulatorChain>));
|
|
75
77
|
|
|
76
|
-
|
|
78
|
+
// Initialize in the background
|
|
79
|
+
baseClient.initializeConsensusSmartContract().catch(error => {
|
|
80
|
+
console.error("Failed to initialize consensus smart contract:", error);
|
|
81
|
+
});
|
|
77
82
|
|
|
83
|
+
return overrideContractActions(baseClient as unknown as GenLayerClient<SimulatorChain>);
|
|
78
84
|
};
|
|
79
|
-
|
package/src/contracts/actions.ts
CHANGED
|
@@ -9,39 +9,43 @@ import {
|
|
|
9
9
|
Address,
|
|
10
10
|
TransactionStatus,
|
|
11
11
|
} from "@/types";
|
|
12
|
-
import {
|
|
12
|
+
import {fromHex, toHex, zeroAddress, encodeFunctionData} from "viem";
|
|
13
13
|
|
|
14
|
-
function makeCalldataObject(
|
|
14
|
+
function makeCalldataObject(
|
|
15
|
+
method: string | undefined,
|
|
16
|
+
args: CalldataEncodable[] | undefined,
|
|
17
|
+
kwargs: {[key: string]: CalldataEncodable} | Map<string, CalldataEncodable> | undefined,
|
|
18
|
+
): CalldataEncodable {
|
|
15
19
|
// this method omits args or kwargs if they are empty
|
|
16
20
|
// it reduces transaction size
|
|
17
|
-
let ret: {[key: string]: CalldataEncodable} = {}
|
|
21
|
+
let ret: {[key: string]: CalldataEncodable} = {};
|
|
18
22
|
|
|
19
23
|
if (method) {
|
|
20
|
-
ret[
|
|
24
|
+
ret["method"] = method;
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
if (args && args.length > 0) {
|
|
24
|
-
ret[
|
|
28
|
+
ret["args"] = args;
|
|
25
29
|
}
|
|
26
30
|
|
|
27
31
|
if (kwargs) {
|
|
28
32
|
if (kwargs instanceof Map) {
|
|
29
33
|
if (kwargs.size > 0) {
|
|
30
|
-
ret[
|
|
34
|
+
ret["kwargs"] = kwargs;
|
|
31
35
|
}
|
|
32
36
|
} else {
|
|
33
|
-
let hasVal = false
|
|
37
|
+
let hasVal = false;
|
|
34
38
|
for (const _k in kwargs) {
|
|
35
39
|
hasVal = true;
|
|
36
|
-
break
|
|
40
|
+
break;
|
|
37
41
|
}
|
|
38
42
|
if (hasVal) {
|
|
39
|
-
ret[
|
|
43
|
+
ret["kwargs"] = kwargs;
|
|
40
44
|
}
|
|
41
45
|
}
|
|
42
46
|
}
|
|
43
47
|
|
|
44
|
-
return ret
|
|
48
|
+
return ret;
|
|
45
49
|
}
|
|
46
50
|
|
|
47
51
|
export const contractActions = (client: GenLayerClient<SimulatorChain>) => {
|
|
@@ -69,11 +73,18 @@ export const overrideContractActions = (client: GenLayerClient<SimulatorChain>)
|
|
|
69
73
|
address: Address;
|
|
70
74
|
functionName: string;
|
|
71
75
|
args?: CalldataEncodable[];
|
|
72
|
-
kwargs?: Map<string, CalldataEncodable> | {
|
|
76
|
+
kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
|
|
73
77
|
stateStatus?: TransactionStatus;
|
|
74
78
|
rawReturn?: RawReturn;
|
|
75
79
|
}): Promise<RawReturn extends true ? `0x${string}` : CalldataEncodable> => {
|
|
76
|
-
const {
|
|
80
|
+
const {
|
|
81
|
+
account,
|
|
82
|
+
address,
|
|
83
|
+
functionName,
|
|
84
|
+
args: callArgs,
|
|
85
|
+
kwargs,
|
|
86
|
+
stateStatus = TransactionStatus.ACCEPTED,
|
|
87
|
+
} = args;
|
|
77
88
|
const encodedData = calldata.encode(makeCalldataObject(functionName, callArgs, kwargs));
|
|
78
89
|
const serializedData = serializeOne(encodedData);
|
|
79
90
|
|
|
@@ -90,9 +101,9 @@ export const overrideContractActions = (client: GenLayerClient<SimulatorChain>)
|
|
|
90
101
|
});
|
|
91
102
|
|
|
92
103
|
if (args.rawReturn) {
|
|
93
|
-
return result
|
|
104
|
+
return result;
|
|
94
105
|
}
|
|
95
|
-
const resultBinary = fromHex(result, "bytes")
|
|
106
|
+
const resultBinary = fromHex(result, "bytes");
|
|
96
107
|
return calldata.decode(resultBinary) as any;
|
|
97
108
|
};
|
|
98
109
|
|
|
@@ -101,103 +112,90 @@ export const overrideContractActions = (client: GenLayerClient<SimulatorChain>)
|
|
|
101
112
|
address: Address;
|
|
102
113
|
functionName: string;
|
|
103
114
|
args?: CalldataEncodable[];
|
|
104
|
-
kwargs?: Map<string, CalldataEncodable> | {
|
|
115
|
+
kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
|
|
105
116
|
value: bigint;
|
|
106
117
|
leaderOnly?: boolean;
|
|
107
118
|
}): Promise<`0x${string}`> => {
|
|
108
119
|
const {account, address, functionName, args: callArgs, kwargs, value = 0n, leaderOnly = false} = args;
|
|
109
120
|
const data = [calldata.encode(makeCalldataObject(functionName, callArgs, kwargs)), leaderOnly];
|
|
110
121
|
const serializedData = serialize(data);
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
if (senderAccount?.type !== "local") {
|
|
114
|
-
const transaction = {
|
|
115
|
-
from: senderAccount?.address,
|
|
116
|
-
to: address,
|
|
117
|
-
data: serializedData,
|
|
118
|
-
value: `0x${value.toString(16)}`,
|
|
119
|
-
};
|
|
122
|
+
return _sendTransaction(address, serializedData, account || client.account, value);
|
|
123
|
+
};
|
|
120
124
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
}
|
|
125
|
+
client.deployContract = async (args: {
|
|
126
|
+
account?: Account;
|
|
127
|
+
code: string | Uint8Array;
|
|
128
|
+
args?: CalldataEncodable[];
|
|
129
|
+
kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
|
|
130
|
+
leaderOnly?: boolean;
|
|
131
|
+
}) => {
|
|
132
|
+
const {account, code, args: constructorArgs, kwargs, leaderOnly = false} = args;
|
|
133
|
+
const data = [code, calldata.encode(makeCalldataObject(undefined, constructorArgs, kwargs)), leaderOnly];
|
|
134
|
+
const serializedData = serialize(data);
|
|
135
|
+
return _sendTransaction(zeroAddress, serializedData, account || client.account);
|
|
136
|
+
};
|
|
126
137
|
|
|
138
|
+
const _sendTransaction = async (
|
|
139
|
+
recipient: `0x${string}`,
|
|
140
|
+
data: `0x${string}`,
|
|
141
|
+
senderAccount?: Account,
|
|
142
|
+
value?: bigint,
|
|
143
|
+
) => {
|
|
127
144
|
if (!senderAccount) {
|
|
128
145
|
throw new Error(
|
|
129
146
|
"No account set. Configure the client with an account or pass an account to this function.",
|
|
130
147
|
);
|
|
131
148
|
}
|
|
132
149
|
|
|
133
|
-
if (!
|
|
134
|
-
throw new Error(
|
|
150
|
+
if (!client.chain.consensusMainContract?.address) {
|
|
151
|
+
throw new Error(
|
|
152
|
+
"Consensus main contract not initialized. Please ensure client is properly initialized.",
|
|
153
|
+
);
|
|
135
154
|
}
|
|
136
155
|
|
|
137
|
-
const
|
|
156
|
+
const encodedData = encodeFunctionData({
|
|
157
|
+
abi: client.chain.consensusMainContract?.abi as any,
|
|
158
|
+
functionName: "addTransaction",
|
|
159
|
+
args: [
|
|
160
|
+
senderAccount.address,
|
|
161
|
+
recipient,
|
|
162
|
+
client.chain.defaultNumberOfInitialValidators,
|
|
163
|
+
client.chain.defaultConsensusMaxRotations,
|
|
164
|
+
data,
|
|
165
|
+
],
|
|
166
|
+
});
|
|
138
167
|
|
|
139
|
-
const
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
168
|
+
const nonce = await client.getCurrentNonce({address: senderAccount.address});
|
|
169
|
+
const transactionRequest = await client.prepareTransactionRequest({
|
|
170
|
+
account: senderAccount,
|
|
171
|
+
to: client.chain.consensusMainContract?.address as Address,
|
|
172
|
+
data: encodedData,
|
|
143
173
|
type: "legacy",
|
|
144
174
|
nonce,
|
|
175
|
+
value: value ?? 0n,
|
|
145
176
|
});
|
|
146
177
|
|
|
147
|
-
return await client.request({
|
|
148
|
-
method: "eth_sendRawTransaction",
|
|
149
|
-
params: [signedTransaction],
|
|
150
|
-
});
|
|
151
|
-
};
|
|
152
|
-
|
|
153
|
-
client.deployContract = async (args: {
|
|
154
|
-
account?: Account;
|
|
155
|
-
code: string | Uint8Array;
|
|
156
|
-
args?: CalldataEncodable[];
|
|
157
|
-
kwargs?: Map<string, CalldataEncodable> | { [key: string]: CalldataEncodable }
|
|
158
|
-
leaderOnly?: boolean;
|
|
159
|
-
}) => {
|
|
160
|
-
const {account, code, args: constructorArgs, kwargs, leaderOnly = false} = args;
|
|
161
|
-
const data = [code, calldata.encode(makeCalldataObject(undefined, constructorArgs, kwargs)), leaderOnly];
|
|
162
|
-
const serializedData = serialize(data);
|
|
163
|
-
const senderAccount = account || client.account;
|
|
164
|
-
|
|
165
178
|
if (senderAccount?.type !== "local") {
|
|
166
|
-
const
|
|
167
|
-
from:
|
|
168
|
-
to:
|
|
169
|
-
data:
|
|
170
|
-
value: "0x0",
|
|
179
|
+
const formattedRequest = {
|
|
180
|
+
from: transactionRequest.from,
|
|
181
|
+
to: transactionRequest.to,
|
|
182
|
+
data: encodedData,
|
|
183
|
+
value: transactionRequest.value ? `0x${transactionRequest.value.toString(16)}` : "0x0",
|
|
171
184
|
};
|
|
172
185
|
|
|
173
186
|
return await client.request({
|
|
174
187
|
method: "eth_sendTransaction",
|
|
175
|
-
params: [
|
|
188
|
+
params: [formattedRequest as any],
|
|
176
189
|
});
|
|
177
190
|
}
|
|
178
191
|
|
|
179
|
-
if (!senderAccount) {
|
|
180
|
-
throw new Error(
|
|
181
|
-
"No account set. Configure the client with an account or pass an account to this function.",
|
|
182
|
-
);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
192
|
if (!senderAccount?.signTransaction) {
|
|
186
193
|
throw new Error("Account does not support signTransaction");
|
|
187
194
|
}
|
|
188
195
|
|
|
189
|
-
const
|
|
196
|
+
const serializedTransaction = await senderAccount.signTransaction(transactionRequest);
|
|
190
197
|
|
|
191
|
-
|
|
192
|
-
data: serializedData,
|
|
193
|
-
type: "legacy",
|
|
194
|
-
nonce,
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
return await client.request({
|
|
198
|
-
method: "eth_sendRawTransaction",
|
|
199
|
-
params: [signedTransaction],
|
|
200
|
-
});
|
|
198
|
+
return client.sendRawTransaction({serializedTransaction: serializedTransaction});
|
|
201
199
|
};
|
|
202
200
|
|
|
203
201
|
return client;
|
package/src/types/chains.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
import {Chain} from "viem";
|
|
2
2
|
|
|
3
|
-
export type SimulatorChain = Chain
|
|
3
|
+
export type SimulatorChain = Chain & {
|
|
4
|
+
consensusMainContract: {
|
|
5
|
+
address: string;
|
|
6
|
+
abi: any[];
|
|
7
|
+
bytecode: string;
|
|
8
|
+
} | null;
|
|
9
|
+
defaultNumberOfInitialValidators: number;
|
|
10
|
+
defaultConsensusMaxRotations: number;
|
|
11
|
+
};
|
package/src/types/clients.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {Transport, Client, PublicActions} from "viem";
|
|
1
|
+
import {Transport, Client, PublicActions, WalletActions} from "viem";
|
|
2
2
|
import {GenLayerTransaction, TransactionHash, TransactionStatus} from "./transactions";
|
|
3
3
|
import {SimulatorChain} from "./chains";
|
|
4
4
|
import {Address, Account} from "./accounts";
|
|
@@ -25,6 +25,7 @@ export type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<
|
|
|
25
25
|
Client<Transport, TSimulatorChain>,
|
|
26
26
|
"transport" | "getTransaction" | "readContract"
|
|
27
27
|
> &
|
|
28
|
+
Omit<WalletActions<TSimulatorChain>, "deployContract" | "writeContract"> &
|
|
28
29
|
Omit<
|
|
29
30
|
PublicActions<Transport, TSimulatorChain>,
|
|
30
31
|
"readContract" | "getTransaction" | "waitForTransactionReceipt"
|
|
@@ -40,7 +41,7 @@ export type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<
|
|
|
40
41
|
functionName: string;
|
|
41
42
|
stateStatus?: TransactionStatus;
|
|
42
43
|
args?: CalldataEncodable[];
|
|
43
|
-
kwargs?: Map<string, CalldataEncodable> | {
|
|
44
|
+
kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
|
|
44
45
|
rawReturn?: RawReturn;
|
|
45
46
|
}) => Promise<RawReturn extends true ? `0x${string}` : CalldataEncodable>;
|
|
46
47
|
writeContract: (args: {
|
|
@@ -48,7 +49,7 @@ export type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<
|
|
|
48
49
|
address: Address;
|
|
49
50
|
functionName: string;
|
|
50
51
|
args?: CalldataEncodable[];
|
|
51
|
-
kwargs?: Map<string, CalldataEncodable> | {
|
|
52
|
+
kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
|
|
52
53
|
value: bigint;
|
|
53
54
|
leaderOnly?: boolean;
|
|
54
55
|
}) => Promise<any>;
|
|
@@ -56,7 +57,7 @@ export type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<
|
|
|
56
57
|
account?: Account;
|
|
57
58
|
code: string | Uint8Array;
|
|
58
59
|
args?: CalldataEncodable[];
|
|
59
|
-
kwargs?: Map<string, CalldataEncodable> | {
|
|
60
|
+
kwargs?: Map<string, CalldataEncodable> | {[key: string]: CalldataEncodable};
|
|
60
61
|
leaderOnly?: boolean;
|
|
61
62
|
}) => Promise<`0x${string}`>;
|
|
62
63
|
getTransaction: (args: {hash: TransactionHash}) => Promise<GenLayerTransaction>;
|
|
@@ -69,4 +70,5 @@ export type GenLayerClient<TSimulatorChain extends SimulatorChain> = Omit<
|
|
|
69
70
|
}) => Promise<GenLayerTransaction>;
|
|
70
71
|
getContractSchema: (address: string) => Promise<ContractSchema>;
|
|
71
72
|
getContractSchemaForCode: (contractCode: string | Uint8Array) => Promise<ContractSchema>;
|
|
73
|
+
initializeConsensusSmartContract: (forceReset?: boolean) => Promise<void>;
|
|
72
74
|
};
|