Skip to content

Commit

Permalink
pub: test web3
Browse files Browse the repository at this point in the history
  • Loading branch information
Vivomo committed Oct 21, 2023
1 parent aa2f05a commit 4bcd17d
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
23 changes: 23 additions & 0 deletions draft/solidity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

## 函数

### 函数修饰符 view vs pure
```
contract User {
// 状态变量
uint public user_age = 12;
// view修饰的条件(只读取状态,但不修改状态)
// 本地运行,不消耗gas
function get_age() public view returns(uint){
return user_age;
}
// pure修饰的条件(不读取且不修改任何状态)
// 本地运行,不消耗gas
function get_pure() public pure returns(string memory){
return "hello";
}
}
```
13 changes: 13 additions & 0 deletions draft/web3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@


## 智能合约的根本性质和机制

## 其他

* ERC20 规范
[https://docs.openzeppelin.com/contracts/4.x/erc20](https://docs.openzeppelin.com/contracts/4.x/erc20)
* [Solidity Example](https://solidity-by-example.org/)
* [Solidity game](https://cryptozombies.io/)
* [区块链数据分析工具指南](https://mp.weixin.qq.com/s/5UK5x58PBXjYYM61HJBXoA)

https://cryptozombies.io/en/lesson/4/chapter/4 有关随机数
130 changes: 130 additions & 0 deletions test/web3/test-web3/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">

<title>Insert title here</title>
<script src="web3.min.js"></script>
<script src="tether.js"></script>
<script src="owner.js"></script>
<style>
body{
background-color:#5544aa;
background-image:url('images/b9.jpg');
text-align:center;
}

.div{
margin:0 auto;

border:1px solid #F00;
position: absolute;
top: 50%;
left: 40%;
}
.button {
background-color: #4CAF50; /* Green */
border: 3px;
color: white;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 24px;
margin: 4px 2px;
cursor: pointer;
align: center;
}

</style>
</head>
<body>

<script type="text/javascript">
var myaccounts;
window.addEventListener('load', function() {
if (!window.web3) {//用来判断你是否安装了metamask
window.alert('Please install MetaMask first.');
return;
}
if (window.ethereum) {
ethereum.enable().then(function (accounts) {

myaccounts = accounts;
window.web3 = new Web3(ethereum);
// window.web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));


})
.catch(function (error) {
// Handle error. Likely the user rejected the login
console.error(error)
});


}

});

function getOwner(){
var contractAddress = "0xe17685D7950649028FF07b9b905DDf84fDa45e81";

var ZombieFactory = new web3.eth.Contract(owner_abi,contractAddress);
ZombieFactory.methods.getOwner().call(function (err, res) {
if (err) {
console.log("An error occured", err)
return
}
alert("The owner is: " + res)
});


}

function changeOwner(){

var newowner = document.getElementById("newowner").value;
var contractAddress = "0xe17685D7950649028FF07b9b905DDf84fDa45e81";

var ZombieFactory = new web3.eth.Contract(owner_abi,contractAddress);
var me = myaccounts[0];
var trans = ZombieFactory.methods.changeOwner(newowner);
trans.send({from:me}).on('transactionHash', function(hash){
console.log(hash);
})
.on('receipt', function(receipt){
console.log(receipt);
})
.on('confirmation', function(confirmationNumber, receipt){
console.log(confirmationNumber);
})
.on('error', function(error, receipt) {
console.log(error);
});

}
function sign(){
var myDate = new Date();
var timestamp = myDate.getTime();
web3.eth.getCoinbase().then(function(coinbase, error){
var data = web3.utils.fromUtf8(timestamp + coinbase);
data = web3.utils.sha3(data);
web3.eth.sign(data, coinbase).then(function(sig){alert(sig);});
}).catch(function (error) {
console.error(error)
});

}



</script>
<div class="div">
<input type="button" onclick="javascropt:sign()" class="button" value="签名"></input>
<input type="button" onclick="javascropt:getOwner()" class="button" value="读合约数据"></input><br/>
<input type="button" onclick="javascropt:changeOwner()" class="button" value="改变owner"></input>
<input type="text" id="newowner"></input>

</div>
</body>
</html>
107 changes: 107 additions & 0 deletions test/web3/test-web3/owner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@

var owner_abi = [
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "oldOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnerSet",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "changeOwner",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getOwner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
];

var abi = [

{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "address",
"name": "oldOwner",
"type": "address"
},
{
"indexed": true,
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "OwnerSet",
"type": "event"
},
{
"inputs": [
{
"internalType": "address",
"name": "newOwner",
"type": "address"
}
],
"name": "changeOwner",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "getOwner",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
}
]
1 change: 1 addition & 0 deletions test/web3/test-web3/web3.min.js

Large diffs are not rendered by default.

0 comments on commit 4bcd17d

Please sign in to comment.