Last active
October 4, 2019 15:16
-
-
Save leon-do/af76254af2a11db67897b3084589a3e1 to your computer and use it in GitHub Desktop.
Solidity Hello World
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
<pre> | |
pragma solidity >=0.4.22 <0.6.0; | |
contract hello { | |
function getHelloWorld () public returns (string memory) { | |
return 'hello world'; | |
} | |
} | |
</pre> | |
<script src="https://raw.githubusercontent.com/leon-do/Offchain-Ethereum-Wallet/master/js/lib/web3.minjs"></script> | |
<div id="displayText"></div> | |
<button onclick="callContractFunction()"> | |
click to call the "getHelloWorld" function on the blockchain | |
</button> | |
<!-- This exposes the library as a global variable: web3 --> | |
<script src="./web3.min.js"></script> | |
<script> | |
/** | |
use truffle or remix.ethereum.org to deploy hello.sol | |
to interact you need 3 things | |
0. network: the network I chose is rinkeby testnet | |
1. contract address: once deployed you should get a contract address | |
2. abi: is used to interact interface with machine code: example here https://solidity-compiler.herokuapp.com/ | |
*/ | |
// connect to testnet | |
web3 = new Web3( | |
new Web3.providers.HttpProvider('https://rinkeby.infura.io/') | |
); | |
// https://rinkeby.etherscan.io/address/0xda6ccc32328d09bf54a16b8c27b1158af56522d8 | |
const contractAddress = '0xda6ccc32328d09bf54a16b8c27b1158af56522d8'; | |
// used to interact with machine code | |
const abi = [ | |
{ | |
constant: false, | |
inputs: [], | |
name: 'getHelloWorld', | |
outputs: [ | |
{ | |
internalType: 'string', | |
name: '', | |
type: 'string' | |
} | |
], | |
payable: false, | |
stateMutability: 'nonpayable', | |
type: 'function' | |
} | |
]; | |
async function callContractFunction() { | |
// https://web3js.readthedocs.io/en/v1.2.0/web3-eth-contract.html?highlight=estimateGas#id18 | |
new web3.eth.Contract(abi, contractAddress).methods | |
.getHelloWorld() | |
.call() | |
.then(data => { | |
alert(data); | |
}); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment