|
1 | 1 | # `@mysten/zksend` |
| 2 | + |
| 3 | +Create claimable links for transferring coins and NFTs. Recipients can claim assets via URL without needing a wallet set up beforehand. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```sh |
| 8 | +npm install @mysten/zksend |
| 9 | +``` |
| 10 | + |
| 11 | +## Limitations |
| 12 | + |
| 13 | +- zkSend only supports Mainnet and Testnet at this time. |
| 14 | +- Objects within links must be publicly transferrable. |
| 15 | + |
| 16 | +## Create a link |
| 17 | + |
| 18 | +You can start creating your own zkSend link using the `ZkSendLinkBuilder` class. This class |
| 19 | +constructor takes an object with the following options: |
| 20 | + |
| 21 | +- **`sender`** (required) - The address of the sender / creator of the link. |
| 22 | +- **`client`** (optional) - The `@mysten/sui` client used to fetch data to construct the link. If not provided, a default client will be used. |
| 23 | +- **`network`** (optional) - The network that the link will be created for. Defaults to `mainnet`. |
| 24 | + |
| 25 | +```ts |
| 26 | +import { ZkSendLinkBuilder } from '@mysten/zksend'; |
| 27 | + |
| 28 | +const link = new ZkSendLinkBuilder({ |
| 29 | + sender: '0x...', |
| 30 | +}); |
| 31 | +``` |
| 32 | + |
| 33 | +### Adding SUI to the link |
| 34 | + |
| 35 | +You can add SUI to the link by calling `link.addClaimableMist()`. This method takes the following params: |
| 36 | + |
| 37 | +- **`amount`** (required) - The amount of MIST (the base unit of SUI) to add to the link. |
| 38 | + |
| 39 | +### Adding non-SUI coins to the link |
| 40 | + |
| 41 | +You can add non-SUI coins to the link by calling `link.addClaimableBalance()`. This method takes the following params: |
| 42 | + |
| 43 | +- **`coinType`** (required) - The coin type of the coin to add to the link (e.g. `0x2::sui::SUI`). |
| 44 | +- **`amount`** (required) - The amount of the coin to add to the link. Represented in the base unit of the coin. |
| 45 | + |
| 46 | +The SDK will automatically perform the necessary coin management logic to transfer the defined amount, such as merging and splitting coin objects. |
| 47 | + |
| 48 | +### Adding objects to the link |
| 49 | + |
| 50 | +You can add a publicly-transferrable object to the link by calling `link.addClaimableObject()`. This method takes the following params: |
| 51 | + |
| 52 | +- **`id`** (required) - The ID of the object. This must be owned by the `sender` you configured when creating the link. |
| 53 | + |
| 54 | +### Adding objects created in the same transaction |
| 55 | + |
| 56 | +You can create objects to add to links in the same transaction the link is created in by using `link.addClaimableObjectRef()`: |
| 57 | + |
| 58 | +- **`ref`** (required) - The reference to the object you want to add to the link. |
| 59 | +- **`type`** (required) - The move type of the object you are adding |
| 60 | + |
| 61 | +```ts |
| 62 | +const tx = new Transaction(); |
| 63 | + |
| 64 | +const link = new ZkSendLinkBuilder({ |
| 65 | + sender: '0x...', |
| 66 | +}); |
| 67 | + |
| 68 | +const newObject = tx.moveCall({ |
| 69 | + target: `${PACKAGE_ID}::your_module::mint`, |
| 70 | +}); |
| 71 | + |
| 72 | +link.addClaimableObjectRef({ |
| 73 | + ref: newObject, |
| 74 | + type: `${PACKAGE_ID}::your_module::YourType`, |
| 75 | +}); |
| 76 | + |
| 77 | +// Adds the link creation transactions to the transaction |
| 78 | +link.createSendTransaction({ |
| 79 | + transaction: tx, |
| 80 | +}); |
| 81 | +``` |
| 82 | + |
| 83 | +### Getting the link URL |
| 84 | + |
| 85 | +At any time, you can get the URL for the link by calling `link.getLink()`. |
| 86 | + |
| 87 | +## Submitting the link transaction |
| 88 | + |
| 89 | +Once you have built your zkSend link, you need to execute a transaction to transfer assets and make the link claimable. |
| 90 | + |
| 91 | +You can call the `link.createSendTransaction()` method, which returns a `Transaction` object that you can sign and submit. |
| 92 | + |
| 93 | +```ts |
| 94 | +const tx = await link.createSendTransaction(); |
| 95 | + |
| 96 | +const { bytes, signature } = tx.sign({ client, signer: keypair }); |
| 97 | + |
| 98 | +const result = await client.executeTransactionBlock({ |
| 99 | + transactionBlock: bytes, |
| 100 | + signature, |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +If you have a keypair you would like to send the transaction with, you can use the `create` method as shorthand for creating the send transaction, signing it, and submitting it: |
| 105 | + |
| 106 | +```ts |
| 107 | +await link.create({ |
| 108 | + signer: yourKeypair, |
| 109 | + // Wait until the new link is ready to be indexed so it is claimable |
| 110 | + waitForTransaction: true, |
| 111 | +}); |
| 112 | +``` |
| 113 | + |
| 114 | +## Claiming a link |
| 115 | + |
| 116 | +To claim a link via the SDK you can use the `ZkSendLink` class: |
| 117 | + |
| 118 | +```ts |
| 119 | +import { ZkSendLink } from '@mysten/zksend'; |
| 120 | + |
| 121 | +// create a link instance from a URL |
| 122 | +const link = await ZkSendLink.fromUrl('https://zksend.com/claim#$abc...'); |
| 123 | + |
| 124 | +// list what claimable assets the link has |
| 125 | +const { nfts, balances } = link.assets; |
| 126 | + |
| 127 | +// claim all the assets from the link |
| 128 | +await link.claimAssets(addressOfClaimer); |
| 129 | +``` |
| 130 | + |
| 131 | +## Listing links you have created |
| 132 | + |
| 133 | +To list the links created by a specific address, you can use the `listCreatedLinks` function: |
| 134 | + |
| 135 | +```ts |
| 136 | +import { listCreatedLinks } from '@mysten/zksend'; |
| 137 | + |
| 138 | +const { links, hasNextPage, cursor } = await listCreatedLinks({ |
| 139 | + address: addressOfCreator, |
| 140 | +}); |
| 141 | + |
| 142 | +// get the claimable assets for this link (will be empty if the link has been claimed) |
| 143 | +const { nfts, balances } = await links[0].assets; |
| 144 | +``` |
| 145 | + |
| 146 | +## Listing transactions and the links they created |
| 147 | + |
| 148 | +`getSentTransactionsWithLinks` will return a list of transactions sent by the provided address. Each result will include the transaction that was sent, along with an array containing any links that were created or regenerated by that transaction. |
| 149 | + |
| 150 | +```ts |
| 151 | +import { getSentTransactionsWithLinks } from '@mysten/zksend'; |
| 152 | + |
| 153 | +const { data, hasNextPage, nextCursor } = await getSentTransactionsWithLinks({ |
| 154 | + address: addressOfCreator, |
| 155 | +}); |
| 156 | + |
| 157 | +for (const { transaction, links } of data) { |
| 158 | + // get the claimable assets for this link (will be empty if the link has been claimed) |
| 159 | + const firstLink = links[0]; |
| 160 | + |
| 161 | + // link is claimed |
| 162 | + firstLink.claimed; |
| 163 | + const { nfts, balances } = firstLink.assets; |
| 164 | + |
| 165 | + // claim link |
| 166 | + await firstLink.link.claimAssets(addressOfClaimer); |
| 167 | +} |
| 168 | +``` |
| 169 | + |
| 170 | +By default `getSentTransactionsWithLinks` will not load the assets for claimed links. This can be changed by passing `loadClaimedAssets: true` to the function. |
| 171 | + |
| 172 | +## Regenerating links |
| 173 | + |
| 174 | +If you lose a link you've created, you can re-generate the link (this can only done from the address that originally created the link): |
| 175 | + |
| 176 | +```ts |
| 177 | +import { listCreatedLinks } from '@mysten/zksend'; |
| 178 | + |
| 179 | +const { links, hasNextPage, cursor } = await listCreatedLinks({ |
| 180 | + address: addressOfCreator, |
| 181 | +}); |
| 182 | + |
| 183 | +// url will be the new link url |
| 184 | +const { url, transaction } = await links[0].link.createRegenerateTransaction(addressOfLinkCreator); |
| 185 | + |
| 186 | +// Execute the transaction to regenerate the link |
| 187 | +const result = await client.signAndExecuteTransaction({ |
| 188 | + transaction, |
| 189 | + signer: keypair, |
| 190 | +}); |
| 191 | +``` |
| 192 | + |
| 193 | +## Bulk link creation |
| 194 | + |
| 195 | +To create multiple links in a single transaction, you can use `ZkSendLinkBuilder.createLinks`: |
| 196 | + |
| 197 | +```ts |
| 198 | +const links = []; |
| 199 | + |
| 200 | +for (let i = 0; i < 10; i++) { |
| 201 | + const link = new ZkSendLinkBuilder({ |
| 202 | + client, |
| 203 | + sender: keypair.toSuiAddress(), |
| 204 | + }); |
| 205 | + |
| 206 | + link.addClaimableMist(100n); |
| 207 | + links.push(link); |
| 208 | +} |
| 209 | + |
| 210 | +const urls = links.map((link) => link.getLink()); |
| 211 | + |
| 212 | +const tx = await ZkSendLinkBuilder.createLinks({ |
| 213 | + links, |
| 214 | +}); |
| 215 | + |
| 216 | +const result = await client.signAndExecuteTransaction({ |
| 217 | + transaction: tx, |
| 218 | + signer: keypair, |
| 219 | +}); |
| 220 | +``` |
| 221 | + |
| 222 | +## Documentation |
| 223 | + |
| 224 | +See the [zkSend SDK documentation](https://sdk.mystenlabs.com/zksend) for more details. |
0 commit comments