You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: examples/with-solana/README.md
+133-2Lines changed: 133 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ This example walks through the following:
7
7
- Construction of a transaction sending the funds out with the `@turnkey/solana` signer
8
8
- Creating, minting, and transferring a SPL token using Turnkey
9
9
10
-
You can try this example quickly on Stackblitz. Follow the instructions below --> [Stackblitz Instructions](#6-stackblitz-example)
10
+
You can try this example quickly on Stackblitz. Follow the instructions below --> [Stackblitz Instructions](#7-stackblitz-example)
11
11
12
12
## Getting started
13
13
@@ -166,7 +166,138 @@ Token balance for warchest: 0.0001
166
166
167
167
Enjoy!
168
168
169
-
### 6/ Stackblitz Example
169
+
### 6/ Running the "Create SPL token transfer with policy" example
170
+
171
+
This example shows the flow of using our policy engine's support for SPL token transfers! Specifically we show how to allow a user (non root user) to send a Solana SPL token transfer by calculating the "associated token address" of the receiving account, given the mint address of the token that we are transferring.
172
+
173
+
To best see how policies can be used effectively, we will go through example in the following steps
174
+
175
+
1. Setup - this step creates the on-chain state and SOME of the Turnkey setup state required to make this transfer (notably, it leaves out the creation of the policy that allows the created non root user to sign the SPL transfer)
176
+
2. Attempt Transfer - since we're attempting the transfer WITHOUT having created the policy allowing it, this will fail
177
+
3. Create Token Policy - this will create a policy that allows Solana transactions initiated by the correct user to be signed if they contain a single instruction which is an SPL token transfer to the correct token account
178
+
4. Attempt Transfer (again) - this will succeed, now that the appropriate policy has been created!
179
+
180
+
NOTE: A very key piece of understanding to glean from this example is the use of "associated token addresses (or "associated token account address") in the creation of SPL related policies. For further context on ATA's look here: https://spl.solana.com/associated-token-account
181
+
182
+
#### Step 1. Setup
183
+
184
+
First we will run the setup command
185
+
186
+
```bash
187
+
$ pnpm token-transfer-policy setup
188
+
```
189
+
190
+
This will create a Solana wallet address that you'll have to fund with devnet SOL via a faucet or cli. A commonly used SOL devnet faucet is available here --> https://faucet.solana.com/
191
+
192
+
Funding your devnet and hitting `y` in the terminal to proceed will set up the necessary on-chain state and Turnkey setup to start, and will output the following information to be used in coming steps
Token Mint public key: FA7umztm6eYcENE22ndEZhd8MCf9C7myJ2KjfCwobj8F
223
+
Non root user created with user id: 13c4609b-8818-40be-aeac-c8b63da0de4a
224
+
```
225
+
226
+
#### Step 2. Attempt to Transfer Tokens (This is expected to FAIL)
227
+
228
+
We will now attempt to transfer these SPL tokens that we've created
229
+
230
+
```bash
231
+
$ pnpm token-transfer-policy attempt-transfer
232
+
```
233
+
234
+
This will prompt you first to enter in the Solana wallet address that is originating the transfer (use the address labeled "Turnkey Solana wallet address" output at the end of the setup step)
235
+
236
+
```bash
237
+
? Enter Solana wallet address originating transfer (created during setup stage):
238
+
```
239
+
240
+
Once you've entered your wallet address you will then be prompted to enter in the Mint address/public key for the token being transferred (use the address labeled "Token Mint public key" output at the end of the setup step)
241
+
242
+
```bash
243
+
? Enter Mint account address of token being transferred (created during setup stage):
244
+
```
245
+
246
+
Since this is being initiated by a non root user, and the policy allowing this has not been created, this is EXPECTED TO FAIL. You will get an error that includes the following information saying that no policies evaluated to the outcome: Allow
message: 'No policies evaluated to outcome: Allow',
253
+
policyEvaluations: []
254
+
}
255
+
],
256
+
code: 7
257
+
```
258
+
259
+
#### Step 3. Create Policy allowing the correct user to make SPL transfers to receiving Associated Token Address
260
+
261
+
This is the most important step! In this step we are taking our receiving address (the Turnkey Warchest), calculating it's "associated token address", given the Mint account address for the token we've created, and creating a policy that allows the correct non root user to sign Solana transactions if and only if they contain a single instruction that is an SPL token transfer to this associated token address.
262
+
263
+
```bash
264
+
$ pnpm token-transfer-policy create-token-policy
265
+
```
266
+
267
+
This will prompt you first to enter in the user ID of the non root user who's credentials are being used to sign the transfer (use the value labeled "Non root user created with user id" output at the end of the setup step)
268
+
269
+
```bash
270
+
? Enter non-root user ID originating the transfer (created during setup stage):
271
+
```
272
+
273
+
Once you've entered this user id, you will then be prompted to enter in the Mint address/public key for the token being transferred (use the address labeled "Token Mint public key" output at the end of the setup step)
274
+
275
+
```bash
276
+
? Enter Mint account address of token being transferred (created during setup stage):
277
+
```
278
+
279
+
This should successfully create your policy!
280
+
281
+
```bash
282
+
New policy created!
283
+
- Name: Let non root user send SPL transfers to the associated token account of WARCHEST: 6nDvKk6emwskFLtEpbQgFX6rsMtzbNY4LkvaNnzkvBaq given the mint address for the token just created
#### Step 4. Attempt to Transfer Tokens (This is expected to SUCCEED)
291
+
292
+
Following the instructions for step 2 above should find success this time -- showing that the policy has successfully allowed the user to transfer tokens!
0 commit comments