/
ethereum.ts
163 lines (156 loc) · 4.18 KB
/
ethereum.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import MulticallABI from '@/abi/Multicall.json';
import { Abi } from 'abitype/src/abi';
import { EIP7412 } from 'erc7412';
import { PythAdapter } from 'erc7412/dist/src/adapters/pyth';
import {
Address,
decodeFunctionResult,
encodeFunctionData,
Hex,
PublicClient,
TransactionRequestBase,
WalletClient,
zeroAddress,
} from 'viem';
async function generate7412CompatibleCall(
client: PublicClient,
from: Address,
txn: Partial<TransactionRequestBase>,
pythUrl: string
) {
const converter = new EIP7412([new PythAdapter(pythUrl)], createMakeMulticall(from));
return await converter.enableERC7412(client as any, txn);
}
function createMakeMulticall(from: Address) {
return (
txns: Partial<TransactionRequestBase>[]
): {
operation: string;
account: Address;
to: Address;
value: bigint;
data: Hex;
} => {
const totalValue = txns.reduce((val, txn) => {
return val + (txn.value || BigInt(0));
}, BigInt(0));
return {
operation: '1', // multicall is a DELEGATECALL
account: from,
to: '0xE2C5658cC5C448B48141168f3e475dF8f65A1e3e',
value: totalValue,
data: encodeFunctionData({
abi: MulticallABI,
functionName: 'aggregate3Value',
args: [
txns.map((txn) => ({
target: txn.to || zeroAddress,
callData: txn.data || '0x',
value: txn.value || '0',
requireSuccess: true,
})),
],
}),
};
};
}
export async function contractCall(
from: Address,
to: Address,
functionName: string,
params: any,
abi: Abi,
publicClient: PublicClient,
pythUrl: string
) {
const data = encodeFunctionData({
abi,
functionName,
args: Array.isArray(params) ? params : [params],
});
const txn = {
account: from,
to,
data,
};
const call = await generate7412CompatibleCall(publicClient, from, txn, pythUrl);
const res = await publicClient.call({ ...call, account: from });
try {
// Attempt to decode the multicall response such that we can return the last return value
// ERC-7412 is causing there to be items prepended to the list from the oracle contract calls
const multicallValue: any = (res as any).data
? decodeFunctionResult({
abi: MulticallABI,
functionName: 'aggregate3Value',
data: (res as any).data as any,
})
: (res as any).data;
if (Array.isArray(multicallValue) && multicallValue[multicallValue.length - 1].success) {
return (res as any).data
? decodeFunctionResult({
abi,
functionName,
data: multicallValue[multicallValue.length - 1].returnData as any,
})
: (res as any).data;
} else {
return (res as any).data
? decodeFunctionResult({
abi,
functionName,
data: (res as any).data as any,
})
: (res as any).data;
}
} catch (e) {
// We land here if the call is not a multicall
return (res as any).data
? decodeFunctionResult({
abi,
functionName,
data: (res as any).data as any,
})
: (res as any).data;
}
}
export async function contractTransaction(
from: Address,
to: Address,
functionName: string,
params: any,
abi: Abi,
publicClient: PublicClient,
walletClient: WalletClient,
pythUrl: string
) {
const data = encodeFunctionData({
abi,
functionName,
args: Array.isArray(params) ? params : [params],
});
const txn = {
account: from,
to,
data,
};
const call = await generate7412CompatibleCall(publicClient, from, txn, pythUrl);
const hash = await walletClient.sendTransaction({
chain: publicClient.chain!,
account: from,
to: call.to,
data: call.data,
value: call.value,
});
return hash;
}
/**
* Truncate an address to a given length
* @param address The address to truncate
* @param length The length to truncate to
* @returns The truncated address
* @example
* truncateAddress('0x1234567890abcdef1234567890abcdef12345678') // '0x123456...12345678'
*/
export const truncateAddress = (address: string, length = 6) => {
return `${address.slice(0, length)}...${address.slice(-length)}`;
};