Skip to content

Commit

Permalink
Merge pull request #3 from zksync-sdk/develop
Browse files Browse the repository at this point in the history
Fix deploy contract transactions. Update integration tests
  • Loading branch information
MaximFischuk authored Nov 17, 2022
2 parents db245b9 + 5e4730d commit 0007b50
Show file tree
Hide file tree
Showing 13 changed files with 388 additions and 285 deletions.
16 changes: 11 additions & 5 deletions src/main/java/io/zksync/methods/request/Transaction.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public Transaction(String from, String to, BigInteger gas, BigInteger gasPrice,
this.transactionType = (long) Transaction712.EIP_712_TX_TYPE;
}

public static Transaction createEtherTransaction(
String from,
BigInteger ergsPrice,
BigInteger ergsLimit,
String to,
BigInteger value) {

Eip712Meta meta = new Eip712Meta(BigInteger.valueOf(160000L), null, null, null);
return new Transaction(from, to, ergsPrice, ergsLimit, value, "0x", meta);
}

public static Transaction create2ContractTransaction(
String from,
BigInteger ergsPrice,
Expand Down Expand Up @@ -295,11 +306,6 @@ public static Transaction createFunctionCallTransaction(
return new Transaction(from, to, ergsPrice, ergsLimit, value, data, meta);
}

public static org.web3j.protocol.core.methods.request.Transaction createEthCallTransaction(String from, String to, String data) {

return org.web3j.protocol.core.methods.request.Transaction.createEthCallTransaction(from, to, data);
}

public String getFrom() {
return from;
}
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/io/zksync/transaction/fee/Fee.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public Fee() {
this.ergsPerPubdataLimit = Uint256.DEFAULT;
}

public Fee(BigInteger ergsLimit, BigInteger maxFeePerErg, BigInteger maxPriorityFeePerErg, BigInteger ergsPerPubdataLimit) {
this.ergsLimit = new Uint256(ergsLimit);
this.maxFeePerErg = new Uint256(maxFeePerErg);
this.maxPriorityFeePerErg = new Uint256(maxPriorityFeePerErg);
this.ergsPerPubdataLimit = new Uint256(ergsPerPubdataLimit);
}

public BigInteger getErgsLimitNumber() {
return ergsLimit.getValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public EthSendTransaction sendEIP1559Transaction(long chainId, BigInteger maxPri
public String sendCall(String to, String data, DefaultBlockParameter defaultBlockParameter) throws IOException {
EthCall ethCall =
zkSync.ethCall(
Transaction.createEthCallTransaction(getFromAddress(), to, data),
org.web3j.protocol.core.methods.request.Transaction.createEthCallTransaction(getFromAddress(), to, data),
defaultBlockParameter)
.send();

Expand Down
44 changes: 13 additions & 31 deletions src/main/java/io/zksync/utils/ContractDeployer.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,20 @@ public static Address extractContractAddress(TransactionReceipt receipt) {
public static byte[] hashBytecode(byte[] bytecode) {
byte[] bytecodeHash = Hash.sha256(bytecode);

if (bytecode.length % 32 != 0) {
throw new IllegalArgumentException("The bytecode length in bytes must be divisible by 32");
}

BigInteger length = BigInteger.valueOf(bytecode.length / 32);
if (length.compareTo(MAX_BYTECODE_SIZE) > 0) {
throw new IllegalArgumentException("Bytecode length must be less than 2^16 bytes");
}

byte[] codeHashVersion = new byte[] { 1, 0 };
byte[] bytecodeLength = Numeric.toBytesPadded(length, 2);

System.arraycopy(bytecodeLength, 0, bytecodeHash, 0, bytecodeLength.length);
System.arraycopy(codeHashVersion, 0, bytecodeHash, 0, codeHashVersion.length);
System.arraycopy(bytecodeLength, 0, bytecodeHash, 2, bytecodeLength.length);

return bytecodeHash;
}
Expand Down Expand Up @@ -157,7 +163,7 @@ public static Function encodeCreate2(byte[] bytecode, byte[] calldata, byte[] sa

return new Function(
"create2",
Arrays.asList(new Bytes32(salt), new Bytes32(bytecodeHash), new Uint256(0), new DynamicBytes(calldata)),
Arrays.asList(new Bytes32(salt), new Bytes32(bytecodeHash), new DynamicBytes(calldata)),
Collections.emptyList()
);
}
Expand All @@ -169,7 +175,7 @@ public static Function encodeCreate2(byte[] bytecode, byte[] calldata, byte[] sa
* @return Encoded contract function
*/
public static Function encodeCreate(byte[] bytecode) {
return encodeCreate(bytecode, new byte[] {}, new byte[32]);
return encodeCreate(bytecode, new byte[] {});
}

/**
Expand All @@ -180,23 +186,11 @@ public static Function encodeCreate(byte[] bytecode) {
* @return Encoded contract function
*/
public static Function encodeCreate(byte[] bytecode, byte[] calldata) {
return encodeCreate(bytecode, calldata, new byte[32]);
}

/**
* Encode `create` deployment function of default factory contract
*
* @param bytecode Compiled bytecode of the contract
* @param calldata Encoded constructor parameters
* @param salt 32 bytes salt
* @return Encoded contract function
*/
public static Function encodeCreate(byte[] bytecode, byte[] calldata, byte[] salt) {
byte[] bytecodeHash = hashBytecode(bytecode);

return new Function(
"create",
Arrays.asList(new Bytes32(new byte[32]), new Bytes32(bytecodeHash), new Uint256(0), new DynamicBytes(calldata)),
Arrays.asList(new Bytes32(new byte[32]), new Bytes32(bytecodeHash), new DynamicBytes(calldata)),
Collections.emptyList()
);
}
Expand Down Expand Up @@ -236,7 +230,7 @@ public static Function encodeCreate2Account(byte[] bytecode, byte[] calldata, by

return new Function(
"create2Account",
Arrays.asList(new Bytes32(salt), new Bytes32(bytecodeHash), new Uint256(0), new DynamicBytes(calldata)),
Arrays.asList(new Bytes32(salt), new Bytes32(bytecodeHash), new DynamicBytes(calldata)),
Collections.emptyList()
);
}
Expand All @@ -248,7 +242,7 @@ public static Function encodeCreate2Account(byte[] bytecode, byte[] calldata, by
* @return Encoded contract function
*/
public static Function encodeCreateAccount(byte[] bytecode) {
return encodeCreateAccount(bytecode, new byte[] {}, new byte[32]);
return encodeCreateAccount(bytecode, new byte[] {});
}

/**
Expand All @@ -259,23 +253,11 @@ public static Function encodeCreateAccount(byte[] bytecode) {
* @return Encoded contract function
*/
public static Function encodeCreateAccount(byte[] bytecode, byte[] calldata) {
return encodeCreateAccount(bytecode, calldata, new byte[32]);
}

/**
* Encode `create` deployment custom account function of default factory contract (see <a href="https://eips.ethereum.org/EIPS/eip-4337">EIP-4337</a>)
*
* @param bytecode Compiled bytecode of the Custom Account contract
* @param calldata Encoded constructor parameters
* @param salt 32 bytes salt
* @return Encoded contract function
*/
public static Function encodeCreateAccount(byte[] bytecode, byte[] calldata, byte[] salt) {
byte[] bytecodeHash = hashBytecode(bytecode);

return new Function(
"createAccount",
Arrays.asList(new Bytes32(new byte[32]), new Bytes32(bytecodeHash), new Uint256(0), new DynamicBytes(calldata)),
Arrays.asList(new Bytes32(new byte[32]), new Bytes32(bytecodeHash), new DynamicBytes(calldata)),
Collections.emptyList()
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/io/zksync/abi/ZkTransactionEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public void testEncodeDeploy() {
)
);

String expected = "0x71f907c6802b2b2a94000000000000000000000000000000000000800680b8a41415dae2000000000000000000000000000000000000000000000000000000000000000000379c09b5568d43b0ac6533a2672ee836815530b412f082f0b2e69915aa50fc00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000082010e808082010e947e5f4552091a69125d5dfcb7b8c2659029395bdf80f906e3b906e00000002b04000041000000000141016f0000002c0400004100000000001403760000002d010000410000000000210376000000000130004c000000090000613d00a5000a0000034f00a5001f0000034f0000008001000039000000400200003900000000001203760000000001000357000000000110004c0000001d0000c13d0000002d010000410000000001010375000000000110004c000000180000c13d00000080010000390000000002000019000000000300001900a500960000034f0000002001000039000000000010037600000000000103760000002e01000041000000a6000103700000000001000019000000a70001037200010000000000020000008006000039000000400500003900000000006503760000002d010000410000000001010375000000040110008c0000005a0000413d0000002c01000041000000000101037500000000010103770000002f02000041000000000121016f000000300210009c000000440000c13d0000000001000357000000000110004c0000005c0000c13d0000002d010000410000000001010375000000040110008a000000010200008a0000003203000041000000000221004b00000000020000190000000002032019000000000131016f000000000431013f000000320110009c00000000010000190000000001034019000000320340009c000000000102c019000000000110004c0000005e0000c13d0000000001000019000000a700010372000000310110009c0000005a0000c13d0000000001000357000000000110004c000000650000c13d0000002d010000410000000001010375000000040110008a00000032020000410000001f0310008c00000000030000190000000003022019000000000121016f000000000410004c0000000002008019000000320110009c00000000010300190000000001026019000000000110004c000000670000c13d0000000001000019000000a7000103720000000001000019000000a7000103720000000001000019000000a7000103720000000001000019000100000006001d00a5008b0000034f000000010200002900000000001203760000003401000041000000a6000103700000000001000019000000a7000103720000002c01000041000000000101037500000004011000390000000001010377000100000005001d00a500720000034f000000010100002900000000010103750000003302000041000000000121016f000000a6000103700002000000000002000000010200008a000100000001001d000000000121013f000200000001001d000000000100001900a5008b0000034f0000000202000029000000000221004b000000820000213d00000001020000290000000001210019000000000200001900a500890000034f0000000200000005000000000001036f000000350100004100000000001003760000001101000039000000040200003900000000001203760000003601000041000000a700010372000000000012035b000000000001036f0000000001010359000000000001036f000000000401037500000000043401cf000000000434022f0000010003300089000000000232022f00000000023201cf000000000242019f0000000000210376000000000001036f0000000504300270000000000540004c0000009e0000613d00000000002103760000002001100039000000010440008a000000000540004c000000990000c13d0000001f0330018f000000000430004c000000a40000613d000000030330021000a5008d0000034f000000000001036f000000000001036f000000a500000374000000a600010370000000a700010372000000000000e001000000000000e001000000000000e001000000000000e0010000000000000000000000000000000000000000000000000000000000ffffff0000000000000000000000000000000000000000000000000000000000ffffe00000000000000000000000000000000000000000000000000000000000ffffc00000000000000000000000000000000000000000000000400000000000000000ffffffff000000000000000000000000000000000000000000000000000000006d4ce63c000000000000000000000000000000000000000000000000000000007cf5dab0000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffffffffffff00000000000000000000000000000000000000000000002000000000000000804e487b7100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024000000000000000080c0";
String expected = "0x71f90ae6802b2b2a94000000000000000000000000000000000000800680b8843cda33510000000000000000000000000000000000000000000000000000000000000000010000517112c421df08d7b49e4dc1312f4ee62268ee4f5683b11d9e2d33525a0000000000000000000000000000000000000000000000000000000000000060000000000000000000000000000000000000000000000000000000000000000082010e808082010e947e5f4552091a69125d5dfcb7b8c2659029395bdf80f90a23b90a20000200000000000200010000000103550000006001100270000000410010019d000000010120018f000000000110004c000000080000c13d00fd00180000040f00fd00090000040f0000008001000039000000400200003900000000001204350000000001000416000000000110004c000000160000c13d000000200100003900000100020000390000000000120439000001200100003900000000000104390000004201000041000000fe0001042e0000000001000019000000ff0001043000040000000000020000000001000410000080020210008c000000330000613d0000000002000411000080010220008c000000330000613d0000004302000041000000000020043900000004020000390000000000120439000000440100004100008002020000390000000003000415000000040330008a00000020033000c900fd00e00000040f000000ff01000039000000030110024f000000000110004c000000560000613d000000040100035f000000000101043b000000000110004c000000330000c13d0000000001000019000000fe0001042e0000008001000039000000400600003900000000001604350000000001000031000000030210008c000000540000a13d0000000102000367000000000302043b000000e003300270000000450430009c0000006c0000613d000000460230009c000000580000613d000000470230009c000000540000c13d0000000002000416000000000220004c000000800000c13d000000040110008a00000048020000410000001f0310008c000000000300001900000000030220190000004801100197000000000410004c0000000002008019000000480110009c00000000010300190000000001026019000000000110004c0000008e0000c13d0000000001000019000000ff000104300000000001000019000000ff000104300000000001000019000000ff000104300000000002000416000000000220004c0000007e0000c13d000000040110008a000000010200008a0000004803000041000000000221004b000000000200001900000000020320190000004801100197000000480410009c00000000030080190000004801100167000000480110009c00000000010200190000000001036019000000000110004c000000840000c13d0000000001000019000000ff000104300000000003000416000000000330004c000000820000c13d000000040110008a00000048030000410000003f0410008c000000000400001900000000040320190000004801100197000000000510004c0000000003008019000000480110009c00000000010400190000000001036019000000000110004c000000a20000c13d0000000001000019000000ff000104300000000001000019000000ff000104300000000001000019000000ff000104300000000001000019000000ff000104300000000001000019000200000006001d00fd00fb0000040f000000020200002900000000020204330000000000120435000000400120021000000049011001970000004c011001c7000000fe0001042e000200000006001d000000000100001900fd00fb0000040f00000001020003670000000402200370000000000202043b0000000001120019000000000221004b00000000020000190000000102004039000000010220018f000000000220004c000000be0000613d0000004a0100004100000000001004350000001101000039000000040200003900000000001204350000004b01000041000000ff000104300000002401200370000000000201043b000000000120004c0000000001000019000000010100c039000000000112004b000000c50000c13d000100000002001d000200000006001d000000000100001900fd00fb0000040f00000001020003670000000402200370000000000202043b0000000001120019000000000221004b00000000020000190000000102004039000000010220018f000000000220004c000000c70000613d0000004a0100004100000000001004350000001101000039000000040200003900000000001204350000004b01000041000000ff00010430000000000200001900fd00f90000040f0000000201000029000000000101043300000040011002100000004901100197000000fe0001042e0000000001000019000000ff00010430000000000200001900fd00f90000040f000000020100002900000000010104330000000102000029000000000220004c000000d10000c13d00000040011002100000004901100197000000fe0001042e00000044021000390000004d03000041000000000032043500000024021000390000001a0300003900000000003204350000004e020000410000000000210435000000040210003900000020030000390000000000320435000000400110021000000049011001970000004f011001c7000000ff000104300002000000000002000200000003001d0000002003300039000100000003001d000000ef002104230000000203000029000000200230011a000000000201035500000048010000410000000102000029000000200220011a00000000021201bd00000000010300190000000200000005000000000001042d0000000203000029000000200230011a000000000201035500000050010000410000000102000029000000200220011a000000000212018d00000000010300190000000200000005000000000001042d000000000012041b000000000001042d000000000101041a000000000001042d000000fd00000432000000fe0001042e000000ff00010430000000000000000100000000000000010000000000000001000000000000000100000000000000000000000000000000000000000000000000000000ffffffff00000002000000000000000000000000000000400000010000000000000000001806aa1896bbf26568e884a7374b41e002500962caba6a15023a8d90e8508b830200020000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000000000000000000000000436dad6000000000000000000000000000000000000000000000000000000006d4ce63c000000000000000000000000000000000000000000000000000000007cf5dab080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000ffffffff00000000000000004e487b71000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024000000000000000000000000000000000000000000000000000000000000002000000000000000000000000054686973206d6574686f6420616c77617973207265766572747300000000000008c379a00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000640000000000000000000000007fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff80c0";

assertEquals(expected, Numeric.toHexString(TransactionEncoder.encode(transaction, null)));
}
Expand Down
Loading

0 comments on commit 0007b50

Please sign in to comment.