Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ERC721 as new project #101

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright 2024 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.console.project;

public class Erc721ProjectCreatorConfig extends ProjectCreatorConfig {
private final String tokenName;
private final String tokenSymbol;

public Erc721ProjectCreatorConfig(
String projectName,
String packageName,
String outputDir,
Boolean withJar,
Boolean withTests,
String tokenName,
String tokenSymbol) {
super(projectName, packageName, outputDir, withJar, withTests);
this.tokenName = tokenName;
this.tokenSymbol = tokenSymbol;
}

public String getTokenName() {
return tokenName;
}

public String getTokenSymbol() {
return tokenSymbol;
}
}
18 changes: 17 additions & 1 deletion src/main/java/org/web3j/console/project/NewProjectCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.web3j.console.Web3jVersionProvider;
import org.web3j.console.project.java.Erc20JavaProjectCreator;
import org.web3j.console.project.java.Erc721JavaProjectCreator;
import org.web3j.console.project.java.Erc777JavaProjectCreator;
import org.web3j.console.project.java.JavaProjectCreatorRunner;
import org.web3j.console.project.kotlin.KotlinProjectCreatorRunner;
Expand All @@ -35,7 +36,7 @@
footer = "Web3j CLI is licensed under the Apache License 2.0")
public class NewProjectCommand extends AbstractProjectCommand implements Runnable {

@Parameters(description = "HelloWorld, ERC20, ERC777", defaultValue = "HelloWorld")
@Parameters(description = "HelloWorld, ERC20, ERC777, ERC721", defaultValue = "HelloWorld")
TemplateType templateType = TemplateType.HelloWorld;

@Override
Expand All @@ -60,6 +61,9 @@ public void run() {
case ERC777:
System.out.println("Generating ERC777 Kotlin project is currently unsupported");
break;
case ERC721:
System.out.println("Generating ERC721 Kotlin project is currently unsupported");
break;
}
} else {
switch (templateType) {
Expand Down Expand Up @@ -93,6 +97,18 @@ public void run() {
interactiveOptions.getTokenInitialSupply("1000000000")))
.run();
break;
case ERC721:
new Erc721JavaProjectCreator(
new Erc721ProjectCreatorConfig(
projectOptions.projectName,
projectOptions.packageName,
projectOptions.outputDir,
projectOptions.generateJar,
projectOptions.generateTests,
interactiveOptions.getTokenName("ERC721"),
interactiveOptions.getTokenSymbol("erc721")))
.run();
break;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/web3j/console/project/TemplateType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
public enum TemplateType {
HelloWorld,
ERC20,
ERC777
ERC777,
ERC721
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.console.project.java;

import org.web3j.console.openapi.project.erc777.CopyUtils;
import org.web3j.console.openapi.utils.PrettyPrinter;
import org.web3j.console.openapi.utils.SimpleFileLogger;
import org.web3j.console.project.Erc721ProjectCreatorConfig;
import org.web3j.console.project.ProjectRunner;
import org.web3j.console.project.templates.java.erc721.Erc721JavaTemplateBuilder;
import org.web3j.console.project.utils.ProgressCounter;
import org.web3j.console.project.utils.ProjectCreationUtils;

public class Erc721JavaProjectCreator extends ProjectRunner {

Erc721ProjectCreatorConfig erc721ProjectCreatorConfig;

public Erc721JavaProjectCreator(Erc721ProjectCreatorConfig erc721ProjectCreatorConfig) {
super(erc721ProjectCreatorConfig);
this.erc721ProjectCreatorConfig = erc721ProjectCreatorConfig;
}

@Override
protected void createProject() {
ProgressCounter progressCounter = new ProgressCounter(true);
progressCounter.processing(
"Creating and building ERC721 project ... Subsequent builds will be faster");
JavaProjectStructure projectStructure =
new JavaProjectStructure(outputDir, packageName, projectName);
ProjectCreationUtils.generateTopLevelDirectories(projectStructure);
try {
new Erc721JavaTemplateBuilder()
.withTokenName(erc721ProjectCreatorConfig.getTokenName())
.withTokenSymbol(erc721ProjectCreatorConfig.getTokenSymbol())
.withProjectNameReplacement(projectName)
.withPackageNameReplacement(packageName)
.withGradleBatScript("project/gradlew.bat.template")
.withGradleScript("project/gradlew.template")
.withGradleSettings("project/settings.gradle.template")
.withWrapperGradleSettings("project/gradlew-wrapper.properties.template")
.withGradlewWrapperJar("gradle-wrapper.jar")
.withGradleBuild("project/erc721/build.gradleErc721.template")
.withMainJavaClass("project/erc721/JavaErc721.template")
.build()
.generateFiles(projectStructure);

CopyUtils.INSTANCE.copyFromResources(
"contracts/ERC721Token.sol", projectStructure.getSolidityPath());
buildProject(projectStructure, progressCounter);
} catch (Exception e) {
e.printStackTrace(SimpleFileLogger.INSTANCE.getFilePrintStream());
PrettyPrinter.INSTANCE.onFailed();
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright 2024 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.console.project.templates.java.erc721;

import org.web3j.console.project.templates.TemplateBuilder;

public class Erc721JavaTemplateBuilder implements TemplateBuilder {

private String tokenName;
private String tokenSymbol;
private String[] defaultOperators;

protected String mainJavaClass;
protected String gradleBuild;
protected String gradleSettings;
protected String gradlewWrapperSettings;
protected String gradlewBatScript;
protected String gradlewScript;
protected String gradlewWrapperJar;
protected String packageNameReplacement;
protected String projectNameReplacement;

public Erc721JavaTemplateBuilder withMainJavaClass(String mainJavaClass) {
this.mainJavaClass = mainJavaClass;
return this;
}

public Erc721JavaTemplateBuilder withGradleBuild(String gradleBuild) {
this.gradleBuild = gradleBuild;
return this;
}

public Erc721JavaTemplateBuilder withGradleSettings(String gradleSettings) {
this.gradleSettings = gradleSettings;
return this;
}

public Erc721JavaTemplateBuilder withWrapperGradleSettings(String gradlewWrapperSettings) {
this.gradlewWrapperSettings = gradlewWrapperSettings;
return this;
}

public Erc721JavaTemplateBuilder withGradleBatScript(String gradlewBatScript) {
this.gradlewBatScript = gradlewBatScript;
return this;
}

public Erc721JavaTemplateBuilder withGradleScript(String gradlewScript) {
this.gradlewScript = gradlewScript;
return this;
}

public Erc721JavaTemplateBuilder withGradlewWrapperJar(String gradlewWrapperJar) {
this.gradlewWrapperJar = gradlewWrapperJar;
return this;
}

public Erc721JavaTemplateBuilder withPackageNameReplacement(String packageNameReplacement) {
this.packageNameReplacement = packageNameReplacement;
return this;
}

public Erc721JavaTemplateBuilder withProjectNameReplacement(String projectNameReplacement) {
this.projectNameReplacement = projectNameReplacement;
return this;
}

public Erc721JavaTemplateBuilder withTokenName(String tokenName) {
this.tokenName = tokenName;
return this;
}

public Erc721JavaTemplateBuilder withTokenSymbol(String tokenSymbol) {
this.tokenSymbol = tokenSymbol;
return this;
}

@Override
public Erc721JavaTemplateProvider build() {
return new Erc721JavaTemplateProvider(
mainJavaClass,
null,
null,
gradleBuild,
gradleSettings,
gradlewWrapperSettings,
gradlewBatScript,
gradlewScript,
gradlewWrapperJar,
packageNameReplacement,
projectNameReplacement,
tokenName,
tokenSymbol);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2024 Web3 Labs Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package org.web3j.console.project.templates.java.erc721;

import java.io.IOException;

import org.web3j.console.project.templates.java.JavaTemplateProvider;

public class Erc721JavaTemplateProvider extends JavaTemplateProvider {

private final String tokenName;
private final String tokenSymbol;

protected Erc721JavaTemplateProvider(
String mainJavaClass,
String solidityContract,
String pathToSolidityFolder,
String gradleBuild,
String gradleSettings,
String gradlewWrapperSettings,
String gradlewBatScript,
String gradlewScript,
String gradlewJar,
String packageNameReplacement,
String projectNameReplacement,
String tokenName,
String tokenSymbol) {
super(
mainJavaClass,
solidityContract,
pathToSolidityFolder,
gradleBuild,
gradleSettings,
gradlewWrapperSettings,
gradlewBatScript,
gradlewScript,
gradlewJar,
packageNameReplacement,
projectNameReplacement,
null);
this.tokenName = tokenName;
this.tokenSymbol = tokenSymbol;
}

@Override
public String loadMainJavaClass() throws IOException {
return super.loadMainJavaClass()
.replaceAll("<NAME>", tokenName)
.replaceAll("<SYMBOL>", tokenSymbol);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import java.io.File
)
class NewOpenApiCommand : AbstractOpenApiCommand() {

@Parameters(description = ["HelloWorld, ERC20, ERC777"], defaultValue = "HelloWorld")
@Parameters(description = ["HelloWorld, ERC20, ERC777, ERC721"], defaultValue = "HelloWorld")
var templateType = TemplateType.HelloWorld

override fun generate(projectFolder: File) {
Expand Down Expand Up @@ -98,6 +98,24 @@ class NewOpenApiCommand : AbstractOpenApiCommand() {
projectStructure.solidityPath)
buildProject(projectStructure.projectRoot, withSwaggerUi = false)
}

TemplateType.ERC721 -> {
val projectStructure = createProjectStructure(
openApiTemplateProvider = OpenApiTemplateProvider(
solidityContract = "",
pathToSolidityFolder = "",
gradleBuild = "project/erc721/build.gradleOpenApiErc721.template",
packageName = projectOptions.packageName,
projectName = projectOptions.projectName,
contextPath = contextPath,
addressLength = (projectOptions.addressLength * 8).toString()
), outputDir = projectOptions.outputDir
)
CopyUtils.copyFromResources(
"contracts/ERC721Token.sol",
projectStructure.solidityPath)
buildProject(projectStructure.projectRoot, withSwaggerUi = false)
}
}

progressCounter.setLoading(false)
Expand Down
23 changes: 23 additions & 0 deletions src/main/resources/contracts/ERC721Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";

contract ERC721Token is ERC721URIStorage {
uint256 private _nextTokenId;

constructor(
string memory name,
string memory symbol) public ERC721(name, symbol) {}

function awardItem(address player, string memory tokenURI)
public
returns (uint256)
{
uint256 tokenId = _nextTokenId++;
_mint(player, tokenId);
_setTokenURI(tokenId, tokenURI);

return tokenId;
}
}
Loading