Skip to content

Commit

Permalink
Merge pull request #197 from dwusiq/dev
Browse files Browse the repository at this point in the history
Configure web3j for each chain
  • Loading branch information
liberhe authored Mar 9, 2024
2 parents a5d7500 + f89462c commit c6fe65d
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/dl/officialsite/aave/AaveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class AaveService {
BigInteger e23 = new BigInteger("100000000000000000000000");


@Autowired
// @Autowired
Web3j web3j;

@Autowired
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/dl/officialsite/aave/DeFiTokenYield.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class DeFiTokenYield {

private String tokeName;

private String protocol;

private String tokenAddress;

private String tokenApy;
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/dl/officialsite/config/NetworkProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.dl.officialsite.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import lombok.Data;

import static com.dl.officialsite.config.NetworkProperties.NETWORK_PREFIX;

@Data
@ConfigurationProperties(prefix = NETWORK_PREFIX)
public class NetworkProperties {

public static final String NETWORK_PREFIX = "network";

private NetworkDetailProperties[] configs;

@Data
public static class NetworkDetailProperties {
private String id;
private String name;
private String rpc;
private Boolean adminRpc;
private Long httpTimeoutSeconds;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand All @@ -19,32 +18,41 @@
import org.web3j.protocol.ipc.UnixIpcService;
import org.web3j.protocol.ipc.WindowsIpcService;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

@Configuration
@ConditionalOnClass(Web3j.class)
@EnableConfigurationProperties(Web3jProperties.class)
@EnableConfigurationProperties({ Web3jProperties.class, NetworkProperties.class })
public class Web3jAutoConfiguration {

private static Log log = LogFactory.getLog(Web3jAutoConfiguration.class);

@Autowired
private Web3jProperties properties;
@Autowired
private NetworkProperties networkProperties;

@Bean
public static final ConcurrentHashMap<String, Web3j> web3jMap = new ConcurrentHashMap<String, Web3j>();

// @Bean
@ConditionalOnMissingBean
public Web3j web3j() {
Web3jService web3jService = buildService(properties.getClientAddress());
log.info("Building service for endpoint: " + properties.getClientAddress());
return Web3j.build(web3jService);
public void web3j() {
for (int i = 0; i < networkProperties.getConfigs().length; i++) {
NetworkProperties.NetworkDetailProperties detail = networkProperties.getConfigs()[i];
Web3jService web3jService = buildService(detail.getRpc());
web3jMap.put(detail.getId(), Web3j.build(web3jService));
log.info("initWeb3jMap.current chain:" + detail.getName() + " rpc:" + detail.getRpc());
}

}

@Bean
@ConditionalOnProperty(
prefix = Web3jProperties.WEB3J_PREFIX, name = "admin-client", havingValue = "true")
@ConditionalOnProperty(prefix = Web3jProperties.WEB3J_PREFIX, name = "admin-client", havingValue = "true")
public Admin admin() {
Web3jService web3jService = buildService(properties.getClientAddress());
log.info("Building admin service for endpoint: " + properties.getClientAddress());
log.info("Building admin service for endpoint: " +
properties.getClientAddress());
return Admin.build(web3jService);
}

Expand Down Expand Up @@ -75,7 +83,7 @@ private void configureTimeouts(OkHttpClient.Builder builder) {
Long tos = properties.getHttpTimeoutSeconds();
if (tos != null) {
builder.connectTimeout(tos, TimeUnit.SECONDS);
builder.readTimeout(tos, TimeUnit.SECONDS); // Sets the socket timeout too
builder.readTimeout(tos, TimeUnit.SECONDS); // Sets the socket timeout too
builder.writeTimeout(tos, TimeUnit.SECONDS);
}
}
Expand All @@ -88,6 +96,4 @@ private static void configureLogging(OkHttpClient.Builder builder) {
}
}



}
16 changes: 16 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,22 @@ logging:
web3j:
client-address: "https://polygon-rpc.com"


network:
configs:
- id: "137"
name: "polygon"
rpc: "https://polygon-rpc.com"
- id: "10"
name: "optimism"
rpc: "https://optimism.llamarpc.com"
- id: "534352"
name: "scroll"
rpc: "https://rpc.scroll.io"
- id: "42161"
name: "arbitrum"
rpc: "https://arbitrum.llamarpc.com"

jobs:
redpacket:
corn: ${CRON_REDPACKET:0/5 * * * * ?}
Expand Down

0 comments on commit c6fe65d

Please sign in to comment.