Skip to content

Commit

Permalink
add interchainTransferFrom
Browse files Browse the repository at this point in the history
  • Loading branch information
milapsheth committed Nov 3, 2023
1 parent 650cc31 commit b6997ee
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
18 changes: 18 additions & 0 deletions contracts/interfaces/ITokenRegistrar.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,22 @@ interface ITokenRegistrar {
string calldata destinationChain,
uint256 gasValue
) external payable;

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

function interchainTransferFrom(
bytes32 tokenId,
string calldata destinationChain,
bytes calldata destinationAddress,
uint256 amount,
bytes calldata metadata,
uint256 gasValue
) external payable;
}
31 changes: 25 additions & 6 deletions contracts/token-registrars/TokenRegistrar.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

pragma solidity ^0.8.0;

import { AddressBytes } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/libs/AddressBytes.sol';
import { SafeTokenTransfer, SafeTokenTransferFrom, SafeTokenCall } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/libs/SafeTransfer.sol';
import { Multicall } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/utils/Multicall.sol';

import { Upgradable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/upgradable/Upgradable.sol';

import { IInterchainTokenService } from '../interfaces/IInterchainTokenService.sol';
Expand All @@ -13,11 +13,9 @@ import { ITokenManagerType } from '../interfaces/ITokenManagerType.sol';
import { ITokenManager } from '../interfaces/ITokenManager.sol';
import { IStandardizedToken } from '../interfaces/IStandardizedToken.sol';

import { AddressBytesUtils } from '../libraries/AddressBytesUtils.sol';

contract TokenRegistrar is ITokenRegistrar, ITokenManagerType, Multicall, Upgradable {
using AddressBytesUtils for bytes;
using AddressBytesUtils for address;
using AddressBytes for bytes;
using AddressBytes for address;
using SafeTokenTransfer for IStandardizedToken;
using SafeTokenTransferFrom for IStandardizedToken;
using SafeTokenCall for IStandardizedToken;
Expand Down Expand Up @@ -201,14 +199,35 @@ contract TokenRegistrar is ITokenRegistrar, ITokenManagerType, Multicall, Upgrad
string calldata destinationChain,
bytes calldata destinationAddress,
uint256 amount,
bytes calldata metadata,
uint256 gasValue
) external payable {
if (bytes(destinationChain).length == 0) {
address tokenAddress = service.interchainTokenAddress(tokenId);
IStandardizedToken token = IStandardizedToken(tokenAddress);
token.safeTransfer(destinationAddress.toAddress(), amount);
} else {
service.interchainTransfer{ value: gasValue }(tokenId, destinationChain, destinationAddress, amount, gasValue);
service.interchainTransfer{ value: gasValue }(tokenId, destinationChain, destinationAddress, amount, metadata);
}
}

function interchainTransferFrom(
bytes32 tokenId,
string calldata destinationChain,
bytes calldata destinationAddress,
uint256 amount,
bytes calldata metadata,
uint256 gasValue
) external payable {
address tokenAddress = service.interchainTokenAddress(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);
token.approve(address(service), amount);
service.interchainTransfer{ value: gasValue }(tokenId, destinationChain, destinationAddress, amount, metadata);
}
}
}
4 changes: 2 additions & 2 deletions test/TokenRegistrars.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ describe('Token Registrsrs', () => {
.withArgs(service.address, destinationChain, service.address, keccak256(payload), payload);
});

it('Should transfer some tokens though the registrar as the deployer', async () => {
it('Should transfer some tokens through the registrar as the deployer', async () => {
const destinationAddress = '0x659703';
const amount = 1234;
const gasValue = 1234;
Expand All @@ -114,7 +114,7 @@ describe('Token Registrsrs', () => {
await (await token.approve(tokenRegistrar.address, amount)).wait();

await expect(
tokenRegistrar.transferCanonicalToken(token.address, destinationChain, destinationAddress, amount, gasValue, {
tokenRegistrar.interchainTransferFrom(tokenId, destinationChain, destinationAddress, amount, '0x', gasValue, {
value: gasValue,
}),
)
Expand Down

0 comments on commit b6997ee

Please sign in to comment.