Skip to content

Commit 5e8832f

Browse files
chore(evm): refactor zkgm to fit max contract size
1 parent 1a4599c commit 5e8832f

File tree

1 file changed

+37
-104
lines changed

1 file changed

+37
-104
lines changed

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

+37-104
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);
@@ -1398,9 +1339,7 @@ contract UCS03Zkgm is
13981339
delete inFlightPacket[packetHash];
13991340
} else {
14001341
ZkgmPacket calldata zkgmPacket = ZkgmLib.decode(ibcPacket.data);
1401-
timeoutInternal(
1402-
ibcPacket, relayer, zkgmPacket.instruction
1403-
);
1342+
timeoutInternal(ibcPacket, relayer, zkgmPacket.instruction);
14041343
}
14051344
}
14061345

@@ -1439,27 +1378,21 @@ contract UCS03Zkgm is
14391378
revert ZkgmLib.ErrUnsupportedVersion();
14401379
}
14411380
timeoutBatch(
1442-
ibcPacket,
1443-
relayer,
1444-
ZkgmLib.decodeBatch(instruction.operand)
1381+
ibcPacket, relayer, ZkgmLib.decodeBatch(instruction.operand)
14451382
);
14461383
} else if (instruction.opcode == ZkgmLib.OP_FORWARD) {
14471384
if (instruction.version > ZkgmLib.INSTR_VERSION_0) {
14481385
revert ZkgmLib.ErrUnsupportedVersion();
14491386
}
14501387
timeoutForward(
1451-
ibcPacket,
1452-
relayer,
1453-
ZkgmLib.decodeForward(instruction.operand)
1388+
ibcPacket, relayer, ZkgmLib.decodeForward(instruction.operand)
14541389
);
14551390
} else if (instruction.opcode == ZkgmLib.OP_MULTIPLEX) {
14561391
if (instruction.version > ZkgmLib.INSTR_VERSION_0) {
14571392
revert ZkgmLib.ErrUnsupportedVersion();
14581393
}
14591394
timeoutMultiplex(
1460-
ibcPacket,
1461-
relayer,
1462-
ZkgmLib.decodeMultiplex(instruction.operand)
1395+
ibcPacket, relayer, ZkgmLib.decodeMultiplex(instruction.operand)
14631396
);
14641397
} else {
14651398
revert ZkgmLib.ErrUnknownOpcode();

0 commit comments

Comments
 (0)