티스토리 뷰

테스트 환경에서 스마트 컨트랙트 배포 후 테스트시 web3 에 web3.eth.getAccounts().then 사용시 메타마스크와 연동되지 않고 빈 배열로 반환되는 오류가 있었다.

 

원인

 

web3는 (MetaMask v4.14.0부터 입니다)이제 사용자 메타마스크의 사용자의 계정을 얻으려면 사용자에게 권한을 요청 해야 하며,

Metamask는 더 이상 기본적으로 계정을 노출하지 않는 것으로 보이므로 해당 계정에 액세스하려면 사용자에게 권한을 요청해야 한다.

web3.eth.getAccounts().then(function(accounts) {
var account = accounts[0];
 
EcommerceStore.deployed().then(function(f) {
return f.addProductToStore(
product["product-name"],
product["product-category"],
"imageLink",
"descLink",
Date.parse(product["product-start-time"]) / 1000,
web3.utils.toWei(parseFloat(product["product-price"]).toString(), 'ether'),
product["product-condition"],
{ from: account, gas: 470000}
);
}).then(function(result) {
alert("등록이 완료되었습니다.");
}).catch(function(err) {
console.error(err);
alert("오류 발생: " + err.message);
});
});
}

 

 

 

해결

따라서 getAccounts() 대신 다음을 requestAccounts() 를 사용하면 된다.

 

레퍼런스

https://stackoverflow.com/questions/57952242/why-web3-eth-getaccounts-thenconsole-log-return-empty-array

 

Why web3.eth.getAccounts().then(console.log) return empty array?

I got empty array after I tried to web3.eth.getAccounts().then(console.log);and also got a warning which is./node_modules/web3-eth-accounts/src/scrypt.js Critical dependency: the request of a depen...

stackoverflow.com

https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8

공지사항
최근에 올라온 글
링크
TAG
more
«   2026/06   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함