This example shows how to sweep funds (tokens + native ETH) from one address to another, built on top of Ethers with Turnkey. Gas sponsorship through Turnkey is available as an option for the sweeps.
Make sure you have Node.js installed locally; we recommend using Node v18+.
$ git clone https://github.com/tkhq/sdk
$ cd sdk/
$ corepack enable # Install `pnpm`
$ pnpm install -r # Install dependencies
$ pnpm run build-all # Compile source code
$ cd examples/sweeper/The first step is to set up your Turnkey organization and account. By following the Quickstart guide, you should have:
- A public/private API key pair for Turnkey
- An organization ID
- A Turnkey wallet account (address), private key address, or a private key ID
Once you've gathered these values, add them to a new .env.local file. Notice that your private key should be securely managed and never be committed to git.
$ cp .env.local.example .env.localNow open .env.local and add the missing environment variables:
API_PUBLIC_KEYAPI_PRIVATE_KEYBASE_URLORGANIZATION_IDSIGN_WITH-- a Turnkey wallet account address, private key address, or private key ID. If you leave this blank, we'll create a wallet for you.DESTINATION_ADDRESS-- Destination address for sweeping tokens
$ pnpm startThe script constructs and signs a and sends series of sweep transactions via Turnkey. If the script exits because your account isn't funded, you can request funds on https://cloud.google.com/application/web3/faucet/ethereum/sepolia or use Turnkey's gas sponsorship feature. By default, this utility will sweep USDC, UNI, and ETH.
Network:
sepolia
Address:
0x2A5111A1b0c0da37750b595b89BBaf1E6B7a8a27
Balance:
0.094678708129562482 Ether
Address: 0x2A5111A1b0c0da37750b595b89BBaf1E6B7a8a27
Balance: 0.094678708129562482
? Use Turnkey gas sponsorship for sweep transactions? › (y/N)
✔ Please confirm: transfer 0.05 UNI (token address 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984) to 0x4A7937B14bb850bd706053e279Db9D9a784A1eF2? … yes
Sent 0.05 UNI (token address 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984) to 0x4A7937B14bb850bd706053e279Db9D9a784A1eF2:
https://goerli.etherscan.io/tx/0x60849266677a53ea2f3eb2ea301d1cd2625c273e84eeec60cce6fbd4c50ab94e
✔ Please confirm: transfer 5.0 USDC (token address 0x07865c6E87B9F70255377e024ace6630C1Eaa37F) to 0x4A7937B14bb850bd706053e279Db9D9a784A1eF2? … yes
Sent 5.0 USDC (token address 0x07865c6E87B9F70255377e024ace6630C1Eaa37F) to 0x4A7937B14bb850bd706053e279Db9D9a784A1eF2:
https://goerli.etherscan.io/tx/0xbbbe9211db006aa275bd85c8238f5050d53f884f9bbb51fa4b199d9f16259e2d
✔ Please confirm: transfer 0.0698606968 ETH (balance of 0.0780434300 - 0.0081827332 for gas) to 0x4A7937B14bb850bd706053e279Db9D9a784A1eF2? … yes
Sent 0.0698606968 ETH to 0x4A7937B14bb850bd706053e279Db9D9a784A1eF2:
https://goerli.etherscan.io/tx/0xff4fe9587fbdf167c43641dae16ee0e250ee0fe794da94c8d8500ca5964168ba