Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow token transfer -- SPL token policy example #446

Merged
merged 23 commits into from
Dec 14, 2024

Conversation

omkarshanbhag
Copy link
Contributor

@omkarshanbhag omkarshanbhag commented Dec 11, 2024

Summary & Motivation

This PR creates an example that highlights 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.

The steps included are

  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)
  2. Attempt Transfer - since we're attempting the transfer WITHOUT having created the policy allowing it, this will fail
  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
  4. Attempt Transfer (again) - this will succeed, now that the appropriate policy has been created!

How I Tested These Changes

Tested the example multiple times

Did you add a changeset?

If updating one of our packages, you'll likely need to add a changeset to your PR. To do so, run pnpm changeset. pnpm changeset will generate a file where you should write a human friendly message about the changes. Note how this (example) includes the package name (should be auto added by the command) along with the type of semver change (major.minor.patch) (which you should set).

These changes will be used at release time to determine what packages to publish and how to bump their version. For more context see this comment.

Copy link

codesandbox-ci bot commented Dec 11, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

@omkarshanbhag omkarshanbhag marked this pull request as draft December 12, 2024 02:13
Copy link
Collaborator

@andrewkmin andrewkmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking 🔥 thus far. In terms of next steps:

stoked about this

@omkarshanbhag omkarshanbhag changed the title Derive associated token address for Solana SPL token transfers Allow token transfer -- SPL token policy example Dec 13, 2024
@omkarshanbhag omkarshanbhag marked this pull request as ready for review December 13, 2024 00:15
@omkarshanbhag
Copy link
Contributor Author

@andrewkmin couple notes here!!

Given that the associated token address calculating method is readily available by from Solana, I think creating a wrapper function for it and exposing may be less helpful/ a point of confusion/extra weight for us.

I think what's most helpful is a full example of how to use this with createPolicy rather than true SDK support for this.

Same with combining this with createPolicy somehow because there are a variety of policies they could possibly create so don't want to be overly prescriptive there.

I've created an extensive example with instructions that I think is most helpful in helping folks understand how to best use our SPL support. Let me know if this makes sense overall.

Copy link
Collaborator

@andrewkmin andrewkmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking good overall!

packages/solana/package.json Outdated Show resolved Hide resolved
examples/with-solana/src/requests/createPolicy.ts Outdated Show resolved Hide resolved
examples/with-solana/src/utils/createTokenTransfer.ts Outdated Show resolved Hide resolved
examples/with-solana/src/requests/utils.ts Outdated Show resolved Hide resolved
examples/with-solana/src/utils/isKeyOfObject.ts Outdated Show resolved Hide resolved
examples/with-solana/README.md Outdated Show resolved Hide resolved
examples/with-solana/README.md Outdated Show resolved Hide resolved

```bash
New policy created!
- Name: Let non root user send SPL transfers to the ATA of WARCHEST: 6nDvKk6emwskFLtEpbQgFX6rsMtzbNY4LkvaNnzkvBaq
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we specify which SPL token we're working with given ATA's are token-specific?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to resolve this one? you could also mention that we're allowing specifically the transfer to the ATA of the warchest wallet associated with the newly created token

examples/with-solana/package.json Outdated Show resolved Hide resolved
Copy link
Collaborator

@andrewkmin andrewkmin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good overall, just some minor comments consider~!

@@ -166,7 +166,138 @@ Token balance for warchest: 0.0001

Enjoy!

### 6/ Stackblitz Example
### 6/ Running the "Create SPL token transfer policy" example
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tiny nit: the reason I would include the with preposition here is that this example contains more than just creating a policy — we're creating a policy and then using it in practice :)


```bash
New policy created!
- Name: Let non root user send SPL transfers to the ATA of WARCHEST: 6nDvKk6emwskFLtEpbQgFX6rsMtzbNY4LkvaNnzkvBaq
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you mean to resolve this one? you could also mention that we're allowing specifically the transfer to the ATA of the warchest wallet associated with the newly created token

@omkarshanbhag omkarshanbhag merged commit cc90919 into main Dec 14, 2024
5 checks passed
@omkarshanbhag omkarshanbhag deleted the omkar/spl-derive-ata branch December 14, 2024 01:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants