salt-sdk - v0.0.45
    Preparing search index...

    Quick start

    The Salt SDK is written in TypeScript, and can be used in Node.js or the browser. Bun and Deno are untested, but may work - let us know on Discord if you give it a try!

    Run the following commands to create a new JS project with the correct dependencies.

    # Create a new project directory and create a package.json file
    mkdir salt-bot
    cd salt-bot
    npm init .

    # Install the two key dependencies
    npm install --save salt-sdk
    npm install --save viem

    You are ready to create a script that uses the Salt SDK. Most of the time, you will be using the methods on the Salt. This is the first place to look for what functionality is available in the SDK. Let's start by creating a new file called index.mjs and add the following code:

    import { Salt } from 'salt-sdk';

    async function main() {
    const salt = new Salt({
    environment: 'TESTNET',
    });

    // Step 2 code will go here
    }

    main().catch(console.error);

    You could run this by calling node index.mjs, but it won't do anything useful yet.

    Tip

    You can see more examples of how to connect to different environments in the Salt SDK constructor documentation.

    import { Salt } from 'salt-sdk';
    // New dependencies
    import { createWalletClient, http } from 'viem';
    import { privateKeyToAccount } from 'viem/accounts';
    import { arbitrumSepolia } from 'viem/chains';

    async function main() {
    const salt = new Salt({
    environment: 'TESTNET',
    });

    // New code
    const account = privateKeyToAccount(process.env.PRIVATE_KEY);
    const signer = createWalletClient({
    account,
    chain: arbitrumSepolia,
    transport: http(),
    });

    await salt.authenticate(signer);
    console.log('Authenticated!');
    }

    main().catch(console.error);

    You can run this, passing your private key as an environment variable:

    PRIVATE_KEY="0x..." node index.mjs
    
    Warning

    A plain environment variable is fine for this local walkthrough, but don't use one for a real key — it's easy to leak via shell history, a committed .env file, or process listings. Load production keys from a secrets manager or another secure source instead.

    And you should see the 'Authenticated!' message.

    At this point, if the keypair you are using is already a collaborator on an organisation and part of an account, you are ready to send a transaction. We are going to transfer some testnet $USDC on Arbitrum Sepolia (chain id: 421614).

    Warning

    Sending a transaction requires a lot of information. See the submitTx documentation for more guidance.

    import { Salt } from 'salt-sdk';
    import { createPublicClient, createWalletClient, http } from 'viem';
    import { privateKeyToAccount } from 'viem/accounts';
    import { arbitrumSepolia, sepolia } from 'viem/chains';

    async function main() {
    const salt = new Salt({
    environment: 'TESTNET',
    });

    const account = privateKeyToAccount(process.env.PRIVATE_KEY);
    const walletClient = createWalletClient({
    account,
    chain: arbitrumSepolia,
    transport: http(),
    });

    await salt.authenticate(walletClient);

    // Changes: Now we are setting up a public client for the destination network
    const publicClient = createPublicClient({
    chain: sepolia,
    transport: http(process.env.RPC_URL),
    });

    try {
    const FROM_SALT_ACCOUNT_ID = '0EEEEEEEE';
    const TO_ADDRESS = '0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE';
    const AMOUNT = '0.01';
    const data = '';
    const gas = undefined;

    await salt.submitTx({
    accountId: FROM_SALT_ACCOUNT_ID,
    to: TO_ADDRESS,
    value: AMOUNT,
    chainId: sepolia.id,
    userAddress: account.address,
    walletClient,
    publicClient,
    data: data ?? undefined,
    gas: gas ?? undefined,
    });

    console.log(`Sent ${AMOUNT} to ${TO_ADDRESS}`);
    } catch (error) {
    console.error('Transfer failed:', error);
    }
    }

    main().catch(console.error);

    Run this with the new extra parameter, RPC_URL:

    PRIVATE_KEY="0x..." RPC_URL="https://sepolia.drpc.org" node index.mjs
    

    submitTx uses a similar interface, but allows you to call arbitrary smart contract functions. Check out the Salt.submitTx for an example.

    Tip

    You can list organisations this keypair is a collaborator on using getOrganisations, and the accounts that belong to them using getAccounts.

    Tip

    You can store your Salt authentication token to avoid having to authenticate each time your code runs. See the constructor or setAuthToken. Remember to store your auth token securely if you do this.

    4. Next steps

    Using the steps outlined above, you should be in a position to start sending transactions from your Salt account. If you need more complex functionality (for example, automatically listening for account invitations), look around the SDK documentation. If something is missing, let us know in Discord.

    Now you know how to do the basics. The next steps are:

    • Learn more about the Salt SDK by exploring the API documentation.
    • Join the Salt community to connect with other developers and get help with your projects.