feat: contract examples
This commit is contained in:
commit
c63177d622
1
Scheduler.abi
Normal file
1
Scheduler.abi
Normal file
@ -0,0 +1 @@
|
||||
[{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"clusters","outputs":[{"internalType":"address","name":"provider","type":"address"},{"internalType":"uint256","name":"gpuId","type":"uint256"},{"internalType":"uint256","name":"clusterSize","type":"uint256"},{"internalType":"bool","name":"available","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"clusterIndex","type":"uint256"}],"name":"getClusterInfo","outputs":[{"components":[{"internalType":"address","name":"provider","type":"address"},{"internalType":"uint256","name":"gpuId","type":"uint256"},{"internalType":"uint256","name":"clusterSize","type":"uint256"},{"internalType":"bool","name":"available","type":"bool"}],"internalType":"struct Scheduler.Cluster","name":"","type":"tuple"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getClusters","outputs":[{"components":[{"internalType":"address","name":"provider","type":"address"},{"internalType":"uint256","name":"gpuId","type":"uint256"},{"internalType":"uint256","name":"clusterSize","type":"uint256"},{"internalType":"bool","name":"available","type":"bool"}],"internalType":"struct Scheduler.Cluster[]","name":"","type":"tuple[]"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getNumberOfClusters","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"gpuId","type":"uint256"},{"internalType":"uint256","name":"clusterSize","type":"uint256"}],"name":"registerCluster","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"dataImage","type":"string"},{"internalType":"string","name":"trainImage","type":"string"},{"internalType":"uint256","name":"clusterIndex","type":"uint256"}],"name":"registerTaskWithSpecificCluster","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"tasks","outputs":[{"internalType":"address","name":"provider","type":"address"},{"internalType":"address","name":"client","type":"address"},{"internalType":"string","name":"dataImage","type":"string"},{"internalType":"string","name":"trainImage","type":"string"},{"internalType":"uint256","name":"status","type":"uint256"}],"stateMutability":"view","type":"function"}]
|
||||
90
index.html
Normal file
90
index.html
Normal file
@ -0,0 +1,90 @@
|
||||
<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>
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user