Skip to content

Commit

Permalink
modalNewContract ByAddress wip
Browse files Browse the repository at this point in the history
  • Loading branch information
bokkypoobah committed Aug 31, 2024
1 parent 1d35858 commit ec0b163
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 4 deletions.
18 changes: 18 additions & 0 deletions docs/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,21 @@ function handleErrors(response) {
}
return response;
}

const imageUrlToBase64 = async url => {
const response = await fetch(url);
if (response.ok) {
const blob = await response.blob();
return new Promise((onSuccess, onError) => {
try {
const reader = new FileReader() ;
reader.onload = function(){ onSuccess(this.result) } ;
reader.readAsDataURL(blob) ;
} catch(e) {
onError(e);
}
});
} else {
return null;
}
};
54 changes: 50 additions & 4 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,17 @@
</b-tabs>

<b-form-group label="Address:" label-for="modalnewcontract-address" label-size="sm" label-cols-sm="3" label-align-sm="right" :state="!modalNewContract.address || validAddress(modalNewContract.address)" :invalid-feedback="'Invalid address'" class="mx-0 my-1 p-0">
<b-form-input size="sm" id="modalnewcontract-address" v-model.trim="modalNewContract.address" debounce="600" placeholder="0x1234...6789" class="w-75"></b-form-input>
<b-input-group size="sm" class="w-75">
<b-form-input size="sm" id="modalnewcontract-address" v-model.trim="modalNewContract.address" debounce="600" placeholder="0x1234...6789"></b-form-input>
<b-input-group-append>
<b-dropdown size="sm" id="dropdown-left" text="" variant="link" v-b-popover.hover.ds500="'Some sample token contracts'" class="m-0 ml-1 p-0">
<b-dropdown-item v-if="sampleContracts.length == 0" disabled>No sample contracts on this network</b-dropdown-item>
<div v-for="(item, index) of sampleContracts" v-bind:key="index">
<b-dropdown-item @click="modalNewContract.address = item.contract;">{{ index }}. {{ 'ERC-' + item.type }} {{ item.contract.substring(0, 8) + '...' + item.contract.slice(-6) + ' ' + item.name }}</b-dropdown-item>
</div>
</b-dropdown>
</b-input-group-append>
</b-input-group>
</b-form-group>
<b-form-group label="" label-size="sm" label-cols-sm="3" label-align-sm="right" :state="!modalNewContract.error" :invalid-feedback="modalNewContract.error" class="mx-0 my-1 p-0">
<b-button size="sm" :disabled="sync.completed != null || !validAddress(modalNewContract.address)" @click="modalNewContractByAddressCheck" variant="primary">Check</b-button>
Expand All @@ -115,6 +125,11 @@
<b-form-group v-if="modalNewContract.totalSupply" label="Total Supply:" label-for="modalnewcontract-totalsupply" label-size="sm" label-cols-sm="3" label-align-sm="right" class="mx-0 my-1 p-0">
<b-form-input size="sm" readonly id="modalnewcontract-totalsupply" :value="formatDecimals(modalNewContract.totalSupply, modalNewContract.decimals)" class="w-25"></b-form-input>
</b-form-group>
<b-form-group v-if="modalNewContract.image" label="Image:" label-for="modalnewcontract-image" label-size="sm" label-cols-sm="3" label-align-sm="right" class="mx-0 my-1 p-0">
<!-- <b-form-input size="sm" readonly id="modalnewcontract-image" :value="formatDecimals(modalNewContract.totalSupply, modalNewContract.decimals)" class="w-25"></b-form-input> -->
<b-img button rounded fluid size="15rem" :src="modalNewContract.image" class="m-2" style="width: 100px;">
</b-img>
</b-form-group>

<div v-if="modalNewContract.tabIndex != 0" class="d-flex flex-wrap m-0 p-0">
<div class="mt-0 pr-1" style="width: 10.0rem;">
Expand Down Expand Up @@ -1076,12 +1091,15 @@ <h6 class="mt-4">Troubleshooting</h6>
show: false,
tabIndex: 0,
// TODO address: null,
address: "0x2823589Ae095D99bD64dEeA80B4690313e2fB519", // WEENUS
address: "0xA35923162C49cF95e6BF26623385eb431ad920D3", // ERC-20 TURBO
// address: "0x8FA600364B93C53e0c71C7A33d2adE21f4351da3", // ERC-721 LChads
// address: "0xB32979486938AA9694BFC898f35DBED459F44424", // ERC-1155 Nyan Cats
contractType: null,
symbol: null,
name: null,
decimals: null,
totalSupply: null,
image: null,
error: null,
},
modalNonFungible: {
Expand Down Expand Up @@ -1970,7 +1988,7 @@ <h6 class="mt-4">Troubleshooting</h6>
const erc721 = new ethers.Contract(contractAddress, ERC721ABI, provider);
const erc1155 = new ethers.Contract(contractAddress, ERC1155ABI, provider);
let [ error, supportsERC721, supportsERC1155 ] = [ null, false, false ];
let [ contractType, symbol, name, decimals, totalSupply ] = [ null, null, null, null, null ];
let [ contractType, symbol, name, decimals, totalSupply, image ] = [ null, null, null, null, null, null ];
const code = await provider.getCode(contractAddress);
// console.log(now() + " INFO getContractInfo - code: " + code);
if (!code || code.length <= 2) {
Expand Down Expand Up @@ -2016,7 +2034,34 @@ <h6 class="mt-4">Troubleshooting</h6>
}
}
}
return { contractType, symbol, name, decimals, totalSupply, error };
if (contractType == 20) {
const erc20Logos = this.network.erc20Logos || [];
console.log(now() + " INFO getContractInfo - erc20Logos: " + JSON.stringify(erc20Logos, null, 2));
for (let i = 0; i < erc20Logos.length && !image; i++) {
const url = erc20Logos[i].replace(/\${contract}/, contractAddress);
console.log(now() + " INFO getContractInfo - url: " + url);
image = await imageUrlToBase64(url);
}
console.log(now() + " INFO getContractInfo - image: " + image);

} else if (contractType == 721 || contractType == 1155) {
const reservoirPrefix = this.network && this.network.reservoir || null;
console.log(now() + " INFO getContractInfo - reservoirPrefix: " + reservoirPrefix);
if (reservoirPrefix) {
let url = reservoirPrefix + "collections/v7?id=" + contractAddress;
console.log(now() + " INFO getContractInfo - url: " + url);
const data = await fetch(url).then(response => response.json());
console.log(now() + " INFO getContractInfo - data: " + JSON.stringify(data, null, 2));
if (data.collections && data.collections.length > 0) {
const collection = data.collections[0];
console.log(now() + " INFO getContractInfo - collection: " + JSON.stringify(collection, null, 2));
symbol = collection.symbol;
name = collection.name;
image = collection.image;
}
}
}
return { contractType, symbol, name, decimals, totalSupply, image, error };
},

async modalNewContractByAddressCheck() {
Expand All @@ -2027,6 +2072,7 @@ <h6 class="mt-4">Troubleshooting</h6>
this.modalNewContract.name = info.name;
this.modalNewContract.decimals = info.decimals;
this.modalNewContract.totalSupply = info.totalSupply;
this.modalNewContract.image = info.image;
this.modalNewContract.error = info.error;
console.log(now() + " INFO modalNewContractByAddressCheck END - this.modalNewContract: " + JSON.stringify(this.modalNewContract, null, 2));
},
Expand Down

0 comments on commit ec0b163

Please sign in to comment.