Skip to content

Commit

Permalink
More contract tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Apr 9, 2024
1 parent 17f328b commit 6148c03
Showing 1 changed file with 72 additions and 4 deletions.
76 changes: 72 additions & 4 deletions contracts/test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {SubstrateTypes} from "./../src/SubstrateTypes.sol";

import {NativeTransferFailed} from "../src/utils/SafeTransfer.sol";
import {PricingStorage} from "../src/storage/PricingStorage.sol";
import {ERC20Lib} from "../src/ERC20Lib.sol";

import {
UpgradeParams,
Expand Down Expand Up @@ -907,7 +908,7 @@ contract GatewayTest is Test {
IGateway(address(gateway)).sendToken{value: fee}(address(token), destPara, recipientAddress32, 0, 1);
}

function testAgentRegisterDot() public {
function testAgentRegisterToken() public {
RegisterForeignTokenParams memory params = RegisterForeignTokenParams({
agentID: assetHubAgentID,
tokenID: dotTokenID,
Expand All @@ -922,8 +923,40 @@ contract GatewayTest is Test {
GatewayMock(address(gateway)).registerForeignTokenPublic(abi.encode(params));
}

function testAgentMintDot() public {
testAgentRegisterDot();
function testAgentRegisterTokenWithAgentIDNotExistWillFail() public {
testAgentRegisterToken();

RegisterForeignTokenParams memory params = RegisterForeignTokenParams({
agentID: bytes32(0),
tokenID: dotTokenID,
name: "DOT",
symbol: "DOT",
decimals: 10
});

vm.expectRevert(Gateway.AgentDoesNotExist.selector);

GatewayMock(address(gateway)).registerForeignTokenPublic(abi.encode(params));
}

function testAgentRegisterSameTokenAgainWillFail() public {
testAgentRegisterToken();

RegisterForeignTokenParams memory params = RegisterForeignTokenParams({
agentID: assetHubAgentID,
tokenID: dotTokenID,
name: "DOT",
symbol: "DOT",
decimals: 10
});

vm.expectRevert(Assets.TokenAlreadyRegistered.selector);

GatewayMock(address(gateway)).registerForeignTokenPublic(abi.encode(params));
}

function testAgentMintToken() public {
testAgentRegisterToken();

uint256 amount = 1000;

Expand All @@ -946,9 +979,22 @@ contract GatewayTest is Test {
assertEq(balance, amount);
}

function testAgentMintNotRegisteredTokenWillFail() public {
MintForeignTokenParams memory params = MintForeignTokenParams({
agentID: assetHubAgentID,
tokenID: bytes32(uint256(1)),
recipient: account1,
amount: 1000
});

vm.expectRevert(Assets.TokenNotRegistered.selector);

GatewayMock(address(gateway)).mintForeignTokenPublic(abi.encode(params));
}

function testSendRelayTokenToAssetHub() public {
// Register and then mint some DOT to account1
testAgentMintDot();
testAgentMintToken();

address dotToken = GatewayMock(address(gateway)).tokenAddressOf(dotTokenID);

Expand All @@ -965,4 +1011,26 @@ contract GatewayTest is Test {

IGateway(address(gateway)).sendToken{value: 0.1 ether}(address(dotToken), destPara, recipientAddress32, 1, 1);
}

function testSendNotRegisteredTokenWillFail() public {
ParaID destPara = assetHubParaID;

vm.expectRevert(Assets.TokenNotRegistered.selector);

IGateway(address(gateway)).sendToken{value: 0.1 ether}(address(0x0), destPara, recipientAddress32, 1, 1);
}

function testSendTokenFromNotMintedAccountWillFail() public {
testAgentRegisterToken();

address dotToken = GatewayMock(address(gateway)).tokenAddressOf(dotTokenID);

ParaID destPara = assetHubParaID;

vm.prank(account1);

vm.expectRevert(abi.encodeWithSelector(ERC20Lib.ERC20InsufficientBalance.selector, account1, 0, 1));

IGateway(address(gateway)).sendToken{value: 0.1 ether}(address(dotToken), destPara, recipientAddress32, 1, 1);
}
}

0 comments on commit 6148c03

Please sign in to comment.