Browse Source

添加铸造服务内容

master
long 3 years ago
parent
commit
724ee54246
  1. 7
      pom.xml
  2. 23
      src/main/java/com/nft/api/NftApi.java
  3. 25
      src/main/java/com/nft/config/NftConfig.java
  4. 13
      src/main/java/com/nft/service/NftCastingService.java
  5. 49
      src/main/java/com/nft/service/impl/NftCastingServiceImpl.java
  6. 5
      src/main/java/com/nft/util/TestMint.java
  7. 8
      src/main/resources/application.yml

7
pom.xml

@ -182,6 +182,13 @@
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- &lt;!&ndash;防止configurationprocessor注解报错&ndash;&gt;
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
&lt;!&ndash;不传递依赖&ndash;&gt;
<optional>true</optional>
</dependency>-->
</dependencies>
<build>

23
src/main/java/com/nft/api/NftApi.java

@ -0,0 +1,23 @@
package com.nft.api;
import com.nft.service.NftCastingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Author _007long
* @Date 2022 05 27
**/
@RequestMapping("/conflux")
@RestController
public class NftApi {
@Autowired
private NftCastingService castingService;
@GetMapping("getUrl")
public String getUrl() {
return castingService.startCasting();
}
}

25
src/main/java/com/nft/config/NftConfig.java

@ -0,0 +1,25 @@
package com.nft.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Author _007long
* @Date 2022 05 27
**/
@Data
@Component
@ConfigurationProperties(prefix ="conflux")
public class NftConfig {
//合约
private String contract;
//拥有者
private String owner;
//私钥
private String privateKey;
//chainId
private String chainId;
//上链地址
private String nodeUrl;
}

13
src/main/java/com/nft/service/NftCastingService.java

@ -0,0 +1,13 @@
package com.nft.service;
/**
* 铸造接口
*/
public interface NftCastingService {
/**
* 开始铸造
* @return
*/
String startCasting();
}

49
src/main/java/com/nft/service/impl/NftCastingServiceImpl.java

@ -0,0 +1,49 @@
package com.nft.service.impl;
import com.nft.config.NftConfig;
import com.nft.service.NftCastingService;
import com.nft.util.AESUtil;
import conflux.web3j.Account;
import conflux.web3j.Cfx;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigInteger;
/**
* @Author _007long
* @Date 2022 05 27
**/
@Service
@Slf4j
public class NftCastingServiceImpl implements NftCastingService {
@Autowired
private NftConfig config;
@Override
public String startCasting() {
try {
Cfx cfx = Cfx.create(config.getNodeUrl(), 3, 1000);
Account account = Account.create(cfx, AESUtil.decrypt(config.getPrivateKey()));
Account.Option opt = new Account.Option();
opt.withValue(BigInteger.ZERO);
opt.withChainId(new BigInteger(config.getChainId()));
opt.withEpochHeight(cfx.getEpochNumber().sendAndGet());
opt.withGasPrice(new BigInteger("1000000000"));
//获取余额
BigInteger balance = cfx.getBalance(account.getAddress()).sendAndGet();
log.info("[mintNft][balance]{}", balance);
if (balance.compareTo(new BigInteger("100000000000000000000")) < 0) {
log.info("[mintNft][balance not enough 100]{}", balance);
//sms notice
//余额不足
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}

5
src/main/java/com/nft/util/App.java → src/main/java/com/nft/util/TestMint.java

@ -6,6 +6,7 @@ import conflux.web3j.AccountManager;
import conflux.web3j.Cfx;
import conflux.web3j.CfxUnit;
import conflux.web3j.contract.ContractCall;
import conflux.web3j.contract.ERC20;
import conflux.web3j.response.Receipt;
import conflux.web3j.response.Transaction;
import conflux.web3j.types.CfxAddress;
@ -19,7 +20,7 @@ import java.io.IOException;
import java.math.BigInteger;
import java.util.Optional;
public class App {
public class TestMint {
public static final Cfx cfx = Cfx.create("https://test.confluxrpc.com");
public static final String privateKey="cfxtest:aapjjbzf3bumeatch25bduathv0xz9wk42e8yzd4r1";
public static void main(String[] args) throws Exception {
@ -73,7 +74,7 @@ public class App {
//用ER721标准
public static void getERinit() {
// ERC721 erc721 = new ERC721(cfx, new CfxAddress(nftAddress), account);
// ERC20 erc20=new ERC20(Cfx cfx, CfxAddress address, Account account);
}
// //转移资产
// public static void move(){

8
src/main/resources/application.yml

@ -27,13 +27,17 @@ mybatis:
configuration:
map-underscore-to-camel-case: true
#测试环境
confluxNft:
conflux:
#合约
contract: cfxtest:acbctakv35gyzcepnh14k239hw6tcw2t7edv34adn3
# 执行地址
# 执行地址 (拥有者)
owner: cfxtest:aapjjbzf3bumeatch25bduathv0xz9wk42e8yzd4r1
###私钥位置
privateKey: 0xa31115fa5cf8fefd57fa0dea7ff013e2d71b8cf13a4d6a7e276c9c5b21bf911b
###chainId
chainId: 1
###上链地址
nodeUrl: https://test.confluxrpc.com
swagger:
show: true
# mybatis-plus

Loading…
Cancel
Save