use of org.starcoin.types.AccountAddress in project starcoin-search by starcoinorg.
the class ElasticSearchHandler method buildTransferRequest.
private List<IndexRequest> buildTransferRequest(Transaction transaction, String indexName) {
List<IndexRequest> requests = new ArrayList<>();
UserTransaction userTransaction = transaction.getUserTransaction();
if (userTransaction == null) {
return requests;
}
RawTransaction rawTransaction = userTransaction.getRawTransaction();
if (rawTransaction == null) {
return requests;
}
TransactionPayload payload = rawTransaction.getTransactionPayload();
if (!(payload.getClass() == TransactionPayload.ScriptFunction.class)) {
// todo script and package handle
logger.warn("other type must handle in future: {}", payload.getClass());
return requests;
}
org.starcoin.types.ScriptFunction function = ((TransactionPayload.ScriptFunction) payload).value;
String functionName = function.function.value;
if (functionName.equals("peer_to_peer") || functionName.equals("peer_to_peer_v2")) {
Transfer transfer = new Transfer();
transfer.setTxnHash(transaction.getTransactionHash());
transfer.setSender(rawTransaction.getSender());
transfer.setTimestamp(transaction.getTimestamp());
transfer.setIdentifier(functionName);
transfer.setReceiver(Hex.encode(function.args.get(0)));
String amount = Hex.encode(function.args.get(1));
if (functionName.equals("peer_to_peer")) {
amount = Hex.encode(function.args.get(2));
}
transfer.setAmount(amount);
transfer.setTypeTag(getTypeTags(function.ty_args));
IndexRequest request = new IndexRequest(indexName);
request.source(JSON.toJSONString(transfer), XContentType.JSON);
requests.add(request);
return requests;
} else if (functionName.equals("batch_peer_to_peer") || functionName.equals("batch_peer_to_peer_v2")) {
// batch handle
Bytes addresses = function.args.get(0);
byte[] addressBytes = addresses.content();
byte[] amountBytes;
if (functionName.equals("batch_peer_to_peer_v2")) {
amountBytes = function.args.get(1).content();
} else {
amountBytes = function.args.get(2).content();
}
int size = addressBytes.length / AccountAddress.LENGTH;
for (int i = 0; i < size; i++) {
byte[] addressByte = Arrays.copyOfRange(addressBytes, i * AccountAddress.LENGTH, (i + 1) * AccountAddress.LENGTH);
AccountAddress address = AccountAddress.valueOf(addressByte);
byte[] amountByte = Arrays.copyOfRange(amountBytes, i * AccountAddress.LENGTH, (i + 1) * AccountAddress.LENGTH);
String amount = Hex.encode(amountByte);
Transfer transfer = new Transfer();
transfer.setTxnHash(transaction.getTransactionHash());
transfer.setSender(rawTransaction.getSender());
transfer.setTimestamp(transaction.getTimestamp());
transfer.setIdentifier(functionName);
transfer.setReceiver(address.toString());
transfer.setAmount(amount);
transfer.setTypeTag(getTypeTags(function.ty_args));
IndexRequest request = new IndexRequest(indexName);
request.source(JSON.toJSONString(transfer), XContentType.JSON);
requests.add(request);
}
logger.info("batch transfer handle: {}", size);
} else {
logger.warn("other scripts not support: {}", function.function.value);
}
return requests;
}
use of org.starcoin.types.AccountAddress in project starcoin-search by starcoinorg.
the class TransactionService method getProposalEvents.
public Result<Event> getProposalEvents(String network, String eventAddress) throws IOException {
SearchRequest searchRequest = new SearchRequest(getIndex(network, Constant.TRANSACTION_EVENT_INDEX));
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.size(ELASTICSEARCH_MAX_HITS);
searchSourceBuilder.from(0);
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.filter(QueryBuilders.matchQuery("tag_name", ServiceUtils.proposalCreatedEvent));
boolQuery.must(QueryBuilders.rangeQuery("transaction_index").gt(0));
searchSourceBuilder.query(boolQuery);
searchRequest.source(searchSourceBuilder);
searchSourceBuilder.trackTotalHits(true);
searchSourceBuilder.sort("timestamp", SortOrder.DESC);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
Result<Event> events = getSearchUnescapeResult(searchResponse, Event.class);
List<Event> proposalEvents = new ArrayList<>();
byte[] addressBytes = ByteUtils.hexToByteArray(eventAddress);
AccountAddress proposer = null;
try {
proposer = AccountAddress.bcsDeserialize(addressBytes);
} catch (DeserializationError deserializationError) {
deserializationError.printStackTrace();
}
for (Event event : events.getContents()) {
byte[] proposalBytes = ByteUtils.hexToByteArray(event.getData());
try {
ProposalCreatedEvent payload = ProposalCreatedEvent.bcsDeserialize(proposalBytes);
if (payload.proposer.equals(proposer)) {
proposalEvents.add(event);
}
} catch (DeserializationError deserializationError) {
deserializationError.printStackTrace();
}
}
events.setContents(proposalEvents);
events.setTotal(proposalEvents.size());
return events;
}
use of org.starcoin.types.AccountAddress in project starcoin-java by starcoinorg.
the class StructTagDeserializer method deserialize.
@Override
public StructTag deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
StructTag structTag = null;
AccountAddress address = null;
Identifier module = null;
Identifier name = null;
List<TypeTag> type_params = new ArrayList<>();
while (!jsonParser.isClosed()) {
String token = jsonParser.nextTextValue();
if (token != null) {
switch(jsonParser.getCurrentName()) {
case "struct_tag_type":
String[] tokens = token.split("::");
address = AccountAddress.valueOf(Hex.decode(tokens[0]));
module = new Identifier(tokens[1]);
name = new Identifier(tokens[2]);
break;
case "struct_tag_params":
// TODO parse
break;
}
}
}
if (address != null && module != null & name != null) {
structTag = new StructTag(address, module, name, type_params);
}
return structTag;
}
use of org.starcoin.types.AccountAddress in project starcoin-java by starcoinorg.
the class ModuleDeserializer method deserialize.
@Override
public ModuleId deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
JsonNode typeTagNode = jsonParser.getCodec().readTree(jsonParser);
String name = typeTagNode.get("name").textValue();
AccountAddress address = AccountAddress.valueOf(Hex.decode(typeTagNode.get("address").textValue()));
return new ModuleId(address, new Identifier(name));
}
Aggregations