Skip to content

Commit

Permalink
Merge pull request #188 from orogvany/internal_tx_api
Browse files Browse the repository at this point in the history
API: Add internal transactions to API
  • Loading branch information
semuxgo authored Jul 4, 2019
2 parents f3e786c + f47648f commit dd20ba7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/main/java/org/semux/api/v2/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@

import static org.semux.core.TransactionType.DELEGATE;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.ethereum.vm.LogInfo;
import org.ethereum.vm.program.InternalTransaction;
import org.semux.Kernel;
import org.semux.api.v2.model.AccountType;
import org.semux.api.v2.model.AccountVoteType;
import org.semux.api.v2.model.BlockType;
import org.semux.api.v2.model.DelegateType;
import org.semux.api.v2.model.InfoType;
import org.semux.api.v2.model.InternalTransactionType;
import org.semux.api.v2.model.LogInfoType;
import org.semux.api.v2.model.PeerType;
import org.semux.api.v2.model.TransactionLimitsType;
Expand All @@ -36,6 +39,7 @@
import org.semux.core.state.Delegate;
import org.semux.crypto.Hex;
import org.semux.net.Peer;
import org.semux.vm.client.Conversion;

public class TypeFactory {

Expand Down Expand Up @@ -154,13 +158,19 @@ public static TransactionType transactionType(Transaction tx) {
}

public static TransactionResultType transactionResultType(TransactionResult result) {
// gas price is in nano sem, not wei
BigInteger fee = BigInteger.valueOf(result.getGasUsed()).multiply(BigInteger.valueOf(result.getGasPrice()));
return new TransactionResultType()
.logs(result.getLogs().stream().map(TypeFactory::logInfoType).collect(Collectors.toList()))
.gasUsed(String.valueOf(result.getGasUsed()))
.gasPrice(String.valueOf(result.getGasPrice()))
.fee(encodeAmount(Amount.Unit.NANO_SEM.of(fee.longValue())))
.code(result.getCode().name())
.internalTransactions(result.getInternalTransactions().stream()
.map(TypeFactory::internalTransactionType).collect(Collectors.toList()))
.returnData(Hex.encode0x(result.getReturnData()));

// TODO: add block number and internal transactions
// TODO: add block number
}

private static LogInfoType logInfoType(LogInfo log) {
Expand All @@ -171,6 +181,14 @@ private static LogInfoType logInfoType(LogInfo log) {
.collect(Collectors.toList()));
}

private static InternalTransactionType internalTransactionType(InternalTransaction internalTransaction) {
return new InternalTransactionType()
.from(Hex.encode0x(internalTransaction.getFrom()))
.to(Hex.encode0x(internalTransaction.getTo()))
.type(internalTransaction.getType().toString())
.amount(encodeAmount(Conversion.weiToAmount(internalTransaction.getValue())));
}

public static String encodeAmount(Amount a) {
return a == null ? null : String.valueOf(a.getNano());
}
Expand Down
42 changes: 42 additions & 0 deletions src/main/resources/org/semux/api/swagger/v2.2.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,51 @@
"format": "int64",
"pattern": "^\\d+$"
},
"gasPrice": {
"type": "string",
"format": "int64",
"pattern": "^\\d+$"
},
"fee": {
"description": "Return the transaction fee in nano SEM",
"type": "string",
"format": "int64",
"pattern": "^\\d+$"
},
"code": {
"description": "The status of the transaction",
"type": "string"
},
"internalTransactions": {
"type": "array",
"items": {
"$ref": "#/definitions/InternalTransactionType"
}
}
}
},
"InternalTransactionType": {
"type": "object",
"properties": {
"type": {
"description" : "Transaction type",
"type": "string"
},
"from": {
"description" : "Sender address",
"type": "string",
"pattern": "^(0x)?[0-9a-fA-F]{40}$"
},
"to": {
"description" : "Receiver address",
"type": "string",
"pattern": "^(0x)?[0-9a-fA-F]{40}$"
},
"amount": {
"description" : "Transaction amount",
"type": "string",
"format": "int64",
"pattern": "^\\d+$"
}
}
},
Expand Down

0 comments on commit dd20ba7

Please sign in to comment.