forked from sprintertech/maintenance-utils
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPostCallWithContractCall.ts
184 lines (168 loc) · 5.68 KB
/
PostCallWithContractCall.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
import { sendSignedTransactionWithRawData } from "./utils/excute_raw_tx";
import dotenv from "dotenv";
import { Web3 } from "web3";
const fs = require("fs");
const fetch = require("node-fetch");
dotenv.config();
const abiPathSprinterName = "src/ABIS/sprinterName.json";
const abiPathERC721Payable = "src/ABIS/ERC721Payable.json";
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 || '';
if (!walletPk) {
throw new Error("Missing environment variable: PRIVATE_KEY");
}
let amount: BigInt;
let approvalAddress: string | undefined;
let contractAddress: string;
let outputTokenAddress: string | undefined;
let callData: string;
const contractAddressesERC721Payable: Record<number, string> = {
11155111: "0x99eb23BEC48bF56C80889cFbcBF2d491F8aC75fe",
84532: "0xAf8De6Aa5004E8e323DCC93C683A55e5eE87b9e9",
1993: "0xAf8De6Aa5004E8e323DCC93C683A55e5eE87b9e9",
};
const contractAddressesSprinterName: Record<number, string> = {
84532: "0x3F9A68fF29B3d86a6928C44dF171A984F6180009",
11155111: "0xf70fb86F700E8Bb7cDf1c20197633518235c3425",
1993: "0x17e4C404aD634E429ebCdF9a10F38A96Ce8eEF27",
421614: "0xD7d5E7d7eaD31E783Df01760FbFad249704Aab14",
};
const usdcAddress: Record<number, string> = {
11155111: "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238",
84532: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
1993: "0xE61e5ed4c4f198c5384Ef57E69aAD1eF0c911004",
};
async function contractCallData(
chainId: number,
tokenType: string,
account: string
) {
const providerURL = process.env.PROVIDER_URL_11155111;
const web3js = new Web3(providerURL);
if (tokenType === "usdc") {
amount = BigInt(1) * BigInt(1e6);
approvalAddress = contractAddressesSprinterName[chainId];
contractAddress = contractAddressesSprinterName[chainId];
outputTokenAddress = usdcAddress[chainId];
const contractABIsprinterName = JSON.parse(
fs.readFileSync(abiPathSprinterName, "utf8")
);
const contract = new web3js.eth.Contract(
contractABIsprinterName,
contractAddress
);
callData = contract.methods
.claimName(`Test${Date.now().toString()}`, account, 100000)
.encodeABI();
} else if (tokenType === "eth") {
amount =
chainId === 11155111
? BigInt(1) * BigInt(1e11)
: BigInt(1) * BigInt(1e14);
approvalAddress = undefined;
contractAddress = contractAddressesERC721Payable[chainId];
outputTokenAddress = undefined;
const contractABIERC721Payable = JSON.parse(
fs.readFileSync(abiPathERC721Payable, "utf8")
);
const contract = new web3js.eth.Contract(
contractABIERC721Payable,
approvalAddress
);
callData = contract.methods
.mintPayable(account, Date.now(), `Test${Date.now()}`)
.encodeABI();
}
}
async function callApi(sendTx: boolean) {
const destChainId = 84532;
const tokenType = "usdc";
const account = "0x9A17FA0A2824EA855EC6aD3eAb3Aa2516EC6626d";
await contractCallData(destChainId, tokenType, account);
const data = {
account: account,
amount: `${amount}`,
destination: destChainId,
destinationContractCall: {
approvalAddress: approvalAddress,
callData: callData,
contractAddress: contractAddress,
gasLimit: 420000,
outputTokenAddress: outputTokenAddress,
},
recipient: account,
threshold: "1",
token: tokenType,
type: "fungible",
whitelistedSourceChains: [11155111],
};
console.log("Request data", data);
try {
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);