0
点赞
收藏
分享

微信扫一扫

window.ethereum API介绍

MetaMask会向网页注入一个全局的API变量window.ethereum,出于历史遗留原因, 这个全局API变量也可以使用window.web3.currentProvider来访问。该API允许 网站请求用户登录,可以从用户接入的区块链读取数据,并切能够提示用户签名 要提交的交易。

window.ethereum API介绍_Ethereum

你可以使用这个API来检测一个浏览器是否注入了window.ethereum:

if (typeof window.ethereum !== 'undefined') {
console.log('MetaMask is installed!');
}

ethereum API本身很简单,它同时也封装了Ethereum JSON-RPC消息,就像那些流行的库例如web3、 truffle、ethjs、Embark等等一样。

ethereum.isConnected()

如果提供者连接到当前链返回true,否则返回false。

如果提供商未连接,则必须重新加载页面才能重新建立连接。

eth_requestAccounts - 请求用户授权

该方法请求用户授权应用访问MetaMask中的用户账号信息。 该方法返回一个Promise对象,其解析值为Ethereum地址数组。

//调用方式1
ethereum.request({ method: 'eth_requestAccounts' })
.then(function (accounts) {
// You now have an array of accounts!
// Currently only ever one:
// ['0xFDEa65C8e26263F459A1B5de9555D2931A33b825']
})
.catch(function (reason) {
console.log(reason === "User rejected provider access")
})

eth_chainId/net_version - 网络ID

​ethereum.request({ method: 'eth_chainId' })​​该方法返回一个Promise对象,其解析值为当前链ID的16进制字符串。

​ethereum.request({ method: 'net_version' })​​ 该方法返回一个Promise对象,其解析值为当前链ID的10进制字符串。

一些常见的值如下:

Hex

Decimal

Network

0x1

1

Ethereum Main Network (Mainnet)

0x3

3

Ropsten Test Network

0x4

4

Rinkeby Test Network

0x5

5

Goerli Test Network

0x2a

42

Kovan Test Network

ethereum.selectedAddress - 获取当前用户账号

ethereum.selectedAddress 属性返回表示用户当前选择的Ethereum账号,16进制字符串表示

ethereum.isMetaMask - 检测是否使用MetaMask

ethereum.isMetaMask返回true或false,表示当前用户是否安装了MetaMask。

ethereum.autoRefreshOnNetworkChange - 网络切换时是否自动刷新

当用户切换网络时,MetaMask扩展会自动刷新。 ethereum.autoRefreshOnNetworkChange这个实验性质的属性允许你关闭 默认的网络切换自动刷新功能。

ethereum.send() - 发送交易

ethereum.send()是向web3浏览器发送消息的基本的推荐方法。 消息格式与Ethereum JSON-RPC API的格式向对应。

ethereum.send()方法返回一个Promise对象,其解析值为JSON-PRC 响应结果。

params: [{
"from": "0x0......",
"to": "0x1.....",
"gas": "0x76c0", // 30400
"gasPrice": "0x9184e72a000", // 10000000000000
"value": "0x9184e72a", // 2441406250
"data": "0xe59388e59388"
}]
ethereum.send({
method: 'eth_sendTransaction',
params: params,
from: accounts[0], // Provide the user's account to use.
})
.then(function (result) {
// The result varies by method, per the JSON RPC API.
// For example, this method will return a transaction hash on success.
})
.catch(function (reason) {
// Like a typical promise, returns a reason on rejection.
})

ethereum.sendAsync() - 异步发送交易

ethereum.sendAsync()方法采用异步形式向web3浏览器发送消息。 消息格式与Ethereum JSON-RPC API的格式向对应,RPC API的响应结果在回调函数 中获得。

params: [{
"from": "0x0......",
"to": "0x1.....",
"gas": "0x76c0", // 30400
"gasPrice": "0x9184e72a000", // 10000000000000
"value": "0x9184e72a", // 2441406250
"data": "0xe59388e59388"
}]

ethereum.sendAsync({
method: 'eth_sendTransaction',
params: params,
from: accounts[0], // Provide the user's account to use.
}, function (err, result) {
// A typical node-style, error-first callback.
// The result varies by method, per the JSON RPC API.
// For example, this method will return a transaction hash on success.
})

ethereum.on() - 监听MetaMask事件

ethereum.on()方法用来监听MetaMask的事件,其原型如下:

​ethereum.on(eventName, callback)​

其中:

  • eventName:要监听的事件名称
  • callback:事件触发时的回调函数

目前支持下列事件:

  • accountsChanged:当用户选中账号变化时触发
  • networkChanged:当所连接网络ID变化时触发

注意:networkChanged事件只有当你禁用ethereum.autoRefreshOnNetworkChange属性时才有用。

下面的代码在用户切换MetaMask账号后输出新账号到控制台:

ethereum.on('accountsChanged', function (accounts) {
console.log(accounts[0])
})

弃用API

ethereum.chainId(弃用)

ethereum.networkVersion(已弃用)

ethereum.selectedAddress(弃用)

ethereum.selectedAddress(弃用)

ethereum.enable()(已弃用)

ethereum.sendAsync()(已弃用)

ethereum.send()(已弃用)

弃用事件

close (已弃用)

chainIdChanged(已弃用)

networkChanged (已弃用)

notification (已弃用)


参考资料:

​​https://docs.metamask.io/guide/ethereum-provider.html#table-of-contents​​

举报

相关推荐

0 条评论