This is my first solidity contract, it was working until I wanted to add extra data to the transaction. I'm not sure if I'm calling it wrong with webjs3 or the contract itself is flawed in some way.
pragma solidity ^0.8.0;
// SPDX-License-Identifier: MIT
contract PaymentSplitter {
address public owner;
address payable public payee1;
address payable public payee2;
string public storedString;
constructor(address payable _payee1, address payable _payee2) {
owner = msg.sender;
payee1 = _payee1;
payee2 = _payee2;
}
receive() external payable {
uint256 totalReceived = msg.value;
uint256 amountToPayee1 = (totalReceived * 90) / 100;
uint256 amountToPayee2 = (totalReceived * 10) / 100;
// Send Ether to payee1
payee1.transfer(amountToPayee1);
// Send Ether to payee2
payee2.transfer(amountToPayee2);
}
event StringProcessed(address indexed sender, string data);
function processStringData(string calldata data) external {
emit StringProcessed(msg.sender, data);
storedString = data;
}
}
webjs
const contractAddress = '0x9b8b9D5Ee2337e0F9D4Dd4c7f8b5bF008e9de3f5';
const contractABI = [{ "inputs": [{ "internalType": "string", "name": "data", "type": "string" }], "name": "processStringData", "outputs": [], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [{ "internalType": "addresspayable", "name": "_payee1", "type": "address" }, { "internalType": "addresspayable", "name": "_payee2", "type": "address" }], "stateMutability": "nonpayable", "type": "constructor" }, { "anonymous": false, "inputs": [{ "indexed": true, "internalType": "address", "name": "sender", "type": "address" }, { "indexed": false, "internalType": "string", "name": "data", "type": "string" }], "name": "StringProcessed", "type": "event" }, { "stateMutability": "payable", "type": "receive" }, { "inputs": [], "name": "owner", "outputs": [{ "internalType": "address", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "payee1", "outputs": [{ "internalType": "addresspayable", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "payee2", "outputs": [{ "internalType": "addresspayable", "name": "", "type": "address" }], "stateMutability": "view", "type": "function" }, { "inputs": [], "name": "storedString", "outputs": [{ "internalType": "string", "name": "", "type": "string" }], "stateMutability": "view", "type": "function" }];
const contract = await new web3.eth.Contract(contractABI, contractAddress);
const fromAddress = accounts[0];
const stringData = checkoutDetails; // Replace with your string data
const data = web3.utils.utf8ToHex(stringData);
const valueInWei = web3.utils.toWei('1', 'ether');
// Construct the data field by prepending the function selector (4 bytes)
const functionSelector = web3.eth.abi.encodeFunctionSignature('processStringData(string)');
const txData = functionSelector + data.slice(2);
// Create a transaction object with the properly formatted data
const tx = {
from: fromAddress,
to: contractAddress,
value: valueInWei,
data: txData,
};
web3.eth.sendTransaction(tx)
.then(receipt => {
console.log('Transaction Receipt:', receipt);
})
.catch(error => {
console.error('Error:', error);
});
Any guidance is appreciated