-
Notifications
You must be signed in to change notification settings - Fork 0
/
zkevm-swap-and-bridge.ts
168 lines (153 loc) · 4.66 KB
/
zkevm-swap-and-bridge.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
164
165
166
167
168
import { ethers } from "ethers";
import minimist from "minimist";
import erc20Abi from "./abis/ERC20.json";
import zkevmBridgerAbi from "./abis/ZkEVMBridger.json";
import {
EcdsaSigningScheme,
OrderBookApi,
OrderKind,
OrderSigningUtils,
SigningScheme,
SupportedChainId,
} from "@cowprotocol/cow-sdk";
import {
Account,
createPublicClient,
createWalletClient,
encodeFunctionData,
http,
parseUnits,
} from "viem";
import { goerli, mainnet } from "viem/chains";
import { privateKeyToAccount } from "viem/accounts";
const assertDefined = (args: { [key: string]: any }) => {
Object.entries(args).forEach(([key, value]) => {
if (value === undefined) {
throw new Error(`Missing argument ${key}`);
}
});
};
const zkEVMBridgeAddress = "0x2a3DD3EB832aF982ec71669E178424b10Dca2EDe";
const vaultRelayerAddress = "0xC92E8bdf79f0507f65a392b0ab4667716BFE0110";
const MAX_UINT = 2n ** 256n - 1n;
// TODO: fix this
const zkevmBridgerAddress = "0x8866d74b2dFf96DC4cbCb11e70ed54b432EE8c3B";
const approveIfNotApproved = async (
client: ReturnType<typeof createPublicClient>,
walletClient: ReturnType<typeof createWalletClient>,
token: string,
account: Account,
amount: BigInt
) => {
const allowance = (await client.readContract({
address: token as `0x${string}`,
abi: erc20Abi,
functionName: "allowance",
args: [account.address, vaultRelayerAddress],
})) as BigInt;
console.log("Current allowance: ", allowance.toString());
if (allowance < amount) {
const setAllowanceTxHash = await walletClient.writeContract({
address: token as `0x${string}`,
abi: erc20Abi,
functionName: "approve",
args: [vaultRelayerAddress, MAX_UINT],
account,
chain: mainnet,
});
return setAllowanceTxHash;
}
};
const main = async () => {
const rpcUrl = "https://eth.llamarpc.com";
// const rpcUrl = "https://ethereum-goerli.publicnode.com";
const transport = http(rpcUrl);
const client = createPublicClient({
chain: mainnet,
transport,
});
// cli arguments
const startIndex =
process.argv.findIndex((x) => x.includes(module.filename)) + 1;
const args = minimist(process.argv.slice(startIndex), {
string: ["privateKey", "fromToken", "toToken", "inputAmount"],
});
const { privateKey, fromToken, toToken, inputAmount } = args;
assertDefined({ privateKey, fromToken, toToken, inputAmount });
const account = privateKeyToAccount(privateKey);
const walletClient = createWalletClient({
account,
transport,
});
const cowChainId = SupportedChainId.MAINNET;
const fromTokenDecimals = (await client.readContract({
abi: erc20Abi,
functionName: "decimals",
address: fromToken,
})) as number;
const fromTokenAmount = parseUnits(inputAmount, fromTokenDecimals);
const approveTx = await approveIfNotApproved(
client,
walletClient,
fromToken,
account,
fromTokenAmount
);
if (approveTx !== undefined) {
const txReceipt = await client.waitForTransactionReceipt({
hash: approveTx,
});
console.log("Approve tx receipt: ", txReceipt);
}
const bridgeHook = {
target: zkevmBridgerAddress,
callData: encodeFunctionData({
abi: zkevmBridgerAbi,
functionName: "bridgeToken",
args: [toToken, account.address],
}),
gasLimit: (1_000_000).toString(),
};
const orderBookApi = new OrderBookApi({ chainId: cowChainId });
const orderData = {
kind: OrderKind.SELL as unknown as Parameters<
typeof orderBookApi.getQuote
>[0]["kind"],
sellToken: fromToken,
buyToken: toToken,
sellAmountBeforeFee: fromTokenAmount.toString(),
from: account.address,
validTo: Math.floor(new Date().getTime() / 1000 + 3600),
appData: JSON.stringify({
backend: { hooks: { post: [bridgeHook], pre: [] } },
}),
receiver: zkevmBridgerAddress,
};
console.log("Order data: ", orderData);
const quoteResponse = await orderBookApi.getQuote(orderData);
// console.log({ quoteResponse });
console.log("Quote response: ", quoteResponse);
const signedOrder = await OrderSigningUtils.signOrder(
{
...quoteResponse.quote,
appData: ethers.utils.id(quoteResponse.quote.appData),
receiver: quoteResponse.quote.receiver!,
},
cowChainId,
new ethers.Wallet(privateKey)
);
const sentOrder = await orderBookApi.sendOrder({
...quoteResponse.quote,
signature: signedOrder.signature,
signingScheme:
signedOrder.signingScheme === EcdsaSigningScheme.EIP712
? SigningScheme.EIP712
: SigningScheme.ETHSIGN,
});
console.log("Sent order: ", sentOrder);
console.log(
"Explorer URL: ",
`https://explorer.cow.fi/orders/${sentOrder}?tab=overview`
);
};
main();