@@ -913,6 +913,100 @@ contract("Cross-chain", (accounts) => {
913913 const balance = await colony . getFundingPotProxyBalance ( domain . fundingPotId , foreignChainId , foreignToken . address ) ;
914914 expect ( balance . toHexString ( ) ) . to . equal ( ethers . utils . parseEther ( "50" ) . toHexString ( ) ) ;
915915 } ) ;
916+
917+ it ( "can exchange tokens in a domain held by the proxy to different tokens also on the proxy" , async ( ) => {
918+ const foreignTokenFactory = new ethers . ContractFactory ( MetaTxToken . abi , MetaTxToken . bytecode , ethersForeignSigner ) ;
919+ const foreignToken2 = await foreignTokenFactory . deploy ( "TT2" , "TT2" , 18 ) ;
920+ await ( await foreignToken2 . unlock ( ) ) . wait ( ) ;
921+ await ( await foreignToken . unlock ( ) ) . wait ( ) ;
922+
923+ let tx = await foreignToken [ "mint(address,uint256)" ] ( proxyColony . address , ethers . utils . parseEther ( "100" ) ) ;
924+ await tx . wait ( ) ;
925+ let p = guardianSpy . getPromiseForNextBridgedTransaction ( ) ;
926+
927+ tx = await proxyColony . claimTokens ( foreignToken . address ) ;
928+ await tx . wait ( ) ;
929+ await p ;
930+
931+ // Check bookkeeping on the home chain
932+ const balance = await colony . getFundingPotProxyBalance ( 1 , foreignChainId , foreignToken . address ) ;
933+ expect ( balance . toHexString ( ) ) . to . equal ( ethers . utils . parseEther ( "100" ) . toHexString ( ) ) ;
934+
935+ // Move tokens from domain 1 to domain 2
936+ tx = await colony [ "addDomain(uint256,uint256,uint256)" ] ( 1 , UINT256_MAX_ETHERS , 1 ) ;
937+ await tx . wait ( ) ;
938+
939+ const domain1 = await colony . getDomain ( 1 ) ;
940+ const domain2 = await colony . getDomain ( 2 ) ;
941+ console . log ( domain2 ) ;
942+ const fundingPot = await colony . getFundingPot ( domain2 . fundingPotId ) ;
943+ console . log ( fundingPot ) ;
944+
945+ tx = await colony [ "moveFundsBetweenPots(uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,uint256,address)" ] (
946+ 1 ,
947+ UINT256_MAX_ETHERS ,
948+ 1 ,
949+ UINT256_MAX_ETHERS ,
950+ 0 ,
951+ domain1 . fundingPotId ,
952+ domain2 . fundingPotId ,
953+ ethers . utils . parseEther ( "70" ) ,
954+ foreignChainId ,
955+ foreignToken . address ,
956+ ) ;
957+ await tx . wait ( ) ;
958+ console . log ( "moved" ) ;
959+ // Exchange tokens
960+ const domain2ReceiverAddress = await homeColonyNetwork . getDomainTokenReceiverAddress ( colony . address , 2 ) ;
961+
962+ const lifi = new ethers . Contract ( LIFI_ADDRESS , LiFiFacetProxyMock . abi , ethersForeignSigner ) ; // Signer doesn't really matter,
963+ // we're just calling encodeFunctionData
964+
965+ const txdata = lifi . interface . encodeFunctionData ( "swapTokensMock(uint256,address,uint256,address,address,uint256)" , [
966+ foreignChainId ,
967+ foreignToken . address ,
968+ foreignChainId ,
969+ foreignToken2 . address ,
970+ domain2ReceiverAddress ,
971+ ethers . utils . parseEther ( "70" ) ,
972+ ] ) ;
973+
974+ p = guardianSpy . getPromiseForNextBridgedTransaction ( ) ;
975+ tx = await colony . exchangeProxyHeldTokensViaLiFi ( 1 , 0 , 2 , txdata , 0 , foreignChainId , foreignToken . address , ethers . utils . parseEther ( "70" ) ) ;
976+ await tx . wait ( ) ;
977+
978+ const receipt = await p ;
979+ const swapEvent = receipt . logs
980+ . filter ( ( e ) => e . address === LIFI_ADDRESS )
981+ . map ( ( e ) => lifi . interface . parseLog ( e ) )
982+ . filter ( ( e ) => e . name === "SwapTokens" ) [ 0 ] ;
983+ expect ( swapEvent ) . to . not . be . undefined ;
984+
985+ // Okay, so we saw the SwapTokens event. Let's do vaguely what it said for the test,
986+ // but in practise this would be the responsibility of whatever entity we've paid to do it
987+ // through LiFi.
988+ await foreignToken2 [ "mint(address,uint256)" ] ( swapEvent . args . _toAddress , swapEvent . args . _amount ) ; // Implicit 1:1 exchange rate
989+
990+ // Sweep token in to the proxy
991+ p = guardianSpy . getPromiseForNextBridgedTransaction ( ) ;
992+ tx = await proxyColony . claimTokensForDomain ( foreignToken2 . address , 2 , { gasLimit : 1000000 } ) ;
993+ await tx . wait ( ) ;
994+
995+ // Wait for the sweep to be bridged
996+ await p ;
997+
998+ // Check bookkeeping on the home chain
999+ const balance1 = await colony . getFundingPotProxyBalance ( 1 , foreignChainId , foreignToken . address ) ;
1000+ const balance2 = await colony . getFundingPotProxyBalance ( 2 , foreignChainId , foreignToken2 . address ) ;
1001+ expect ( balance1 . toHexString ( ) ) . to . equal ( ethers . utils . parseEther ( "30" ) . toHexString ( ) ) ;
1002+ expect ( balance2 . toHexString ( ) ) . to . equal ( ethers . utils . parseEther ( "70" ) . toHexString ( ) ) ;
1003+
1004+ // And check balances of the proxy with the tokens
1005+ const balance3 = await foreignToken . balanceOf ( proxyColony . address ) ;
1006+ const balance4 = await foreignToken2 . balanceOf ( proxyColony . address ) ;
1007+ expect ( balance3 . toHexString ( ) ) . to . equal ( ethers . utils . parseEther ( "30" ) . toHexString ( ) ) ;
1008+ expect ( balance4 . toHexString ( ) ) . to . equal ( ethers . utils . parseEther ( "70" ) . toHexString ( ) ) ;
1009+ } ) ;
9161010 } ) ;
9171011
9181012 describe ( "making arbitrary transactions on another chain" , async ( ) => {
0 commit comments