forked from sygmaprotocol/maintenance-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
PostCallNoContractCall.ts
108 lines (96 loc) · 3.14 KB
/
PostCallNoContractCall.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
import { sendSignedTransactionWithRawData } from "./utils/excute_raw_tx";
import dotenv from "dotenv";
const fetch = require("node-fetch");
dotenv.config();
// const apiUrl = "https://api.test.sprinter.buildwithsygma.com/solution/call";
const apiUrl = "http://127.0.0.1:8080/solution/call";
const walletPk = process.env.PRIVATE_KEY || ``;
async function callApi(sendTx: boolean) {
const account = "0x9A17FA0A2824EA855EC6aD3eAb3Aa2516EC6626d";
const data = {
account: account,
amount: "240000",
destination: 11155111,
// destinationContractCall: {
// approvalAddress: CONTRACT_ADDRESS,
// callData: callData,
// contractAddress: CONTRACT_ADDRESS,
// gasLimit: 420000,
// outputTokenAddress: token_ADDRESS
// },
// recipient:"",
threshold: "1",
token: "usdc",
type: "fungible",
whitelistedSourceChains: [84532],
whitelistedTools: ["relay"],
};
try {
console.log("Requst data", data);
const response = await fetch(apiUrl, {
method: "POST",
headers: {
accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify(data),
});
if (!response.ok) {
const errorBody = await response.json();
throw new Error(
`HTTP error! status: ${response.status}, message: ${JSON.stringify(
errorBody
)}`
);
}
const jsonResponse = await response.json();
console.log("API Response:", JSON.stringify(jsonResponse, null, 2));
if (sendTx) {
const transactionData = jsonResponse.data[0]?.transaction;
const approvals = jsonResponse.data[0]?.approvals || [];
// Step 1: Handle Approval Transactions
for (const approval of approvals) {
const approvalTo = approval.to;
const approvalData = approval.data;
const approvalValue = approval.value || "0x0";
const approvalChainID = approval.chainId;
const providerUrl =
process.env[`PROVIDER_URL_${approvalChainID}`] || "";
console.log(`Sending approval transaction to ${approvalTo}`);
await sendSignedTransactionWithRawData(
approvalTo,
approvalValue,
approvalData,
providerUrl,
walletPk,
approvalChainID
);
}
// Step 2: Send Main Transaction
const mainData = transactionData?.data;
const mainTo = transactionData?.to;
const transactionValue = transactionData?.value || "0x0";
const transactionChainID = transactionData?.chainId;
const providerUrl =
process.env[`PROVIDER_URL_${transactionChainID}`] || "";
if (mainData && mainTo) {
console.log(`Sending main transaction to ${mainTo}`);
await sendSignedTransactionWithRawData(
mainTo,
transactionValue,
mainData,
providerUrl,
walletPk,
transactionChainID
);
} else {
console.error(
"Main transaction data is missing from the API response."
);
}
}
} catch (error) {
console.error("Error during API call and transaction handling:", error);
}
}
callApi(true);