-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
EthanBizzy
authored and
EthanBizzy
committed
Apr 6, 2024
1 parent
f55bd64
commit 63d4b67
Showing
10 changed files
with
641 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
33 changes: 33 additions & 0 deletions
33
basic/81-web3j-erc20/Web3App/src/main/java/org/web3j/Erc20Query.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
basic/81-web3j-erc20/Web3App/src/main/java/org/web3j/Web3App.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
basic/81-web3j-erc20/Web3App/src/main/solidity/ERC20Token.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
basic/81-web3j-erc20/Web3App/src/test/java/org/web3j/generated/contracts/ERC20TokenTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.