-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from yerim216/nft
Nft 개발
- Loading branch information
Showing
8 changed files
with
602 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
application.yml | ||
HELP.md | ||
.gradle | ||
build/ | ||
|
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.
32 changes: 32 additions & 0 deletions
32
src/main/java/zerobibim/flory/domain/contract/controller/NftController.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 zerobibim.flory.domain.contract.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.*; | ||
import zerobibim.flory.domain.contract.NFT; | ||
import zerobibim.flory.domain.contract.NFT.*; | ||
|
||
import java.math.BigInteger; | ||
|
||
@RestController | ||
@RequestMapping("/api/nft") | ||
public class NftController { | ||
private final NFT nft; | ||
|
||
@Autowired | ||
public NftController(NFT nft) { | ||
this.nft = nft; | ||
} | ||
|
||
// Define your API endpoints here | ||
@PostMapping("/mint") | ||
public String mintToken(@RequestParam String to, @RequestParam BigInteger tokenId, @RequestParam String uri) { | ||
try { | ||
// Mint a new token | ||
return "Token minted successfully"; | ||
} catch (Exception e) { | ||
// Handle exceptions appropriately (e.g., log the error) | ||
return "Error minting token: " + e.getMessage(); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/zerobibim/flory/domain/contract/dto/NFTRequestDto.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,16 @@ | ||
package zerobibim.flory.domain.contract.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class NFTRequestDto { | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class MemberNFTInfo { | ||
private String walletAddress; | ||
private String ipfsUrl; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/zerobibim/flory/domain/contract/service/IPFSPhotoService.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,38 @@ | ||
package zerobibim.flory.domain.contract.service; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import okhttp3.*; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
|
||
@Service | ||
public class IPFSPhotoService { | ||
|
||
@Value("${pinata.JWT}") | ||
private String PINATE_JWT; | ||
public String insertIpfs(Long memberPhotoId) throws IOException { | ||
|
||
OkHttpClient client = new OkHttpClient(); | ||
|
||
MediaType mediaType = MediaType.parse("application/json"); | ||
RequestBody body = RequestBody.create(mediaType, "{\"pinataContent\":{\"name\": \"" +memberPhoto.getName() + "\"" | ||
+ ",\"description\":\"" + memberPhoto.getDescription() + "\",\"image\":\"" + memberPhoto.getImageUrl() + "\"}}"); | ||
|
||
Request request = new Request.Builder() | ||
.url("https://api.pinata.cloud/pinning/pinJSONToIPFS") | ||
.post(body) | ||
.addHeader("Authorization", PINATA_JWT) | ||
.addHeader("accept", "application/json") | ||
.addHeader("content-type", "application/json") | ||
.build(); | ||
|
||
ResponseBody responsebody = client.newCall(request).execute().body(); | ||
ObjectMapper mapper = new ObjectMapper(); | ||
assert responsebody != null; | ||
JsonNode jsonNode = mapper.readTree(responsebody.byteStream()); | ||
return jsonNode.get("IpfsHash").asText(); | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
src/main/java/zerobibim/flory/domain/contract/service/Web3jService.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,34 @@ | ||
package zerobibim.flory.domain.contract.service; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Service; | ||
import org.web3j.abi.EventEncoder; | ||
import org.web3j.abi.datatypes.generated.Uint256; | ||
import org.web3j.protocol.Web3j; | ||
import org.web3j.protocol.core.DefaultBlockParameter; | ||
import org.web3j.protocol.core.DefaultBlockParameterName; | ||
import org.web3j.protocol.core.methods.response.*; | ||
import zerobibim.flory.domain.contract.NFT; | ||
import zerobibim.flory.domain.contract.dto.NFTRequestDto; | ||
|
||
import java.math.BigInteger; | ||
import java.util.Arrays; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Service("web3jService") | ||
public class Web3jService { | ||
|
||
private final NFT nft; | ||
|
||
public TransactionReceipt nftCreate(NFTRequestDto.MemberNFTInfo memberNFTInfo) throws Exception { | ||
|
||
return nft.mint(memberNFTInfo.getWalletAddress(), "ipfs://" +) | ||
.sendAsync() | ||
.get(); | ||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/zerobibim/flory/global/config/Web3jConfig.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,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); | ||
} | ||
} |