-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from v3xlabs/backend
Backend
- Loading branch information
Showing
13 changed files
with
2,211 additions
and
244 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# ENS Auto Renewal | ||
# Intent-Based ENS Solver Network For ENS Auto-Renewals | ||
|
||
## Frontend | ||
|
||
|
@@ -19,38 +19,33 @@ Current flow: | |
|
||
## API Endpoints | ||
|
||
### POST /verify | ||
### Add new intent | ||
|
||
Verifies signature and token balance plus approvals for name renewal request. If both are correct, it adds the transaction to the solvers network mempool. | ||
curl -X POST http://localhost:3000/api/verify-intent \ | ||
-H "Content-Type: application/json" \ | ||
-d '{ | ||
"names": ["test1", "test2"], | ||
"value": "123456789", | ||
"signature": "0x4c1ffe17790d5773ba5c357893adc5a94e44cd8fd437363bc639597e6c054eef6f591b3bd95dbbf822b663b75817fcb5b68ecbcb4c05daf68d6aa16c2224d3db1b" | ||
}' | ||
|
||
Request body: | ||
### Get intents for address | ||
|
||
```json | ||
{ | ||
"message": { | ||
"names": ["vitalik.eth", "name.eth"], | ||
"value": "1000000000000000000" | ||
}, | ||
"signature": "0x...", | ||
"signer": "0x..." | ||
} | ||
``` | ||
curl http://localhost:3000/api/intents/0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 | ||
|
||
Response: | ||
## Get all expiry dates | ||
|
||
```json | ||
{ | ||
"ok": true | ||
} | ||
``` | ||
curl http://localhost:3000/api/expiry-dates | ||
|
||
```` | ||
## Setup | ||
1. Install dependencies: | ||
```bash | ||
npm install [email protected] express dotenv | ||
``` | ||
```` | ||
|
||
2. Dotenv onfiguration: | ||
|
||
|
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
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
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,27 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
|
||
contract Token is ERC20, Ownable { | ||
uint256 public constant INITIAL_SUPPLY = 1000000 * 10**18; | ||
uint256 public constant MAX_SUPPLY = 10000000 * 10**18; | ||
|
||
constructor() ERC20("ActionToken", "ACT") Ownable(msg.sender) { | ||
_mint(msg.sender, INITIAL_SUPPLY); | ||
} | ||
|
||
function mint(address to, uint256 amount) public onlyOwner { | ||
require(totalSupply() + amount <= MAX_SUPPLY, "Exceeds maximum supply"); | ||
_mint(to, amount); | ||
} | ||
|
||
function burn(uint256 amount) public { | ||
_burn(msg.sender, amount); | ||
} | ||
|
||
function decimals() public pure override returns (uint8) { | ||
return 18; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
.env | ||
intents.db |
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,2 @@ | ||
{"intentId":1,"signer":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266","timestamp":"2024-11-16T14:43:30.697Z","expiryDates":[{"name":"test1","expiry":1823413368},{"name":"test2","expiry":1827410916}]} | ||
{"intentId":2,"signer":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266","timestamp":"2024-11-16T14:48:57.130Z","expiryDates":[{"name":"test1","expiry":1823413368},{"name":"test2","expiry":1827410916}]} |
Binary file not shown.
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,76 @@ | ||
const express = require("express"); | ||
const ethers = require("ethers"); | ||
require("dotenv").config(); | ||
|
||
const ERC20_ABI = ["function balanceOf(address) view returns (uint256)"]; | ||
|
||
const app = express(); | ||
app.use(express.json()); | ||
|
||
function checkSig(data) { | ||
const hash = ethers.utils.keccak256( | ||
ethers.utils.defaultAbiCoder.encode( | ||
["string", "string[]", "uint256"], | ||
["RENEW_NAME", data.msg.names, data.msg.value] | ||
) | ||
); | ||
const addr = ethers.utils.verifyMessage( | ||
ethers.utils.arrayify(hash), | ||
data.sig | ||
); | ||
return addr.toLowerCase() === data.signer.toLowerCase(); | ||
} | ||
|
||
async function checkTokens(addr, value) { | ||
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL); | ||
const token = new ethers.Contract( | ||
process.env.TOKEN_ADDRESS, | ||
ERC20_ABI, | ||
provider | ||
); | ||
const bal = await token.balanceOf(addr); | ||
return bal.gte(value); | ||
} | ||
|
||
function validateInput(data) { | ||
if (!data.msg?.names?.length) return "no names"; | ||
if (!data.msg?.value) return "no value"; | ||
if (!data.sig || !data.signer) return "missing sig or signer"; | ||
try { | ||
ethers.BigNumber.from(data.msg.value); | ||
} catch { | ||
return "bad value format"; | ||
} | ||
if (!ethers.utils.isAddress(data.signer)) return "invalid address"; | ||
return null; | ||
} | ||
|
||
/** | ||
* ! mocked for now - just checks if sig structure is correct and if signer has enough funds | ||
* todo - add check for user having allowance set for solver smart contract | ||
* add mempool | ||
* submitting action request to solver API | ||
* todo - add more actions | ||
* test with sepolia ens | ||
* oracles for other tokens | ||
*/ | ||
app.post("/submit", async (req, res) => { | ||
try { | ||
const err = validateInput(req.body); | ||
if (err) return res.status(400).json({ error: err }); | ||
|
||
if (!checkSig(req.body)) | ||
return res.status(400).json({ error: "bad signature" }); | ||
|
||
const hasTokens = await checkTokens(req.body.signer, req.body.msg.value); | ||
if (!hasTokens) return res.status(400).json({ error: "not enough tokens" }); | ||
|
||
res.json({ ok: true }); | ||
} catch (e) { | ||
console.error(e); | ||
res.status(500).json({ error: "server error" }); | ||
} | ||
}); | ||
|
||
const port = process.env.PORT || 3000; | ||
app.listen(port, () => console.log(`Running on ${port}`)); |
Oops, something went wrong.