77 lines
1.7 KiB
JavaScript
Executable File
77 lines
1.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
import { promises as fs } from 'fs'
|
|
import path from 'path'
|
|
|
|
class SmartContract {
|
|
constructor() {
|
|
this.stateFilePath = path.resolve('./contract/state.json')
|
|
this.state = { hue: 0 }
|
|
this.loadState()
|
|
}
|
|
|
|
async loadState() {
|
|
try {
|
|
const data = await fs.readFile(this.stateFilePath, 'utf-8')
|
|
this.state = JSON.parse(data)
|
|
} catch (error) {
|
|
console.error('Error loading state:', error)
|
|
}
|
|
}
|
|
|
|
async saveState() {
|
|
try {
|
|
await fs.writeFile(this.stateFilePath, JSON.stringify(this.state, null, 2))
|
|
} catch (error) {
|
|
console.error('Error saving state:', error)
|
|
}
|
|
}
|
|
|
|
async setHue(call) {
|
|
if (call.msatoshi < 1000000) {
|
|
throw new Error('pay at least 1000 sats!')
|
|
}
|
|
|
|
if (typeof call.payload.hue !== 'number') {
|
|
throw new Error('hue is not a number!')
|
|
}
|
|
|
|
if (call.payload.hue < 0 || call.payload.hue > 360) {
|
|
throw new Error('hue is out of the 0~360 range!')
|
|
}
|
|
|
|
this.state.hue = call.payload.hue
|
|
await this.saveState()
|
|
|
|
this.send('86fa35fef8ab4f5906cedfcd966a69e0126973097fd4ea270ddefc505a1a824e', call.msatoshi)
|
|
}
|
|
|
|
send(address, msatoshi) {
|
|
console.log(`Sending ${msatoshi} msatoshi to ${address}`)
|
|
// Here you would implement the actual logic to transfer the msatoshi
|
|
}
|
|
}
|
|
|
|
export default SmartContract
|
|
|
|
let hue = process.argv[2] || 99
|
|
hue = parseInt(hue)
|
|
|
|
const contract = new SmartContract()
|
|
|
|
const call = {
|
|
msatoshi: 1500000, // Example value
|
|
payload: {
|
|
hue: hue // Example value within the valid range
|
|
}
|
|
};
|
|
|
|
(async () => {
|
|
try {
|
|
await contract.setHue(call)
|
|
console.log('Hue set successfully:', contract.state.hue)
|
|
} catch (error) {
|
|
console.error(error.message)
|
|
}
|
|
})()
|