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: show contract address in the transaction result response #219

Merged
merged 1 commit into from
Jul 18, 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
6 changes: 5 additions & 1 deletion src/main/java/org/semux/api/v2/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.stream.Collectors;

import org.ethereum.vm.LogInfo;
import org.ethereum.vm.util.HashUtil;
import org.semux.Kernel;
import org.semux.api.v2.model.AccountType;
import org.semux.api.v2.model.AccountVoteType;
Expand Down Expand Up @@ -171,7 +172,10 @@ public static TransactionResultType transactionResultType(Transaction tx, Transa
.code(result.getCode().name())
.internalTransactions(result.getInternalTransactions().stream()
.map(TypeFactory::internalTransactionType).collect(Collectors.toList()))
.returnData(Hex.encode0x(result.getReturnData()));
.returnData(Hex.encode0x(result.getReturnData()))
.contractAddress(
tx.getType().equals(CREATE) ? Hex.encode0x(HashUtil.calcNewAddress(tx.getFrom(), tx.getNonce()))
: null);
}

private static LogInfoType logInfoType(LogInfo log) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/org/semux/api/swagger/v2.3.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,11 @@
"type": "string",
"pattern": "^(0x)?[0-9a-fA-F]*$"
},
"contractAddress": {
"description": "Contract address if this is a CREATE transaction, or NULL",
"type": "string",
"pattern": "^(0x)?[0-9a-fA-F]{40}$"
},
"gas": {
"description": "The gas limit set by the sender",
"type": "string",
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/org/semux/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,12 @@ public static Transaction createTransaction(Config config, Key from, Key to, Amo
}

public static Transaction createTransaction(Config config, Key from, Key to, Amount value, long nonce) {
return createTransaction(config, TransactionType.TRANSFER, from, to, value, 0);
}

public static Transaction createTransaction(Config config, TransactionType type, Key from, Key to, Amount value,
long nonce) {
Network network = config.network();
TransactionType type = TransactionType.TRANSFER;
Amount fee = config.spec().minTransactionFee();
long timestamp = TimeUtil.currentTimeMillis();
byte[] data = {};
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/org/semux/api/v2/SemuxApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@

import javax.ws.rs.BadRequestException;

import org.ethereum.vm.util.HashUtil;
import org.ethereum.vm.util.HexUtil;
import org.junit.Test;
import org.semux.Network;
import org.semux.TestUtils;
Expand Down Expand Up @@ -458,7 +460,7 @@ public void getTransactionTest() {
@Test
public void getTransactionReceiptTest() {
Key from = new Key(), to = new Key();
Transaction tx = createTransaction(config, from, to, Amount.Unit.SEM.of(1));
Transaction tx = createTransaction(config, CREATE, from, to, Amount.Unit.SEM.of(1), 1);
TransactionResult res = new TransactionResult();
Block block = createBlock(chain.getLatestBlockNumber() + 1, Collections.singletonList(tx),
Collections.singletonList(res));
Expand All @@ -477,6 +479,8 @@ public void getTransactionReceiptTest() {
assertTrue(response.getResult().getLogs().isEmpty());
assertTrue(response.getResult().getInternalTransactions().isEmpty());
assertEquals("0x", response.getResult().getReturnData());
String address = Hex.encode0x(HashUtil.calcNewAddress(from.toAddress(), 1));
assertEquals(address, response.getResult().getContractAddress());
}

@Test
Expand Down