Hello World with Paladin¶
This tutorial walks you through deploying and interacting with a simple HelloWorld smart contract using the Paladin SDK. The example demonstrates how to deploy a contract, interact with it by calling its sayHello function, and retrieve and verify the emitted event.
The code for this tutorial can be found in examples/helloworld.
Prerequisites¶
Before starting, ensure you have:
- Git and Node.js 20.x or newer installed
- A running Paladin network to deploy and interact with smart contracts
Running the Example¶
Follow the Getting Started instructions to set up a Paladin environment.
⚠️ To ensure you are using a stable version, always clone the most recent release tag instead of
main.
Clone the repository at the latest release:
REPO=https://github.com/LFDT-Paladin/paladin.git
TAG=$(git ls-remote --tags $REPO | cut -d/ -f3 | grep -v '\-rc' | sort -V | tail -n1)
git clone $REPO -b $TAG
Once cloned, navigate to the example:
The HelloWorld solidity contract is located at: solidity/contracts/tutorials/HelloWorld.sol
Follow the example README to run the code.
Overview¶
This tutorial demonstrates how traditional, non-private Ethereum smart contract interactions can be achieved via a Paladin node and its transaction manager. Later tutorials will demonstrate how to make use of more advanced features of the Paladin APIs.
We have a HelloWorld smart contract, which:
- Emits a "welcome" message as an event when its
sayHellofunction is called.
Key Artifacts¶
To deploy and interact with the contract, we use:
- ABI: Describes the contract's interface, including its functions and events.
- Bytecode: The compiled contract code.
These are pre-compiled and provided in the helloWorldJson object.
Step-by-Step Walkthrough¶
Step 1: Deploy the Contract¶
const deploymentTxID = await paladin.ptx.sendTransaction({
type: TransactionType.PUBLIC,
abi: helloWorldJson.abi,
bytecode: helloWorldJson.bytecode,
from: owner.lookup,
data: {},
});
Key Differences (vs. calling a contract function)¶
- Deployment requires
bytecode, as it is creating a new contract on the blockchain. - No
toaddress is specified, since a contract does not yet exist at this stage. - No specific function is called, since this is an initial deployment.
What happens:¶
- The
sendTransactionmethod sends a deployment transaction to the blockchain via Paladin. - The function returns a
deploymentTxIDthat uniquely identifies the transaction.
Step 2: Confirm the Deployment¶
const deploymentReceipt = await paladin.pollForReceipt(
deploymentTxID,
10000,
true
);
if (!deploymentReceipt?.contractAddress) {
logger.error("Deployment failed!");
return false;
}
logger.log(
"Contract deployed successfully at address:",
deploymentReceipt.contractAddress
);
What happens:¶
- We use
pollForReceiptto wait for the deployment transaction to be confirmed. - If successful, the receipt includes the new
contractAddress, which we will use in the next step.
Step 3: Call the sayHello Function¶
const name = "Paladin User"; // Example name for the greeting
const sayHelloTxID = await paladin.ptx.sendTransaction({
type: TransactionType.PUBLIC,
abi: helloWorldJson.abi,
function: "sayHello",
from: owner.lookup,
to: deploymentReceipt.contractAddress,
data: { name: name },
});
Key Differences (vs. contract deployment)¶
- Function calls require a
toaddress, since the contract already exists. - No
bytecodeis needed, as we are invoking an existing contract, not creating one. - A specific function (
sayHello) is provided, along with its arguments indata.
What happens:¶
- The
sendTransactionmethod sends a transaction to call thesayHellofunction of the deployed contract. - The
dataobject includes the function arguments—in this case, thenameof the person being greeted.
Step 4: Confirm the Function Call¶
const functionReceipt = await paladin.pollForReceipt(sayHelloTxID, 10000, true);
if (!functionReceipt?.transactionHash) {
logger.error("Receipt retrieval failed!");
return false;
}
logger.log("sayHello function executed successfully!");
What happens:¶
- Similar to the deployment step, we wait for confirmation of the
sayHellofunction call usingpollForReceipt.
Step 5: Retrieve the Emitted Event¶
const events = await paladin.bidx.decodeTransactionEvents(
functionReceipt.transactionHash,
helloWorldJson.abi,
"pretty=true"
);
logger.log(events[0].data["message"]);
What happens:¶
- We use
decodeTransactionEventsto extract event data from thesayHellotransaction.
Key Concepts Demonstrated¶
This tutorial introduces fundamental Paladin concepts:
- Contract Deployment - Creating new smart contracts on the Paladin network
- Function Invocation - Calling existing contract functions
- Transaction Management - Using Paladin's transaction APIs
- Event Handling - Retrieving and processing contract events
- Receipt Processing - Confirming transaction success and extracting results
Conclusion¶
Congratulations! You've successfully:
- Deployed the
HelloWorldcontract - Called its
sayHellofunction - Retrieved and validated the emitted event
This simple example demonstrates how to interact with smart contracts using the Paladin SDK.
Next Steps¶
Now that you've deployed and interacted with the HelloWorld contract, you're ready to explore more complex interactions with smart contracts. In the next tutorial, we will introduce you to a Storage contract where you will write and read from the blockchain!