Skip to content

Commit

Permalink
feat: nft 생성 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
junseokkim committed Nov 22, 2023
1 parent a8bfcd3 commit e7fee8b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 82 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package zerobibim.flory.domain.purchase.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import zerobibim.flory.domain.purchase.dto.request.NFTRequestDto;
import zerobibim.flory.domain.purchase.service.Web3jService;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/nft")
public class NftController {
private final Web3jService web3jService;
// Define your API endpoints here
@PostMapping("/mint")
public TransactionReceipt mintToken() throws Exception {
return web3jService.nftCreate(new NFTRequestDto.MemberNFTInfo("0x06f5ad6ca1fdbba1ac90b64472a36ca58a83b8af", "ipfs://QmVumYTYwP4F1DVLmaE527g7JDfB6Z1tzYFm8VE3zGPo1M"));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package zerobibim.flory.domain.contract.dto;
package zerobibim.flory.domain.purchase.dto.request;

import lombok.AllArgsConstructor;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import zerobibim.flory.domain.contract.dto.NFTRequestDto;
//import zerobibim.flory.domain.contract.service.Web3jService;
import zerobibim.flory.domain.purchase.dto.request.NFTRequestDto;
import zerobibim.flory.domain.flower.entity.Flower;
import zerobibim.flory.domain.flower.service.FlowerService;
import zerobibim.flory.domain.member.entity.Member;
Expand All @@ -31,7 +30,7 @@ public class PurchaseService implements EntityLoader<Purchase, Long> {
private final PurchaseMapper purchaseMapper;
private final MemberService memberService;
private final FlowerService flowerService;
// private final Web3jService web3jService;
private final Web3jService web3jService;

@Transactional
public PurchaseIdResponse createPurchase(PurchaseCreateRequest request) {
Expand All @@ -47,12 +46,12 @@ public PurchaseIdResponse createPurchase(PurchaseCreateRequest request) {

newPurchase.setIsNft(LocalDate.now());

// NFTRequestDto.MemberNFTInfo nftInfo = new NFTRequestDto.MemberNFTInfo(receiver.getWalletAddress(), "ipfs://QmZUS5QQK4nSKLFWui54vVp4CJTEBaqfcfBJCry7vjRVCc");
// try {
// TransactionReceipt transactionReceipt = web3jService.nftCreate(nftInfo);
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
NFTRequestDto.MemberNFTInfo nftInfo = new NFTRequestDto.MemberNFTInfo(receiver.getWalletAddress(), flower.getImage().getIpfsUrl());
try {
TransactionReceipt transactionReceipt = web3jService.nftCreate(nftInfo);
} catch (Exception e) {
throw new RuntimeException(e);
}
return new PurchaseIdResponse(newPurchase.getId());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package zerobibim.flory.domain.purchase.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.web3j.protocol.core.methods.response.*;
import zerobibim.flory.domain.contract.NFT;
import zerobibim.flory.domain.purchase.dto.request.NFTRequestDto;


@Slf4j
@RequiredArgsConstructor
@Service
public class Web3jService {

private final NFT nft;

public TransactionReceipt nftCreate(NFTRequestDto.MemberNFTInfo memberNFTInfo) throws Exception {

return nft.create(memberNFTInfo.getWalletAddress(), memberNFTInfo.getIpfsUrl())
.sendAsync()
.get();
}

}
94 changes: 47 additions & 47 deletions src/main/java/zerobibim/flory/global/config/Web3jConfig.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
//package zerobibim.flory.global.config;
//
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.web3j.crypto.Credentials;
//import org.web3j.crypto.ECKeyPair;
//import org.web3j.protocol.Web3j;
//import org.web3j.protocol.http.HttpService;
//import org.web3j.tx.Contract;
//import org.web3j.tx.gas.StaticGasProvider;
//import zerobibim.flory.domain.contract.NFT;
//
//import java.math.BigInteger;
//
//@Configuration
//public class Web3jConfig {
//
// @Value("${infura.API_URL}")
// private String INFURA_API_URL;
//
// @Value("${metamask.PRIVATE_KEY}")
// private String PRIVATE_KEY;
//
// @Value("${metamask.CONTRACT_ADDRESS}")
// private String CONTRACT_ADDRESS;
//
// @Bean
// public Web3j web3j() {
// return Web3j.build(new HttpService(INFURA_API_URL));
// }
//
// @Bean
// public Credentials credentials() {
// BigInteger privateKeyInBT = new BigInteger(PRIVATE_KEY, 16);
// return Credentials.create(ECKeyPair.create(privateKeyInBT));
// }
//
// @Bean
// public NFT nft() {
// BigInteger gasPrice = Contract.GAS_PRICE;
// BigInteger gasLimit = Contract.GAS_LIMIT;
// //StaticGasProvider gasProvider = new StaticGasProvider(gasPrice, gasLimit);
//
// return NFT.load(CONTRACT_ADDRESS, web3j(), credentials(), gasPrice, gasLimit);
// }
//}
package zerobibim.flory.global.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.ECKeyPair;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;
import org.web3j.tx.gas.StaticGasProvider;
import zerobibim.flory.domain.contract.NFT;

import java.math.BigInteger;

@Configuration
public class Web3jConfig {

@Value("${infura.API_URL}")
private String INFURA_API_URL;

@Value("${metamask.PRIVATE_KEY}")
private String PRIVATE_KEY;

@Value("${metamask.CONTRACT_ADDRESS}")
private String CONTRACT_ADDRESS;

@Bean
public Web3j web3j() {
return Web3j.build(new HttpService(INFURA_API_URL));
}

@Bean
public Credentials credentials() {
BigInteger privateKeyInBT = new BigInteger(PRIVATE_KEY, 16);
return Credentials.create(ECKeyPair.create(privateKeyInBT));
}

@Bean
public NFT nft() {
BigInteger gasPrice = Contract.GAS_PRICE;
BigInteger gasLimit = Contract.GAS_LIMIT;
//StaticGasProvider gasProvider = new StaticGasProvider(gasPrice, gasLimit);

return NFT.load(CONTRACT_ADDRESS, web3j(), credentials(), gasPrice, gasLimit);
}
}

0 comments on commit e7fee8b

Please sign in to comment.