Running a Stable Diffusion Job from a smart contract with Lilypad v0
Open this contract in Remix -> Click here
This example uses the Stable Diffusion Docker image found on the Bacalhau Docs.
For more info on how to create the Stable Diffusion Script and Docker Image see this tutorial or watch the video below.
This example can be found in the Examples folder in the Lilypad Project Github.
To run this example, you can simply deploy it to any supported network and pass in the contact address to the constructor which corresponds to your network. See Deployed Network Details.
Open this example in remix -> click here
In this example the Lilypad Stable Diffusion Job Results are returned as an IPFS v0 CID. You can use Brave browser or an IPFS gateway to see the results.
For example for a given return CID, type the following into your browser url
In Brave: ipfs://[CID]
In Other Browsers: www.w3s.link/ipfs/[CID]
or www.ipfs.io/ipfs/[CID]
A folder of the Outputs will be shown:
You can also access the image directly by navigating to
In Brave: ipfs://[CID]/outputs/image0.png
In Other Browsers: www.w3s.link/ipfs/[CID]
or www.ipfs.io/ipfs/[CID]/outputs/image0.png
Stable Diffusion
Drand Off-chain Voting
Ethereum Address Fraud Detector
Running a fraud detection job with Lilypad
Open this contract in Remix -> Click here
This example uses the Fraud Detection Docker image found on Docker Hub HackFS.
Run the Image with Bacalhau using the command bacalhau docker --network=http --domain=api.etherscan.com run hakymulla/hackfs:inference -- python predict.py --address "0x427aE6048C7d2DEd45a07Ea46F2873d0F9ddDb35"
Run Bacalhau get [Job ID] to the format of the spec needed to call runLilypadJob
.
For more info on the dataset, how to create the Fraud Detection Script and Docker Image see My HackFS Github.
This example can be found in the Examples folder in the Lilypad-v0 Project Github.
To run this example, you can simply deploy it to any supported network and pass in the contact address to the constructor which corresponds to your network. See deployed-network-details.md
.
Open this example in remix -> click here
To Deploy on Filecoin Calibration Net, use the LilypadEvents Contract Address deployed on the testnet (0xdC7612fa94F098F1d7BB40E0f4F4db8fF0bC8820).
Call the IsFraudDetector function and add an Ethereum Address as input.
IsFraudDetector is a payable function that calls the the bridge runLilypadJob.
In this example the Lilypad FraudDetection Job Results is returned as a StdOut. You can use the allResult function to see the results.
Let's build on the above example by Creating and Interacting with Tableland table using smart contracts.
Open this example in remix -> click here
During contract deployment, a tableland table is created. The bridge while calling the lilypadFulfilled also saves the results of inference to the table which can be used to monitor the model perfomance.
Storing a reference to the table ID and prefix within your contract is advisable since it enhances ease of reference in different methods, facilitating smoother execution.
The TablelandDeployments contract helps set up the TablelandTables interface by returning ITablelandTables instantiated with the correct registry address, inferred by the chain being used. This is added to the constructor. The table columns includes an id, address and the result.
A insert function is created to write to the above table
notice the onlyBridge modifier, only the bridge can write to the table and happens during the call back.
The above insert function is called in the lilypadFulfilled function
Query the table on Click here.
Implementing an unbiased off-chain voting process using Lilypad and Bacalhau, with Drand Time-lock encryption
This example illustrates how leverages Lilypad, Bacalhau, and Drand to implement an unbiased off-chain voting mechanism. The purpose of using these technologies is to ensure that voting happens in an unbiased, decentralized manner, thus promoting true democracy within the DefiKicks ecosystem.
The contracts and docker images have also been added as an example in
For more info on DeFi Kicks and their use of Lilypad, see the
The DeFi Kicks project utilizes a three-step mechanism to ensure an unbiased voting system. Here's a summarized outline of how it operates:
The voting process is performed off-chain to maintain efficiency and privacy. Users cast their votes by encrypting them on the front end, signing the message for authenticity, and then sending them to be stored. Time-lock encryption is utilized, making the vote only decryptable after a predetermined amount of time, which ensures fairness and protects against any attempts to tamper with the votes.
Once the voting phase for a proposal has ended, anyone can request a vote resolution. This action requires a certain fee and can only be performed if the vote is in the ResolutionToRequest state. This fee covers the usage of Lilypad and Bacalhau for off-chain vote resolution.
Once a vote resolution request has been submitted and the necessary fee paid, Bacalhau, via Lilypad, takes care of the off-chain resolution. It processes the vote data, decrypting the votes when the predetermined time has elapsed, and generates the final voting results. This off-chain resolution ensures a high level of security and accuracy, providing an unbiased outcome for the voting process.
In summary, this mechanism leverages the strengths of off-chain voting and resolution in a decentralized environment, guaranteeing privacy, fairness, and tamper resistance in the voting process, which are all critical to maintaining the trust and integrity of the DeFi Kicks project.
Vote details are packaged into a message object and signed cryptographically. This message is then encrypted using a time-lock mechanism, which ensures decryption only after a certain time. The encrypted vote, along with the user's account details and signature, are stored as a vote object. Lastly, the new vote is stored and the system is notified.
The Lilypad job is defined as docker image that runs a javascript script that fetches all the votes and resolves the voting process. We will go into the details of the script in a later section.
The Lilypad job spec is dinamically built to send the proposal ID as environment variable together with a node url used to fetch the vote power of each voter. The node url is also used to fetch the proposal details from the chain.
The script run in Bacalhau perform the following steps:
Retrieve all the off-chain votes from a decentralised storage (Ceramic stream or IPFS)
Decrypt the votes using Drand timelock decryption function
Verify the signature of the decrypted vote
Compute the vote power of each voter
Compute the vote result using the vote power of each voter
Compute the inflationary rewards for each voter that voted for the winning option
Put the reward information in a Merkle tree and compute the Merkle root together with the proof for each voter
Encode all the information in a hex string and console.log it as this is the stdout that will be sent back to the Governor smart contract by the Bacalhau operators that run the job.
Once the job is picked up by the Bacalhau operators and executed, the resolutions are brought on-chain and the Governor smart contract receives a lilypadFulfilled
callback from Lilypad.
The _result
string contains all the vote resolution information encoded in a hex string. The string is decoded and the resolution information is stored in the smart contract.
The following code snippet shows how the off-chain voting mechanism is implemented in the frontend of the DefiKicks application. The original code can be found in the
Once the off-chain voting phase has ended anyone can request it's resolution by calling the requestVoteResolution
function in the . This function in turn will trigger a Lilypad job that will fetches all the votes and resolve the voting process.
See the for more details.
Here is the vote decryption and verification code, for the full script see the
For more information on how the result is decoded see the