This example shows how to deploy a smart contract using Ethers with Turnkey.
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/deployer/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 (crypto) 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_IDPRIVATE_KEY_ID-- if you leave it blank, we'll create one for you via calling the Turnkey APIINFURA_KEY-- if this is not set, it will default to using the Community Infura key
$ pnpm startThis script will do the following:
- Compile the contract specified in
compile.ts - Deploy the contract via Turnkey signer
Notes:
- To only compile, run
pnpm compile - If you prefer to use
solc/solcjsvia CLI, generate the ABI and binary files, and import them inindex.tsvia something resembling the following:
const abi = require("<some-abi.json>");
const bytecode = fs.readFileSync("<some-compiled-contract.bin>").toString();Sample output:
Network:
goerli (chain ID 5)
Address:
0x2A5111A1b0c0da37750b595b89BBaf1E6B7a8a27
Balance:
0.090843405342321879 Ether
Contract address
0xaaCADe99B5D2638534d7E6F6b6635005752D65fb
Contract has been deployed:
https://goerli.etherscan.io/tx/0x86eca7a545c6128923c77f321ca4b5a9ed66925ed223f6577dca73b9b8ea13f9