-
Notifications
You must be signed in to change notification settings - Fork 1
/
appData.ts
253 lines (238 loc) · 6.94 KB
/
appData.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { BigNumber, ethers } from "ethers";
import axios from "axios";
import { AppData, CowHook } from "./types";
function PermittableToken(
provider: ethers.providers.JsonRpcProvider,
address: string
): ethers.Contract {
return new ethers.Contract(
address,
[
`
function decimals() view returns (uint8)
`,
`
function name() view returns (string)
`,
`
function version() view returns (string)
`,
`
function nonces(address owner) view returns (uint256)
`,
`
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
)
`,
],
provider
);
}
/// Permit Token Hook!
export async function buildPermitHook(
wallet: ethers.Wallet,
provider: ethers.providers.JsonRpcProvider,
spender: string,
amount: BigNumber,
permittableTokenAddress: string,
chainId: number
): Promise<CowHook> {
const token = PermittableToken(provider, permittableTokenAddress);
const permit = {
owner: wallet.address,
spender,
value: amount,
nonce: await token.nonces(wallet.address),
deadline: ethers.constants.MaxUint256,
};
const permitSignature = ethers.utils.splitSignature(
await wallet._signTypedData(
{
name: await token.name(),
version: await token.version(),
chainId,
verifyingContract: token.address,
},
{
Permit: [
{ name: "owner", type: "address" },
{ name: "spender", type: "address" },
{ name: "value", type: "uint256" },
{ name: "nonce", type: "uint256" },
{ name: "deadline", type: "uint256" },
],
},
permit
)
);
const permitParams = [
permit.owner,
permit.spender,
permit.value,
permit.deadline,
permitSignature.v,
permitSignature.r,
permitSignature.s,
];
const permitHook = {
target: token.address,
callData: token.interface.encodeFunctionData("permit", permitParams),
gasLimit: `${await token.estimateGas.permit(...permitParams)}`,
};
console.log("permit hook:", permitHook);
return permitHook;
}
/// Hook to Claim Validator Rewards for withdrawalAddress
export function buildClaimHook(
provider: ethers.providers.JsonRpcProvider,
withdrawalAddress: string,
claimContract: string
): CowHook {
const CLAIM_CONTRACT = new ethers.Contract(
claimContract,
[
`function withdrawableAmount(address user) view returns (u256)`,
`function claimWithdrawal(address _address) public`,
],
provider
);
const claimHook = {
target: CLAIM_CONTRACT.address,
callData: CLAIM_CONTRACT.interface.encodeFunctionData("claimWithdrawal", [
withdrawalAddress,
]),
// Example Tx: https://gnosisscan.io/tx/0x9501e8cbe873126bfb52ceab26a8644f1f8607f0de2937fa29d2112569480c59
// Gas Limit: 82264
// gasLimit: `${await DEPOSIT_CONTRACT.estimateGas.claimWithdrawal(...withdrawalAddress)}`,
// Now it doesn't need to be async!
gasLimit: "82264",
};
return claimHook;
}
export function generateAppData(
preHooks: object[],
postHooks: object[]
): AppData {
const appData = JSON.stringify({
appCode: "CoW Swap",
version: "0.9.0",
metadata: {
hooks: {
pre: preHooks,
post: postHooks,
},
},
});
const appHash = ethers.utils.id(appData);
console.log(`Constructed AppData with Hash ${appHash}`);
console.log("App Data Content:", appData);
// https://api.cow.fi/docs/#/
// This needs to be posted to https://api.cow.fi/xdai/api/v1/app_data/{app_hash}
// with payload {"fullAppData": "{appData}"}
// CURL: Note that this has to be an escaped JSON string.
// curl -X 'PUT' \
// 'https://api.cow.fi/xdai/api/v1/app_data/{APP_HASH}' \
// -H 'accept: application/json' \
// -H 'Content-Type: application/json' \
// -d '{
// "fullAppData":"{\"appCode\":\"CoW Swap\",\"metadata\":{\"hooks\":{\"post\":[],\"pre\":[{\"callData\":\"0xa3066aab000000000000000000000000{WITHDRAWAL_ADDRESS}\",\"gasLimit\":\"82264\",\"target\":\"0x0B98057eA310F4d31F2a452B414647007d1645d9\"}],\"version\":\"0.1.0\"}},\"version\":\"0.10.0\"}"}'
return { hash: appHash, data: appData };
}
export async function postAppData(appData: AppData): Promise<void> {
const provider = new ethers.providers.JsonRpcProvider(
process.env.NODE_URL || "https://rpc.gnosischain.com/"
);
// TODO - EOA permit SAFE via set allowance and encode transfer from hook.
let { hash, data } = appData;
const url = `https://api.cow.fi/xdai/api/v1/app_data/${hash}`;
const requestBody = {
fullAppData: data,
};
try {
await axios.put(url, requestBody, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
});
console.log("App data posted successfully");
} catch (error: any) {
console.error("Error updating app data:", error.message);
}
}
export async function buildTransferBalanceFromHook(
provider: ethers.providers.JsonRpcProvider,
tokenAddress: string,
from: string,
to: string
): Promise<CowHook> {
const TRANSFER_BALANCE_FROM_CONTRACT =
"0xD4121d2d90CE7C5F7FB66c4E96815fc377481635";
console.log("tokenAddres:", tokenAddress);
console.log("from:", from);
console.log("to:", from);
const transfer = new ethers.Contract(
TRANSFER_BALANCE_FROM_CONTRACT,
[
`function transferBalanceFrom(address token, address from, address to) public`,
],
provider
);
console.log("transfer:", transfer);
const params = [tokenAddress, from, to];
return {
target: transfer.address,
callData: transfer.interface.encodeFunctionData(
"transferBalanceFrom",
params
),
gasLimit: `${await transfer.estimateGas.transferBalanceFrom(...params)}`,
};
}
export async function mixedEoaSafeAppData(
eoaAddress: string,
safeAddress: string,
claimContractAddress: string,
claimTokenAddress: string
): Promise<AppData> {
const provider = new ethers.providers.JsonRpcProvider(
process.env.NODE_URL || "https://rpc.gnosischain.com/"
);
// TODO - EOA permit SAFE via set allowance and encode transfer from hook.
let appData = await generateAppData(
[
buildClaimHook(provider, safeAddress, claimContractAddress),
await buildTransferBalanceFromHook(
provider,
claimTokenAddress,
eoaAddress,
safeAddress
),
],
[]
);
await postAppData(appData);
return appData;
}
export async function safeOnlyAppData(
safeAddress: string,
claimContractAddress: string
): Promise<AppData> {
const provider = new ethers.providers.JsonRpcProvider(
process.env.NODE_URL || "https://rpc.gnosischain.com/"
);
// TODO - EOA permit SAFE via set allowance and encode transfer from hook.
let appData = generateAppData(
[buildClaimHook(provider, safeAddress, claimContractAddress)],
[]
);
await postAppData(appData);
return appData;
}