Skip to content

Commit

Permalink
Merge pull request #16 from yerim216/develop
Browse files Browse the repository at this point in the history
Fix: nft 컨트랙트 수정
  • Loading branch information
yerim216 authored Nov 23, 2023
2 parents e7fee8b + d982899 commit 6c48ac1
Showing 1 changed file with 177 additions and 0 deletions.
177 changes: 177 additions & 0 deletions src/main/java/zerobibim/flory/domain/contract/NFT.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import org.web3j.abi.EventEncoder;
import org.web3j.abi.TypeReference;
import org.web3j.abi.datatypes.Address;
import org.web3j.abi.datatypes.Bool;
import org.web3j.abi.datatypes.Event;
import org.web3j.abi.datatypes.Function;
import org.web3j.abi.datatypes.Type;
import org.web3j.abi.datatypes.Utf8String;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.crypto.Credentials;
import org.web3j.protocol.Web3j;
Expand All @@ -39,8 +41,32 @@
public class NFT extends Contract {
public static final String BINARY = "Bin file was not provided";

public static final String FUNC_APPROVE = "approve";

public static final String FUNC_CREATE = "create";

public static final String FUNC_GETAPPROVED = "getApproved";

public static final String FUNC_ISAPPROVEDFORALL = "isApprovedForAll";

public static final String FUNC_OWNEROF = "ownerOf";

public static final String FUNC_SAFETRANSFERFROM = "safeTransferFrom";

public static final String FUNC_SETAPPROVALFORALL = "setApprovalForAll";

public static final String FUNC_TOKENURI = "tokenURI";

public static final String FUNC_TRANSFERFROM = "transferFrom";

public static final Event APPROVAL_EVENT = new Event("Approval",
Arrays.<TypeReference<?>>asList(new TypeReference<Address>(true) {}, new TypeReference<Address>(true) {}, new TypeReference<Uint256>(true) {}));
;

public static final Event APPROVALFORALL_EVENT = new Event("ApprovalForAll",
Arrays.<TypeReference<?>>asList(new TypeReference<Address>(true) {}, new TypeReference<Address>(true) {}, new TypeReference<Bool>() {}));
;

public static final Event TRANSFER_EVENT = new Event("Transfer",
Arrays.<TypeReference<?>>asList(new TypeReference<Address>(true) {}, new TypeReference<Address>(true) {}, new TypeReference<Uint256>(true) {}));
;
Expand All @@ -63,6 +89,74 @@ protected NFT(String contractAddress, Web3j web3j, TransactionManager transactio
super(BINARY, contractAddress, web3j, transactionManager, contractGasProvider);
}

public static List<ApprovalEventResponse> getApprovalEvents(TransactionReceipt transactionReceipt) {
List<Contract.EventValuesWithLog> valueList = staticExtractEventParametersWithLog(APPROVAL_EVENT, transactionReceipt);
ArrayList<ApprovalEventResponse> responses = new ArrayList<ApprovalEventResponse>(valueList.size());
for (Contract.EventValuesWithLog eventValues : valueList) {
ApprovalEventResponse typedResponse = new ApprovalEventResponse();
typedResponse.log = eventValues.getLog();
typedResponse.owner = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.approved = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.tokenId = (BigInteger) eventValues.getIndexedValues().get(2).getValue();
responses.add(typedResponse);
}
return responses;
}

public static ApprovalEventResponse getApprovalEventFromLog(Log log) {
Contract.EventValuesWithLog eventValues = staticExtractEventParametersWithLog(APPROVAL_EVENT, log);
ApprovalEventResponse typedResponse = new ApprovalEventResponse();
typedResponse.log = log;
typedResponse.owner = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.approved = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.tokenId = (BigInteger) eventValues.getIndexedValues().get(2).getValue();
return typedResponse;
}

public Flowable<ApprovalEventResponse> approvalEventFlowable(EthFilter filter) {
return web3j.ethLogFlowable(filter).map(log -> getApprovalEventFromLog(log));
}

public Flowable<ApprovalEventResponse> approvalEventFlowable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
filter.addSingleTopic(EventEncoder.encode(APPROVAL_EVENT));
return approvalEventFlowable(filter);
}

public static List<ApprovalForAllEventResponse> getApprovalForAllEvents(TransactionReceipt transactionReceipt) {
List<Contract.EventValuesWithLog> valueList = staticExtractEventParametersWithLog(APPROVALFORALL_EVENT, transactionReceipt);
ArrayList<ApprovalForAllEventResponse> responses = new ArrayList<ApprovalForAllEventResponse>(valueList.size());
for (Contract.EventValuesWithLog eventValues : valueList) {
ApprovalForAllEventResponse typedResponse = new ApprovalForAllEventResponse();
typedResponse.log = eventValues.getLog();
typedResponse.owner = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.operator = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.approved = (Boolean) eventValues.getNonIndexedValues().get(0).getValue();
responses.add(typedResponse);
}
return responses;
}

public static ApprovalForAllEventResponse getApprovalForAllEventFromLog(Log log) {
Contract.EventValuesWithLog eventValues = staticExtractEventParametersWithLog(APPROVALFORALL_EVENT, log);
ApprovalForAllEventResponse typedResponse = new ApprovalForAllEventResponse();
typedResponse.log = log;
typedResponse.owner = (String) eventValues.getIndexedValues().get(0).getValue();
typedResponse.operator = (String) eventValues.getIndexedValues().get(1).getValue();
typedResponse.approved = (Boolean) eventValues.getNonIndexedValues().get(0).getValue();
return typedResponse;
}

public Flowable<ApprovalForAllEventResponse> approvalForAllEventFlowable(EthFilter filter) {
return web3j.ethLogFlowable(filter).map(log -> getApprovalForAllEventFromLog(log));
}

public Flowable<ApprovalForAllEventResponse> approvalForAllEventFlowable(DefaultBlockParameter startBlock, DefaultBlockParameter endBlock) {
EthFilter filter = new EthFilter(startBlock, endBlock, getContractAddress());
filter.addSingleTopic(EventEncoder.encode(APPROVALFORALL_EVENT));
return approvalForAllEventFlowable(filter);
}

public static List<TransferEventResponse> getTransferEvents(TransactionReceipt transactionReceipt) {
List<Contract.EventValuesWithLog> valueList = staticExtractEventParametersWithLog(TRANSFER_EVENT, transactionReceipt);
ArrayList<TransferEventResponse> responses = new ArrayList<TransferEventResponse>(valueList.size());
Expand Down Expand Up @@ -97,6 +191,15 @@ public Flowable<TransferEventResponse> transferEventFlowable(DefaultBlockParamet
return transferEventFlowable(filter);
}

public RemoteFunctionCall<TransactionReceipt> approve(String to, BigInteger tokenId) {
final Function function = new Function(
FUNC_APPROVE,
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, to),
new org.web3j.abi.datatypes.generated.Uint256(tokenId)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<TransactionReceipt> create(String to, String tokenUri) {
final Function function = new Function(
FUNC_CREATE,
Expand All @@ -106,6 +209,64 @@ public RemoteFunctionCall<TransactionReceipt> create(String to, String tokenUri)
return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<String> getApproved(BigInteger tokenId) {
final Function function = new Function(FUNC_GETAPPROVED,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(tokenId)),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}

public RemoteFunctionCall<Boolean> isApprovedForAll(String owner, String operator) {
final Function function = new Function(FUNC_ISAPPROVEDFORALL,
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, owner),
new org.web3j.abi.datatypes.Address(160, operator)),
Arrays.<TypeReference<?>>asList(new TypeReference<Bool>() {}));
return executeRemoteCallSingleValueReturn(function, Boolean.class);
}

public RemoteFunctionCall<String> ownerOf(BigInteger tokenId) {
final Function function = new Function(FUNC_OWNEROF,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(tokenId)),
Arrays.<TypeReference<?>>asList(new TypeReference<Address>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}

public RemoteFunctionCall<TransactionReceipt> safeTransferFrom(String from, String to, BigInteger tokenId) {
final Function function = new Function(
FUNC_SAFETRANSFERFROM,
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, from),
new org.web3j.abi.datatypes.Address(160, to),
new org.web3j.abi.datatypes.generated.Uint256(tokenId)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<TransactionReceipt> setApprovalForAll(String operator, Boolean approved) {
final Function function = new Function(
FUNC_SETAPPROVALFORALL,
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, operator),
new org.web3j.abi.datatypes.Bool(approved)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

public RemoteFunctionCall<String> tokenURI(BigInteger tokenId) {
final Function function = new Function(FUNC_TOKENURI,
Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Uint256(tokenId)),
Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {}));
return executeRemoteCallSingleValueReturn(function, String.class);
}

public RemoteFunctionCall<TransactionReceipt> transferFrom(String from, String to, BigInteger tokenId) {
final Function function = new Function(
FUNC_TRANSFERFROM,
Arrays.<Type>asList(new org.web3j.abi.datatypes.Address(160, from),
new org.web3j.abi.datatypes.Address(160, to),
new org.web3j.abi.datatypes.generated.Uint256(tokenId)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}

@Deprecated
public static NFT load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new NFT(contractAddress, web3j, credentials, gasPrice, gasLimit);
Expand All @@ -124,6 +285,22 @@ public static NFT load(String contractAddress, Web3j web3j, TransactionManager t
return new NFT(contractAddress, web3j, transactionManager, contractGasProvider);
}

public static class ApprovalEventResponse extends BaseEventResponse {
public String owner;

public String approved;

public BigInteger tokenId;
}

public static class ApprovalForAllEventResponse extends BaseEventResponse {
public String owner;

public String operator;

public Boolean approved;
}

public static class TransferEventResponse extends BaseEventResponse {
public String from;

Expand Down

0 comments on commit 6c48ac1

Please sign in to comment.