@avail-project/ca-common 1.0.1 → 2.1.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.
@@ -1,113 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.YieldYakAggregator = void 0;
4
- const viem_1 = require("viem");
5
- const es_toolkit_1 = require("es-toolkit");
6
- const iface_1 = require("./iface");
7
- const data_1 = require("../data");
8
- const definition_1 = require("../proto/definition");
9
- const yakaggregator_abi_1 = require("../evmabi/yakaggregator.abi");
10
- const YakAggregatorAddresses = new Map([
11
- [(0, data_1.encodeChainID36)(definition_1.Universe.ETHEREUM, 42161), '0xb32C79a25291265eF240Eb32E9faBbc6DcEE3cE3'],
12
- [(0, data_1.encodeChainID36)(definition_1.Universe.ETHEREUM, 10), '0xCd887F78c77b36B0b541E77AfD6F91C0253182A2'],
13
- [(0, data_1.encodeChainID36)(definition_1.Universe.ETHEREUM, 43114), '0xC4729E56b831d74bBc18797e0e17A295fA77488c'],
14
- ].map(([chainID, addr]) => {
15
- return [(0, viem_1.bytesToHex)(chainID), addr];
16
- }));
17
- class YieldYakAggregator {
18
- clients = new Map();
19
- constructor(clients) {
20
- for (const client of clients) {
21
- const chainIDHex = (0, viem_1.bytesToHex)(client.chainID.toBytes());
22
- const aggAddr = YakAggregatorAddresses.get(chainIDHex);
23
- if (aggAddr == null) {
24
- continue;
25
- }
26
- this.clients.set(chainIDHex, {
27
- chainID: client.chainID,
28
- client: client.client,
29
- aggregatorAddress: aggAddr
30
- });
31
- }
32
- }
33
- async getQuotes(_requests) {
34
- const requestsWithOriginalIndexes = _requests.map((r, rid) => {
35
- return {
36
- req: r,
37
- idx: rid,
38
- };
39
- });
40
- const responses = new Array(_requests.length).fill(null);
41
- // it's so sad that JS doesn't have a proper binary data type
42
- const groupedByChainID = (0, es_toolkit_1.groupBy)(requestsWithOriginalIndexes, r => (0, viem_1.bytesToHex)(r.req.chain.toBytes()));
43
- await Promise.all(Array.from(Object.entries(groupedByChainID)).map(async ([chainIDHex, requests]) => {
44
- const config = this.clients.get(chainIDHex);
45
- if (config == null) {
46
- return;
47
- }
48
- const reverseIndexes = [];
49
- const mc3calls = [];
50
- for (const req of requests) {
51
- const inputTokenHex = (0, viem_1.bytesToHex)(req.req.inputToken.subarray(12));
52
- const outputTokenHex = (0, viem_1.bytesToHex)(req.req.outputToken.subarray(12));
53
- let args;
54
- switch (req.req.type) {
55
- case iface_1.QuoteType.EXACT_IN: {
56
- args = [req.req.inputAmount, inputTokenHex, outputTokenHex, 0, 1];
57
- break;
58
- }
59
- case iface_1.QuoteType.EXACT_OUT: {
60
- args = [req.req.outputAmount, outputTokenHex, inputTokenHex, 0, 1];
61
- break;
62
- }
63
- }
64
- const indexes = [];
65
- for (let steps = 1; steps !== 5; steps++) {
66
- const clonedArgs = (0, es_toolkit_1.clone)(args);
67
- clonedArgs[3] = steps;
68
- const idx = mc3calls.push({
69
- address: config.aggregatorAddress,
70
- abi: yakaggregator_abi_1.YakAggregatorABI,
71
- functionName: 'findBestPathWithGas',
72
- args: clonedArgs
73
- });
74
- indexes.push(idx - 1);
75
- }
76
- reverseIndexes.push([req, indexes]);
77
- }
78
- const _final = await config.client.multicall({
79
- allowFailure: false,
80
- contracts: mc3calls,
81
- multicallAddress: '0xcA11bde05977b3631167028862bE2a173976CA11'
82
- });
83
- for (const [req, indexes] of reverseIndexes) {
84
- const collected = [];
85
- for (const index of indexes) {
86
- collected.push(_final[index]);
87
- }
88
- // @ts-expect-error the typing in maxBy is wrong, it can work with anything that is comparable
89
- const optimalChoice = (0, es_toolkit_1.maxBy)(collected, route => (0, es_toolkit_1.last)(route.amounts));
90
- if (optimalChoice.path.length === 0) {
91
- responses[req.idx] = null;
92
- return;
93
- }
94
- // we have to reverse everything
95
- if (req.req.type === iface_1.QuoteType.EXACT_OUT) {
96
- optimalChoice.adapters.reverse();
97
- optimalChoice.amounts.reverse();
98
- optimalChoice.path.reverse();
99
- }
100
- const output = (0, es_toolkit_1.last)(optimalChoice.amounts);
101
- responses[req.idx] = {
102
- type: req.req.type,
103
- inputAmount: optimalChoice.amounts[0],
104
- outputAmountLikely: output,
105
- outputAmountMinimum: output,
106
- offer: optimalChoice
107
- };
108
- }
109
- }));
110
- return responses;
111
- }
112
- }
113
- exports.YieldYakAggregator = YieldYakAggregator;
@@ -1,109 +0,0 @@
1
- import { bytesToHex } from "viem";
2
- import { clone as _clone, groupBy, last as _last, maxBy } from "es-toolkit";
3
- import { QuoteType } from "./iface";
4
- import { encodeChainID36 } from "../data";
5
- import { Universe } from "../proto/definition";
6
- import { YakAggregatorABI } from "../evmabi/yakaggregator.abi";
7
- const YakAggregatorAddresses = new Map([
8
- [encodeChainID36(Universe.ETHEREUM, 42161), '0xb32C79a25291265eF240Eb32E9faBbc6DcEE3cE3'],
9
- [encodeChainID36(Universe.ETHEREUM, 10), '0xCd887F78c77b36B0b541E77AfD6F91C0253182A2'],
10
- [encodeChainID36(Universe.ETHEREUM, 43114), '0xC4729E56b831d74bBc18797e0e17A295fA77488c'],
11
- ].map(([chainID, addr]) => {
12
- return [bytesToHex(chainID), addr];
13
- }));
14
- export class YieldYakAggregator {
15
- clients = new Map();
16
- constructor(clients) {
17
- for (const client of clients) {
18
- const chainIDHex = bytesToHex(client.chainID.toBytes());
19
- const aggAddr = YakAggregatorAddresses.get(chainIDHex);
20
- if (aggAddr == null) {
21
- continue;
22
- }
23
- this.clients.set(chainIDHex, {
24
- chainID: client.chainID,
25
- client: client.client,
26
- aggregatorAddress: aggAddr
27
- });
28
- }
29
- }
30
- async getQuotes(_requests) {
31
- const requestsWithOriginalIndexes = _requests.map((r, rid) => {
32
- return {
33
- req: r,
34
- idx: rid,
35
- };
36
- });
37
- const responses = new Array(_requests.length).fill(null);
38
- // it's so sad that JS doesn't have a proper binary data type
39
- const groupedByChainID = groupBy(requestsWithOriginalIndexes, r => bytesToHex(r.req.chain.toBytes()));
40
- await Promise.all(Array.from(Object.entries(groupedByChainID)).map(async ([chainIDHex, requests]) => {
41
- const config = this.clients.get(chainIDHex);
42
- if (config == null) {
43
- return;
44
- }
45
- const reverseIndexes = [];
46
- const mc3calls = [];
47
- for (const req of requests) {
48
- const inputTokenHex = bytesToHex(req.req.inputToken.subarray(12));
49
- const outputTokenHex = bytesToHex(req.req.outputToken.subarray(12));
50
- let args;
51
- switch (req.req.type) {
52
- case QuoteType.EXACT_IN: {
53
- args = [req.req.inputAmount, inputTokenHex, outputTokenHex, 0, 1];
54
- break;
55
- }
56
- case QuoteType.EXACT_OUT: {
57
- args = [req.req.outputAmount, outputTokenHex, inputTokenHex, 0, 1];
58
- break;
59
- }
60
- }
61
- const indexes = [];
62
- for (let steps = 1; steps !== 5; steps++) {
63
- const clonedArgs = _clone(args);
64
- clonedArgs[3] = steps;
65
- const idx = mc3calls.push({
66
- address: config.aggregatorAddress,
67
- abi: YakAggregatorABI,
68
- functionName: 'findBestPathWithGas',
69
- args: clonedArgs
70
- });
71
- indexes.push(idx - 1);
72
- }
73
- reverseIndexes.push([req, indexes]);
74
- }
75
- const _final = await config.client.multicall({
76
- allowFailure: false,
77
- contracts: mc3calls,
78
- multicallAddress: '0xcA11bde05977b3631167028862bE2a173976CA11'
79
- });
80
- for (const [req, indexes] of reverseIndexes) {
81
- const collected = [];
82
- for (const index of indexes) {
83
- collected.push(_final[index]);
84
- }
85
- // @ts-expect-error the typing in maxBy is wrong, it can work with anything that is comparable
86
- const optimalChoice = maxBy(collected, route => _last(route.amounts));
87
- if (optimalChoice.path.length === 0) {
88
- responses[req.idx] = null;
89
- return;
90
- }
91
- // we have to reverse everything
92
- if (req.req.type === QuoteType.EXACT_OUT) {
93
- optimalChoice.adapters.reverse();
94
- optimalChoice.amounts.reverse();
95
- optimalChoice.path.reverse();
96
- }
97
- const output = _last(optimalChoice.amounts);
98
- responses[req.idx] = {
99
- type: req.req.type,
100
- inputAmount: optimalChoice.amounts[0],
101
- outputAmountLikely: output,
102
- outputAmountMinimum: output,
103
- offer: optimalChoice
104
- };
105
- }
106
- }));
107
- return responses;
108
- }
109
- }
@@ -1,21 +0,0 @@
1
- import { Hex, PublicClient } from "viem";
2
- import { Aggregator, Quote, QuoteRequestExactInput, QuoteRequestExactOutput } from "./iface";
3
- import { OmniversalChainID } from "../data";
4
- type YakOffer = {
5
- amounts: bigint[];
6
- adapters: Hex[];
7
- path: Hex[];
8
- gasEstimate: bigint;
9
- };
10
- export type YakAggregatorQuote = Quote & {
11
- offer: YakOffer;
12
- };
13
- export declare class YieldYakAggregator implements Aggregator {
14
- private readonly clients;
15
- constructor(clients: {
16
- chainID: OmniversalChainID;
17
- client: PublicClient;
18
- }[]);
19
- getQuotes(_requests: (QuoteRequestExactInput | QuoteRequestExactOutput)[]): Promise<(YakAggregatorQuote | null)[]>;
20
- }
21
- export {};