Skip to content

Commit

Permalink
Send tokens via token manager
Browse files Browse the repository at this point in the history
  • Loading branch information
DimaStebaev committed Jan 8, 2025
1 parent ab288d8 commit acc6e19
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 34 deletions.
16 changes: 14 additions & 2 deletions contracts/schain/ExecutionLayer/ExecutionManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,24 @@ contract ExecutionManager is AccessControlEnumerableUpgradeable, IExecutionManag
}

function _sendNextMetaAction(MetaActionContainer storage metaAction, TokenInfo[] memory tokens) private {
console.log("_sendNextMetaAction");
if (metaAction.metaAction.hasNextMetaAction()) {
console.log("has next");
Protocol.MetaAction memory nextMetaAction = Protocol.decodeMetaAction(metaAction.metaAction.nextMetaAction);
SchainHash targetChainHash = nextMetaAction.targetChainHash;
erc20TokenManager.messageProxy().postOutgoingMessage(
address remoteExecutionManagerAddress = address(_getRemoteExecutionManager(targetChainHash));

for (uint256 i = 0; i < tokens.length; ++i) {
erc20TokenManager.transferToSchainERC20Direct(
targetSchainName,
tokens[i].token,
tokens[i].number,
remoteExecutionManagerAddress);
}

erc20TokenManager.messageProxy.postOutgoingMessage(
targetChainHash,
address(_getRemoteExecutionManager(targetChainHash)),
remoteExecutionManagerAddress,
Protocol.encodeMetaActionMessage(metaAction.id, nextMetaAction, tokens)
);
} else {
Expand Down
85 changes: 53 additions & 32 deletions test/ExecutionManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { SignerWithAddress } from "@nomicfoundation/hardhat-ethers/signers";
import { ethers } from "hardhat";
import { ExecutionManager, MessageProxyForSchain, Protocol, TokenManagerERC20 } from "../typechain";
import { ExecutionManager, MessageProxyForSchain, Protocol, TokenManagerERC20, TokenManagerLinker } from "../typechain";
import { deployExecutionManager } from "./utils/deploy/schain/executionManager";
import { AgentMock } from "./utils/agent/AgentMock";
import { deployMessageProxyForSchainTester } from "./utils/deploy/test/messageProxyForSchainTester";
Expand All @@ -11,9 +11,10 @@ import { deployTokenManagerLinker } from "./utils/deploy/schain/tokenManagerLink
import { deployCommunityLocker } from "./utils/deploy/schain/communityLocker";

interface SchainSetup {
messageProxy: MessageProxyForSchain,
executionManager: ExecutionManager,
tokenManager: TokenManagerERC20
messageProxy: MessageProxyForSchain;
executionManager: ExecutionManager;
tokenManager: TokenManagerERC20;
tokenManagerLinker: TokenManagerLinker;
}

enum MetaActionStatus {
Expand All @@ -39,7 +40,12 @@ describe("ExecutionManager", () => {

const executionManager = await deployExecutionManager(messageProxy);

return { messageProxy, executionManager, tokenManager: tokenManagerErc20 }
return {
messageProxy,
executionManager,
tokenManager: tokenManagerErc20,
tokenManagerLinker
}
}

const setupMultipleSchains = async (quantity: number) => {
Expand All @@ -50,17 +56,18 @@ describe("ExecutionManager", () => {
}

for (const [schainName, schainSetup] of schains) {
await schainSetup.executionManager.grantRole(
await schainSetup.executionManager.CONTROLLER_ROLE(),
deployer.address
);
await schainSetup.tokenManager.grantRole(
await schainSetup.executionManager.CONTROLLER_ROLE(),
deployer.address
await schainSetup.messageProxy.registerExtraContractForAll(
schainSetup.tokenManager
);
await schainSetup.messageProxy.registerExtraContractForAll(
schainSetup.executionManager
);
await schainSetup.tokenManagerLinker.registerTokenManager(schainSetup.tokenManager);
await schainSetup.tokenManager.enableAutomaticDeploy();
await schainSetup.executionManager.grantRole(
await schainSetup.executionManager.CONTROLLER_ROLE(),
deployer.address
);
for (const [remoteSchainName, remoteSchainSetup] of schains) {
if (schainName !== remoteSchainName) {
await schainSetup.messageProxy.addConnectedChain(remoteSchainName);
Expand Down Expand Up @@ -178,26 +185,40 @@ describe("ExecutionManager", () => {
const value = ethers.parseEther("1");
await token.mint(user, value);

await targetTokenManager.connect(user).transferToSchainERC20(sourceSchainName, token, value);

// const send = await ethers.getContractAt(
// "Send",
// await sourceExecutionManager.getExecutorAddress(
// ethers.id("Send")
// )
// );
// const metaAction = (await sourceExecutionManager.createMetaAction(
// targetSchainHash,
// [{
// executor: ethers.id("Send"),
// arguments: await send.encodeArguments(user)
// }]
// )).toObject();

// const executeReceipt = await (await sourceExecutionManager.connect(user).execute(
// metaAction
// )).wait();
// assert(executeReceipt);
await token.connect(user).approve(targetTokenManager, value);
await targetTokenManager.connect(user).transferToSchainERC20(
sourceSchainName,
token, value
);

await agent.deliverMessages();

const cloneAddress = await sourceTokenManager.clonesErc20(targetSchainHash, token);
const clone = await ethers.getContractAt("ERC20OnChain", cloneAddress);

expect(await token.balanceOf(user)).to.be.equal(0n);
expect(await clone.balanceOf(user)).to.be.equal(value);

const send = await ethers.getContractAt(
"Send",
await sourceExecutionManager.getExecutorAddress(
ethers.id("Send")
)
);
const metaAction = (await sourceExecutionManager.createMetaAction(
targetSchainHash,
[{
executor: ethers.id("Send"),
arguments: await send.encodeArguments(user)
}]
)).toObject();

await clone.connect(user).approve(sourceExecutionManager, value);
const executeReceipt = await (await sourceExecutionManager.connect(user).execute(
metaAction,
[{token: clone, number: value}]
)).wait();
assert(executeReceipt);
// let metaActionId = "0x";
// for (const log of executeReceipt.logs) {
// const event = sourceExecutionManager.interface.parseLog(log);
Expand Down

0 comments on commit acc6e19

Please sign in to comment.