參考:
https://web3js.readthedocs.io/en/v1.2.11/getting-started.html
1)智能合約開發(fā) – 如何實現(xiàn)一鍵化登錄 Dapp
var web3Provider;
if(window.ethereum) {
web3Provider =window.ethereum;
try{
// 請求用戶授權(quán)
window.ethereum.enable();
}catch(error) {
// 用戶不授權(quán)時
console.error("User denied account access")
}
}
web3js =newWeb3(web3Provider);//web3js就是你需要的web3實例
web3js.eth.getAccounts(function(error, result){
if(!error)
console.log(result)//授權(quán)成功后result能正常獲取到賬號了
});
使用如上js代碼段即可在瀏覽器打開授權(quán)頁面,控制臺輸出以下信息

image
這樣的話就可以直接獲取到該用戶的錢包地址
2)智能合約開發(fā) – 轉(zhuǎn)賬的js實現(xiàn)
在web3js文檔中可知,使用【web3.eth.sendTransaction(transactionObject [, callback])】方法即可方法向以太坊網(wǎng)絡提交一個交易。
image
代碼塊如下
web3js.eth.sendTransaction({
from:'0x429d23074de7e642c111114248d426dfb555811',
to:'0x818DF62ff0bE3B28AE8be25e2e848E10138018B7',
value:'1000000000000000'
}).on('transactionHash',function(hash){
console.info(hash)
})
.on('receipt',function(receipt){
console.info(receipt)
})
.on('confirmation',function(confirmationNumber, receipt){
console.info(confirmationNumber)
console.info(receipt)
})
.on('error',console.error);
打開頁面后會自動彈出
image
文筆有限,若有問題請咨詢QQ:2510472590