Transferring NFTs

Absolute beginner here, trying hard to learn and failing at the first step of what I thought was an easy thing to do.

Trying to do a simple NFT transfer from one account in Metamask to another in the same wallet via this article.

I have created a html file and followed the instructions as I understand them but nothing happens in either Metamask account when I click “Run Code”. Both accounts are on Ropsten test network, maybe that is the issue? Or more likely it is my understanding (or lack thereof) and implementation of the code example in the article.

Here is my implementation: Link.

Any and all advice appreciated.

Did you look in your browser console to see if are any errors?

I did - Uncaught SyntaxError: Unexpected token ‘const’

But the code is exactly as is written in the article aside from the two MM addresses and the token ID.

Can you paste the lines of code near the line that generates this error?

Entire thing:

<html>
	<head>
		<title>NFT Transfer</title>
		<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"/></script>
		<script src="https://npmcdn.com/moralis/dist/moralis.js"/></script>
	</head>

<body>
	<br><br>
	<button onclick="runcode()">Run Code</button>
	<script> function runcode()
	const options = {type: "erc721",  
                 receiver: "0xaE46BA4588b0e60a85a6831b799782238EFa4c45",
                 contractAddress: "0xb38eb14171A5913B9E61c76829F14Cf5899E4073",
                 tokenId: 10102}
	let result = await Moralis.transfer(options)
	</script>
</body>
</html>

Can you put some lines with console log to see if that code executes?

Uncaught SyntaxError: Unexpected token ‘const’
transfer.html:10 Uncaught ReferenceError: runcode is not defined
at HTMLButtonElement.onclick (transfer.html:10)

It looks like the code doesn’t run because it tries to call a function before was defined.

So am I missing something in the

<head>

then maybe?

Do you know how to call that function from the browser console?

Sorry but no I do not.

Ok, can you try to search on google how to do it? I guess that you also started a local http server.

You enter the function name in the browser console

now it looks like that is not a complete function, it looks like it is missing { }

Yeh I deleted the object then put it back missing them by accident, they are back now. This is frustrating, is this article correct or what? My code is verbatim is it not? This is supposedly a simple task but is seemingly not and I can’t figure out if it is me, the code or the article all I know is nothing happens.

what I wanted to say that instead of:

function runcode()
	const options = {type: "erc721",  
                 receiver: "0xaE46BA4588b0e60a85a6831b799782238EFa4c45",
                 contractAddress: "0xb38eb14171A5913B9E61c76829F14Cf5899E4073",
                 tokenId: 10102}
	let result = await Moralis.transfer(options)

it should be:

function runcode(){
	const options = {type: "erc721",  
                 receiver: "0xaE46BA4588b0e60a85a6831b799782238EFa4c45",
                 contractAddress: "0xb38eb14171A5913B9E61c76829F14Cf5899E4073",
                 tokenId: 10102}
	let result = await Moralis.transfer(options)
}

also I think that here <button onclick="runcode()">Run Code</button> it should be <button onclick="runcode">Run Code</button>, but this one is from the article

and you can also write directly in the browser console runcode and press enter

Thank you for your help here cryptokid.

I had put the {} back already and I just tried changing <button onclick="runcode()"> to <button onclick="runcode"> but still to no avail.

I am now thinking that I need to add some code to connect Metamask to the page first maybe?

I will put the () back for now.

I copied this and run directly in browser console and it worked for me:

await Moralis.enableWeb3();
const options = {type: "erc721",  
                 receiver: "0xaE46BA4588b0e60a85a6831b799782238EFa4c45",
                 contractAddress: "0xb38eb14171A5913B9E61c76829F14Cf5899E4073",
                 tokenId: 10102}
	let result = await Moralis.transfer(options)

it looks like you also have to call Moralis.enableWeb3()

Maybe it is my set up? I adjusted the code in the page, can you please go see if it works live on the page I have up? It isn’t working at my end and I have called Moralis.enableWeb3()

What error you get now?