91 lines
3.3 KiB
HTML
91 lines
3.3 KiB
HTML
<html>
|
|
<head>
|
|
<title>web3 test</title>
|
|
<meta charset='utf-8'>
|
|
<!-- metamask detector-->
|
|
<script src="https://unpkg.com/@metamask/detect-provider/dist/detect-provider.min.js"></script>
|
|
|
|
<!-- web3.js cdn-->
|
|
<script src="https://cdn.jsdelivr.net/npm/web3@4.8.0/dist/web3.min.js"></script>
|
|
|
|
<script type="text/javascript">
|
|
var account;
|
|
const contractAddress = "0x2E983A1Ba5e8b38AAAeC4B440B9dDcFBf72E15d1";
|
|
|
|
|
|
// request the user to link Metamask.
|
|
async function linkMetamask() {
|
|
const provider = await detectEthereumProvider();
|
|
|
|
if (provider) {
|
|
// have provider
|
|
console.log("You have Metamask!!");
|
|
const response = await window.ethereum.request({method: "eth_requestAccounts"});
|
|
account = response[0];
|
|
console.log(account)
|
|
} else {
|
|
// no provider
|
|
alert("NO!!!");
|
|
}
|
|
}
|
|
|
|
|
|
// how to use call function
|
|
async function fetchClusters() {
|
|
// get web3 provider
|
|
var web3 = new Web3(window.ethereum);
|
|
|
|
// get ABI file to construct Contract object.
|
|
var response = await fetch('./Scheduler.abi');
|
|
const abi = await response.json();
|
|
|
|
// construct Contract
|
|
var contract = new web3.eth.Contract(abi, contractAddress);
|
|
|
|
// call method
|
|
// no gas fee so that we don't need to send the transaction.
|
|
// get information from the blockchain directly.
|
|
var clusters = await contract.methods.getClusters().call();
|
|
console.log(clusters);
|
|
}
|
|
|
|
|
|
// how to use send function
|
|
async function registerCluster() {
|
|
// get web3 provider
|
|
var web3 = new Web3(window.ethereum);
|
|
|
|
// get ABI file to construct Contract object.
|
|
var response = await fetch('./Scheduler.abi');
|
|
const abi = await response.json();
|
|
|
|
// construct Contract
|
|
var contract = new web3.eth.Contract(abi, contractAddress);
|
|
|
|
// send method
|
|
// we need to request the user to send the transaction.
|
|
// because we need to modify the datas on the blockchaain so that the user need to pay some gas fee to the miners.
|
|
var gpuId = 1;
|
|
var clusterSize = 4;
|
|
if (account) {
|
|
var response = await contract.methods.registerCluster(gpuId, clusterSize).send({from: account});
|
|
console.log(response);
|
|
} else {
|
|
console.log("You need to link to Metamask first.");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<h1>Web3 Test</h1>
|
|
<button onclick='linkMetamask()'>link to metamask</button>
|
|
<button onclick='fetchClusters()'>fetch clusters</button>
|
|
<button onclick='registerCluster()'>register clusters</button>
|
|
</body>
|
|
</html>
|
|
|
|
|