const MultiSenderWidget = ({ context, NearAPI }) => { // Initialize the state State.init({ tokenContract: "", csvInput: "", isConnected: context.accountId ? true : false, // Check if user is connected }); const simpleSplit = (str, delimiter) => { const result = []; let temp = ""; for (let i = 0; i < str.length; i++) { if (str[i] === delimiter) { result.push(temp); temp = ""; } else { temp += str[i]; } } result.push(temp); return result; }; const parseNearAmount = (amount) => { const parts = simpleSplit(amount, "."); const wholePart = parts[0]; const fractionalPart = parts[1] || ""; return wholePart + fractionalPart.padEnd(24, "0"); }; const handleMultiSend = () => { // Ensure csvInput is a string const csvInput = String(State.csvInput); // Debugging: Log the current CSV input console.log("CSV Input:", csvInput); const rows = simpleSplit(csvInput, ";"); const transactions = rows.map((row) => { const [recipient, amount] = simpleSplit(row, ",").map((item) => item.trim() ); // Debugging: Log the recipient and amount console.log("Recipient:", recipient, "Amount:", amount); return { receiver_id: recipient, amount: parseNearAmount(amount), }; }); // Use Promise.all to handle multiple transactions Promise.all( transactions.map((tx) => { // Debugging: Log the transaction details console.log("Transaction:", tx); return Near.call([ { contractName: State.tokenContract, methodName: "ft_transfer", args: { receiver_id: tx.receiver_id, amount: tx.amount, }, gas: "300000000000000", // gas limit deposit: "1", // deposit, in yoctoNEAR }, ]); }) ) .then(() => { console.log("Tokens sent successfully"); }) .catch((error) => { console.error("Error during multisend:", error); }); }; const handleCSVInput = (e) => { State.update({ csvInput: e.target.value }); }; return ( <div> <h1>NEAR Social Multisender</h1> <div> <label>Token Contract:</label> <input type="text" value={State.tokenContract} onChange={(e) => State.update({ tokenContract: e.target.value })} /> </div> <div> <label>CSV Input:</label> <textarea placeholder="wallet1, amount1; wallet2, amount2; ..." value={State.csvInput} onChange={handleCSVInput} /> </div> <div> <button onClick={handleMultiSend}>Send Tokens</button> </div> <div> <h3>CSV Template:</h3> <p>wallet1, amount1; wallet2, amount2; ...</p> </div> </div> ); }; return <MultiSenderWidget />;