Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add data feed contract #48

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions data-feed-oracle/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# EditorConfig is awesome: https://EditorConfig.org
root = true

[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 2
insert_final_newline = true
trim_trailing_whitespace = false

[*.ral]
indent_size = 4
2 changes: 2 additions & 0 deletions data-feed-oracle/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
**/dist/
**/templates/
16 changes: 16 additions & 0 deletions data-feed-oracle/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"extends": [
"prettier",
"plugin:prettier/recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"header/header": ["off"]
},
"parserOptions": {
"project": "tsconfig.json",
"ecmaVersion": 2020,
"sourceType": "module"
},
"parser": "@typescript-eslint/parser"
}
1 change: 1 addition & 0 deletions data-feed-oracle/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.ral linguist-language=Rust
9 changes: 9 additions & 0 deletions data-feed-oracle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
node_modules/
coverage/
lerna-debug.log
/dist
/dev
.deployments.devnet.json
.ralph-lsp/
artifacts/
package-lock.json
25 changes: 25 additions & 0 deletions data-feed-oracle/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
node_modules/

# Dotfiles
.vscode
.DS_Store

# Files generated by devnet
/dev/*
!dev/user.conf

# Files generated by executing smart contracts
/artifacts/

# Config files
license-header.js
/configs/

# Tests
coverage/

# GitHub Actions
.github/

src/**/*.test.ts
**/fixtures/
51 changes: 51 additions & 0 deletions data-feed-oracle/.project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{
"fullNodeVersion": "v3.8.5",
"compilerOptionsUsed": {
"ignoreUnusedConstantsWarnings": false,
"ignoreUnusedVariablesWarnings": false,
"ignoreUnusedFieldsWarnings": false,
"ignoreUnusedPrivateFunctionsWarnings": false,
"ignoreUpdateFieldsCheckWarnings": false,
"ignoreCheckExternalCallerWarnings": false,
"ignoreUnusedFunctionReturnWarnings": false,
"skipAbstractContractCheck": false
},
"infos": {
"AddOracle": {
"sourceFile": "scripts.ral",
"sourceCodeHash": "38d7d6b6b3ebfd5fbb85dcfd13997014d597f200a2a3b881f5374aed2c071ef0",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"CompleteRequest": {
"sourceFile": "scripts.ral",
"sourceCodeHash": "38d7d6b6b3ebfd5fbb85dcfd13997014d597f200a2a3b881f5374aed2c071ef0",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"MakeRequest": {
"sourceFile": "scripts.ral",
"sourceCodeHash": "38d7d6b6b3ebfd5fbb85dcfd13997014d597f200a2a3b881f5374aed2c071ef0",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"RemoveOracle": {
"sourceFile": "scripts.ral",
"sourceCodeHash": "38d7d6b6b3ebfd5fbb85dcfd13997014d597f200a2a3b881f5374aed2c071ef0",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"Request": {
"sourceFile": "datafeed.ral",
"sourceCodeHash": "79a138c32a05f6a6b65b3ed129bdc52be68955cbfa3302ec90e9d1bdf945689f",
"bytecodeDebugPatch": "",
"codeHashDebug": ""
},
"WeatherDataFeed": {
"sourceFile": "datafeed.ral",
"sourceCodeHash": "79a138c32a05f6a6b65b3ed129bdc52be68955cbfa3302ec90e9d1bdf945689f",
"bytecodeDebugPatch": "=6-2=1-3+8=2-1+8=3-2+b0=2-2+19=2-2+c64200=11-1+6=60+7a7e0214696e73657274206174206d617020706174683a2000=33-1+5=60+7a7e021472656d6f7665206174206d617020706174683a2000=99-1+a=88+7a7e0214696e73657274206174206d617020706174683a2000=520",
"codeHashDebug": "38a3377c87319f21b2ac259d6cadb6cfb2388389a169394c5417d2741f993910"
}
}
}
25 changes: 25 additions & 0 deletions data-feed-oracle/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# My dApp Template

## Install

```
npm install
```

## Start a local devnet for testing and development

Please refer to the documentation here: https://wiki.alephium.org/full-node/devnet

## Compile

Compile the TypeScript files into JavaScript:

```
npm run compile
```

## Testing

```
npm run test
```
50 changes: 50 additions & 0 deletions data-feed-oracle/alephium.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Configuration } from '@alephium/cli'
import { Number256 } from '@alephium/web3'

// Settings are usually for configuring
export type Settings = {
issueTokenAmount: Number256
openaiAPIKey?: string
ipfs?: {
infura?: {
projectId: string,
projectSecret: string
}
}
}

const defaultSettings: Settings = {
issueTokenAmount: 100n,
openaiAPIKey: process.env.OPENAI_API_KEY || '',
ipfs: {
infura: {
projectId: process.env.IPFS_INFURA_PROJECT_ID || '',
projectSecret: process.env.IPFS_INFURA_PROJECT_SECRET || ''
}
}
}

const configuration: Configuration<Settings> = {
networks: {
devnet: {
nodeUrl: 'http://127.0.0.1:22973',
// here we could configure which address groups to deploy the contract
privateKeys: ['a642942e67258589cd2b1822c631506632db5a12aabcf413604e785300d762a5'],
settings: defaultSettings
},

testnet: {
nodeUrl: process.env.NODE_URL as string,
privateKeys: process.env.PRIVATE_KEYS === undefined ? [] : process.env.PRIVATE_KEYS.split(','),
settings: defaultSettings
},

mainnet: {
nodeUrl: process.env.NODE_URL as string,
privateKeys: process.env.PRIVATE_KEYS === undefined ? [] : process.env.PRIVATE_KEYS.split(','),
settings: defaultSettings
}
}
}

export default configuration
107 changes: 107 additions & 0 deletions data-feed-oracle/contracts/datafeed.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
struct Request {
mut lat: U256,
mut lon: U256,
mut status: Bool,
mut temp: ByteVec
}

Contract WeatherDataFeed(
owner: Address,
mut authorizedOraclesCount: U256,
mut lastTimestamp: U256,
mut lastRequestId: ByteVec,
fee: U256,
feeWallet: Address
) {
mapping[Address, Bool] authorizedOracles
mapping[ByteVec, Request] requests

event NewRequest(requestId: ByteVec, lat: U256, lon: U256)
event RequestCompleted(requestId: ByteVec, temp: ByteVec)

enum ErrorCodes {
UnauthorizedOracle = 0
InvalidCaller = 1
InvalidTimestamp = 2
}

@using(preapprovedAssets = true, updateFields = true)
pub fn addOracle(newOracle: Address) -> () {
// Ensure only the owner can add oracles
checkCaller!(callerAddress!() == owner, ErrorCodes.InvalidCaller)

authorizedOracles.insert!(callerAddress!(), newOracle, true)
authorizedOraclesCount = authorizedOraclesCount + 1
}

@using(updateFields = true)
pub fn removeOracle(oracle: Address) -> () {
// Ensure only the owner can remove oracles
checkCaller!(callerAddress!() == owner, ErrorCodes.InvalidCaller)

authorizedOracles.remove!(callerAddress!(), oracle)
authorizedOraclesCount = authorizedOraclesCount - 1
}

pub fn checkOracle(oracle: Address) -> Bool {
return authorizedOracles[oracle]
}

@using(preapprovedAssets = true, checkExternalCaller = false)
pub fn makeRequest(lat: U256, lon: U256) -> ByteVec {
let caller = callerAddress!()

// Create a unique request ID
let requestId = blake2b!(toByteVec!(lat) ++ toByteVec!(lon) ++ toByteVec!(blockTimeStamp!()) ++ toByteVec!(caller))

// Initialize the request
requests.insert!(caller, requestId, Request {
lat: lat,
lon: lon,
status: false,
temp: zeros!(5)
})

// Pay fee to the oracle
transferToken!(caller, feeWallet, ALPH, fee)

// Emit the new request event
emit NewRequest(requestId, lat, lon)

return requestId
}

@using(updateFields = true)
pub fn completeRequest(requestId: ByteVec, temp: ByteVec, publicKey: ByteVec, signature: ByteVec, timestamp: U256) -> () {
let caller = callerAddress!()

// Ensure only authorized oracles can complete requests
checkCaller!(authorizedOracles[caller], ErrorCodes.UnauthorizedOracle)

// Ensure the timestamp is valid
assert!(lastTimestamp <= timestamp, ErrorCodes.InvalidTimestamp)

// Verify signature
verifySecP256K1!(requestId, publicKey, signature)

// Update the request
let existingRequest = requests[requestId]
requests[requestId] = Request {
lat: existingRequest.lat,
lon: existingRequest.lon,
status: true,
temp: temp
}

// Update state variables
lastTimestamp = blockTimeStamp!()
lastRequestId = requestId

// Emit the completion event
emit RequestCompleted(requestId, temp)
}

pub fn getRequest(requestId: ByteVec) -> Request {
return requests[requestId]
}
}
31 changes: 31 additions & 0 deletions data-feed-oracle/contracts/scripts.ral
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// invoke contract:
TxScript AddOracle(dataFeed: WeatherDataFeed, oracle: Address) {
dataFeed.addOracle{callerAddress!() -> ALPH : minimalContractDeposit!() * 2}(oracle)
}

TxScript RemoveOracle(
dataFeed: WeatherDataFeed,
oracle: Address
){
dataFeed.removeOracle(oracle)
}

TxScript MakeRequest(
dataFeed: WeatherDataFeed,
lat: U256,
lon: U256,
fee: U256
){
let _ = dataFeed.makeRequest{callerAddress!() -> ALPH : fee + minimalContractDeposit!() * 2}(lat, lon)
}

TxScript CompleteRequest(
dataFeed: WeatherDataFeed,
requestId: ByteVec,
temp: ByteVec,
publicKey: ByteVec,
signature: ByteVec,
timestamp: U256
){
dataFeed.completeRequest(requestId, temp, publicKey, signature, timestamp)
}
7 changes: 7 additions & 0 deletions data-feed-oracle/jest-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"testPathIgnorePatterns": [".*/node_modules/", ".*/dist/.*"],
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
},
"testMatch": ["<rootDir>/**/*.test.ts"]
}
Loading