Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: Add internal transactions to API #188

Merged
merged 3 commits into from
Jul 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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+$"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another field to indicate a success or failure? I was also thinking about the depth and index

}
}
},
Expand Down