From 39cc1206d88f3c0bb79c895118fb9a306d96298d Mon Sep 17 00:00:00 2001 From: Andrey Dodonov <44361144+AndreyDodonov-EH@users.noreply.github.com> Date: Fri, 11 Oct 2024 19:33:16 +0200 Subject: [PATCH] Update example of subscribing to a transaction in accordance with changes in web3.js web3.js changed the way how we subscribe to events, update this example accordingly. Optionally querying the balances may be removed for this example to be concise. --- docs/introduction-to-smart-contracts.rst | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/docs/introduction-to-smart-contracts.rst b/docs/introduction-to-smart-contracts.rst index 45857c3f891c..bd1a30b6fdfc 100644 --- a/docs/introduction-to-smart-contracts.rst +++ b/docs/introduction-to-smart-contracts.rst @@ -186,16 +186,17 @@ and any user interface calls the automatically generated ``balances`` function f .. code-block:: javascript - Coin.Sent().watch({}, '', function(error, result) { - if (!error) { - console.log("Coin transfer: " + result.args.amount + - " coins were sent from " + result.args.from + - " to " + result.args.to + "."); - console.log("Balances now:\n" + - "Sender: " + Coin.balances.call(result.args.from) + - "Receiver: " + Coin.balances.call(result.args.to)); - } + Coin.events.Sent().on('data', async function (event) { + console.log("Coin transfer: " + event.returnValues.amount + + " coins were sent from " + event.returnValues.from + + " to " + event.returnValues.to + ".") + const senderBalance = await Coin.methods.balances(event.returnValues.from).call(); + const receiverBalance = await Coin.methods.balances(event.returnValues.to).call(); + console.log("Balances now:\n" + + "Sender: " + senderBalance + + "\nReceiver: " + receiverBalance); }) + Coin.events.Sent().on('error', console.error); .. index:: coin