Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fixed token flow #144

Merged
merged 2 commits into from
Nov 3, 2023
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
10 changes: 3 additions & 7 deletions contracts/interfaces/ITokenRegistrar.sol
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ interface ITokenRegistrar {
uint256 gasValue
) external payable;

function interchainTransferFrom(
bytes32 tokenId,
string calldata destinationChain,
bytes calldata destinationAddress,
uint256 amount,
uint256 gasValue
) external payable;
function tokenTransferFrom(bytes32 tokenId, uint256 amount) external payable;

function tokenApprove(bytes32 tokenId, uint256 amount) external payable;
}
26 changes: 10 additions & 16 deletions contracts/token-registrars/TokenRegistrar.sol
Original file line number Diff line number Diff line change
Expand Up @@ -214,24 +214,18 @@ contract TokenRegistrar is ITokenRegistrar, ITokenManagerType, Multicall, Upgrad
}
}

function interchainTransferFrom(
bytes32 tokenId,
string calldata destinationChain,
bytes calldata destinationAddress,
uint256 amount,
uint256 gasValue
) external payable {
address tokenAddress = service.interchainTokenAddress(tokenId);
function tokenTransferFrom(bytes32 tokenId, uint256 amount) external payable {
address tokenAddress = service.tokenAddress(tokenId);
IStandardizedToken token = IStandardizedToken(tokenAddress);

if (bytes(destinationChain).length == 0) {
token.safeTransferFrom(msg.sender, destinationAddress.toAddress(), amount);
} else {
token.safeTransferFrom(msg.sender, address(this), amount);
if (!token.approve(address(service), amount)) revert NotApproved(tokenAddress);
token.safeTransferFrom(msg.sender, address(this), amount);
}

// slither-disable-next-line arbitrary-send-eth
service.interchainTransfer{ value: gasValue }(tokenId, destinationChain, destinationAddress, amount, new bytes(0));
}
function tokenApprove(bytes32 tokenId, uint256 amount) external payable {
address tokenAddress = service.tokenAddress(tokenId);
IStandardizedToken token = IStandardizedToken(tokenAddress);
address tokenManager = service.tokenManagerAddress(tokenId);

token.safeCall(abi.encodeWithSelector(token.approve.selector, tokenManager, amount));
}
}
82 changes: 70 additions & 12 deletions test/TokenRegistrars.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,47 @@ describe('Token Registrars', () => {
.withArgs(service.address, destinationChain, service.address, keccak256(payload), payload);
});

it('Should transfer some tokens to the registrar', async () => {
const amount = 123456;

await deployToken();

const params = defaultAbiCoder.encode(['bytes', 'address'], ['0x', token.address]);

await expect(tokenRegistrar.registerCanonicalToken(token.address))
.to.emit(service, 'TokenManagerDeployed')
.withArgs(tokenId, tokenManagerAddress, LOCK_UNLOCK, params);

await token.approve(tokenRegistrar.address, amount).then((tx) => tx.wait());

await expect(tokenRegistrar.tokenTransferFrom(tokenId, amount))
.to.emit(token, 'Transfer')
.withArgs(wallet.address, tokenRegistrar.address, amount);
});

it('Should approve some tokens from the registrar to the token manager', async () => {
const amount = 123456;

await deployToken();

const params = defaultAbiCoder.encode(['bytes', 'address'], ['0x', token.address]);

await expect(tokenRegistrar.registerCanonicalToken(token.address))
.to.emit(service, 'TokenManagerDeployed')
.withArgs(tokenId, tokenManagerAddress, LOCK_UNLOCK, params);

tokenManagerAddress = await service.validTokenManagerAddress(tokenId);

await expect(tokenRegistrar.tokenApprove(tokenId, amount))
.to.emit(token, 'Approval')
.withArgs(tokenRegistrar.address, tokenManagerAddress, amount);
});

it('Should transfer some tokens through the registrar as the deployer', async () => {
const amount = 123456;
const destinationAddress = '0x57689403';
const gasValue = 45960;

await deployToken();

const params = defaultAbiCoder.encode(['bytes', 'address'], ['0x', token.address]);
Expand All @@ -105,20 +145,38 @@ describe('Token Registrars', () => {
.to.emit(service, 'TokenManagerDeployed')
.withArgs(tokenId, tokenManagerAddress, LOCK_UNLOCK, params);

await token.approve(tokenRegistrar.address, amount).then((tx) => tx.wait());

tokenManagerAddress = await service.validTokenManagerAddress(tokenId);

// TODO: fix test
// await token.approve(tokenRegistrar.address, amount).then((tx) => tx.wait());

// await expect(
// tokenRegistrar.interchainTransferFrom(tokenId, '', arrayify(wallet.address), amount, 0),
// )
// // .to.emit(service, 'InterchainTransfer')
// // .withArgs(tokenId, destinationChain, destinationAddress, amount)
// .to.emit(token, 'Transfer')
// .withArgs(wallet.address, tokenRegistrar.address, amount)
// .to.emit(token, 'Transfer')
// .withArgs(tokenRegistrar.address, wallet.address, amount);
const txs = [];

txs.push(await tokenRegistrar.populateTransaction.tokenTransferFrom(tokenId, amount));
txs.push(await tokenRegistrar.populateTransaction.tokenApprove(tokenId, amount));
txs.push(
await tokenRegistrar.populateTransaction.interchainTransfer(
tokenId,
destinationChain,
destinationAddress,
amount,
gasValue,
),
);

await expect(
tokenRegistrar.multicall(
txs.map((tx) => tx.data),
{ value: gasValue },
),
)
.to.emit(token, 'Transfer')
.withArgs(wallet.address, tokenRegistrar.address, amount)
.and.to.emit(token, 'Approval')
.withArgs(tokenRegistrar.address, tokenManagerAddress, amount)
.and.to.emit(token, 'Transfer')
.withArgs(tokenRegistrar.address, tokenManagerAddress, amount)
.and.to.emit(service, 'InterchainTransfer')
.withArgs(tokenId, destinationChain, destinationAddress, amount);
});
});

Expand Down