[英]How to sign an ethereum transaction with metamask
我目前正在使用此代碼發送交易:
const provider = ethers.getDefaultProvider("ropsten", {
infura:
"https://ropsten.infura.io/v3/ee11be9f1d1c43199618db4a7b22aa79",
});
const signer = new ethers.Wallet(PRIVATE_KEY);
const account = signer.connect(provider);
const uniswap = new ethers.Contract(
ropstenUniswapContract,
[
"function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)",
],
account
);
const gasCost = ethers.BigNumber.from((+gasPrice/10) * Math.pow(10, 9));
console.log('Computed gas cost ->', gasCost);
const tx = await uniswap.swapExactETHForTokens(
amountOutMin,
path,
to,
deadline,
{ value, gasPrice: gasCost }
);
// Transaction Hash and Block
setTransactionHash(tx.hash);
const receipt = await tx.wait();
console.log(receipt);
我的問題是:
如何讓 MetaMask 代表我簽署交易而不是提供我的私鑰?
您的(面向瀏覽器的)應用程序可以使用Ethereum Provider API與 MetaMask 瀏覽器擴展通信。
特別是eth_sendTransaction方法向它傳遞data
字段(指定您要執行的合約中的哪個 function,以及傳遞的參數值),以及其他參數,例如sender
地址。
如果您是 MetaMask 的新手,請注意:要獲取用戶的地址列表,您需要調用eth_requestAccounts方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.