Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <pre id="logs"></pre>

<script type="text/javascript">
  class C {
    bold(s) {
      return `<b>${s}</b>`
    }
    italic(s) {
      return `<i>${s}</i>`
    }
    get dim() {
      return this
    }
  }
  var c = new C()

  function log(x) {
    const logEl = document.getElementById("logs")
    if (typeof x !== 'string') {
      x = JSON.stringify(x, null, 2)
    }
    logEl.innerHTML = `${logEl.innerHTML}\n${x}`;
  }
  console.log = log
  
  function error(x) {
    const logEl = document.getElementById("logs")
    if (typeof x !== 'string') {
      x = JSON.stringify(x, null, 2)
    }
    logEl.innerHTML = `ERROR: ${logEl.innerHTML}\n${x}`;
  }
  console.error = error;

  function assert(expr, message) {
    if (!expr) {
      console.log(`ASSERTION ERROR: ${message ?? 'unknown assertion error'}`)
    }
  }

  function printAccountInfo(
    accountInfo,
    opts
  ) {
    const {
      includeData = false
    } = opts ?? {}
    const {
      owner,
      data,
      ...rest
    } = accountInfo
    console.log({
      owner: owner.toBase58(),
      data: includeData ? data.toString() : `<${data.length} bytes>`,
      ...rest,
    })
  }
  async function ensureBackend(luzid) {
    try {
      const res = await luzid.ping.ping()
      return luzid
    } catch (err) {
      console.log('Please start the "luzid" backend in a local terminal')
      return false
    }
  }

function printSolanaExplorerAccountUrl(accountAddr, opts) {
  const { tab = null } = opts ?? {}
  let url = `https://explorer.solana.com/address/${accountAddr}`
  if (tab != null) url += `/${tab}` 
  console.log(`${url}?cluster=custom&customUrl=http%3A%2F%2Flocalhost%3A8899`)
}
</script>
              
            
!

CSS

              
                
              
            
!

JS

              
                import { Buffer } from "https://esm.sh/buffer"
import { LuzidSdk, Cluster, AccountModification } from "https://esm.sh/@luzid/sdk";
import * as web3 from "https://esm.sh/@solana/web3.js"

// The program accounts we will clone and interact with
const programAddr = 'SoLXmnP9JvL6vJ7TN1VqtTxqsc2izmPfF9CsMDEuRzJ'
const postAddr = '5ZspQX4nw95meJHpXBF425NytnNTDgtLBBGVDK9EWmRy'

const BPF_LOADER_UPGRADABLE_PROGRAM_ID = new web3.PublicKey(
  'BPFLoaderUpgradeab1e11111111111111111111111'
)

async function main() {
  const luzid = await ensureBackend(new LuzidSdk())
  if (luzid == null) return
      
  const conn = new web3.Connection(Cluster.Development.apiUrl)
  
  
  // 1. Clone a Devnet account of the program which is holding a post
  {
    console.log(
      c.bold('\n1. Cloning an account of the SolX program from devnet...')
    )

    await luzid.mutator.cloneAccount(Cluster.Devnet, postAddr, {
      commitment: 'confirmed',
    })

    const acc = await conn.getAccountInfo(new web3.PublicKey(postAddr))
    printAccountInfo(acc)

    console.log(
      '\nThe account was cloned. View it in the Solana Explorer at this URL:\n'
    )
    printSolanaExplorerAccountUrl(postAddr, { tab: 'anchor-account' })
    console.log(
      c.dim.italic(
        '\nNotice that it is not showing any anchor data (parsed data) yet.'
      )
    )

    // Assertions (you can safely ignore these)
    {
      assert(
        acc.owner.equals(new web3.PublicKey(programAddr)),
        'owner is solx program'
      )

      assert(acc.lamports > 9000000, 'lamports > 9000000')
      assert(acc.data.length >= 1000, 'data was cloned')
    }
  }

  // 2. Clone the account of the program itself which will auto-clone the IDL account as well
  {
    console.log(c.bold('\n2. Cloning the SolX program account from devnet...'))

    await luzid.mutator.cloneAccount(Cluster.Devnet, programAddr, {
      commitment: 'confirmed',
    })

    const programAcc = await conn.getAccountInfo(
      new web3.PublicKey(programAddr)
    )
    printAccountInfo(programAcc)

    console.log(
      c.dim.italic(
        'The program account was cloned and you can refresh the Solana explorer and should see anchor data' +
          ' for the first account we cloned since now it can access its IDL.'
      )
    )

    // Assertions (you can safely ignore these)
    {
      // Assert the program account itself was properly cloned
      assert(programAcc.lamports > 1000000, 'lamports > 1000000')
      assert(programAcc.data.length >= 36, 'data was cloned')
      assert(
        programAcc.owner.equals(BPF_LOADER_UPGRADABLE_PROGRAM_ID),
        'owner is bpf loader upgradable'
      )
      assert(programAcc.executable, 'executable')

      // Assert the account holding the executable data was properly cloned
      const execDataAddr = 'J1ct2BY6srXCDMngz5JxkX3sHLwCqGPhy9FiJBc8nuwk'
      const execDataAcc = await conn.getAccountInfo(
        new web3.PublicKey(execDataAddr)
      )

      assert(execDataAcc.lamports > 2000000000, 'lamports > 2000000000')
      assert(execDataAcc.data.length >= 400000, 'data was cloned')
      assert(
        execDataAcc.owner.equals(BPF_LOADER_UPGRADABLE_PROGRAM_ID),
        'owner is bpf loader upgradable'
      )
      assert(!execDataAcc.executable, 'not executable')

      // Assert the account holding the IDL was properly cloned
      const idlAddr = 'EgrsyMAsGYMKjcnTvnzmpJtq3hpmXznKQXk21154TsaS'
      const idlAcc = await conn.getAccountInfo(new web3.PublicKey(idlAddr))

      assert(idlAcc.lamports > 6000000, 'lamports > 6000000')
      assert(idlAcc.data.length >= 700, 'data was cloned')
      assert(
        idlAcc.owner.equals(new web3.PublicKey(programAddr)),
        'owner is solx program'
      )
      assert(!execDataAcc.executable, 'not executable')
    }
  }

  // 3. Now for fun let's modify the account data of the first account we cloned
  {
    console.log(
      c.bold('\n3. Modifying the account data of the first account...')
    )

    const acc = await conn.getAccountInfo(new web3.PublicKey(postAddr))

    // Let's UPPER CASE the first word (the message starts at offset 52)
    const data = acc.data
    data[52] = data[52] - 32
    data[53] = data[53] - 32
    data[54] = data[54] - 32
    data[55] = data[55] - 32
    data[56] = data[56] - 32

    await luzid.mutator.modifyAccount(
      AccountModification.forAddr(postAddr).setData(data),
      { commitment: 'confirmed' }
    )

    const msgUtf8 = data.subarray(52, 74).toString('utf8')
    console.log(`\nModified message: ${msgUtf8}`)

    console.log(
      '\nRefresh the Solana explorer and you should see the change:\n'
    )

    printSolanaExplorerAccountUrl(postAddr, { tab: 'anchor-account' })

    // Assertions (you can safely ignore these)
    {
      assert(
        acc.owner.equals(new web3.PublicKey(programAddr)),
        'owner is still solx program'
      )
      assert(msgUtf8.startsWith('HELLO'), 'data was modified to upper case')
    }
  } 
}

main()
  .catch((err: any) => {
    console.error(err.toString())
  })

              
            
!
999px

Console