-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
laciferin2024
committed
Dec 10, 2024
1 parent
8b776b7
commit 52d13d6
Showing
14 changed files
with
846 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# Setup Darts Network for development on your machine | ||
|
||
## Setup repo | ||
|
||
```shell | ||
git repo clone darts2024/darts | ||
cd darts | ||
``` | ||
|
||
```bash | ||
make | ||
cp .env.example .env | ||
./darts version | ||
``` | ||
|
||
## Setup env vars for services | ||
|
||
### Generate addresses for services | ||
|
||
```shell | ||
cd hardhat | ||
pnpm gen-env | ||
``` | ||
|
||
1. Open `hardhat/.env.gen` | ||
2. Pick the private keys and paste it appropriately to .env file | ||
|
||
### For `DARTS_SOLVER` | ||
|
||
```shell | ||
source .env | ||
cd hardhat | ||
pnpm account $SOLVER_PRIVATE_KEY | ||
``` | ||
|
||
1. Copy the account address | ||
2. Paste it to DARTS_SOLVER in the `.env` file | ||
|
||
### Setup Metamask | ||
|
||
1. **Install MetaMask**: If you haven't already, install the MetaMask extension for your web browser or the MetaMask | ||
mobile app from the respective app store. | ||
|
||
2. **Open MetaMask**: Launch the MetaMask extension or app on your device. | ||
|
||
3. **Access Settings**: Look for the settings menu in MetaMask. This is usually represented by a gear or three dots | ||
icon. | ||
|
||
4. **Select "Import Account"**: In the settings menu, find the option to import an account. Click on it to proceed. | ||
|
||
5. **Enter Private Key**: You'll be prompted to enter the private key associated with the account you want to import. | ||
Make sure you have the correct private key. | ||
|
||
6. **Complete Import**: Follow the prompts to complete the import process. You may need to confirm your action with a | ||
password or additional verification. | ||
|
||
7. **Verify and Access Imported Account**: Once the import process is complete, you should see the imported account in | ||
your MetaMask wallet along with any existing accounts you have. | ||
|
||
8. **Ensure Security**: After importing your account, it's important to ensure the security of your MetaMask wallet. | ||
Make sure to keep your private key secure and never share it with anyone. | ||
|
||
> Do the same for all the private keys | ||
## Setup Darts Services | ||
|
||
### Setup Solver | ||
|
||
`darts solver` | ||
|
||
Solver runs on $SERVER_PORT. Solver stores $SERVER_URL in the blockchain for `RP` & `JC` to communicate with it | ||
over `http` | ||
|
||
### Setup Resource Provider | ||
|
||
`darts rp` | ||
|
||
RP posts resource offers to `DARTS SOLVER` over the solver url published by solver. | ||
|
||
### Run a darts module on the rp | ||
|
||
`darts run cowsay:v0.1.3 -i Message="Ideomind is full of ideas"` | ||
|
||
`run` command leverages `jobcreator` under the hood. **JC** posts the **job offer** to solver over solver url. | ||
`solver` matches the job offer with the suitable resource offers and creates many deals. The deals are queried by **RP** | ||
and | ||
**JC** where they can accept or reject the deals. Upon acceptance of a deal on both ends, the **RP** runs the job and | ||
submits the results to **solver**. **Solver** then verifies the job results. **JC** queries the verifies the results and | ||
downloads the results from the **Solver**. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
## IPC geth replacement | ||
|
||
requirements: | ||
|
||
```bash | ||
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh | ||
``` | ||
|
||
restart terminal | ||
|
||
```bash | ||
curl -L https://foundry.paradigm.xyz | bash | ||
``` | ||
|
||
restart terminal | ||
|
||
```bash | ||
git clone [email protected]:consensus-shipyard/fendermint.git | ||
git clone [email protected]:consensus-shipyard/ipc-solidity-actors.git | ||
cd fendermint | ||
export CARGO_NET_GIT_FETCH_WITH_CLI=true | ||
sudo apt-get install protobuf-compiler clang | ||
foundryup | ||
make build docker-build | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
## Running jobs from smart contracts | ||
|
||
We have deloyed the [DartsOnChainJobCreator](../hardhat/contracts/DartsOnChainJobCreator.sol) contract which you can | ||
use to trigger running jobs on the Darts network from other smart contracts. | ||
|
||
It works in tandem with the `darts jobcreator` on-chain which will watch the on-chain contract and manage jobs on | ||
behalf of contracts that submit them. | ||
|
||
#### Creating a job | ||
|
||
You will need to know the contract address for the on-chain job creator so we can submit transactions to it. | ||
|
||
The production controller address is `0xCCAaFD2AdD790788436f10e2C84585C46388b9aF` and you can ask it for the address of | ||
the on-chain job creator `getJobCreatorAddress()` | ||
|
||
Running a job involves 2 phases: | ||
|
||
- calling `approve` on the ERC-20 contract to allow the solver to spend your tokens | ||
- trigger the job via the on chain job manager | ||
|
||
Now we know the address of the on-chain job controller - we can ask it for 3 things: | ||
|
||
- the address of the ERC-20 token contract - `getTokenAddress()` | ||
- how much the required deposit it - `getRequiredDeposit()` | ||
- the address of the solver that will handle running the job for us - `getControllerAddress()` | ||
|
||
Knowing these 3 things means we can call the standard ERC-20 `approve` to allow the solver to spend our tokens on our | ||
behalf. | ||
|
||
Now - we can call the `runJob` method of the on chain controller from another contract. This will cause the job-creator | ||
service to kick in and do the following things: | ||
|
||
- check that funds have been approved for the solver | ||
- transfer those funds to it's wallet | ||
- run the job on Darts | ||
- call the `submitResults` method on the on-chain job creator | ||
- the on-chain job creator will call the `submitResults` of the original calling contract | ||
|
||
The following is an example on-chain smart contract: | ||
|
||
```solidity | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.6; | ||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; | ||
import "./dartsJobManager.sol"; | ||
import "./dartsJobClient.sol"; | ||
contract ExampleClient is Ownable, Initializable, IDartsJobClient { | ||
address private jobManagerAddress; | ||
IDartsJobManager private jobManagerContract; | ||
mapping(uint256 => string) private jobResults; | ||
event JobCreated( | ||
uint256 id, | ||
string message | ||
); | ||
event JobCompleted( | ||
uint256 id, | ||
string dealId, | ||
string dataId | ||
); | ||
function initialize(address _jobManagerAddress) public initializer { | ||
setJobManagerAddress(_jobManagerAddress); | ||
} | ||
function setJobManagerAddress(address _jobManagerAddress) public onlyOwner { | ||
require(_jobManagerAddress != address(0), "Job manager address"); | ||
jobManagerAddress = _jobManagerAddress; | ||
jobManagerContract = IDartsJobManager(jobManagerAddress); | ||
} | ||
function getJobResult(uint256 _jobID) public view returns (string memory) { | ||
return jobResults[_jobID]; | ||
} | ||
function runCowsay( | ||
string memory message | ||
) public { | ||
string[] memory inputs = new string[](1); | ||
inputs[0] = string(abi.encodePacked("Message=", message)); | ||
uint256 id = jobManagerContract.runJob( | ||
"cowsay:v0.0.1", | ||
inputs, | ||
msg.sender | ||
); | ||
emit JobCreated( | ||
id, | ||
message | ||
); | ||
} | ||
function submitResults( | ||
uint256 id, | ||
string memory dealId, | ||
string memory dataId | ||
) public override { | ||
jobResults[id] = dataId; | ||
emit JobCompleted( | ||
id, | ||
dealId, | ||
dataId | ||
); | ||
} | ||
} | ||
``` | ||
|
||
Here is an example of a script that brings all of this together: | ||
|
||
```typescript | ||
import bluebird from "bluebird" | ||
import { | ||
connectToken, | ||
connectJobManager, | ||
connectExampleClient, | ||
getWallet, | ||
getAddress, | ||
} from "../utils/web3" | ||
import { ethers } from "hardhat" | ||
|
||
async function main() { | ||
// it's annoying to not be able to use argv but hardhat complains about it | ||
const message = process.env.MESSAGE || "Hello World!" | ||
|
||
const token = await connectToken() | ||
const manager = await connectJobManager() | ||
const client = await connectExampleClient() | ||
|
||
const setRequiredDepositTx = await manager | ||
.connect(getWallet("solver")) | ||
.setRequiredDeposit(ethers.parseEther("2")) | ||
await setRequiredDepositTx.wait() | ||
|
||
const requiredDeposit = await manager.getRequiredDeposit() | ||
|
||
console.log(`requiredDeposit: ${Number(requiredDeposit)}`) | ||
|
||
const paytokensTx = await token | ||
.connect(getWallet("job_creator")) | ||
.approve(getAddress("solver"), requiredDeposit) | ||
await paytokensTx.wait() | ||
|
||
console.log(`tokens approved: ${paytokensTx.hash}`) | ||
|
||
const runjobTx = await client | ||
.connect(getWallet("job_creator")) | ||
.runCowsay(message) | ||
const receipt = await runjobTx.wait() | ||
if (!receipt) throw new Error(`no receipt`) | ||
|
||
console.log(`submitted job: ${runjobTx.hash}`) | ||
|
||
let jobID = 0 | ||
|
||
receipt.logs.forEach((log) => { | ||
const logs = client.interface.parseLog(log as any) | ||
if (!logs) return | ||
jobID = Number(logs.args[0]) | ||
}) | ||
|
||
console.log(`Job ID: ${jobID}`) | ||
console.log(`Waiting for job to be completed...`) | ||
|
||
let result = "" | ||
|
||
while (!result) { | ||
result = await client.getJobResult(jobID) | ||
if (!result) { | ||
await bluebird.delay(1000) | ||
} | ||
} | ||
|
||
console.log(`Job result: ${result}`) | ||
} | ||
|
||
main().catch((error) => { | ||
console.error(error) | ||
process.exitCode = 1 | ||
}) | ||
``` |