Avatar
0
monkey Enlightened
monkey Enlightened
Dự án NFT đơn giản với Metamask và Web3: Khởi tạo dự án
Dự án của chúng ta sẽ sử dụng java web, cấu trúc thư mục sẽ kiểu thế này:
<img src="https://stackask.com/wp-content/uploads/2021/12/Screen-Shot-2021-12-16-at-12.26.01-PM.png" alt="" class="alignnone size-full wp-image-3757" />
  1. file pom:
    <span><span><span>&lt;?</span>xml version=<span>"1.0"</span> encoding=<span>"UTF-8"</span><span>?&gt;</span></span>
    <span>&lt;<span>project</span> <span>xmlns</span>=<span>"http://maven.apache.org/POM/4.0.0"</span>
             <span>xmlns:xsi</span>=<span>"http://www.w3.org/2001/XMLSchema-instance"</span>
             <span>xsi:schemaLocation</span>=<span>"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"</span>&gt;</span>
      <span>&lt;<span>modelVersion</span>&gt;</span>4.0.0<span>&lt;/<span>modelVersion</span>&gt;</span>
    
      <span>&lt;<span>parent</span>&gt;</span>
        <span>&lt;<span>groupId</span>&gt;</span>com.tvd12<span>&lt;/<span>groupId</span>&gt;</span>
        <span>&lt;<span>artifactId</span>&gt;</span>ezyfox<span>&lt;/<span>artifactId</span>&gt;</span>
        <span>&lt;<span>version</span>&gt;</span>1.0.2<span>&lt;/<span>version</span>&gt;</span>
      <span>&lt;/<span>parent</span>&gt;</span>
    
      <span>&lt;<span>groupId</span>&gt;</span>org.example<span>&lt;/<span>groupId</span>&gt;</span>
      <span>&lt;<span>artifactId</span>&gt;</span>nft-game-web<span>&lt;/<span>artifactId</span>&gt;</span>
      <span>&lt;<span>version</span>&gt;</span>1.0.0<span>&lt;/<span>version</span>&gt;</span>
    
      <span>&lt;<span>properties</span>&gt;</span>
        <span>&lt;<span>ezy.http.version</span>&gt;</span>0.2.0<span>&lt;/<span>ezy.http.version</span>&gt;</span>
        <span>&lt;<span>ezy.platform.version</span>&gt;</span>0.0.1a<span>&lt;/<span>ezy.platform.version</span>&gt;</span>
      <span>&lt;/<span>properties</span>&gt;</span>
    
      <span>&lt;<span>dependencies</span>&gt;</span>
        <span>&lt;<span>dependency</span>&gt;</span>
          <span>&lt;<span>groupId</span>&gt;</span>com.tvd12<span>&lt;/<span>groupId</span>&gt;</span>
          <span>&lt;<span>artifactId</span>&gt;</span>ezyhttp-server-boot<span>&lt;/<span>artifactId</span>&gt;</span>
          <span>&lt;<span>version</span>&gt;</span>$</span><span>{ezy.http.version}</span><span><span>&lt;/<span>version</span>&gt;</span>
        <span>&lt;/<span>dependency</span>&gt;</span>
        <span>&lt;<span>dependency</span>&gt;</span>
          <span>&lt;<span>groupId</span>&gt;</span>org.youngmonkeys<span>&lt;/<span>groupId</span>&gt;</span>
          <span>&lt;<span>artifactId</span>&gt;</span>ezyplatform-devtools<span>&lt;/<span>artifactId</span>&gt;</span>
          <span>&lt;<span>version</span>&gt;</span>$</span><span>{ezy.platform.version}</span><span><span>&lt;/<span>version</span>&gt;</span>
        <span>&lt;/<span>dependency</span>&gt;</span>
      <span>&lt;/<span>dependencies</span>&gt;</span>
    <span>&lt;/<span>project</span>&gt;</span></span>
    
  1. File NftGameStartup:
    package org.example.nft_game;
    
    import com.tvd12.ezyhttp.core.boot.EzyHttpApplicationBootstrap;
    
    public class NftGameStartup {
        public static void main(String[] args) throws Exception {
            EzyHttpApplicationBootstrap.start(NftGameStartup.class);
        }
    }
    
  • Answer
blockchain metamask
Remain: 5
5 Answers
Avatar
monkey Enlightened
monkey Enlightened
  1. File HomeController:
    package org.example.nft_game.controller;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Map;
    
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader;
    import com.tvd12.ezyfox.stream.EzyInputStreamLoader;
    import com.tvd12.ezyfox.stream.EzyInputStreams;
    import com.tvd12.ezyfox.util.EzyMapBuilder;
    import com.tvd12.ezyhttp.server.core.annotation.Controller;
    import com.tvd12.ezyhttp.server.core.annotation.DoGet;
    import com.tvd12.ezyhttp.server.core.view.Redirect;
    
    @Controller
    public class HomeController {
    
        private final ObjectMapper objectMapper;
        private final EzyInputStreamLoader inputStreamLoader;
        private static final String CONTRACT_ADDRESS = "0xA9F6009507ef930C67B98F7A12CB0CA4321aAaDB";
    
        public HomeController(ObjectMapper objectMapper) {
            this.objectMapper = objectMapper;
            this.inputStreamLoader = new EzyAnywayInputStreamLoader();
        }
    
        @DoGet("/")
        public Redirect home() {
            return Redirect.to("/index.html");
        }
    
        @SuppressWarnings("unchecked")
        @DoGet("/contract")
        public Map contractGet() throws IOException {
            try (InputStream inputStream = inputStreamLoader.load("abi.json")) {
                String api = EzyInputStreams.toStringUtf8(inputStream);
                return EzyMapBuilder
                    .mapBuilder()
                    .put("address", CONTRACT_ADDRESS)
                    .put("abi", objectMapper.readValue(api, Map[].class))
                    .build();
            }
        }
    }
    
  • 1
  • Reply
Avatar
monkey Enlightened
monkey Enlightened
4. File style.css:
body {
    background-color: #fff;
}
5. File main.js:
var stringToHex = function(str) {
    var hex, i;
    var result = "0x";
    for (i = 0; i < str.length; i++) {
        hex = str.charCodeAt(i).toString(16);
        result += hex;
    }
    return result
}

var hexToString = function(hex) {
    var i;
    var hexes = hex.match(/.{1,2}/g) || [];
    var result = "";
    for(i = 0; i < hexes.length; i++) {
        result += String.fromCharCode(parseInt(hexes[i], 16));
    }

    return result;
}
  • 1
  • Reply
Avatar
monkey Enlightened
monkey Enlightened
  1. File abi.json, ở <a href="https://stackask.com/question/du-an-nft-don-gian-voi-metamask-va-web3-khoi-tao-moi-truong/" target="_blank">post trước</a> chúng ta đã deploy contract rồi, hãy tìm đến file: <code>truffle/build/contracts/GameItem.json</code> và copy phần "abi" array ( lưu ý chỉ copy phần array thôi nhé, không copy cả "abi": )
  2. File application.yaml:
    resources:
      enable: true
    
  1. File log4j.properties
    log4j.properties
    
    # Root logger option
    log4j.rootLogger=INFO, stdout
    
    # Redirect log messages to console
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    
    
  • 1
  • Reply
Avatar
Nguyễn Thái Sơn Professional
@SuppressWarnings("unchecked") 
@DoGet("/contract") 
public Map contractGet() throws IOException { 
	try (InputStream inputStream = inputStreamLoader.load("abi.json")) { 
		String api = EzyInputStreams.toStringUtf8(inputStream); 
		return EzyMapBuilder.mapBuilder()
			.put("address", CONTRACT_ADDRESS)
			.put("abi", objectMapper.readValue(api, Map[].class))
			.build();
		}
	}
Tại sao lại phải để annotation superwarning ở đây ạ
  • 0
  • Reply
EzyMapBuilderbuildMap@SuppressWarnings("unchecked")
vào để IDE nó đỡ cảnh báo em ạ.
 –  tvd12 1639789326000
Avatar
monkey Enlightened
monkey Enlightened
index.html
<span>&lt;!DOCTYPE html&gt;</span>
<span>&lt;<span>html</span> <span>lang</span>=<span>"en"</span>&gt;</span>
<span>&lt;<span>head</span>&gt;</span>
  <span>&lt;<span>meta</span> <span>charset</span>=<span>"UTF-8"</span>&gt;</span>
  <span>&lt;<span>title</span>&gt;</span>NFT Game<span>&lt;/<span>title</span>&gt;</span>
  <span>&lt;<span>link</span> <span>rel</span>=<span>"stylesheet"</span> <span>href</span>=<span>"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"</span> <span>integrity</span>=<span>"sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"</span> <span>crossorigin</span>=<span>"anonymous"</span>&gt;</span>
  <span>&lt;<span>link</span> <span>rel</span>=<span>"stylesheet"</span> <span>href</span>=<span>"/css/style.css"</span>&gt;</span>
<span>&lt;/<span>head</span>&gt;</span>
<span>&lt;<span>body</span>&gt;</span>
<span>&lt;<span>div</span> <span>class</span>=<span>"container"</span>&gt;</span>
  <span>&lt;<span>div</span> <span>class</span>=<span>"row"</span>&gt;</span>
    <span>&lt;<span>div</span> <span>class</span>=<span>"col-md-12 text-center"</span>&gt;</span>
      <span>&lt;<span>h3</span>&gt;</span>Welcome to NFT Game<span>&lt;/<span>h3</span>&gt;</span>
    <span>&lt;/<span>div</span>&gt;</span>
  <span>&lt;/<span>div</span>&gt;</span>
  <span>&lt;<span>div</span> <span>class</span>=<span>"row"</span>&gt;</span>
    <span>&lt;<span>div</span> <span>class</span>=<span>"col-md-12 text-center"</span>&gt;</span>
      <span>&lt;<span>button</span> <span>id</span>=<span>"enableEthereumButton"</span> <span>class</span>=<span>"btn btn-primary"</span>&gt;</span>Enable Ethereum<span>&lt;/<span>button</span>&gt;</span>
    <span>&lt;/<span>div</span>&gt;</span>
  <span>&lt;/<span>div</span>&gt;</span>
<span>&lt;/<span>div</span>&gt;</span>
<span>&lt;<span>script</span> <span>src</span>=<span>"https://code.jquery.com/jquery-3.6.0.js"</span> <span>integrity</span>=<span>"sha256-H+K7U5CnXl1h5ywQfKtSj8PCmoN9aaq30gDh27Xc0jk="</span> <span>crossorigin</span>=<span>"anonymous"</span>&gt;</span><span></span><span>&lt;/<span>script</span>&gt;</span>
<span>&lt;<span>script</span> <span>src</span>=<span>"https://cdnjs.cloudflare.com/ajax/libs/web3/1.6.1/web3.min.js"</span> <span>integrity</span>=<span>"sha512-5erpERW8MxcHDF7Xea9eBQPiRtxbse70pFcaHJuOhdEBQeAxGQjUwgJbuBDWve+xP/u5IoJbKjyJk50qCnMD7A=="</span> <span>crossorigin</span>=<span>"anonymous"</span> <span>referrerpolicy</span>=<span>"no-referrer"</span>&gt;</span><span></span><span>&lt;/<span>script</span>&gt;</span>
<span>&lt;<span>script</span> <span>src</span>=<span>"/js/main.js"</span>&gt;</span><span></span><span>&lt;/<span>script</span>&gt;</span>
<span>&lt;<span>script</span>&gt;</span><span>
<span>const</span> ethereumButton = $(<span>'#enableEthereumButton'</span>);

<span>async</span> <span><span>function</span> <span>getAccount</span>(<span></span>) </span>{
  <span>const</span> accounts = <span>await</span> ethereum.request({ <span>method</span>: <span>'eth_requestAccounts'</span> });
  <span>console</span>.log(<span>JSON</span>.stringify(accounts));
}

(<span><span>function</span>(<span></span>) </span>{
<span>if</span> (<span>typeof</span> <span>window</span>.ethereum === <span>'undefined'</span>) {
  <span>return</span>;
}
<span>console</span>.log(<span>'MetaMask is installed!'</span>);

ethereumButton.on(<span>'click'</span>, () =&gt; {
  getAccount();
});
})();
</span><span>&lt;/<span>script</span>&gt;</span>
<span>&lt;/<span>body</span>&gt;</span>
<span>&lt;/<span>html</span>&gt;</span>
  • 1
  • Reply