Skip to content

Commit

Permalink
Merge pull request #199 from vmware-samples/revert-198-ramki-new-feat…
Browse files Browse the repository at this point in the history
…ures

Revert "Features: 1) Blockchain URL and contract re-deploy are configurable through config.json 2) Web3 wallet (Metamask etc.) is not mandatory for blockchain reads. All blockchain reads use json rpc provider with the specified blockchain url."
  • Loading branch information
ramkri123 authored Apr 23, 2023
2 parents 14c057a + 202af63 commit 9d183de
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,16 @@ const fs = require("fs");
const hre = require("hardhat");

async function main() {
let config = await fs.readFileSync('./src/assets/config.json');
config = JSON.parse(config);

if (config['DEFAULT_NFT_CONTRACT_ADDRESS'] == '' || config['REDEPLOY_CONTRACT'] == 'true') {
const DigitalArt = await hre.ethers.getContractFactory("DigitalArt");
const digitalArt = await DigitalArt.deploy();
const DigitalArt = await hre.ethers.getContractFactory("DigitalArt");
const digitalArt = await DigitalArt.deploy();

await digitalArt.deployed();
console.log(`DigitalArt Smart Contract deployed to ${digitalArt.address}`);
await digitalArt.deployed();
console.log(`DigitalArt Smart Contract deployed to ${digitalArt.address}`);

config['DEFAULT_NFT_CONTRACT_ADDRESS'] = digitalArt.address;
await fs.writeFileSync('./src/assets/config.json', JSON.stringify(config));
} else {
console.log("DigitalArt Smart Contract already deployed at " + config['DEFAULT_NFT_CONTRACT_ADDRESS']);
}
let config = await fs.readFileSync('./src/assets/config.json');
config = JSON.parse(config);
config['DEFAULT_NFT_CONTRACT_ADDRESS'] = digitalArt.address;
await fs.writeFileSync('./src/assets/config.json', JSON.stringify(config));
}

// We recommend this pattern to be able to use async/await everywhere
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const baseData = {
contract: null,
contractAddressOverride: Config.DEFAULT_NFT_CONTRACT_ADDRESS,
totalArtsSupply: null,
jsonrpcContract: null,
};

@Injectable({ providedIn: 'root' })
Expand Down Expand Up @@ -62,12 +61,10 @@ export class DigitalArtsService {
if (baseData.contract) { return baseData.contract; }

baseData.contract = new ethers.Contract(this.mainContractAddress, DigitalArtAbi.abi, this.ethService.web3Provider);
baseData.jsonrpcContract = new ethers.Contract(this.mainContractAddress, DigitalArtAbi.abi, this.ethService.jsonrpcProvider);

try{
baseData.totalArtsSupply = await baseData.jsonrpcContract.totalSupply();
baseData.totalArtsSupply = await baseData.contract.totalSupply();
console.log("Contract deployed successfully at " + baseData.contract.address);
console.log("totalArtsSupply " + baseData.totalArtsSupply);
this.mainContractStatus = 'CONNECTED';

this.mainContract = new ethers.Contract(baseData.contract.address, DigitalArtAbi.abi, this.ethService.web3Provider) as DigitalArtContract;
Expand All @@ -92,7 +89,7 @@ export class DigitalArtsService {
}
baseData.contract = new ethers.Contract(newAddress, DigitalArtAbi.abi, this.ethService.web3Provider);
try {
await baseData.jsonrpcContract.totalSupply();
await baseData.contract.totalSupply();
console.log(`NFT Contract Address Changed: ${newAddress}`);
window.location.reload();
} catch (e) {
Expand All @@ -113,9 +110,7 @@ export class DigitalArtsService {
//sets up inits
async waitForIt() {
if (this.initPromise) { return this.initPromise; }
if (this.ethService.jsonrpcOnly == false) {
await this.ethService.waitForEthWindow();
}
await this.ethService.waitForEthWindow();
await this.initializeDigitalArtsContract();
const prom = new Promise<boolean>(async resolve => {
if (this.ready) { return resolve(true); }
Expand Down Expand Up @@ -154,14 +149,14 @@ export class DigitalArtsService {
//getter
async fetchArtByTokenId(index: string) {
await this.waitForIt();
const artObjData = await baseData.jsonrpcContract.DigitalArtArr(index) as any as DigitalArt;
const artObjData = await this.mainContract.DigitalArtArr(index) as any as DigitalArt;
const artObj: DigitalArt = {
title: artObjData.title,
artistName: artObjData.artistName,
image: artObjData.image,
tokId: artObjData.tokId,
};
artObj.ownerHistory = await baseData.jsonrpcContract.getOwnerToken(index);
artObj.ownerHistory = await this.contract.getOwnerToken(index);
artObj.effectiveImageUrl = artObj.image.startsWith('http://localhost') ?
artObj.image.replace(/http\:\/\/localhost\.com/g, '')
: artObj.image;
Expand All @@ -186,7 +181,6 @@ export class DigitalArtsService {

//standard mint function
async mint(title: string, artist: string, imageUrl: string) {
if (this.ethService.jsonrpcOnly) { return; }
await this.waitForIt();
const signer = await this.ethService.web3Provider.getSigner(this.ethService.currentAccount);
const newContract = await this.contract.connect(signer);
Expand All @@ -207,7 +201,6 @@ export class DigitalArtsService {

//for mock art minting
async testMint(title: string, artist: string, imageUrl: string){
if (this.ethService.jsonrpcOnly) { return; }
const newContract = await this.contract.connect(this.ethService.testAccount);
let transaction;
try {
Expand All @@ -221,7 +214,6 @@ export class DigitalArtsService {

//standard transfer function
async transfer(tokenId: string, to: string) {
if (this.ethService.jsonrpcOnly) { return; }
await this.waitForIt();
const signer = await this.ethService.web3Provider.getSigner(this.ethService.currentAccount);
const newContract = await this.contract.connect(signer);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Injectable } from '@angular/core';
import { ethers } from 'ethers';
import Config from 'src/assets/config.json';

declare var window: any;
let web3Provider: any;
Expand All @@ -18,8 +17,6 @@ export class EthereumService {
currentSigner: any;
accounts: string[] = [];
testAccount;
jsonrpcProvider: any;
jsonrpcOnly: boolean = false;

constructor() {
this.initialize();
Expand All @@ -46,26 +43,15 @@ export class EthereumService {
} else if(window.web3) { // if deprecated window.web3 provided instead
this.setWeb3Provider(new ethers.providers.Web3Provider(window.web3));
} else {
//throw new Error('Non-valid/Ethereum browser extension detected. Use MetaMask!');
this.jsonrpcOnly = true;
throw new Error('Non-valid/Ethereum browser extension detected. Use MetaMask!');
}

this.jsonrpcProvider = new ethers.providers.JsonRpcProvider(Config.BLOCKCHAIN_URL);
this.jsonrpcProvider.getBlockNumber().then((result) => {
console.log("Current block number: " + result);
});

// account handling
if (this.jsonrpcOnly) {
this.currentAccount = '0x5a51Ed9DD3bAC1cdf3Ae4e01593e95d81337dfDD';
console.log("jsonrpc current account: " + this.currentAccount);
} else {
try {
await window.ethereum.request({ method: 'eth_requestAccounts' });
await this.getAccounts();
} catch (e) {
console.log(e);
}
try {
await window.ethereum.request({ method: 'eth_requestAccounts' });
await this.getAccounts();
} catch (e) {
console.log(e);
}
}

Expand All @@ -81,7 +67,6 @@ export class EthereumService {
async getAccounts() {
this.accounts = await web3Provider.listAccounts();
this.currentAccount = this.accounts[0];
console.log("web3 current account: " + this.currentAccount);
// await this.getAccountBalance(this.currentAccount);
this.currentSigner = await this.web3Provider.getSigner(this.currentAccount);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"DEFAULT_NFT_CONTRACT_ADDRESS":"0x0B6Ab24A032D5eACA954C2f4457618bDD12e9969","REDEPLOY_CONTRACT":"false","BLOCKCHAIN_URL":"http://54.213.130.0:8545"}
{"DEFAULT_NFT_CONTRACT_ADDRESS":""}

0 comments on commit 9d183de

Please sign in to comment.