Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ public enum AbiFunction {
VOTE("vote"),
UNVOTE("unvote"),
VALIDATOR_REGISTRATION("registerValidator"),
VALIDATOR_RESIGNATION("resignValidator");
VALIDATOR_RESIGNATION("resignValidator"),
TRANSFER("transfer"),
APPROVE("approve");

private final String functionName;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.arkecosystem.crypto.transactions.builder;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.arkecosystem.crypto.enums.AbiFunction;
import org.arkecosystem.crypto.transactions.types.AbstractTransaction;
import org.arkecosystem.crypto.transactions.types.EvmCall;
import org.arkecosystem.crypto.utils.AbiEncoder;

public class TokenApproveBuilder extends AbstractTransactionBuilder<TokenApproveBuilder> {
public TokenApproveBuilder contractAddress(String address) {
this.transaction.recipientAddress = address;
return this.instance();
}

public TokenApproveBuilder spender(String address, BigInteger amount) {
List<Object> args = new ArrayList<>();
args.add(address);
args.add(amount);

try {
String payload =
new AbiEncoder("Abi.Token.json")
.encodeFunctionCall(AbiFunction.APPROVE.toString(), args);

this.transaction.data = payload.replaceFirst("^0x", "");
} catch (Exception e) {
throw new RuntimeException("Error encoding token approve", e);
}

return this.instance();
}

@Override
protected AbstractTransaction getTransactionInstance() {
return new EvmCall();
}

@Override
protected TokenApproveBuilder instance() {
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.arkecosystem.crypto.transactions.builder;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import org.arkecosystem.crypto.enums.AbiFunction;
import org.arkecosystem.crypto.transactions.types.AbstractTransaction;
import org.arkecosystem.crypto.transactions.types.EvmCall;
import org.arkecosystem.crypto.utils.AbiEncoder;

public class TokenTransferBuilder extends AbstractTransactionBuilder<TokenTransferBuilder> {
public TokenTransferBuilder contractAddress(String address) {
this.transaction.recipientAddress = address;
return this.instance();
}

public TokenTransferBuilder recipient(String address, BigInteger amount) {
List<Object> args = new ArrayList<>();
args.add(address);
args.add(amount);

try {
String payload =
new AbiEncoder("Abi.Token.json")
.encodeFunctionCall(AbiFunction.TRANSFER.toString(), args);

this.transaction.data = payload.replaceFirst("^0x", "");
} catch (Exception e) {
throw new RuntimeException("Error encoding token transfer", e);
}

return this.instance();
}

@Override
protected AbstractTransaction getTransactionInstance() {
return new EvmCall();
}

@Override
protected TokenTransferBuilder instance() {
return this;
}
}
4 changes: 3 additions & 1 deletion src/main/java/org/arkecosystem/crypto/utils/AbiBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ public abstract class AbiBase {
protected List<Map<String, Object>> abi;

public AbiBase() throws IOException {
String abiFilePath = "Abi.Consensus.json";
this("Abi.Consensus.json");
}

public AbiBase(String abiFilePath) throws IOException {
InputStream abiInputStream = getClass().getClassLoader().getResourceAsStream(abiFilePath);

ObjectMapper mapper = new ObjectMapper();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ public AbiEncoder() throws IOException {
super();
}

public AbiEncoder(String abiFilePath) throws IOException {
super(abiFilePath);
}

public String encodeFunctionCall(String functionName) throws Exception {
return encodeFunctionCall(functionName, Collections.emptyList());
}
Expand Down Expand Up @@ -266,7 +270,7 @@ private Map<String, Object> encodeNumber(Object value, boolean signed) throws Ex
}
}
String hex = bigValue.toString(16);
String encoded = String.format("%064s", hex);
String encoded = "0".repeat(Math.max(0, 64 - hex.length())) + hex;

return Map.of("dynamic", false, "encoded", "0x" + encoded);
}
Expand Down
Loading
Loading