Skip to content

Commit e00ac72

Browse files
authored
zkgm: use same encoding in all impls (#3835)
2 parents 7a03fa4 + 5e8832f commit e00ac72

File tree

2 files changed

+44
-129
lines changed

2 files changed

+44
-129
lines changed

evm/contracts/apps/ucs/03-zkgm/Zkgm.sol

+42-127
Original file line numberDiff line numberDiff line change
@@ -361,22 +361,14 @@ contract UCS03Zkgm is
361361
return address(ibcHandler);
362362
}
363363

364-
function transferAndCall(
364+
function internalSendOverChannel(
365365
uint32 channelId,
366366
bytes calldata receiver,
367367
address baseToken,
368368
uint256 baseAmount,
369369
bytes calldata quoteToken,
370-
uint256 quoteAmount,
371-
bytes calldata contractAddress,
372-
bytes calldata contractCalldata,
373-
uint64 timeoutHeight,
374-
uint64 timeoutTimestamp,
375-
bytes32 salt
376-
) public {
377-
if (baseAmount == 0) {
378-
revert ZkgmLib.ErrInvalidAmount();
379-
}
370+
uint256 quoteAmount
371+
) internal returns (Instruction memory) {
380372
uint256 origin = tokenOrigin[baseToken];
381373
// Verify the unwrap
382374
(address wrappedToken,) =
@@ -397,13 +389,14 @@ contract UCS03Zkgm is
397389
);
398390
channelBalance[channelId][address(baseToken)] += baseAmount;
399391
}
392+
400393
// TODO: make this non-failable as it's not guaranteed to exist
401-
Instruction[] memory instructions = new Instruction[](2);
402394
IERC20Metadata sentTokenMeta = IERC20Metadata(baseToken);
403395
string memory symbol = sentTokenMeta.symbol();
404396
string memory name = sentTokenMeta.name();
405397
uint8 decimals = sentTokenMeta.decimals();
406-
instructions[0] = Instruction({
398+
399+
return Instruction({
407400
version: ZkgmLib.INSTR_VERSION_1,
408401
opcode: ZkgmLib.OP_FUNGIBLE_ASSET_ORDER,
409402
operand: ZkgmLib.encodeFungibleAssetOrder(
@@ -421,6 +414,28 @@ contract UCS03Zkgm is
421414
})
422415
)
423416
});
417+
}
418+
419+
function transferAndCall(
420+
uint32 channelId,
421+
bytes calldata receiver,
422+
address baseToken,
423+
uint256 baseAmount,
424+
bytes calldata quoteToken,
425+
uint256 quoteAmount,
426+
bytes calldata contractAddress,
427+
bytes calldata contractCalldata,
428+
uint64 timeoutHeight,
429+
uint64 timeoutTimestamp,
430+
bytes32 salt
431+
) public {
432+
if (baseAmount == 0) {
433+
revert ZkgmLib.ErrInvalidAmount();
434+
}
435+
Instruction[] memory instructions = new Instruction[](2);
436+
instructions[0] = internalSendOverChannel(
437+
channelId, receiver, baseToken, baseAmount, quoteToken, quoteAmount
438+
);
424439
instructions[1] = Instruction({
425440
version: ZkgmLib.INSTR_VERSION_0,
426441
opcode: ZkgmLib.OP_MULTIPLEX,
@@ -453,39 +468,6 @@ contract UCS03Zkgm is
453468
);
454469
}
455470

456-
function call(
457-
uint32 channelId,
458-
bytes calldata contractAddress,
459-
bytes calldata contractCalldata,
460-
uint64 timeoutHeight,
461-
uint64 timeoutTimestamp,
462-
bytes32 salt
463-
) public {
464-
ibcHandler.sendPacket(
465-
channelId,
466-
timeoutHeight,
467-
timeoutTimestamp,
468-
ZkgmLib.encode(
469-
ZkgmPacket({
470-
salt: keccak256(abi.encodePacked(msg.sender, salt)),
471-
path: 0,
472-
instruction: Instruction({
473-
version: ZkgmLib.INSTR_VERSION_0,
474-
opcode: ZkgmLib.OP_MULTIPLEX,
475-
operand: ZkgmLib.encodeMultiplex(
476-
Multiplex({
477-
sender: abi.encodePacked(msg.sender),
478-
eureka: true,
479-
contractAddress: contractAddress,
480-
contractCalldata: contractCalldata
481-
})
482-
)
483-
})
484-
})
485-
)
486-
);
487-
}
488-
489471
function transferV2(
490472
uint32 channelId,
491473
bytes calldata receiver,
@@ -501,52 +483,11 @@ contract UCS03Zkgm is
501483
if (baseAmount == 0) {
502484
revert ZkgmLib.ErrInvalidAmount();
503485
}
504-
uint256 origin = tokenOrigin[baseToken];
505-
// Verify the unwrap
506-
(address wrappedToken,) =
507-
internalPredictWrappedTokenMemory(0, channelId, quoteToken);
508-
// Only allow unwrapping if the quote asset is the unwrapped asset.
509-
if (
510-
ZkgmLib.lastChannelFromPath(origin) == channelId
511-
&& abi.encodePacked(baseToken).eq(abi.encodePacked(wrappedToken))
512-
) {
513-
IZkgmERC20(baseToken).burn(msg.sender, baseAmount);
514-
} else {
515-
// We reset the origin, the asset will not be unescrowed on the destination
516-
origin = 0;
517-
// TODO: extract this as a step before verifying to allow for ERC777
518-
// send hook
519-
SafeERC20.safeTransferFrom(
520-
IERC20(baseToken), msg.sender, address(this), baseAmount
521-
);
522-
channelBalance[channelId][address(baseToken)] += baseAmount;
523-
}
524-
525-
// TODO: make calls to sentTokenMeta non-failable as it's not guaranteed to exist
526-
IERC20Metadata sentTokenMeta = IERC20Metadata(baseToken);
527-
uint256 nbOfInstructions = 1;
528-
if (msg.value > 0) {
529-
nbOfInstructions = 2;
530-
}
531-
Instruction[] memory instructions = new Instruction[](nbOfInstructions);
532-
instructions[0] = Instruction({
533-
version: ZkgmLib.INSTR_VERSION_1,
534-
opcode: ZkgmLib.OP_FUNGIBLE_ASSET_ORDER,
535-
operand: ZkgmLib.encodeFungibleAssetOrder(
536-
FungibleAssetOrder({
537-
sender: abi.encodePacked(msg.sender),
538-
receiver: receiver,
539-
baseToken: abi.encodePacked(baseToken),
540-
baseTokenPath: origin,
541-
baseTokenSymbol: sentTokenMeta.symbol(),
542-
baseTokenName: sentTokenMeta.name(),
543-
baseTokenDecimals: sentTokenMeta.decimals(),
544-
baseAmount: baseAmount,
545-
quoteToken: quoteToken,
546-
quoteAmount: quoteAmount
547-
})
548-
)
549-
});
486+
Instruction[] memory instructions =
487+
new Instruction[](msg.value > 0 ? 2 : 1);
488+
instructions[0] = internalSendOverChannel(
489+
channelId, receiver, baseToken, baseAmount, quoteToken, quoteAmount
490+
);
550491
if (msg.value > 0) {
551492
weth.deposit{value: msg.value}();
552493
address wethBaseToken = address(weth);
@@ -897,15 +838,13 @@ contract UCS03Zkgm is
897838
return executeFungibleAssetOrder(
898839
ibcPacket,
899840
relayer,
900-
relayerMsg,
901-
salt,
902841
path,
903-
order.sender,
904842
order.receiver,
905843
order.baseToken,
906844
order.baseAmount,
907845
order.baseTokenSymbol,
908846
order.baseTokenName,
847+
0,
909848
order.baseTokenPath,
910849
order.quoteToken,
911850
order.quoteAmount
@@ -916,15 +855,13 @@ contract UCS03Zkgm is
916855
return executeFungibleAssetOrder(
917856
ibcPacket,
918857
relayer,
919-
relayerMsg,
920-
salt,
921858
path,
922-
order.sender,
923859
order.receiver,
924860
order.baseToken,
925861
order.baseAmount,
926862
order.baseTokenSymbol,
927863
order.baseTokenName,
864+
order.baseTokenDecimals,
928865
order.baseTokenPath,
929866
order.quoteToken,
930867
order.quoteAmount
@@ -1099,15 +1036,13 @@ contract UCS03Zkgm is
10991036
function executeFungibleAssetOrder(
11001037
IBCPacket calldata ibcPacket,
11011038
address relayer,
1102-
bytes calldata relayerMsg,
1103-
bytes32 salt,
11041039
uint256 path,
1105-
bytes calldata orderSender,
11061040
bytes calldata orderReceiver,
11071041
bytes calldata orderBaseToken,
11081042
uint256 orderBaseAmount,
11091043
string calldata orderBaseTokenSymbol,
11101044
string calldata orderBaseTokenName,
1045+
uint8 orderBaseTokenDecimals,
11111046
uint256 orderBaseTokenPath,
11121047
bytes calldata orderQuoteToken,
11131048
uint256 orderQuoteAmount
@@ -1133,6 +1068,7 @@ contract UCS03Zkgm is
11331068
abi.encode(
11341069
orderBaseTokenName,
11351070
orderBaseTokenSymbol,
1071+
orderBaseTokenDecimals,
11361072
address(this)
11371073
)
11381074
),
@@ -1403,16 +1339,13 @@ contract UCS03Zkgm is
14031339
delete inFlightPacket[packetHash];
14041340
} else {
14051341
ZkgmPacket calldata zkgmPacket = ZkgmLib.decode(ibcPacket.data);
1406-
timeoutInternal(
1407-
ibcPacket, relayer, zkgmPacket.salt, zkgmPacket.instruction
1408-
);
1342+
timeoutInternal(ibcPacket, relayer, zkgmPacket.instruction);
14091343
}
14101344
}
14111345

14121346
function timeoutInternal(
14131347
IBCPacket calldata ibcPacket,
14141348
address relayer,
1415-
bytes32 salt,
14161349
Instruction calldata instruction
14171350
) internal {
14181351
if (instruction.opcode == ZkgmLib.OP_FUNGIBLE_ASSET_ORDER) {
@@ -1424,8 +1357,6 @@ contract UCS03Zkgm is
14241357
ZkgmLib.decodeFungibleAssetOrderV0(instruction.operand);
14251358
timeoutFungibleAssetOrder(
14261359
ibcPacket,
1427-
relayer,
1428-
salt,
14291360
order.sender,
14301361
order.baseToken,
14311362
order.baseTokenPath,
@@ -1436,8 +1367,6 @@ contract UCS03Zkgm is
14361367
ZkgmLib.decodeFungibleAssetOrder(instruction.operand);
14371368
timeoutFungibleAssetOrder(
14381369
ibcPacket,
1439-
relayer,
1440-
salt,
14411370
order.sender,
14421371
order.baseToken,
14431372
order.baseTokenPath,
@@ -1449,30 +1378,21 @@ contract UCS03Zkgm is
14491378
revert ZkgmLib.ErrUnsupportedVersion();
14501379
}
14511380
timeoutBatch(
1452-
ibcPacket,
1453-
relayer,
1454-
salt,
1455-
ZkgmLib.decodeBatch(instruction.operand)
1381+
ibcPacket, relayer, ZkgmLib.decodeBatch(instruction.operand)
14561382
);
14571383
} else if (instruction.opcode == ZkgmLib.OP_FORWARD) {
14581384
if (instruction.version > ZkgmLib.INSTR_VERSION_0) {
14591385
revert ZkgmLib.ErrUnsupportedVersion();
14601386
}
14611387
timeoutForward(
1462-
ibcPacket,
1463-
relayer,
1464-
salt,
1465-
ZkgmLib.decodeForward(instruction.operand)
1388+
ibcPacket, relayer, ZkgmLib.decodeForward(instruction.operand)
14661389
);
14671390
} else if (instruction.opcode == ZkgmLib.OP_MULTIPLEX) {
14681391
if (instruction.version > ZkgmLib.INSTR_VERSION_0) {
14691392
revert ZkgmLib.ErrUnsupportedVersion();
14701393
}
14711394
timeoutMultiplex(
1472-
ibcPacket,
1473-
relayer,
1474-
salt,
1475-
ZkgmLib.decodeMultiplex(instruction.operand)
1395+
ibcPacket, relayer, ZkgmLib.decodeMultiplex(instruction.operand)
14761396
);
14771397
} else {
14781398
revert ZkgmLib.ErrUnknownOpcode();
@@ -1482,15 +1402,14 @@ contract UCS03Zkgm is
14821402
function timeoutBatch(
14831403
IBCPacket calldata ibcPacket,
14841404
address relayer,
1485-
bytes32 salt,
14861405
Batch calldata batch
14871406
) internal {
14881407
uint256 l = batch.instructions.length;
14891408
for (uint256 i = 0; i < l; i++) {
14901409
timeoutInternal(
14911410
ibcPacket,
14921411
relayer,
1493-
keccak256(abi.encode(i, salt)),
1412+
// keccak256(abi.encode(i, salt)),
14941413
batch.instructions[i]
14951414
);
14961415
}
@@ -1499,14 +1418,12 @@ contract UCS03Zkgm is
14991418
function timeoutForward(
15001419
IBCPacket calldata ibcPacket,
15011420
address relayer,
1502-
bytes32 salt,
15031421
Forward calldata forward
15041422
) internal {}
15051423

15061424
function timeoutMultiplex(
15071425
IBCPacket calldata ibcPacket,
15081426
address relayer,
1509-
bytes32 salt,
15101427
Multiplex calldata multiplex
15111428
) internal {
15121429
if (!multiplex.eureka) {
@@ -1527,8 +1444,6 @@ contract UCS03Zkgm is
15271444

15281445
function timeoutFungibleAssetOrder(
15291446
IBCPacket calldata ibcPacket,
1530-
address relayer,
1531-
bytes32 salt,
15321447
bytes calldata orderSender,
15331448
bytes calldata orderBaseToken,
15341449
uint256 orderBaseTokenPath,

evm/contracts/apps/ucs/03-zkgm/ZkgmERC20.sol

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ contract ZkgmERC20 is ERC20, IZkgmERC20 {
1212
constructor(
1313
string memory n,
1414
string memory s,
15-
address a,
16-
uint8 d
15+
uint8 d,
16+
address a
1717
) ERC20(n, s) {
1818
admin = a;
1919
_decimals = d;

0 commit comments

Comments
 (0)