|
| 1 | +# Example: `foundry` |
| 2 | + |
| 3 | +This example shows how to set up and broadcast a smart contract from a Turnkey wallet using Foundry & Forge. |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +### 0 / Foundry versioning |
| 8 | + |
| 9 | +Make sure you have [Foundry](https://getfoundry.sh/introduction/installation) installed locally. |
| 10 | +Until >v1.4.4 release, it is also necessary to use a Nightly build for the Turnkey flag to be enabled. |
| 11 | + |
| 12 | +```bash |
| 13 | +$ curl -L https://foundry.paradigm.xyz | bash |
| 14 | +$ foundryup --install nightly |
| 15 | +``` |
| 16 | + |
| 17 | +### 1/ Cloning the example |
| 18 | + |
| 19 | +```bash |
| 20 | +$ git clone https://github.com/tkhq/sdk |
| 21 | +$ cd sdk/examples/foundry/ |
| 22 | +``` |
| 23 | + |
| 24 | +### 2/ Setting up Turnkey |
| 25 | + |
| 26 | +The first step is to set up your Turnkey organization and account. By following the [Quickstart](https://docs.turnkey.com/getting-started/quickstart) guide, you should have: |
| 27 | + |
| 28 | +- A public/private API key pair for Turnkey |
| 29 | +- An organization ID |
| 30 | +- A Turnkey wallet account (address), private key address, or a private key ID |
| 31 | + |
| 32 | +Once you've gathered these values, add them to a new `.env` file. Notice that your private key should be securely managed and **_never_** be committed to git. |
| 33 | + |
| 34 | +```bash |
| 35 | +$ cp .env.example .env |
| 36 | +``` |
| 37 | + |
| 38 | +Now open `.env` and add the missing environment variables: |
| 39 | + |
| 40 | +- `TURNKEY_API_PRIVATE_KEY` |
| 41 | +- `TURNKEY_ORGANIZATION_ID` |
| 42 | +- `TURNKEY_ADDRESS` -- a Turnkey wallet account address, private key address, or private key ID |
| 43 | + |
| 44 | +For convenience we have hardcoded several popular chains and RPCs in `foundry.toml`, but you're welcome to change for different values. |
| 45 | + |
| 46 | +### 3/ Running the sample deploy script |
| 47 | + |
| 48 | +The following command will compile and broadcast Foundry's demo `Counter.sol` contract to the Sepolia network. |
| 49 | +You will need some SepoliaETH in your Turnkey wallet address. |
| 50 | + |
| 51 | +```bash |
| 52 | +$ forge script script/Counter.s.sol --rpc-url sepolia --turnkey --broadcast |
| 53 | +``` |
| 54 | + |
| 55 | +Visit the relevant chain explorer to view your transaction; you have successfully signed and deployed your first contract with Turnkey! |
| 56 | + |
| 57 | +See the following for a sample output: |
| 58 | + |
| 59 | +``` |
| 60 | +[⠊] Compiling... |
| 61 | +No files changed, compilation skipped |
| 62 | +Script ran successfully. |
| 63 | +
|
| 64 | +## Setting up 1 EVM. |
| 65 | +
|
| 66 | +========================== |
| 67 | +
|
| 68 | +Chain 11155111 |
| 69 | +
|
| 70 | +Estimated gas price: 0.001037667 gwei |
| 71 | +
|
| 72 | +Estimated total gas used for script: 203856 |
| 73 | +
|
| 74 | +Estimated amount required: 0.000000211534643952 ETH |
| 75 | +
|
| 76 | +========================== |
| 77 | +
|
| 78 | +##### sepolia |
| 79 | +✅ [Success] Hash: 0xf492f7059d91ec1b91537a82b55289d1091e2a498db2881bd04d19b26ad29e0d |
| 80 | +Contract Address: 0x2352D015613705F2c365dEead916d45BAaf547d4 |
| 81 | +Block: 9624270 |
| 82 | +Paid: 0.000000162718420767 ETH (156813 gas * 0.001037659 gwei) |
| 83 | +
|
| 84 | +✅ Sequence #1 on sepolia | Total Paid: 0.000000162718420767 ETH (156813 gas * avg 0.001037659 gwei) |
| 85 | + |
| 86 | +
|
| 87 | +========================== |
| 88 | +``` |
0 commit comments