Created
March 24, 2023 09:53
-
-
Save Madjarx/c09d93b33207e46a6ea424b419a1fb5c to your computer and use it in GitHub Desktop.
Transfer Eth Node RED node that i do not want to discard but i will drop it from my project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <script type="text/javascript"> | |
| RED.nodes.registerType('transfer', { | |
| category: 'General nodes', | |
| defaults: { | |
| rpc: { value: '', type: 'rpc', required: true }, | |
| privateKey: { value: '', type: 'privateKey', required: true }, | |
| recipient: { value: '', required: true }, | |
| recipientType: { value: 'str', required: true }, | |
| amount: { value: '', required: true, }, | |
| amountType: { value: 'str', required: true } | |
| }, | |
| inputs: 1, | |
| outputs: 1, | |
| label: function () { | |
| return this.name || 'Transfer'; | |
| }, | |
| icon: "font-awesome/fa-exchange", | |
| oneditprepare: function () { | |
| $("#node-input-recipient").typedInput({ | |
| default: "str", | |
| types: ["str", "msg", "flow", "global"], | |
| typeField: $("#node-input-recipientType") | |
| }) | |
| $("#node-input-amount").typedInput({ | |
| default: "str", | |
| types: ["str", "msg", "flow", "global"], | |
| typeField: $("#node-input-amountType") | |
| }) | |
| }, | |
| }); | |
| </script> | |
| <script type="text/html" data-template-name="transfer"> | |
| <div class="form-row"> | |
| <label for="node-input-rpc">Provider (RPC)</label> | |
| <input type="text" id="node-input-rpc"> | |
| </div> | |
| <div class="form-row"> | |
| <label for="node-input-privateKey">Sender Wallet</label> | |
| <input type="text" id="node-input-privateKey"> | |
| </div> | |
| <div class="form-row"> | |
| <label for="node-input-recipient">Receiver Wallet</label> | |
| <input type="text" id="node-input-recipient" style="width: 70%;" placeholder="Recipient Address"> | |
| <input type="hidden" id="node-input-recipientType"> | |
| </div> | |
| <div class="form-row"> | |
| <label for="node-input-amount">Amount:</label> | |
| <input type="text" id="node-input-amount" style="width: 70%;" placeholder="Amount in Native Tokens"> | |
| <input type="hidden" id="node-input-amountType"> | |
| </div> | |
| </script> | |
| <script type="text/html" data-help-name="transfer"> | |
| <p> | |
| The Transfer node allows users to transfer funds from one Ethereum account to another using the selected RPC Configuration node and the Wallet Configuration node. | |
| </p> | |
| </script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module.exports = function (RED) { | |
| const { ethers } = require("ethers"); | |
| function TransferNode(config) { | |
| RED.nodes.createNode(this, config); | |
| const node = this; | |
| node.rpcConfig = RED.nodes.getNode(config.rpc); | |
| node.privateKey = RED.nodes.getNode(config.privateKey); | |
| node.recipient = config.recipient; | |
| node.amount = config.amount; | |
| node.on("input", async function (msg, send, done) { | |
| if ( | |
| node.rpcConfig && | |
| node.rpcConfig.provider && | |
| node.privateKey && | |
| node.privateKey.key | |
| ) { | |
| try { | |
| const key = node.privateKey.key; | |
| console.log(key) | |
| const provider = node.rpcConfig.provider; | |
| const wallet = new ethers.Wallet(key, provider); | |
| // Evaluate values and convert them into wei | |
| const recipientAddress = RED.util.evaluateNodeProperty( | |
| config.recipient, | |
| config.recipientAddress || "str", | |
| node, | |
| msg | |
| ); | |
| const amountInEth = RED.util.evaluateNodeProperty( | |
| config.amount, | |
| config.amountType || "str", | |
| node, | |
| msg | |
| ); | |
| const weiAmount = ethers.parseEther(amountInEth); | |
| console.log(amountInEth) | |
| console.log(weiAmount) | |
| try { | |
| const transaction = await wallet.sendTransaction({ | |
| to: recipientAddress, | |
| value: ethers.parseEther(amountInEth), | |
| }); | |
| const receipt = await transaction.wait(); | |
| msg.payload = { | |
| transactionHash: receipt.transactionHash, | |
| blockNumber: receipt.blockNumber, | |
| confirmations: receipt.confirmations, | |
| }; | |
| send(msg); | |
| } catch (error) { | |
| this.error("Error transferring Ether: " + error.message); | |
| } | |
| } catch (error) { | |
| node.error("Error sending transaction: " + error); | |
| } | |
| } else { | |
| node.error("No RPC or Wallet configuration found"); | |
| } | |
| send(msg); | |
| if (done) done(); | |
| }); | |
| } | |
| RED.nodes.registerType("transfer", TransferNode); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment