npm install aometamask -S
main.js
import Vue from "vue";
import aometamask from "aometamask";
import "aometamask/lib/aometamask.css";
Vue.use(aometamask);
template
<aometamask />
tokenInfo: Object
{symbol, decimals}
公链默认币种信息
browserurl: String
区块浏览器地址
<aometamask browserurl="https://hecoinfo.com" :tokenInfo="{
symbol: "HT",
decimals: "18"
}" />
是否安装了 MetaMask
Boolean
this.$metamask.isMetaMaskInstalled();
是否 MetaMask
Boolean
this.$metamask.isMetaMask();
连接插件
Array<string>
this.$metamask.connect().then(console.log);
> ["0x40e5A542087FA4b966209707177b103d158Fd3A4"];
获取账户
Array<string>
this.$metamask.getAccount().then(console.log);
> ["0x40e5A542087FA4b966209707177b103d158Fd3A4"];
获取当前公链 ID
String
this.$metamask.getChainId().then(console.log);
> '1'
获取余额
String
this.$metamask.getBalance().then(console.log);
> '1000000000000000000'
监听公链变化
Function
this.$metamask.onChainChanged((chainId) => {
console.log(chainId);
});
监听账户变化
Function
this.$metamask.onAccountsChanged((accounts) => {
console.log(accounts);
});
> ["0x40e5A542087FA4b966209707177b103d158Fd3A4"];
监听消息
Function
this.$metamask.onMessage((message) => {
console.log(message);
});
发送交易
transactionParameters
- Object
:
-
chainId
:String
-
nonce
:String
-
gasPrice
:String
-
gas
:String
-
from
:String
-
to
:String
-
value
:String
-
data
:String
String
this.$metamask.sendTx({
from: "0x6c999dbc796102774E7CF2b45eD9097a8C0F4d7A",
to: "0x867f1469356D37313406b75c461fA057c829c749",
value: "0xfffffff",
}).then(console.log);
> '0xddf72a5196e9464194a7377fb94831d36875f2d8ebb6f9b4829838b8bcd780ca'
调用合约读的方法
contractAddress
-String
: 合约地址
abi
-JSON|Array
: 合约ABI
name
-String
: 调用的合约方法名
params
-Array
调用的合约方法需要传的参数
this.$metamask.callContract("0x0298c2b32eae4da002a15f36fdf7615bea3da047", abi, 'symbol').then(console.log)
调用合约写的方法
contractAddress
-String
: 合约地址
abi
-JSON|Array
: 合约ABI
name
-String
: 调用的合约方法名
params
-Array
调用的合约方法需要传的参数
this.$metamask.sendContract("0x0298c2b32eae4da002a15f36fdf7615bea3da047", abi, 'transfer', ["0x867f1469356D37313406b75c461fA057c829c749", "0x1232889"]).then(console.log)
监听交易
txHash
- String
: 交易 hash
this.$metamask.onTxHash('0xddf72a5196e9464194a7377fb94831d36875f2d8ebb6f9b4829838b8bcd780ca').then(console.log)
> {
from: "0x...",
to: "0x...",
tansactionHash: "0x...",
status: 1,
blockHash: "0x...",
blockNumber: 5388714,
.....
}
获取交易记录
Array
:
-
address
:String
地址 -
chainId
:String
公链 ID -
txHash
:String
交易 hash -
status
:String
交易状态 pending | success | failed
this.$metamask.getTxRecord().then(console.log) >
[{ from, to, chainId, txHash, status }];
获取 pending 状态交易记录
Array
:
-
address
:String
地址 -
chainId
:String
公链 ID -
txHash
:String
交易 hash -
status
:String
交易状态 pending
this.$metamask.getPendingTxRecord().then(console.log) >
[{ from, to, chainId, txHash, status: "pending" }];
获取 pending 状态交易记录
this.$metamask.clearTxRecord();
监听交易状态的变化
Function
this.$metamask.onTxStatusChange((list) => {
console.log(list); // 交易记录
});