fn apply_balance_tracking(
    storage: &mut dyn Storage,
    source_chain: ChainName,
    destination_chain: ChainName,
    message: &ItsMessage,
) -> Result<(), Error> {
    match message {
        ItsMessage::InterchainTransfer {
            token_id, amount, ..
        } => {
            // Update the balance on the source chain
@>          update_token_balance(
                storage,
                token_id.clone(),
                source_chain.clone(),
                *amount,
                false,
            )
@>          .change_context_lazy(|| Error::BalanceUpdateFailed(source_chain, token_id.clone()))?;

            // Update the balance on the destination chain
@>          update_token_balance(
                storage,
                token_id.clone(),
                destination_chain.clone(),
                *amount,
                true,
            )
@>          .change_context_lazy(|| {
                Error::BalanceUpdateFailed(destination_chain, token_id.clone())
            })?
        }
        // Start balance tracking for the token on the destination chain when a token deployment is seen
        // No invariants can be assumed on the source since the token might pre-exist on the source chain
        ItsMessage::DeployInterchainToken { token_id, .. } => {
@>          start_token_balance(storage, token_id.clone(), destination_chain.clone(), true)
                .change_context(Error::InvalidStoreAccess)?
        }
        ...
    };

    Ok(())
}
pub fn start_token_balance(
    storage: &mut dyn Storage,
    token_id: TokenId,
    chain: ChainName,
    track_balance: bool,
) -> Result<(), Error> {
    let key = TokenChainPair { token_id, chain };

    match TOKEN_BALANCES.may_load(storage, key.clone())? {
        None => {
            let initial_balance = if track_balance {
@>              TokenBalance::Tracked(Uint256::zero())
            } else {
                TokenBalance::Untracked
            };

            TOKEN_BALANCES
                .save(storage, key, &initial_balance)?
                .then(Ok)
        }
        Some(_) => Err(Error::TokenAlreadyRegistered {
            token_id: key.token_id,
            chain: key.chain,
        }),
    }
}

pub fn update_token_balance(
    storage: &mut dyn Storage,
    token_id: TokenId,
    chain: ChainName,
    amount: Uint256,
    is_deposit: bool,
) -> Result<(), Error> {
    let key = TokenChainPair { token_id, chain };

    let token_balance = TOKEN_BALANCES.may_load(storage, key.clone())?;

    match token_balance {
        Some(TokenBalance::Tracked(balance)) => {
            let token_balance = if is_deposit {
                balance
@>                  .checked_add(amount)
                    .map_err(|_| Error::MissingConfig)?
            } else {
                balance
@>                  .checked_sub(amount)
                    .map_err(|_| Error::MissingConfig)?
            }
            .then(TokenBalance::Tracked);

            TOKEN_BALANCES.save(storage, key.clone(), &token_balance)?;
        }
@>      Some(_) | None => (),
    }

    Ok(())
}
describe('PoC remote deploy to original chain', () => {
    const tokenName = 'Token Name';
    const tokenSymbol = 'TN';
    const tokenDecimals = 13;
    let sourceAddress;

    before(async () => {
        sourceAddress = service.address;
    });

    it('Can request to remote deploy for original chain', async () => {

        const salt = getRandomBytes32();
        const tokenId = await service.interchainTokenId(wallet.address, salt);
        const tokenManagerAddress = await service.tokenManagerAddress(tokenId);
        const minter = '0x';
        const operator = '0x';
        const tokenAddress = await service.interchainTokenAddress(tokenId);
        const params = defaultAbiCoder.encode(['bytes', 'address'], [operator, tokenAddress]);
        let payload = defaultAbiCoder.encode(
            ['uint256', 'bytes32', 'string', 'string', 'uint8', 'bytes', 'bytes'],
            [MESSAGE_TYPE_DEPLOY_INTERCHAIN_TOKEN, tokenId, tokenName, tokenSymbol, tokenDecimals, minter, operator],
        );
        const commandId = await approveContractCall(gateway, sourceChain, sourceAddress, service.address, payload);

        // 1. Receive remote deployment messages and deploy interchain tokens on the destination chain

        await expect(service.execute(commandId, sourceChain, sourceAddress, payload))
            .to.emit(service, 'InterchainTokenDeployed')
            .withArgs(tokenId, tokenAddress, AddressZero, tokenName, tokenSymbol, tokenDecimals)
            .and.to.emit(service, 'TokenManagerDeployed')
            .withArgs(tokenId, tokenManagerAddress, NATIVE_INTERCHAIN_TOKEN, params);
        const tokenManager = await getContractAt('TokenManager', tokenManagerAddress, wallet);
        expect(await tokenManager.tokenAddress()).to.equal(tokenAddress);
        expect(await tokenManager.hasRole(service.address, OPERATOR_ROLE)).to.be.true;

        // 2. Can request a remote deployment to the source(original) chain

        const destChain = sourceChain;

        payload = defaultAbiCoder.encode(
            ['uint256', 'bytes32', 'string', 'string', 'uint8', 'bytes'],
            [MESSAGE_TYPE_DEPLOY_INTERCHAIN_TOKEN, tokenId, tokenName, tokenSymbol, tokenDecimals, minter],
        );

        await expect(
            reportGas(
                service.deployInterchainToken(salt, destChain, tokenName, tokenSymbol, tokenDecimals, minter, gasValue, {
                    value: gasValue,
                }),
                'Send deployInterchainToken to remote chain',
            ),
        )
            .to.emit(service, 'InterchainTokenDeploymentStarted')
            .withArgs(tokenId, tokenName, tokenSymbol, tokenDecimals, minter, destChain)
            .and.to.emit(gasService, 'NativeGasPaidForContractCall')
            .withArgs(service.address, destChain, service.address, keccak256(payload), gasValue, wallet.address)
            .and.to.emit(gateway, 'ContractCall')
            .withArgs(service.address, destChain, service.address, keccak256(payload), payload);

        // 3. ITSHub will initialize the balance of the original chain
    });
});
