Skip to content

Commit

Permalink
basic 81 task: web3j erc20
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanBizzy authored and EthanBizzy committed Apr 6, 2024
1 parent f55bd64 commit 63d4b67
Show file tree
Hide file tree
Showing 10 changed files with 641 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ DAPP架构请参考文章--[从架构维度看Web2.0与Web3.0应用之别](https
74. [erc20-meta-token](https://github.com/0xsequence/erc20-meta-token)
75. [golang-dapp](basic/75-golang-dapp)
76. [Push Protocol](https://docs.epns.io/developers)
77. [orbit](basic/77-orbit)
78. [wallet connect](basic/78-wallet-connect)
79. [hardhat-foundry](basic/79-hardhat-foundry)
80. [Circle CCTP](basic/80-crossChainTransfer)
81. [Web3j-erc20](./basic/81-web3j-erc20/README-cn.md)


## 项目任务

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ You are welcome to PR improvements to existing tutorial projects or to create mo
77. [orbit](basic/77-orbit)
78. [wallet connect](basic/78-wallet-connect)
79. [hardhat-foundry](basic/79-hardhat-foundry)
80. [Circle CCTP](basic/80-circle-cctp)
80. [Circle CCTP](basic/80-crossChainTransfer)
81. [Web3j-erc20](./basic/81-web3j-erc20/README-cn.md)

Welcome to submit pull request, [Add a new basic task or update the above task](https://github.com/Dapp-Learning-DAO/Dapp-Learning/issues/new)

Expand Down
352 changes: 352 additions & 0 deletions basic/81-web3j-erc20/README-cn.md

Large diffs are not rendered by default.

100 changes: 100 additions & 0 deletions basic/81-web3j-erc20/Web3App/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
plugins {
id 'java'
id 'org.jetbrains.kotlin.jvm' version '1.8.10'
id 'application'
id "com.github.johnrengelman.shadow" version "5.2.0"
id 'org.web3j' version '4.11.2'
}

group 'org.web3j'
version '0.1.0'

sourceCompatibility = 17

repositories {
mavenLocal()
mavenCentral()
maven { url "https://hyperledger.jfrog.io/hyperledger/besu-maven" }
maven { url "https://artifacts.consensys.net/public/maven/maven/" }
maven { url "https://splunk.jfrog.io/splunk/ext-releases-local" }
}

web3j {
generatedPackageName = 'org.web3j.generated.contracts'
includedContracts = ['ERC20Token']
}

node {
nodeProjectDir.set(file("$projectDir"))
}

ext {
web3jVersion = '4.11.2'
logbackVersion = '1.4.14'
klaxonVersion = '5.5'
besuPluginVersion = '24.1.1'
besuInternalVersion = '24.1.1'
besuInternalCryptoVersion = '23.1.3'
besuCryptoDepVersion = '0.8.3'
}

dependencies {
implementation "org.web3j:core:$web3jVersion",
"ch.qos.logback:logback-core:$logbackVersion",
"ch.qos.logback:logback-classic:$logbackVersion",
"com.beust:klaxon:$klaxonVersion"
implementation "org.web3j:web3j-unit:$web3jVersion"
implementation "org.web3j:web3j-evm:$web3jVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.3.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'

implementation "org.hyperledger.besu:plugin-api:$besuPluginVersion"
implementation "org.hyperledger.besu.internal:besu:$besuInternalVersion"
implementation "org.hyperledger.besu.internal:api:$besuInternalVersion"
implementation "org.hyperledger.besu:evm:$besuPluginVersion"
implementation "org.hyperledger.besu.internal:config:$besuInternalVersion"
implementation "org.hyperledger.besu.internal:core:$besuInternalVersion"
implementation "org.hyperledger.besu.internal:crypto:$besuInternalCryptoVersion"
implementation "org.hyperledger.besu.internal:rlp:$besuInternalVersion"
implementation "org.hyperledger.besu.internal:kvstore:$besuInternalVersion"
implementation "org.hyperledger.besu.internal:metrics-core:$besuInternalVersion"
implementation "org.hyperledger.besu.internal:trie:$besuInternalVersion"
implementation "org.hyperledger.besu.internal:util:$besuInternalVersion"
implementation "org.hyperledger.besu:bls12-381:$besuCryptoDepVersion"
implementation "org.hyperledger.besu:secp256k1:$besuCryptoDepVersion"
}

jar {
manifest {
attributes(
'Main-Class': 'org.web3j.Web3App',
'Multi-Release':'true'
)
}
}

application {
mainClassName = 'org.web3j.Web3App'
}

test {
useJUnitPlatform()
}

compileKotlin {
kotlinOptions.jvmTarget = "17"
}

compileTestKotlin {
kotlinOptions.jvmTarget = "17"
}

solidity {
evmVersion = 'constantinople'
}

shadowJar {
zip64 = true
}

10 changes: 10 additions & 0 deletions basic/81-web3j-erc20/Web3App/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pluginManagement {
repositories {
mavenLocal()
gradlePluginPortal()
maven { url "https://hyperledger.jfrog.io/hyperledger/besu-maven" }
maven { url "https://artifacts.consensys.net/public/maven/maven/" }
maven { url "https://splunk.jfrog.io/splunk/ext-releases-local" }
}
}
rootProject.name = 'Web3App';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.web3j;

import org.web3j.crypto.Credentials;
import org.web3j.generated.contracts.ERC20Token;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.DefaultGasProvider;

import java.math.BigInteger;

public class Erc20Query {

private static final String nodeUrl = System.getenv().getOrDefault("WEB3J_NODE_URL", "<node_url>");
private static final String pk1 = System.getenv().getOrDefault("PK_ACCT1", "<private_key>");

private static final String erc20Addr = System.getenv().getOrDefault("ERC20_ADDR", "0x70cbbb4f4f51a392a68aaffc3aae4fda6eb89081");


public static void main(String[] args) throws Exception {
Web3j web3j = Web3j.build(new HttpService(nodeUrl));
Credentials credentials = Credentials.create(pk1);
ERC20Token eRC20Token= ERC20Token.load(erc20Addr, web3j, credentials, new DefaultGasProvider());
if (eRC20Token.isValid()) {
String name = eRC20Token.name().send();
System.out.println("token name is " + name);
String symbol = eRC20Token.symbol().send();
System.out.println("token symbol is " + symbol);
BigInteger totalSupply = eRC20Token.totalSupply().send();
System.out.println("total supply is " + totalSupply);
}
web3j.shutdown();
}
}
32 changes: 32 additions & 0 deletions basic/81-web3j-erc20/Web3App/src/main/java/org/web3j/Web3App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.web3j;

import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.gas.DefaultGasProvider;

import java.math.BigInteger;

import org.web3j.generated.contracts.ERC20Token;

public class Web3App {

// ERC20 token deployment parameters
private static final String NAME = "byd";
private static final String SYMBOL = "byd";
private static final BigInteger INITIAL_SUPPLY = new BigInteger("1000000000");
private static final String nodeUrl = System.getenv().getOrDefault("WEB3J_NODE_URL", "<node_url>");
private static final String pk1 = System.getenv().getOrDefault("PK_ACCT1", "<private_key>");


public static void main(String[] args) throws Exception {
System.out.println("nodeUrl is: " + nodeUrl);
System.out.println("private key is: [" + pk1 +"]");
Credentials credentials = Credentials.create(pk1);
Web3j web3j = Web3j.build(new HttpService(nodeUrl));
System.out.println("Deploying ERC20 contract ...");
ERC20Token erc20Token = ERC20Token.deploy(web3j, credentials, new DefaultGasProvider(), NAME, SYMBOL, INITIAL_SUPPLY).send();
System.out.println("Contract address: " + erc20Token.getContractAddress());
}
}
23 changes: 23 additions & 0 deletions basic/81-web3j-erc20/Web3App/src/main/solidity/ERC20Token.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/**
* @title ERC20Token
* @dev Simple ERC20 Token based on OpenZeppelin implementation, where all tokens are pre-assigned to the creator.
* Note they can later distribute these tokens as they wish using `transfer` and other
* `ERC20`.
*/
contract ERC20Token is ERC20 {

/**
* @dev Constructor that gives msg.sender all of existing tokens.
*/
constructor (
string memory name,
string memory symbol,
uint256 initialSupply) public ERC20(name, symbol) {
_mint(msg.sender, initialSupply);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package org.web3j.generated.contracts;

import java.lang.Exception;
import java.lang.String;
import java.math.BigInteger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.web3j.EVMTest;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.TransactionManager;
import org.web3j.tx.gas.ContractGasProvider;

@EVMTest
class ERC20TokenTest {
private static ERC20Token eRC20Token;

@Test
public void transferFrom() throws Exception {
TransactionReceipt transactionReceiptVar = eRC20Token.transferFrom("REPLACE_ME", "REPLACE_ME", BigInteger.ONE).send();
Assertions.assertTrue(transactionReceiptVar.isStatusOK());
}

@Test
public void name() throws Exception {
String stringVar = eRC20Token.name().send();
Assertions.assertEquals("REPLACE_ME", stringVar);
}

@Test
public void transfer() throws Exception {
TransactionReceipt transactionReceiptVar = eRC20Token.transfer("REPLACE_ME", BigInteger.ONE).send();
Assertions.assertTrue(transactionReceiptVar.isStatusOK());
}

@Test
public void symbol() throws Exception {
String stringVar = eRC20Token.symbol().send();
Assertions.assertEquals("REPLACE_ME", stringVar);
}

@Test
public void totalSupply() throws Exception {
BigInteger bigIntegerVar = eRC20Token.totalSupply().send();
Assertions.assertEquals(BigInteger.ONE, bigIntegerVar);
}

@Test
public void getDeploymentBinary() throws Exception {
String stringVar = eRC20Token.getDeploymentBinary().send();
Assertions.assertEquals("REPLACE_ME", stringVar);
}

@BeforeAll
static void deploy(Web3j web3j, TransactionManager transactionManager, ContractGasProvider contractGasProvider) throws Exception {
eRC20Token = ERC20Token.deploy(web3j, transactionManager, contractGasProvider, "REPLACE_ME", "REPLACE_ME", BigInteger.ONE).send();
}

@Test
public void balanceOf() throws Exception {
BigInteger bigIntegerVar = eRC20Token.balanceOf("REPLACE_ME").send();
Assertions.assertEquals(BigInteger.ONE, bigIntegerVar);
}

@Test
public void decimals() throws Exception {
BigInteger bigIntegerVar = eRC20Token.decimals().send();
Assertions.assertEquals(BigInteger.ONE, bigIntegerVar);
}

@Test
public void allowance() throws Exception {
BigInteger bigIntegerVar = eRC20Token.allowance("REPLACE_ME", "REPLACE_ME").send();
Assertions.assertEquals(BigInteger.ONE, bigIntegerVar);
}

@Test
public void approve() throws Exception {
TransactionReceipt transactionReceiptVar = eRC20Token.approve("REPLACE_ME", BigInteger.ONE).send();
Assertions.assertTrue(transactionReceiptVar.isStatusOK());
}
}
Binary file added basic/81-web3j-erc20/assets/web3j_network.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 63d4b67

Please sign in to comment.