-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartr.js
executable file
·76 lines (61 loc) · 1.98 KB
/
smartr.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
const fs = require('fs')
const solc = require('solc')
const git = require('simple-git')()
const contractPath = './data/Example.sol'
const statePath = './data/state.json'
const ledgerPath = './data/ledger.json'
// Compile the Solidity contract
const compileContract = () => {
const input = fs.readFileSync(contractPath, 'utf8')
const output = JSON.parse(solc.compile(JSON.stringify({
language: 'Solidity',
sources: {
'./data/Example.sol': {
content: input
}
},
settings: {
outputSelection: {
'*': {
'*': ['*']
}
}
}
})))
return output.contracts['./data/Example.sol'].Example
}
// Load the state and ledger
const loadStateAndLedger = () => {
const state = JSON.parse(fs.readFileSync(statePath, 'utf8'))
const ledger = JSON.parse(fs.readFileSync(ledgerPath, 'utf8'))
return { state, ledger }
}
// Save the state and ledger
const saveStateAndLedger = (state, ledger) => {
fs.writeFileSync(statePath, JSON.stringify(state, null, 2))
fs.writeFileSync(ledgerPath, JSON.stringify(ledger, null, 2))
}
// Execute a function from the smart contract
const executeFunction = (functionName, ...args) => {
const { abi, evm } = compileContract()
const { state, ledger } = loadStateAndLedger()
// Find the function to execute
const func = abi.find((entry) => entry.name === functionName)
if (!func) {
throw new Error(`Function "${functionName}" not found in the smart contract`)
}
// Execute the function
const { newValue } = args
state.value = newValue
// Update the ledger with the transaction
const transaction = { functionName, args, timestamp: new Date().toISOString() }
ledger.push(transaction)
// Save the updated state and ledger
saveStateAndLedger(state, ledger)
// Commit the changes to the Git repository
// git.add([statePath, ledgerPath]).commit('Update state and ledger')
}
// Example usage
const params = process.argv[2] || 21
executeFunction('setValue', params)