If you like video tutorials, here is one of our latest workshops on building an Agent to transact onchain using the Viem SDK with the AI Agent Contract Template. In this tutorial, you will learn:
How to test the WapoJS functions like deriveSecret(salt)to derive an ECDSA key based on your secret added to your Agent Contract's secret vault.
Build and deploy your Agent Contract to IPFS then add your secret salt that will derive an ECDSA key for your deployed agent.
Sign and verify a message using the viem SDK with your agent's derived ECDSA key.
Debug your Agent Contract due to a failed transaction on Base Sepolia.
Resolve the error and execute a successful transaction on Base Sepolia.
Getting Started
Prepare
Clone git repo or use degit to get the source code.
Create .env file with the default ThirdWeb API key for publishing your Agent Contract to IPFS
cp.env.example.env
Build your Agent
npmrunbuild
Test your Agent locally
npmruntest
Expected Test Results
Nowyouarereadytopublishyouragent,addsecrets,andinteractwithyouragentinthefollowingsteps:-Execute:'npm run publish-agent'-Setsecrets:'npm run set-secrets'- Go to the url produced by setting the secrets (e.g. https://wapo-testnet.phala.network/ipfs/QmPQJD5zv3cYDRM25uGAVjLvXGNyQf9Vonz7rqkQB52Jae?key=b092532592cbd0cf)
✓tests/index.test.ts (5) 7035ms✓TestViemSDKAIAgentContract (5) 7035ms✓DeriveAccount✓SignMessageData✓VerifySignature✓SendTXonBaseSepolia6991ms✓POST/TestFiles1passed (1)Tests5passed (5)Startat16:40:10Duration7.39s (transform 34ms,setup6ms,collect181ms,tests7.04s,environment0ms,prepare42ms)
Publishing Your Agent
Upload your compiled AI Agent code to IPFS.
npmrunpublish-agent
Upon a successful upload, the command should show the URL to access your AI Agent.
-UploadingfiletoIPFS.Thismaytakeawhiledependingonfilesizes.✔SuccessfullyuploadedfiletoIPFS.✔FilesstoredatthefollowingIPFSURI:ipfs://QmVJ3xknfRevUkc68iZc4RdPSLL2gLD8WagwMQCdGMyC4g✔ Open this link to view your upload: https://b805a9b72767504353244e0422c2b5f9.ipfscdn.io/ipfs/bafybeidhk5nzutxyx3xusgjl4v6nkvscdoiowzofc7hqnf3l4xipieshie/
AgentContractdeployedat:https://wapo-testnet.phala.network/ipfs/QmVJ3xknfRevUkc68iZc4RdPSLL2gLD8WagwMQCdGMyC4gIfyouragentrequiressecrets,ensuretodothefollowing:1) Edit the ./secrets/default.json file or create a new JSON file in the ./secrets folder and add your secrets to it.2) Run command: 'npm run set-secrets' or 'npm run set-secrets [path-to-json-file]'Deploymentinformationupdatedin./logs/latestDeployment.json
Note that your latest deployment information will be logged to in file ./logs/latestDeployment.json. This file is updated every time you publish a new Agent Contract to IPFS. This file is also used to get the IPFS CID of your Agent Contract when setting secrets for your Agent Contract.
If ThirdWeb fails to publish, please signup for your own ThirdWeb account to publish your Agent Contract to IPFS. Signup or login at https://thirdweb.com/dashboard/
Whenever you log into ThirdWeb, create a new API key and replace the default API Key with yours in the .env file.
THIRDWEB_API_KEY="YOUR_THIRDWEB_API_KEY"
Accessing The Published Agent
Once published, your AI Agent is available at the URL: https://wapo-testnet.phala.network/ipfs/<your-cid>. You can get it from the "Publish to IPFS" step.
By default, all the compiled JS code is visible for anyone to view if they look at IPFS CID. This makes private info like API keys, signer keys, etc. vulnerable to be stolen. To protect devs from leaking keys, we have added a field called secret in the Request object. It allows you to store secrets in a vault for your AI Agent to access.
To add your secrets,
Edit the default.json file or create a new JSON file in the ./secrets folder and add your secrets to it.
{"secretSalt":"SALTY_BAE"}
Run command to set the secrets
npmrunset-secrets# or if you have a custom JSON filenpmrunset-secrets<path-to-json-file>
Note that all your secrets will be logged in file ./logs/secrets.log. This file is updated every time you add new secrets to your Agent Contract. If you have not published an Agent Contract, yet, this command will fail since there is not a CID to map the secrets to.
The API returns a token and a key. The key is the id of your secret. It can be used to specify which secret you are going to pass to your frame. The token can be used by the developer to access the raw secret. You should never leak the token.
To verify the secret, run the following command where key and token are replaced with the values from adding your secret to the vault.
Check the Hono docs for information on accessing URL queries or body from a post request.
We recommend using @hono/tiny to avoid a large bundle size and the 20MB final artifact limitation.
Example
// Access query value for a URL query named `type`let queries =c.req.queries() || {}constgetType= (queries.type) ?queries.type[0] asstring:''// Access body from post requestconstdata=awaitc.req.json()
Debugging Your Agent
To debug your agent, you can use the following command:
For more information check the MDN docs on console object.
Run a Local Testnet With Docker
Run a local testnet with docker support. All you need to do to get a local testnet started is run:
Running the local testnet may return an error if port 8000 is already in use.
npmrundev
Make a Request to Your Local Build
# GET requestcurlhttp://127.0.0.1:8000/local# GET request with URL queriescurlhttp://127.0.0.1:8000/local?query1=one&query2=two# POST requestcurlhttp://127.0.0.1:8000/local-XPOST-H'content-type: application/json'-d'{"foo": "bar"}'
Congratulations! You have deployed and transacted on Base Sepolia! You now have the tools to connect to any top LLM API with the RedPill Agent Contract template and perform onchain actions with the viem SDK.