Last active
December 16, 2023 07:47
-
-
Save ImaginaryBIT/2f3fb64ce49d1c8aa0e4f4b033e492ac to your computer and use it in GitHub Desktop.
This file contains hidden or 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
############################################################################### | |
################## Solidity Smart Contract source code review ################# | |
############################################################################### | |
convert bytes to integer | |
# python pow(2,256) | |
web3.utils.padLeft('0x3456ff', 64); | |
> "0x000000000000003456ff" | |
web3.utils.toTwosComplement('0x1'); | |
> "0x0000000000000000000000000000000000000000000000000000000000000001" | |
web3.utils.isAddress('0xc1912fee45d61c87cc5ea59dae31190fffff232d'); | |
> true | |
web3.utils.hexToNumberString('0xea'); | |
> "234" | |
web3.utils.hexToAscii('0x4920686176652031303021'); | |
> "I have 100!" | |
web3.utils.asciiToHex('I have 100!'); | |
> "0x4920686176652031303021" | |
web3.utils.toWei('1', 'ether'); | |
> "1000000000000000000" | |
### EVM | |
The Ethereum Virtual Machine (EVM) is a virtual component that is contained in every Ethereum node and is able to execute bytecode for contracts. | |
Smart contracts are usually written in high-level languages like Solidity and are then converted into EVM bytecode | |
Every Ethereum node runs on the EVM to maintain consensus across the blockchain. | |
### ABI file - Application Binary Interface | |
Private functions aren’t visible and can’t be accessed from other smart contracts or addresses. | |
smart contract code | |
``` | |
// SPDX-License-Identifier: MIT | |
pragma solidity >=0.5.0; | |
contract Rent{ | |
event RentPaid( | |
address indexed _from, | |
string indexed _tenant, | |
uint _rent | |
); | |
function deposit(string memory _tenant) public payable{ | |
emit RentPaid(msg.sender, _tenant, msg.value); | |
} | |
} | |
``` | |
ABI file | |
``` | |
[ | |
{ | |
"anonymous": false, | |
"inputs": [ | |
{ | |
"indexed": true, | |
"internalType": "address", | |
"name": "_from", | |
"type": "address" | |
}, | |
{ | |
"indexed": true, | |
"internalType": "string", | |
"name": "_tenant", | |
"type": "string" | |
}, | |
{ | |
"indexed": false, | |
"internalType": "uint256", | |
"name": "_rent", | |
"type": "uint256" | |
} | |
], | |
"name": "RentPaid", | |
"type": "event" | |
}, | |
{ | |
"inputs": [ | |
{ | |
"internalType": "string", | |
"name": "_tenant", | |
"type": "string" | |
} | |
], | |
"name": "deposit", | |
"outputs": [], | |
"stateMutability": "payable", | |
"type": "function" | |
} | |
] | |
``` | |
### Function types: | |
- public: When a function is defined as public it can be called from any other contract, including a smart contract that contains it or address. | |
- external Keyword external means that a function can be called by other contracts or addresses. If we want to call an external function, we can do it using this keyword. | |
- private: Private functions aren’t visible and can’t be accessed from other smart contracts or addresses. | |
- internal Internal functions can be called from the same smart contract or any other inherited from it. | |
- view: this function returns data and does not modify the contract's data | |
- constant: this function returns data and does not modify the contract's data | |
- pure: this function does not modify or even read the contract's data | |
- payable: when someone call this function they might send ether along | |
### Transactions vs Calls | |
writing data is called a transaction whereas reading data is called a call | |
#### Transactions | |
Transactions fundamentally change the state of the network. A transaction can be as simple as sending Ether to another account, or as complicated as executing a contract function or adding a new contract to the network. The defining characteristic of a transaction is that it writes (or changes) data. Transactions cost Ether to run, known as "gas", and transactions take time to process. When you execute a contract's function via a transaction, you cannot receive that function's return value because the transaction isn't processed immediately. In general, functions meant to be executed via a transaction will not return a value; they will return a transaction id instead. So in summary, transactions: | |
* Cost gas (Ether)›› | |
* Change the state of the network | |
* Aren't processed immediately | |
* Won't expose a return value (only a transaction id). | |
› | |
#### Calls | |
Calls, on the other hand, are very different. Calls can be used to execute code on the network, though no data will be permanently changed. Calls are free to run, and their defining characteristic is that they read data. When you execute a contract function via a call you will receive the return value immediately. In summary, calls: | |
* Are free (do not cost gas) | |
* Do not change the state of the network | |
* Are processed immediately | |
* Will expose a return value (hooray!) | |
### connect to a node | |
``` | |
geth attach ipc:/home/enchanter/.gophersland_ethereum_r1/geth.ipc | |
geth attach ws://127.0.0.1:8585 | |
geth attach http://127.0.0.1:8585 | |
``` | |
### transfer eth | |
``` | |
web3.eth.sendTransaction({ | |
from: "0xed9d02e382b34818e88b88a309c7fe71e65f419d", | |
to: "0xfe3b557e8fb62b89f4916b721be55ceb828dbd73", | |
value: web3.toWei(1, "ether") | |
}) | |
``` | |
### geth commands | |
``` | |
eth._requestManager eth.getBlockUncleCount eth.getWork | |
eth.accounts eth.getCode eth.hashrate | |
eth.blockNumber eth.getCoinbase eth.iban | |
eth.call eth.getCompilers eth.icapNamereg | |
eth.coinbase eth.getGasPrice eth.isSyncing | |
eth.compile eth.getHashrate eth.mining | |
eth.constructor eth.getMining eth.namereg | |
eth.contract eth.getPendingTransactions eth.pendingTransactions | |
eth.defaultAccount eth.getProtocolVersion eth.protocolVersion | |
eth.defaultBlock eth.getRawTransaction eth.resend | |
eth.estimateGas eth.getRawTransactionFromBlock eth.sendIBANTransaction | |
eth.filter eth.getStorageAt eth.sendRawTransaction | |
eth.gasPrice eth.getSyncing eth.sendTransaction | |
eth.getAccounts eth.getTransaction eth.sign | |
eth.getBalance eth.getTransactionCount eth.signTransaction | |
eth.getBlock eth.getTransactionFromBlock eth.submitTransaction | |
eth.getBlockNumber eth.getTransactionReceipt eth.submitWork | |
eth.getBlockTransactionCount eth.getUncle eth.syncing | |
personal._requestManager personal.getListWallets personal.newAccount | |
personal.constructor personal.importRawKey personal.sendTransaction | |
personal.deriveAccount personal.listAccounts personal.sign | |
personal.ecRecover personal.listWallets personal.unlockAccount | |
personal.getListAccounts personal.lockAccount | |
admin.addPeer admin.importChain admin.startRPC | |
admin.constructor admin.isPrototypeOf admin.startWS | |
admin.datadir admin.nodeInfo admin.stopRPC | |
admin.exportChain admin.peers admin.stopWS | |
admin.getDatadir admin.propertyIsEnumerable admin.toLocaleString | |
admin.getNodeInfo admin.removePeer admin.toString | |
admin.getPeers admin.sleep admin.valueOf | |
admin.hasOwnProperty admin.sleepBlocks | |
miner.constructor miner.setEtherbase miner.toLocaleString | |
miner.getHashrate miner.setExtra miner.toString | |
miner.hasOwnProperty miner.setGasPrice miner.valueOf | |
miner.isPrototypeOf miner.start | |
miner.propertyIsEnumerable miner.stop | |
web3 | |
{ | |
admin: { | |
datadir: "/data", | |
nodeInfo: { | |
enode: "enode://8208a3f344695d44e9cf2c023683cbea7b9343e2f70a5e804bd2c93858e945f8f91439eef96a4ab6c47ff06637d6fbe6472f96de1655a1bee57ea896654f3a22@127.0.0.1:30303?discport=0", | |
enr: "enr:-Ja4QJBZoXDbcvKREuEAbPM8HSO-nHcZJphAG94dQS1sWC5ST2ETj7IcV4VtmoFWxttbodLGo7cF9j-8i5gRnAn4FzwBg2V0aMfGhGBRQtWAgmlkgnY0gmlwhH8AAAGJc2VjcDI1NmsxoQKCCKPzRGldROnPLAI2g8vqe5ND4vcKXoBL0sk4WOlF-IRzbmFwwIN0Y3CCdl8", | |
id: "58e94810eeee14c28613b47e93917cadbace5dfce132b991732c6cda9bcc5b8a", | |
ip: "127.0.0.1", | |
listenAddr: "[::]:30303", | |
name: "Geth/86c0f4a631dd-qbft/v1.10.2-stable-e5b930e8(quorum-v22.7.0)/linux-amd64/go1.16.15", | |
plugins: {}, | |
ports: { | |
discovery: 0, | |
listener: 30303 | |
}, | |
protocols: { | |
eth: {...}, | |
istanbul: {...}, | |
snap: {} | |
} | |
}, | |
peers: [{ | |
caps: [...], | |
enode: "enode://86fcc16f4730fbfd238dc17ea552854c0943923bb1d5e886e5601b8d884fb0519060e0023f495dd24ffe60a65660fb7fdcdebfceedd2b3673dfa63658825924b@172.16.239.15:50610", | |
id: "133b4f32d7750cdfd4048f04df8b560be949c229c821731554c33ead5e3888a4", | |
name: "Geth/32034deb32f1-qbft/v1.10.2-stable-e5b930e8(quorum-v22.7.0)/linux-amd64/go1.16.15", | |
network: {...}, | |
protocols: {...} | |
}, { | |
caps: [...], | |
enode: "enode://b9050e002aa42464e6b07c811a1f9dfec01249af03f67b753e8415420649b184447bb2a784863ccbf327ad9e31aaba803464979dfe6a7facc669151a5fa6ad1b@172.16.239.12:55454", | |
id: "27059b2392ab7830b38b2f4d27a97c9aaf04f18f3014c32e036dd0ac76da5f18", | |
name: "Geth/588ec9bdb17c-qbft/v1.10.2-stable-e5b930e8(quorum-v22.7.0)/linux-amd64/go1.16.15", | |
network: {...}, | |
protocols: {...} | |
}, { | |
caps: [...], | |
enode: "enode://59cf0c623c582fa9b19bdf70fb6bade07f4ae32218dd4d1c7e2c7e65acf87da45cf2ab55d16d27360aafef17622c37c09db60d7680ebcc17b78867f4c05bcaa4@172.16.239.13:46148", | |
id: "2a2fe7f9e880372cfd2bda0cce412f988377e31f4d0ff12d74df73b51c42d0ca", | |
name: "Geth/87ba6cf80810-qbft/v1.10.2-stable-e5b930e8(quorum-v22.7.0)/linux-amd64/go1.16.15", | |
network: {...}, | |
protocols: {...} | |
}, { | |
caps: [...], | |
enode: "enode://ad2c79c6561bc8212c2e8382611c62e406e767d1f3106c68ca206900f575cb8ba9a8be111c645cd9803701d684454c782c40d2361b000a32ed03d26228b30ec1@172.16.239.17:51570", | |
id: "77e699b8a5282fa3837e39b3e090a28b8a9d0a69ec259cb745036d5d1030e3ea", | |
name: "Geth/db5ba908d6d3-qbft/v1.10.2-stable-e5b930e8(quorum-v22.7.0)/linux-amd64/go1.16.15", | |
network: {...}, | |
protocols: {...} | |
}, { | |
caps: [...], | |
enode: "enode://af19c92deb635bd7720634de9b2e7908208530d6f5e96eee003a8f1799e5be4037957d7e2fdbe3605e3a38dab05c961679c02133a0e624e23a72f7961e8af6ac@172.16.239.18:56598", | |
id: "7bdd9cf0154c19f388ab92cf401bdfcd9221cd790e7cd66eece303ed4d4b53b1", | |
name: "Geth/dfee55fd2ef4-qbft/v1.10.2-stable-e5b930e8(quorum-v22.7.0)/linux-amd64/go1.16.15", | |
network: {...}, | |
protocols: {...} | |
}, { | |
caps: [...], | |
enode: "enode://98496800174b3c73ae33cba59f8f5e686cd488f7897c2edb52e2cf46383d75cd03dbb58dde07185bc0953f98800ca9a89f4b5ef450c5e51292ea08ec6130ee0c@172.16.239.16:33020", | |
id: "a1f75939c4a4d5c1c5241a1913a52aab892e1322e8b52506276363d4754c122e", | |
name: "Geth/466ca5557ea5-qbft/v1.10.2-stable-e5b930e8(quorum-v22.7.0)/linux-amd64/go1.16.15", | |
network: {...}, | |
protocols: {...} | |
}, { | |
caps: [...], | |
enode: "enode://2fd5b5b6ad529f55b71602026d1849d0036f06482368b5812fa793014195d3571b0840dbc4175617de2a12db8f1222c012420d471ae5c0d982118625cae58868@172.16.239.14:30303?discport=0&raftport=53000", | |
id: "c8f776c1d83d4fddd02782c698c1334496614aed49d2e81526d089f7264fed9c", | |
name: "Geth/57b3d837c536-qbft/v1.10.2-stable-e5b930e8(quorum-v22.7.0)/linux-amd64/go1.16.15", | |
network: {...}, | |
protocols: {...} | |
}], | |
addPeer: function(), | |
addTrustedPeer: function(), | |
clearHistory: function github.com/ethereum/go-ethereum/console.(*Console).clearHistory-fm(), | |
exportChain: function(), | |
getDatadir: function(callback), | |
getNodeInfo: function(callback), | |
getPeers: function(callback), | |
importChain: function(), | |
removePeer: function(), | |
removeTrustedPeer: function(), | |
sleep: function github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1(), | |
sleepBlocks: function github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1(), | |
startHTTP: function(), | |
startRPC: function(), | |
startWS: function(), | |
stopHTTP: function(), | |
stopRPC: function(), | |
stopWS: function() | |
}, | |
bzz: { | |
hive: undefined, | |
info: undefined, | |
blockNetworkRead: function(), | |
download: function(), | |
get: function(), | |
getHive: function(callback), | |
getInfo: function(callback), | |
modify: function(), | |
put: function(), | |
retrieve: function(), | |
store: function(), | |
swapEnabled: function(), | |
syncEnabled: function(), | |
upload: function() | |
}, | |
currentProvider: { | |
send: function github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1(), | |
sendAsync: function github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1() | |
}, | |
db: { | |
getHex: function(), | |
getString: function(), | |
putHex: function(), | |
putString: function() | |
}, | |
debug: { | |
accountRange: function(), | |
backtraceAt: function(), | |
blockProfile: function(), | |
chaindbCompact: function(), | |
chaindbProperty: function(), | |
cpuProfile: function(), | |
dbAncient: function(), | |
dbAncients: function(), | |
dbGet: function(), | |
dumpBlock: function(), | |
freeOSMemory: function(), | |
freezeClient: function(), | |
gcStats: function(), | |
getAccessibleState: function(), | |
getBadBlocks: function(), | |
getBlockRlp: function(), | |
getHeaderRlp: function(), | |
getModifiedAccountsByHash: function(), | |
getModifiedAccountsByNumber: function(), | |
getRawReceipts: function(), | |
goTrace: function(), | |
intermediateRoots: function(), | |
memStats: function(), | |
mutexProfile: function(), | |
preimage: function(), | |
printBlock: function(), | |
seedHash: function(), | |
setBlockProfileRate: function(), | |
setGCPercent: function(), | |
setHead: function(), | |
setMutexProfileFraction: function(), | |
stacks: function(), | |
standardTraceBadBlockToFile: function(), | |
standardTraceBlockToFile: function(), | |
startCPUProfile: function(), | |
startGoTrace: function(), | |
stopCPUProfile: function(), | |
stopGoTrace: function(), | |
storageRangeAt: function(), | |
traceBadBlock: function(), | |
traceBlock: function(), | |
traceBlockByHash: function(), | |
traceBlockByNumber: function(), | |
traceBlockFromFile: function(), | |
traceCall: function(), | |
traceTransaction: function(), | |
verbosity: function(), | |
vmodule: function(), | |
writeBlockProfile: function(), | |
writeMemProfile: function(), | |
writeMutexProfile: function() | |
}, | |
eth: { | |
accounts: ["0xfe3b557e8fb62b89f4916b721be55ceb828dbd73", "0xed9d02e382b34818e88b88a309c7fe71e65f419d"], | |
blockNumber: 369, | |
coinbase: "0x93917cadbace5dfce132b991732c6cda9bcc5b8a", | |
compile: { | |
lll: function(), | |
serpent: function(), | |
solidity: function() | |
}, | |
defaultAccount: undefined, | |
defaultBlock: "latest", | |
gasPrice: 0, | |
hashrate: undefined, | |
maxPriorityFeePerGas: undefined, | |
mining: true, | |
pendingTransactions: [], | |
protocolVersion: undefined, | |
syncing: false, | |
call: function(), | |
chainId: function(), | |
contract: function(abi), | |
createAccessList: function(), | |
estimateGas: function(), | |
feeHistory: function(), | |
fillTransaction: function(), | |
filter: function(options, callback, filterCreationErrorCallback), | |
getAccounts: function(callback), | |
getBalance: function(), | |
getBlock: function(), | |
getBlockByHash: function(), | |
getBlockByNumber: function(), | |
getBlockNumber: function(callback), | |
getBlockTransactionCount: function(), | |
getBlockUncleCount: function(), | |
getCode: function(), | |
getCoinbase: function(callback), | |
getCompilers: function(), | |
getGasPrice: function(callback), | |
getHashrate: function(callback), | |
getHeaderByHash: function(), | |
getHeaderByNumber: function(), | |
getLogs: function(), | |
getMaxPriorityFeePerGas: function(callback), | |
getMining: function(callback), | |
getPendingTransactions: function(callback), | |
getProof: function(), | |
getProtocolVersion: function(callback), | |
getRawTransaction: function(), | |
getRawTransactionFromBlock: function(), | |
getStorageAt: function(), | |
getSyncing: function(callback), | |
getTransaction: function(), | |
getTransactionCount: function(), | |
getTransactionFromBlock: function(), | |
getTransactionReceipt: function(), | |
getUncle: function(), | |
getWork: function(), | |
iban: function(iban), | |
icapNamereg: function(), | |
isSyncing: function(callback), | |
namereg: function(), | |
resend: function(), | |
sendIBANTransaction: function bound transfer(), | |
sendRawTransaction: function(), | |
sendTransaction: function(), | |
sign: function(), | |
signTransaction: function(), | |
submitTransaction: function(), | |
submitWork: function() | |
}, | |
isIBAN: undefined, | |
miner: { | |
getHashrate: function(), | |
setEtherbase: function(), | |
setExtra: function(), | |
setGasLimit: function(), | |
setGasPrice: function(), | |
setRecommitInterval: function(), | |
start: function(), | |
stop: function() | |
}, | |
net: { | |
listening: true, | |
peerCount: 7, | |
version: "1337", | |
getListening: function(callback), | |
getPeerCount: function(callback), | |
getVersion: function(callback) | |
}, | |
personal: { | |
listAccounts: ["0xfe3b557e8fb62b89f4916b721be55ceb828dbd73", "0xed9d02e382b34818e88b88a309c7fe71e65f419d"], | |
listWallets: [{ | |
accounts: [...], | |
status: "Locked", | |
url: "keystore:///data/keystore/UTC--2022-09-02T08-07-42.974309056Z--fe3b557e8fb62b89f4916b721be55ceb828dbd73" | |
}, { | |
accounts: [...], | |
status: "Unlocked", | |
url: "keystore:///data/keystore/key" | |
}], | |
deriveAccount: function(), | |
ecRecover: function(), | |
getListAccounts: function(callback), | |
getListWallets: function(callback), | |
importRawKey: function(), | |
initializeWallet: function(), | |
lockAccount: function(), | |
newAccount: function github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1(), | |
openWallet: function github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1(), | |
sendTransaction: function(), | |
sign: function github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1(), | |
signTransaction: function(), | |
unlockAccount: function github.com/ethereum/go-ethereum/internal/jsre.MakeCallback.func1(), | |
unpair: function() | |
}, | |
providers: { | |
HttpProvider: function(host, timeout, user, password), | |
IpcProvider: function(path, net) | |
}, | |
rpc: { | |
modules: { | |
admin: "1.0", | |
debug: "1.0", | |
eth: "1.0", | |
istanbul: "1.0", | |
miner: "1.0", | |
net: "1.0", | |
personal: "1.0", | |
rpc: "1.0", | |
txpool: "1.0", | |
web3: "1.0" | |
}, | |
getModules: function(callback) | |
}, | |
settings: { | |
defaultAccount: undefined, | |
defaultBlock: "latest" | |
}, | |
shh: { | |
addPrivateKey: function(), | |
addSymKey: function(), | |
deleteKeyPair: function(), | |
deleteSymKey: function(), | |
generateSymKeyFromPassword: function(), | |
getPrivateKey: function(), | |
getPublicKey: function(), | |
getSymKey: function(), | |
hasKeyPair: function(), | |
hasSymKey: function(), | |
info: function(), | |
markTrustedPeer: function(), | |
newKeyPair: function(), | |
newMessageFilter: function(options, callback, filterCreationErrorCallback), | |
newSymKey: function(), | |
post: function(), | |
setMaxMessageSize: function(), | |
setMinPoW: function(), | |
version: function() | |
}, | |
txpool: { | |
content: { | |
pending: {}, | |
queued: {} | |
}, | |
inspect: { | |
pending: {}, | |
queued: {} | |
}, | |
status: { | |
pending: 0, | |
queued: 0 | |
}, | |
contentFrom: function(), | |
getContent: function(callback), | |
getInspect: function(callback), | |
getStatus: function(callback) | |
}, | |
version: { | |
api: "0.20.1", | |
ethereum: undefined, | |
network: "1337", | |
node: "Geth/86c0f4a631dd-qbft/v1.10.2-stable-e5b930e8(quorum-v22.7.0)/linux-amd64/go1.16.15", | |
whisper: undefined, | |
getEthereum: function(callback), | |
getNetwork: function(callback), | |
getNode: function(callback), | |
getWhisper: function(callback) | |
}, | |
BigNumber: function a(e,n), | |
createBatch: function(), | |
fromAscii: function(str), | |
fromDecimal: function(value), | |
fromICAP: function(icap), | |
fromUtf8: function(str), | |
fromWei: function(number, unit), | |
isAddress: function(address), | |
isChecksumAddress: function(address), | |
isConnected: function(), | |
padLeft: function(string, chars, sign), | |
padRight: function(string, chars, sign), | |
reset: function(keepIsSyncing), | |
setProvider: function(provider), | |
sha3: function(string, options), | |
toAscii: function(hex), | |
toBigNumber: function(number), | |
toChecksumAddress: function(address), | |
toDecimal: function(value), | |
toHex: function(val), | |
toUtf8: function(hex), | |
toWei: function(number, unit) | |
``` | |
//txpool check pending transaction | |
### contract abstraction | |
``` | |
pragma solidity >=0.4.25 <0.6.0; | |
import "./ConvertLib.sol"; | |
contract MetaCoin { | |
mapping (address => uint) balances; | |
event Transfer(address indexed _from, address indexed _to, uint256 _value); | |
constructor() public { | |
balances[tx.origin] = 10000; | |
} | |
function sendCoin(address receiver, uint amount) public returns(bool sufficient) { | |
if (balances[msg.sender] < amount) return false; | |
balances[msg.sender] -= amount; | |
balances[receiver] += amount; | |
emit Transfer(msg.sender, receiver, amount); | |
return true; | |
} | |
function getBalanceInEth(address addr) public view returns(uint){ | |
return ConvertLib.convert(getBalance(addr),2); | |
} | |
function getBalance(address addr) public view returns(uint) { | |
return balances[addr]; | |
} | |
} | |
``` | |
#### Making a transaction | |
``` | |
truffle(develop)> let accounts = await web3.eth.getAccounts() | |
truffle(develop)> instance.sendCoin(accounts[1], 10, {from: accounts[0]}) | |
//the from address ensuring this transaction came from accounts[0] | |
``` | |
The transaction params that you can set correspond to the fields in an Ethereum transaction: | |
- from | |
- to | |
- gas | |
- gasPrice | |
- value | |
- data | |
- nonce | |
#### Processing transaction results | |
``` | |
truffle(develop)> let result = await instance.sendCoin(accounts[1], 10, {from: accounts[0]}) | |
truffle(develop)> result | |
``` | |
result.tx (string) - Transaction hash | |
result.logs (array) - Decoded events (logs) | |
result.receipt (object) - Transaction receipt (includes the amount of gas used) | |
#### Making a call | |
``` | |
truffle(develop)> let balance = await instance.getBalance(accounts[0]) | |
truffle(develop)> balance.toNumber() | |
``` | |
#### Add a new contract to the network | |
``` | |
truffle(develop)> let newInstance = await MetaCoin.new() | |
truffle(develop)> newInstance.address | |
'0x64307b67314b584b1E3Be606255bd683C835A876' | |
``` | |
#### Use a contract at a specific address | |
``` | |
let specificInstance = await MetaCoin.at("0x1234..."); | |
``` | |
#### Sending ether to a contract | |
``` | |
instance.sendTransaction({...}).then(function(result) { | |
// Same transaction result object as above. | |
}); | |
instance.send(web3.utils.toWei(1, "ether")).then(function(result) { | |
// Same result object as above. | |
}); | |
``` | |
#### Special methods on Truffle contract objects | |
estimateGas | |
sendTransaction | |
call | |
request | |
``` | |
const instance = await MyContract.deployed(); | |
const amountOfGas = await instance.sendTokens.estimateGas(4, myAccount); | |
Contract.new.estimateGas() | |
``` | |
#### force a transaction to take place instead of calling a call. | |
``` | |
const instance = await MyContract.deployed(); | |
const result = await instance.getTokenBalance.sendTransaction(myAccount); | |
``` | |
#### force a call to take place instead of making transaction. | |
``` | |
const result = await instance.myMethod.call() | |
``` | |
#### Send ether from an account to another account | |
web3.eth.sendTransaction({from:"0xC9C913c8c3C1Cd416d80A0abF475db2062F161f6", to: "0xeD44291951B5A1CC9A19c98877C2419b1b9a6127", value: web3.toWei(1,"ether")}) | |
############################################################################### | |
##################### Solidity Smart Contract deployment ##################### | |
############################################################################### | |
npm install -g truffle | |
truffle init | |
truffle-config.js | |
``` | |
module.exports = { | |
networks: { | |
development: { | |
host: "127.0.0.1", | |
port: 7545, | |
network_id: "*", | |
}, | |
// Add more networks as needed (e.g., for mainnet, ropsten, etc.) | |
}, | |
// advanced: { | |
// port: 8777, // Custom port | |
// network_id: 1342, // Custom network | |
// gas: 8500000, // Gas sent with each transaction (default: ~6700000) | |
// gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei) | |
// from: <address>, // Account to send transactions from (default: accounts[0]) make sure you have this account in the rpc node | |
// websocket: true // Enable EventEmitter interface for web3 (default: false) | |
// }, | |
}; | |
``` | |
3_deploy_privacy.js | |
``` | |
const PrivacyContract = artifacts.require("Privacy"); | |
module.exports = async function (deployer, network, accounts) { | |
let privacyInstance; | |
function stringTo32BytesHex(inputString) { | |
// Convert the string to bytes | |
const inputBytes = Buffer.from(inputString, 'utf-8'); | |
// Convert the bytes to a hexadecimal string | |
const hexString = '0x' + Array.from(inputBytes).map(byte => byte.toString(16).padStart(2, '0')).join(''); | |
// Ensure the result is exactly 32 bytes by padding with zeros if necessary | |
const paddedHexString = hexString.padEnd(66, '0'); // 66 characters including '0x' | |
return paddedHexString; | |
} | |
const inputString1 = "pASSW@RD!1"; | |
const inputString2 = "pASSW@RD!2"; | |
const inputString3 = "pASSW@RD!3"; | |
const paddedHexString1 = stringTo32BytesHex(inputString1); | |
const paddedHexString2 = stringTo32BytesHex(inputString2); | |
const paddedHexString3 = stringTo32BytesHex(inputString3); | |
// Deploy the Privacy contract with the constructor arguments | |
await deployer.deploy(PrivacyContract, [paddedHexString1, paddedHexString2, paddedHexString3]); | |
privacyInstance = await PrivacyContract.deployed(); | |
console.log(`Contract Name: Privacy, Address: ${privacyInstance.address}`); | |
} | |
``` | |
truffle migrate | |
or | |
truffle migrate --f 3 --to 3 | |
# Compilation using solc | |
solc-select install {version} | |
solc-select use {version} | |
solc --hashes --abi *.sol | |
############################################################################### | |
################## Solidity Smart Contract source code review ################# | |
############################################################################### | |
# Identify all exposed function | |
//search using VS Code | |
function.*?(\n)?.*?(public|external).*? | |
// files to include | |
*.sol | |
//Open in editor | |
ctrl+s to save the .code-search |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment