본문 바로가기

블록체인

[1일차] 이더리움 테스트넷 연결하기 ( web3j )

  1. web3j 라이브러리 다운받기
    https://web3j.readthedocs.io/en/latest/getting_started.html



  2. 테스트넷 사용하기

    Web3j web3 = Web3j.build(new HttpService("https://morden.infura.io/your-token")); token 받기 : https://infura.io/

  3. Web3j 설정

@Configuration
public class EthClientConfiguration {

@Value("${ethereum.testnet-url}")
private String testnetUrl;

@Value("${ethereum.testnet-token}")
private String testnetToken;

@Bean
public Web3j web3j() {
Web3j web3j = Web3j.build(new HttpService(testnetUrl + "/" + testnetToken));

return web3j;
}
}


 4. Web3j 제대로 연결되었는지 확인

@Service
public class TransferService {

@Autowired
private Web3j web3j;

public String test() throws IOException {
Web3ClientVersion web3ClientVersion = null;
web3ClientVersion = web3j.web3ClientVersion().send();
String clientVersion = web3ClientVersion.getWeb3ClientVersion();
System.out.println(clientVersion);
return clientVersion;
}
}