@bronlabs/intents-sdk 1.0.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/README.md +114 -0
- package/abi/OracleAggregator.json +219 -0
- package/abi/OrderEngine.json +499 -0
- package/dist/config.d.ts +19 -0
- package/dist/config.js +2 -0
- package/dist/config.js.map +1 -0
- package/dist/contracts.d.ts +3 -0
- package/dist/contracts.js +8 -0
- package/dist/contracts.js.map +1 -0
- package/dist/eventQueue.d.ts +9 -0
- package/dist/eventQueue.js +21 -0
- package/dist/eventQueue.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/networks/evm.d.ts +13 -0
- package/dist/networks/evm.js +95 -0
- package/dist/networks/evm.js.map +1 -0
- package/dist/networks/index.d.ts +14 -0
- package/dist/networks/index.js +2 -0
- package/dist/networks/index.js.map +1 -0
- package/dist/orderIndexer.d.ts +28 -0
- package/dist/orderIndexer.js +122 -0
- package/dist/orderIndexer.js.map +1 -0
- package/dist/orderProcessor.d.ts +14 -0
- package/dist/orderProcessor.js +35 -0
- package/dist/orderProcessor.js.map +1 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +9 -0
- package/dist/utils.js.map +1 -0
- package/package.json +44 -0
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { BigNumber, ethers } from 'ethers';
|
|
2
|
+
import { log } from '../utils.js';
|
|
3
|
+
export class EvmNetwork {
|
|
4
|
+
constructor(rpcUrl, confirmations) {
|
|
5
|
+
this.nativeAssetDecimals = 18;
|
|
6
|
+
this.retryDelay = 5000;
|
|
7
|
+
this.rpcUrl = rpcUrl;
|
|
8
|
+
this.provider = new ethers.providers.JsonRpcProvider(rpcUrl);
|
|
9
|
+
this.confirmations = confirmations;
|
|
10
|
+
}
|
|
11
|
+
async getDecimals(tokenAddress) {
|
|
12
|
+
if (tokenAddress === "0x0") {
|
|
13
|
+
return this.nativeAssetDecimals;
|
|
14
|
+
}
|
|
15
|
+
const response = await fetch(this.rpcUrl, {
|
|
16
|
+
method: 'POST',
|
|
17
|
+
body: JSON.stringify({
|
|
18
|
+
id: 1,
|
|
19
|
+
jsonrpc: "2.0",
|
|
20
|
+
method: "eth_call",
|
|
21
|
+
params: [
|
|
22
|
+
{
|
|
23
|
+
to: tokenAddress,
|
|
24
|
+
data: "0x313ce567"
|
|
25
|
+
},
|
|
26
|
+
"latest"
|
|
27
|
+
]
|
|
28
|
+
})
|
|
29
|
+
});
|
|
30
|
+
const { result } = await response.json();
|
|
31
|
+
return parseInt(result, 16);
|
|
32
|
+
}
|
|
33
|
+
async getTxData(txHash, tokenAddress) {
|
|
34
|
+
const currentBlock = await this.provider.getBlockNumber();
|
|
35
|
+
// Native token - ETH
|
|
36
|
+
if (tokenAddress === "0x0") {
|
|
37
|
+
const response = await fetch(this.rpcUrl, {
|
|
38
|
+
method: 'POST',
|
|
39
|
+
body: JSON.stringify({
|
|
40
|
+
id: 1,
|
|
41
|
+
jsonrpc: "2.0",
|
|
42
|
+
method: "eth_getTransactionByHash",
|
|
43
|
+
params: [txHash]
|
|
44
|
+
})
|
|
45
|
+
});
|
|
46
|
+
const { result } = await response.json();
|
|
47
|
+
if (!result) {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
const { to, value, blockNumber } = result;
|
|
51
|
+
log.info(`Confirmations ${txHash}: ${currentBlock - blockNumber}`);
|
|
52
|
+
return {
|
|
53
|
+
to: to,
|
|
54
|
+
token: tokenAddress,
|
|
55
|
+
amount: BigNumber.from(value),
|
|
56
|
+
confirmed: (currentBlock - blockNumber) >= this.confirmations
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
// ERC20 token
|
|
60
|
+
const response = await fetch(this.rpcUrl, {
|
|
61
|
+
method: 'POST',
|
|
62
|
+
body: JSON.stringify({
|
|
63
|
+
id: 1,
|
|
64
|
+
jsonrpc: "2.0",
|
|
65
|
+
method: "eth_getTransactionReceipt",
|
|
66
|
+
params: [txHash]
|
|
67
|
+
})
|
|
68
|
+
});
|
|
69
|
+
const { result } = await response.json();
|
|
70
|
+
if (!result) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
const receipt = result;
|
|
74
|
+
log.info(`Confirmations ${txHash}: ${currentBlock - receipt.blockNumber}`);
|
|
75
|
+
return {
|
|
76
|
+
to: '0x' + receipt.logs[0].topics[2].slice(26),
|
|
77
|
+
token: receipt.to,
|
|
78
|
+
amount: BigNumber.from(receipt.logs[0].data),
|
|
79
|
+
confirmed: (currentBlock - receipt.blockNumber) >= this.confirmations
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
async transfer(privateKey, to, value, tokenAddress) {
|
|
83
|
+
const signer = new ethers.Wallet(privateKey, this.provider);
|
|
84
|
+
if (tokenAddress === "0x0") {
|
|
85
|
+
const { hash } = await signer.sendTransaction({ to, value });
|
|
86
|
+
return hash;
|
|
87
|
+
}
|
|
88
|
+
const tokenContract = new ethers.Contract(tokenAddress, [
|
|
89
|
+
'function transfer(address to, uint256 amount) returns (bool)'
|
|
90
|
+
], signer);
|
|
91
|
+
const { hash } = await tokenContract.transfer(to, value);
|
|
92
|
+
return hash;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=evm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"evm.js","sourceRoot":"","sources":["../../src/networks/evm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAG3C,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAWlC,MAAM,OAAO,UAAU;IAOrB,YAAY,MAAc,EAAE,aAAqB;QAHhC,wBAAmB,GAAW,EAAE,CAAC;QACzC,eAAU,GAAW,IAAI,CAAC;QAGjC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,IAA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CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;YAC5C,SAAS,EAAE,CAAC,YAAY,GAAG,OAAO,CAAC,WAAW,CAAC,IAAI,IAAI,CAAC,aAAa;SACtE,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,UAAkB,EAAE,EAAU,EAAE,KAAgB,EAAE,YAAoB;QACnF,MAAM,MAAM,GAAG,IAAI,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,IAAI,CAAC,QAAQ,CAAC,CAAC;QAE5D,IAAI,YAAY,KAAK,KAAK,EAAE,CAAC;YAC3B,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,MAAM,CAAC,eAAe,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;YAC7D,OAAO,IAAI,CAAC;QACd,CAAC;QAED,MAAM,aAAa,GAAG,IAAI,MAAM,CAAC,QAAQ,CAAC,YAAY,EAAE;YACtD,8DAA8D;SAC/D,EAAE,MAAM,CAAC,CAAC;QAEX,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,aAAa,CAAC,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC;QACzD,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BigNumber } from 'ethers';
|
|
2
|
+
export interface TransactionData {
|
|
3
|
+
to: string;
|
|
4
|
+
token: string;
|
|
5
|
+
amount: BigNumber;
|
|
6
|
+
confirmed: boolean;
|
|
7
|
+
}
|
|
8
|
+
export interface Network {
|
|
9
|
+
getDecimals(tokenAddress: string): Promise<number>;
|
|
10
|
+
getTxData(txHash: string, tokenAddress: string): Promise<TransactionData | undefined>;
|
|
11
|
+
transfer(privateKey: string, to: string, value: BigNumber, tokenAddress: string): Promise<string>;
|
|
12
|
+
readonly retryDelay: number;
|
|
13
|
+
}
|
|
14
|
+
export * from './evm.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/networks/index.ts"],"names":[],"mappings":"AAoBA,cAAc,UAAU,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { IntentsConfig } from './config.js';
|
|
3
|
+
export interface OrderStatusChangedEvent {
|
|
4
|
+
type: 'OrderStatusChanged';
|
|
5
|
+
data: {
|
|
6
|
+
orderId: string;
|
|
7
|
+
status: number;
|
|
8
|
+
};
|
|
9
|
+
event: ethers.Event;
|
|
10
|
+
retries: number;
|
|
11
|
+
}
|
|
12
|
+
type EventProcessor = (event: OrderStatusChangedEvent) => Promise<void>;
|
|
13
|
+
export declare class OrderIndexer {
|
|
14
|
+
private readonly config;
|
|
15
|
+
private readonly provider;
|
|
16
|
+
private readonly orderEngine;
|
|
17
|
+
private readonly eventQueue;
|
|
18
|
+
private readonly processors;
|
|
19
|
+
private isRunning;
|
|
20
|
+
private lastProcessedBlock;
|
|
21
|
+
constructor(config: IntentsConfig);
|
|
22
|
+
addProcessor(processor: EventProcessor): void;
|
|
23
|
+
start(): Promise<void>;
|
|
24
|
+
stop(): Promise<void>;
|
|
25
|
+
private startIndexingLoop;
|
|
26
|
+
private processEventQueue;
|
|
27
|
+
}
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { ethers } from 'ethers';
|
|
2
|
+
import { sleep, log } from './utils.js';
|
|
3
|
+
import { EventQueue } from './eventQueue.js';
|
|
4
|
+
import { initOrderEngine } from './contracts.js';
|
|
5
|
+
export class OrderIndexer {
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.config = config;
|
|
8
|
+
this.provider = new ethers.providers.JsonRpcProvider(config.rpcUrl);
|
|
9
|
+
this.orderEngine = initOrderEngine(config.orderEngineAddress, this.provider);
|
|
10
|
+
this.eventQueue = new EventQueue();
|
|
11
|
+
this.processors = [];
|
|
12
|
+
this.isRunning = false;
|
|
13
|
+
this.lastProcessedBlock = 0;
|
|
14
|
+
}
|
|
15
|
+
addProcessor(processor) {
|
|
16
|
+
this.processors.push(processor);
|
|
17
|
+
}
|
|
18
|
+
async start() {
|
|
19
|
+
if (this.isRunning) {
|
|
20
|
+
log.warn('Indexer is already running');
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
this.isRunning = true;
|
|
24
|
+
log.info(`Starting indexer from block ${this.lastProcessedBlock}`);
|
|
25
|
+
try {
|
|
26
|
+
await this.startIndexingLoop();
|
|
27
|
+
}
|
|
28
|
+
catch (error) {
|
|
29
|
+
log.error('Error starting indexer:', error);
|
|
30
|
+
this.isRunning = false;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
async stop() {
|
|
34
|
+
if (!this.isRunning) {
|
|
35
|
+
log.warn('Indexer is not running');
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
log.info('Stopping indexer...');
|
|
39
|
+
this.isRunning = false;
|
|
40
|
+
// Wait for event queue to be processed
|
|
41
|
+
while (!this.eventQueue.isEmpty()) {
|
|
42
|
+
log.info(`Waiting for ${this.eventQueue.size()} events to be processed before stopping`);
|
|
43
|
+
await sleep(1000);
|
|
44
|
+
}
|
|
45
|
+
log.info('Indexer stopped');
|
|
46
|
+
}
|
|
47
|
+
async startIndexingLoop() {
|
|
48
|
+
if (!this.isRunning)
|
|
49
|
+
return;
|
|
50
|
+
try {
|
|
51
|
+
const currentBlock = await this.provider.getBlockNumber();
|
|
52
|
+
if (!this.lastProcessedBlock) {
|
|
53
|
+
this.lastProcessedBlock = currentBlock - this.config.startBlockOffset;
|
|
54
|
+
}
|
|
55
|
+
if (currentBlock > this.lastProcessedBlock) {
|
|
56
|
+
log.info(`Indexing from block ${this.lastProcessedBlock + 1} to ${currentBlock} (${currentBlock - this.lastProcessedBlock} blocks)`);
|
|
57
|
+
// Fetch historical events in chunks to avoid RPC limitations
|
|
58
|
+
const chunkSize = 500;
|
|
59
|
+
for (let fromBlock = this.lastProcessedBlock + 1; fromBlock <= currentBlock; fromBlock += chunkSize) {
|
|
60
|
+
const toBlock = Math.min(fromBlock + chunkSize - 1, currentBlock);
|
|
61
|
+
const events = await this.orderEngine.queryFilter(this.orderEngine.filters.OrderStatusChanged(), fromBlock, toBlock);
|
|
62
|
+
log.info(`Found ${events.length} events between blocks ${fromBlock} and ${toBlock}`);
|
|
63
|
+
for (const event of events) {
|
|
64
|
+
const { args: { orderId, status } } = this.orderEngine.interface.parseLog({
|
|
65
|
+
topics: event.topics,
|
|
66
|
+
data: event.data,
|
|
67
|
+
});
|
|
68
|
+
this.eventQueue.add({
|
|
69
|
+
type: 'OrderStatusChanged',
|
|
70
|
+
data: {
|
|
71
|
+
orderId,
|
|
72
|
+
status: parseInt(status.toString(), 10)
|
|
73
|
+
},
|
|
74
|
+
event,
|
|
75
|
+
retries: 0
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
this.lastProcessedBlock = toBlock;
|
|
79
|
+
await this.processEventQueue();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
log.error('Error in indexing loop: ', error);
|
|
85
|
+
}
|
|
86
|
+
// Schedule next iteration
|
|
87
|
+
if (this.isRunning) {
|
|
88
|
+
setTimeout(() => this.startIndexingLoop(), this.config.pollingInterval);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
async processEventQueue() {
|
|
92
|
+
if (this.eventQueue.isEmpty() || this.processors.length === 0)
|
|
93
|
+
return;
|
|
94
|
+
log.info(`Processing event queue with ${this.eventQueue.size()} events`);
|
|
95
|
+
while (!this.eventQueue.isEmpty() && this.isRunning) {
|
|
96
|
+
const event = this.eventQueue.peek();
|
|
97
|
+
if (!event)
|
|
98
|
+
continue;
|
|
99
|
+
try {
|
|
100
|
+
// Process the event with all registered processors
|
|
101
|
+
for (const processor of this.processors) {
|
|
102
|
+
await processor(event);
|
|
103
|
+
}
|
|
104
|
+
this.eventQueue.remove();
|
|
105
|
+
}
|
|
106
|
+
catch (error) {
|
|
107
|
+
log.error(`Error processing event:`, error);
|
|
108
|
+
// Retry logic
|
|
109
|
+
if (event.retries < this.config.maxRetries) {
|
|
110
|
+
event.retries++;
|
|
111
|
+
log.info(`Retrying event processing (${event.retries}/${this.config.maxRetries})`);
|
|
112
|
+
await sleep(this.config.retryDelay);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
log.error(`Max retries reached for event, removing from queue:`, event);
|
|
116
|
+
this.eventQueue.remove(); // Could add dead letter queue here for manual inspection
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=orderIndexer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orderIndexer.js","sourceRoot":"","sources":["../src/orderIndexer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,YAAY,CAAC;AACxC,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAejD,MAAM,OAAO,YAAY;IAUvB,YAAY,MAAqB;QAC/B,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,QAAQ,GAAG,IAAI,MAAM,CAAC,SAAS,CAAC,eAAe,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC;QAEpE,IAAI,CAAC,WAAW,GAAG,eAAe,CAChC,MAAM,CAAC,kBAAkB,EACzB,IAAI,CAAC,QAAQ,CACd,CAAC;QAEF,IAAI,CAAC,UAAU,GAAG,IAAI,UAAU,EAA2B,CAAC;QAC5D,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;QACvB,IAAI,CAAC,kB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CAAC;gBACzB,CAAC;gBAED,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC;YAC3B,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,GAAG,CAAC,KAAK,CAAC,yBAAyB,EAAE,KAAK,CAAC,CAAC;gBAE5C,cAAc;gBACd,IAAI,KAAK,CAAC,OAAO,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;oBAC3C,KAAK,CAAC,OAAO,EAAE,CAAC;oBAChB,GAAG,CAAC,IAAI,CAAC,8BAA8B,KAAK,CAAC,OAAO,IAAI,IAAI,CAAC,MAAM,CAAC,UAAU,GAAG,CAAC,CAAC;oBACnF,MAAM,KAAK,CAAC,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;gBACtC,CAAC;qBAAM,CAAC;oBACN,GAAG,CAAC,KAAK,CAAC,qDAAqD,EAAE,KAAK,CAAC,CAAC;oBACxE,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC,CAAC,yDAAyD;gBACrF,CAAC;YACH,CAAC;QACH,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EventQueue } from './eventQueue.js';
|
|
2
|
+
interface OrderEvent {
|
|
3
|
+
orderId: string;
|
|
4
|
+
status: string;
|
|
5
|
+
}
|
|
6
|
+
export declare abstract class OrderProcessor {
|
|
7
|
+
protected isRunning: boolean;
|
|
8
|
+
protected delayedQueue: EventQueue<OrderEvent>;
|
|
9
|
+
protected constructor();
|
|
10
|
+
abstract process(orderId: string, status: string): Promise<void>;
|
|
11
|
+
stop(): Promise<void>;
|
|
12
|
+
private processDelayedQueue;
|
|
13
|
+
}
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { EventQueue } from './eventQueue.js';
|
|
2
|
+
import { log, sleep } from './utils.js';
|
|
3
|
+
export class OrderProcessor {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.isRunning = true;
|
|
6
|
+
this.delayedQueue = new EventQueue();
|
|
7
|
+
this.processDelayedQueue().then(() => undefined);
|
|
8
|
+
}
|
|
9
|
+
async stop() {
|
|
10
|
+
if (!this.isRunning) {
|
|
11
|
+
log.warn('Order processor already stopped');
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
log.info('Stopping Order processor...');
|
|
15
|
+
this.isRunning = false;
|
|
16
|
+
await sleep(3000);
|
|
17
|
+
log.info('Order processor stopped.');
|
|
18
|
+
}
|
|
19
|
+
async processDelayedQueue() {
|
|
20
|
+
while (!this.delayedQueue.isEmpty() && this.isRunning) {
|
|
21
|
+
const event = this.delayedQueue.peek();
|
|
22
|
+
if (event) {
|
|
23
|
+
try {
|
|
24
|
+
await this.process(event.orderId, event.status);
|
|
25
|
+
this.delayedQueue.remove();
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
log.error(`Error processing delayed event:`, error);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
setTimeout(async () => await this.processDelayedQueue(), 5000);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=orderProcessor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"orderProcessor.js","sourceRoot":"","sources":["../src/orderProcessor.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,MAAM,YAAY,CAAC;AAOxC,MAAM,OAAgB,cAAc;IAIlC;QAHU,cAAS,GAAY,IAAI,CAAC;QAIlC,IAAI,CAAC,YAAY,GAAG,IAAI,UAAU,EAAc,CAAC;QAEjD,IAAI,CAAC,mBAAmB,EAAE,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IACnD,CAAC;IAID,KAAK,CAAC,IAAI;QACR,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,GAAG,CAAC,IAAI,CAAC,iCAAiC,CAAC,CAAC;YAC5C,OAAO;QACT,CAAC;QAED,GAAG,CAAC,IAAI,CAAC,6BAA6B,CAAC,CAAC;QAExC,IAAI,CAAC,SAAS,GAAG,KAAK,CAAC;QAEvB,MAAM,KAAK,CAAC,IAAI,CAAC,CAAC;QAElB,GAAG,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;IACvC,CAAC;IAEO,KAAK,CAAC,mBAAmB;QAC/B,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,OAAO,EAAE,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;YACtD,MAAM,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,IAAI,EAAE,CAAC;YAEvC,IAAI,KAAK,EAAE,CAAC;gBACV,IAAI,CAAC;oBACH,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;oBAEhD,IAAI,CAAC,YAAY,CAAC,MAAM,EAAE,CAAC;gBAC7B,CAAC;gBAAC,OAAO,KAAK,EAAE,CAAC;oBACf,GAAG,CAAC,KAAK,CAAC,iCAAiC,EAAE,KAAK,CAAC,CAAC;gBACtD,CAAC;YACH,CAAC;QACH,CAAC;QAED,UAAU,CAAC,KAAK,IAAI,EAAE,CAAC,MAAM,IAAI,CAAC,mBAAmB,EAAE,EAAE,IAAI,CAAC,CAAC;IACjE,CAAC;CACF"}
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as winston from 'winston';
|
|
2
|
+
export const log = winston.createLogger({
|
|
3
|
+
format: winston.format.combine(winston.format.colorize(), winston.format.timestamp(), winston.format.printf(({ level, message, timestamp }) => {
|
|
4
|
+
return `${timestamp} ${level}\t${message}`;
|
|
5
|
+
})),
|
|
6
|
+
transports: [new winston.transports.Console()]
|
|
7
|
+
});
|
|
8
|
+
export const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
9
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,OAAO,MAAM,SAAS,CAAC;AAEnC,MAAM,CAAC,MAAM,GAAG,GAAG,OAAO,CAAC,YAAY,CAAC;IACtC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC,OAAO,CAC5B,OAAO,CAAC,MAAM,CAAC,QAAQ,EAAE,EACzB,OAAO,CAAC,MAAM,CAAC,SAAS,EAAE,EAC1B,OAAO,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,EAAqC,EAAE,EAAE;QACzF,OAAO,GAAG,SAAS,IAAI,KAAK,KAAK,OAAO,EAAE,CAAC;IAC7C,CAAC,CAAC,CACH;IACD,UAAU,EAAE,CAAC,IAAI,OAAO,CAAC,UAAU,CAAC,OAAO,EAAE,CAAC;CAC/C,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,KAAK,GAAG,CAAC,EAAU,EAAiB,EAAE,CAAC,IAAI,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bronlabs/intents-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "SDK for Intents DeFi smart contracts",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"files": [
|
|
9
|
+
"dist/**/*",
|
|
10
|
+
"abi/**/*",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"defi",
|
|
15
|
+
"blockchain",
|
|
16
|
+
"ethereum",
|
|
17
|
+
"intents",
|
|
18
|
+
"sdk",
|
|
19
|
+
"smart-contracts"
|
|
20
|
+
],
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git@github.com:bronlabs-intents/intents-sdk.git"
|
|
24
|
+
},
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"engines": {
|
|
27
|
+
"node": ">=22.0.0"
|
|
28
|
+
},
|
|
29
|
+
"scripts": {
|
|
30
|
+
"build": "npm run clean && tsc",
|
|
31
|
+
"clean": "rimraf dist",
|
|
32
|
+
"prepublishOnly": "npm run build"
|
|
33
|
+
},
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"dotenv": "^16.5.0",
|
|
36
|
+
"ethers": "^5.8.0",
|
|
37
|
+
"winston": "^3.17.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"rimraf": "^6.0.1",
|
|
41
|
+
"typescript": "^5.8.3",
|
|
42
|
+
"@types/node": "^22.15.31"
|
|
43
|
+
}
|
|
44
|
+
}
|