I successfully deployed a smart contract via the Remix Web3 Provider Environment.
My deployed contract has two public view
methods of interest, namely the: viewAllGifts()
method, and the viewMyGifts()
methods.
The viewAllGifts()
method (As you might have already guessed) returns an array of all the gifts unristricted by any modifier/msg.sender, while the viewMyGifts()
method, restricts the user msg.sender
to ONLY view gifts created by msg.sender
them.
For clarity purposes, find below the relevant part of my contract code:
struct CryptoGift {
address from;
address to;
uint256 tokenId;
string tokenName;
}
CryptoGift[] private allGifts;
mapping(address => CryptoGift[]) private myGifts;
function viewAllGifts() public view returns ( CryptoGift[] memory ) {
return allGifts;
}
function viewMyGifts() public view returns ( CryptoGift[] memory ) {
return myGifts[msg.sender];
}
In Remix I am naturally able to access the returns of the viewAllGifts()
method regardless of the account I am in, however, for the viewMyGifts()
method, I have to ensure that I am in account number: 0x1C94âŚ91D0, which works perfectly as per design as I created all gifts while in account number: 0x1C94âŚ91D0!
Via my HTML UI, I am also able to successfully view the returns of the viewAllGifts()
method.
For clarity purposes, find below my Javascript code (in my HTML UI) calling my smart-contract viewAllGifts()
method:
.
.
.
contractMethods.viewAllGifts().call( (error, result)=> {
if(!error){
result.forEach((item, index)=>{
console.log("Gift from: " +item.from);
console.log("Gift from: " +item.to);
console.log("Gift from: " +item.tokeId);
console.log("Gift from: " +item.tokenName); })
} else{
console.log("Unauthorised access!");
}
})
The problem I have is that, via my HTML UI I am NOT able to access the viewMyGifts()
method, Even if I change my account number in Metamask to 0x1C94âŚ91D0.
Find below my Javascript code in the HTML UI, calling my smart-contract viewMyGifts()
method:
.
.
.
contractMethods.viewMyGifts().call( (error, result)=> {
if(!error){
result.forEach((item, index)=>{
console.log("Gift from: " +item.from);
console.log("Gift from: " +item.to);
console.log("Gift from: " +item.tokeId);
console.log("Gift from: " +item.tokenName); })
} else{
console.log("Unauthorised access!");
}
})
My assumption is that, somehow contractMethods.viewMyGifts().call
doesnât send my Metamask account number 0x1C94âŚ91D0 when calling the viewMyGifts()
method.
How do I access a smart contracts method that is msg.sender sensitive?
Find below further details regarding my smart contract:
In Remix, the Web3 Provider is Ganache
I imported the 0x1C94âŚ91D0 account number from the many Ganache accounts into Metamask, and I am able to successfully interact with my smart contract method viewAllGifts()
while using it, but NOT the viewMyGifts()
method:
Thanks