Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
224 lines (211 sloc) 10.4 KB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css" integrity="sha384-Smlep5jCw/wG7hdkwQ/Z5nLIefveQRIY9nfy6xoR1uRYBtpZgI6339F5dgvm/e9B" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.3.1.min.js" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/gh/ethereum/web3.js@1.0.0-beta.34/dist/web3.min.js" type="text/javascript"></script>
<title>Validator example</title>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1>Validator example</h1>
This validator checks if inputed address is an ERC-721 smart contract and if so checks if its safeTransferFrom function is correctly implemented.
<br/>
We assume that you have Metamask installed and you are on mainnet. It will not cost you any gas to validate any smart contrac, even though these transactions are run using the network.
<br/><br/>
For your convenience here are some smart contract that you can just copy-paste to test:
<br/><br/>
<table class="table">
<thead>
<tr>
<th scope="col">Address</th>
<th scope="col">Description</th>
<th scope="col">Test result</th>
</tr>
</thead>
<tbody>
<tr>
<td>0x06012c8cf97BEaD5deAe237070F9587f8E7A266d</td>
<td>CryptoKitties smart contract. It is pre ERC-721 standard.</td>
<td>Failure</td>
</tr>
<tr>
<td>0xE9e3F9cfc1A64DFca53614a0182CFAD56c10624F</td>
<td>Su Squares smart contract. It is ERC-721 and implements enumerable extensions so all test will start automatically.</td>
<td>Success</td>
</tr>
<tr>
<td>0xf87e31492faf9a91b02ee0deaad50d51d56d5d4d</td>
<td>Decentraland smart contract. It is ERC-721 but does not implement enumerable so you will get a prompt to input a valid token id.</td>
<td>Success</td>
</tr>
</tbody>
</table>
</div>
</div>
<br/>
<div class="row">
<div class="col-sm-12">
<input id="address" placeholder="Input smart contract address" class="form-control form-control-lg">
<input id="token" placeholder="Input valid token" class="form-control form-control-lg invisible">
<button class="m-a btn btn-primary" id="validate">Validate</button>
</div>
</div>
<br/>
<div class="row">
<div class="col-sm-12">
<div class="alert" id="outcome" role="alert"></div>
</div>
</div>
</div>
</body>
<script>
window.addEventListener('load', async () => {
if (window.ethereum) {
window.web3 = new Web3(ethereum);
try {
await ethereum.enable();
} catch (error) {
setAlertLabel('No web3 provider. Install Metamask.', false);
}
}
else if (window.web3) {
window.web3 = new Web3(web3.currentProvider);
}
else {
setAlertLabel('No web3 provider. Install Metamask.', false);
}
$('#validate').click(async function(){
const addr = $('#address').val().trim();
const token = $('#token').val().trim();
if(token !== '') {
const owner = await getTokenOwner(addr, token);
runSafeTransferTests(addr, owner, token);
} else if (await baseValidation(1, addr)) {
setAlertLabel('Is a contract', true);
if (await baseValidation(2, addr) && await baseValidation(3, addr)) {
if (await baseValidation(4, addr)) {
const validId = await getTokenId(addr);
const owner = await getTokenOwner(addr, validId);
runSafeTransferTests(addr, owner, validId);
} else {
$("#token").removeClass('invisible');
setAlertLabel('No enumerable extension. Input a valid token ID.', false);
}
} else {
setAlertLabel('Not a ERC-721 contract.', false);
}
} else {
setAlertLabel('Is not a contract', false);
}
});
});
async function baseValidation(test, addr) {
const abi = [{"inputs":[{"name":"_caseId","type":"uint256"},{"name":"_target","type":"address"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}];
const bytecode = '0x608060405234801561001057600080fd5b506040516040806102bd8339810180604052604081101561003057600080fd5b508051602090910151600182141561005657610051816100a760201b60201c565b6100a0565b816002141561006e57610051816100cc60201b60201c565b8160031415610086576100518161016860201b60201c565b816004141561009e57610051816101eb60201b60201c565bfe5b5050610274565b6100c3816001600160a01b031661026e60201b6100091760201c565b6100c957fe5b50565b604080517f01ffc9a700000000000000000000000000000000000000000000000000000000808252600482015290516000916001600160a01b038416916301ffc9a791602480820192602092909190829003018186803b15801561012f57600080fd5b505afa158015610143573d6000803e3d6000fd5b505050506040513d602081101561015957600080fd5b505190508061016457fe5b5050565b604080517f01ffc9a70000000000000000000000000000000000000000000000000000000081527f80ac58cd00000000000000000000000000000000000000000000000000000000600482015290516000916001600160a01b038416916301ffc9a791602480820192602092909190829003018186803b15801561012f57600080fd5b604080517f01ffc9a70000000000000000000000000000000000000000000000000000000081527f780e9d6300000000000000000000000000000000000000000000000000000000600482015290516000916001600160a01b038416916301ffc9a791602480820192602092909190829003018186803b15801561012f57600080fd5b3b151590565b603b806102826000396000f3fe6080604052600080fd5b3b15159056fea165627a7a723058202cf6c3268162b39aabc886838fdf4803288299d95ab055a65746f04b3cb7334f0029';
return new Promise(async (resolve, reject) => {
try {
const basicValidator = new web3.eth.Contract(abi, {
from: '0xe96D860C8BBB30F6831E6E65d327295B7A0C524f', // default from address
});
await basicValidator.deploy({
data: bytecode,
arguments: [test, addr]
}).estimateGas((err, gas) => {
if (!err) {
resolve(true);
} else if (String(err).includes('gas required exceeds allowance or always failing transaction')) {
resolve(false);
} else {
resolve(false);
}
}).catch((e) => resolve(false));
} catch(e) {
resolve(false);
}
});
}
async function getTokenId(addr) {
const abi = [{"constant":true,"inputs":[{"name":"_index","type":"uint256"}],"name":"tokenByIndex","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];
const erc721Enumerable = new web3.eth.Contract(abi, addr, {
from: '0xe96D860C8BBB30F6831E6E65d327295B7A0C524f', // default from address
});
return new Promise(async (resolve, reject) => {
await erc721Enumerable.methods.tokenByIndex(0).call()
.then((id) => resolve(id))
.catch((e) => resolve(null));
});
}
async function getTokenOwner(addr, id) {
const abi = [{"constant":true,"inputs":[{"name":"_tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"}];
const erc721 = new web3.eth.Contract(abi, addr, {
from: '0xe96D860C8BBB30F6831E6E65d327295B7A0C524f', // default from address
});
return new Promise(async (resolve, reject) => {
await erc721.methods.ownerOf(id).call()
.then((id) => resolve(id))
.catch((e) => resolve(null));
});
}
async function checkSafeTransfer(addr, owner, id, test) {
let receiverContract = '0x3324ee50d08af84ea6febb53ed8905067e9118a9';
if (test === 2)
{
receiverContract = '0x7b0a8a3f36fe03e6bcab71cc51d04d88df92f621';
} else if (test === 3) {
receiverContract = '0xf4000c1e8f216b4ad89fb3897cbbbd59ce642e21';
}
const abi = [{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_tokenId","type":"uint256"}],"name":"safeTransferFrom","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_tokenId","type":"uint256"},{"name":"_data","type":"bytes"}],"name":"safeTransferFrom","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}];
const erc721 = new web3.eth.Contract(abi, addr);
return new Promise(async (resolve, reject) => {
if(test === 2)
{
await erc721.methods.safeTransferFrom(owner, receiverContract, id, '0x66666666').estimateGas({ from: owner }, (err, gas) => {
if (!err) {
resolve(true);
} else if (String(err).includes('gas required exceeds allowance or always failing transaction')) {
resolve(false);
} else {
resolve(false);
}
}).catch((e) => resolve(false));
} else {
await erc721.methods.safeTransferFrom(owner, receiverContract, id).estimateGas({ from: owner }, (err, gas) => {
if (!err) {
resolve(true);
} else if (String(err).includes('gas required exceeds allowance or always failing transaction')) {
resolve(false);
} else {
resolve(false);
}
}).catch((e) => resolve(false));
}
});
}
async function runSafeTransferTests(addr, owner, id) {
setAlertLabel('Checking safe transfer', true);
if (await checkSafeTransfer(addr, owner, id, 1) &&
await checkSafeTransfer(addr, owner, id, 2) &&
!await checkSafeTransfer(addr, owner, id, 3))
{
setAlertLabel('Safe transfer test successful.', true);
} else {
setAlertLabel('Safe transfer test failed.', false);
}
}
function setAlertLabel(text, success) {
if (success) {
$("#outcome").removeClass('alert-danger');
$("#outcome").addClass('alert-success');
} else {
$("#outcome").removeClass('alert-success');
$("#outcome").addClass('alert-danger');
}
$("#outcome").html(text);
}
</script>
</html>
You can’t perform that action at this time.