github.com/ethereum-optimism/optimism@v1.7.2/packages/sdk/test-next/bridgeEcoToken.spec.ts (about) 1 import ethers from 'ethers' 2 import { describe, expect, it } from 'vitest' 3 import { Address, PublicClient, parseEther } from 'viem' 4 5 import { 6 l1TestClient, 7 l2TestClient, 8 l1PublicClient, 9 l2PublicClient, 10 } from './testUtils/viemClients' 11 import { BRIDGE_ADAPTER_DATA, CrossChainMessenger, L2ChainID } from '../src' 12 import { l1Provider, l2Provider } from './testUtils/ethersProviders' 13 14 const ECO_WHALE: Address = '0x982E148216E3Aa6B38f9D901eF578B5c06DD7502' 15 16 // we should instead use tokenlist as source of truth 17 const ECO_L1_TOKEN_ADDRESS: Address = 18 '0x3E87d4d9E69163E7590f9b39a70853cf25e5ABE3' 19 const ECO_L2_TOKEN_ADDRESS: Address = 20 '0xD2f598c826429EEe7c071C02735549aCd88F2c09' 21 22 const getERC20TokenBalance = async ( 23 publicClient: PublicClient, 24 tokenAddress: Address, 25 ownerAddress: Address 26 ) => { 27 const result = await publicClient.readContract({ 28 address: tokenAddress, 29 abi: [ 30 { 31 inputs: [{ name: 'owner', type: 'address' }], 32 name: 'balanceOf', 33 outputs: [{ name: '', type: 'uint256' }], 34 stateMutability: 'view', 35 type: 'function', 36 }, 37 ], 38 functionName: 'balanceOf', 39 args: [ownerAddress], 40 }) 41 42 return result as bigint 43 } 44 45 const getL1ERC20TokenBalance = async (ownerAddress: Address) => { 46 return getERC20TokenBalance( 47 l1PublicClient, 48 ECO_L1_TOKEN_ADDRESS, 49 ownerAddress 50 ) 51 } 52 53 const getL2ERC20TokenBalance = async (ownerAddress: Address) => { 54 return getERC20TokenBalance( 55 l2PublicClient, 56 ECO_L2_TOKEN_ADDRESS, 57 ownerAddress 58 ) 59 } 60 61 describe('ECO token', () => { 62 it('sdk should be able to deposit to l1 bridge contract correctly', async () => { 63 await l1TestClient.impersonateAccount({ address: ECO_WHALE }) 64 65 const l1EcoWhaleSigner = await l1Provider.getSigner(ECO_WHALE) 66 const preBridgeL1EcoWhaleBalance = await getL1ERC20TokenBalance(ECO_WHALE) 67 68 const crossChainMessenger = new CrossChainMessenger({ 69 l1SignerOrProvider: l1EcoWhaleSigner, 70 l2SignerOrProvider: l2Provider, 71 l1ChainId: 5, 72 l2ChainId: 420, 73 bedrock: true, 74 bridges: BRIDGE_ADAPTER_DATA[L2ChainID.OPTIMISM_GOERLI], 75 }) 76 77 await crossChainMessenger.approveERC20( 78 ECO_L1_TOKEN_ADDRESS, 79 ECO_L2_TOKEN_ADDRESS, 80 ethers.utils.parseEther('0.1') 81 ) 82 83 const txResponse = await crossChainMessenger.depositERC20( 84 ECO_L1_TOKEN_ADDRESS, 85 ECO_L2_TOKEN_ADDRESS, 86 ethers.utils.parseEther('0.1') 87 ) 88 89 await txResponse.wait() 90 91 const l1EcoWhaleBalance = await getL1ERC20TokenBalance(ECO_WHALE) 92 expect(l1EcoWhaleBalance).toEqual( 93 preBridgeL1EcoWhaleBalance - parseEther('0.1') 94 ) 95 }, 20_000) 96 97 it('sdk should be able to withdraw into the l2 bridge contract correctly', async () => { 98 await l2TestClient.impersonateAccount({ address: ECO_WHALE }) 99 const l2EcoWhaleSigner = await l2Provider.getSigner(ECO_WHALE) 100 101 const preBridgeL2EcoWhaleBalance = await getL2ERC20TokenBalance(ECO_WHALE) 102 103 const crossChainMessenger = new CrossChainMessenger({ 104 l1SignerOrProvider: l1Provider, 105 l2SignerOrProvider: l2EcoWhaleSigner, 106 l1ChainId: 5, 107 l2ChainId: 420, 108 bedrock: true, 109 bridges: BRIDGE_ADAPTER_DATA[L2ChainID.OPTIMISM_GOERLI], 110 }) 111 112 const txResponse = await crossChainMessenger.withdrawERC20( 113 ECO_L1_TOKEN_ADDRESS, 114 ECO_L2_TOKEN_ADDRESS, 115 ethers.utils.parseEther('0.1') 116 ) 117 118 await txResponse.wait() 119 120 const l2EcoWhaleBalance = await getL2ERC20TokenBalance(ECO_WHALE) 121 expect(l2EcoWhaleBalance).toEqual( 122 preBridgeL2EcoWhaleBalance - parseEther('0.1') 123 ) 124 }, 20_000) 125 })