|
| 1 | +--- |
| 2 | +title: "Running Move Scripts" |
| 3 | +--- |
| 4 | + |
| 5 | +import CodeBlock from '@theme/CodeBlock'; |
| 6 | + |
| 7 | +# How can I run Move Scripts? |
| 8 | + |
| 9 | +Move scripts are supported in the Aptos TypeScript SDK, Aptos Wallet Adapter, |
| 10 | +and in the Aptos CLI. |
| 11 | + |
| 12 | +## Running scripts with the TypeScript SDK |
| 13 | + |
| 14 | +To use a script with the TypeScript SDK, add the `bytecode` directly to the |
| 15 | +transaction in place of an entry function name. |
| 16 | + |
| 17 | +```ts |
| 18 | +import { readFileSync } from "fs"; |
| 19 | +import { Aptos, Account, AccountAddress } from "@aptos-labs/ts-sdk"; |
| 20 | + |
| 21 | +// Setup client, and account to sign |
| 22 | +const aptos = new Aptos(); |
| 23 | +const account = Account.generate(); |
| 24 | + |
| 25 | +// Load script bytecode |
| 26 | +const buffer = readFileSync("./transfer_half.mv", "buffer"); |
| 27 | +const bytecode = new Uint8Array.from(buffer); |
| 28 | + |
| 29 | +// Build a transaction with the bytecode of the script |
| 30 | +const transaction = await aptos.transaction.build.simple({ |
| 31 | + sender: account.accountAddress, |
| 32 | + data: { |
| 33 | + bytecode, |
| 34 | + typeArguments: ["0x1::aptos_coin::AptosCoin"], |
| 35 | + functionArguments: ["0x1"], |
| 36 | + }, |
| 37 | +}); |
| 38 | + |
| 39 | +// Submit and wait for the transaction to complete |
| 40 | +const pendingTxn = await aptos.signAndSubmitTransaction({ |
| 41 | + signer: account, |
| 42 | + transaction, |
| 43 | +}); |
| 44 | +await aptos.waitForTransaction({ transactionHash: pendingTxn.hash }); |
| 45 | +``` |
| 46 | + |
| 47 | +## Running scripts with the Aptos Wallet Adapter |
| 48 | + |
| 49 | +:::warning |
| 50 | +Not all wallets support scripts, but when the wallet supports scripts, it can be |
| 51 | +provided as below |
| 52 | +::: |
| 53 | + |
| 54 | +Similar to the TypeScript SDK, the same inputs are accepted as a transaction |
| 55 | +type on the wallet adapter. Just simply load the bytecode as a hex `string` or |
| 56 | +a `uint8array`. |
| 57 | + |
| 58 | +```ts |
| 59 | +import { useWallet } from "@aptos-labs/wallet-adapter-react"; |
| 60 | + |
| 61 | +//... |
| 62 | + |
| 63 | +// Load the bytecode either as a uint8array or a hex encoded string |
| 64 | +const BYTECODE_IN_HEX = "0xa11ceb0b...78979"; |
| 65 | + |
| 66 | +export default function App() { |
| 67 | + const { signAndSubmitTransaction } = useWallet(); |
| 68 | + |
| 69 | + function submitScript() { |
| 70 | + signAndSubmitTransaction({ |
| 71 | + data: { |
| 72 | + bytecode: BYTECODE_IN_HEX, |
| 73 | + typeArguments: ["0x1::aptos_coin::AptosCoin"], |
| 74 | + functionArguments: ["0x1"], |
| 75 | + }, |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + // ... |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +## Running scripts with the CLI |
| 84 | + |
| 85 | +Running scripts with the CLI can be run with the command |
| 86 | + |
| 87 | +```shell |
| 88 | +aptos move run-script |
| 89 | +``` |
| 90 | + |
| 91 | +There are two ways to run it, with a pre-compiled script, or it will compile the |
| 92 | +script in-place similar to the compile step. |
| 93 | + |
| 94 | +If you already have a compiled script, you can run it |
| 95 | +with `--compiled-script-path` like the example below: |
| 96 | + |
| 97 | +```shell |
| 98 | +aptos move run-script --compiled-script-path /opt/git/developer-docs/apps/docusaurus/static/move-examples/scripts/transfer_half/script.mv |
| 99 | +``` |
| 100 | + |
| 101 | +Similarly, if it's not compiled, just use `--script-path` |
| 102 | + |
| 103 | +```shell |
| 104 | +aptos move run-script --script-path ./sources/transfer_half.move |
| 105 | +``` |
0 commit comments