SDK

@weft/sdk

npm i https://weft.so/sdk/weft-sdk-0.4.0.tgz
import { Weft } from '@weft/sdk'

const weft = new Weft({ connection, wallet })
const { value, logs, units } = await weft.run(`return 6 * 7`)

One client and one call: run weaves the source, sends it, and returns its value, logs and units.

new Weft({ connection, wallet })

A client bound to a connection and a wallet. The wallet signs and pays; the connection is any Solana RPC.

Returns

weftWeft
the client
TypeScript · @weft/sdk
import { Connection } from '@solana/web3.js'
import { Weft } from '@weft/sdk'

const weft = new Weft({
  connection: new Connection(RPC_URL),
  wallet, // a wallet adapter or a Keypair
})

weft.run(source, options?)

Weaves the source, sends it in one transaction (or posts it to a script account first when it is long) and waits for the result.

Options

lang'js' | 'py' | 'lua' | 'lisp'
the source language; read from the source when left out
accountsRecord<string, PublicKey>
accounts the script may touch, by the names it uses
unitsnumber
the compute budget, 200,000 by default, up to 1,400,000

Returns

valueunknown
what the script returned, decoded from return data
logsstring[]
the transaction’s program logs
unitsnumber
compute units the run spent
signaturestring
the transaction signature
slotnumber
the slot it landed in
TypeScript · @weft/sdk
const { value, logs } = await weft.run(
  `transfer(accounts.alice, 25_000_000)
return account(accounts.alice).lamports`,
  { accounts: { alice } },
)

weft.simulate(source, options?)

Runs the script against the current chain state without sending it: the same logs, value and units, nothing signed.

Options

lang'js' | 'py' | 'lua' | 'lisp'
the source language; read from the source when left out
accountsRecord<string, PublicKey>
accounts the script may touch, by the names it uses
unitsnumber
the compute budget, 200,000 by default, up to 1,400,000

Returns

valueunknown
what the script returned, decoded from return data
logsstring[]
the transaction’s program logs
unitsnumber
compute units the run spent
slotnumber
the slot it landed in
TypeScript · @weft/sdk
const { units } = await weft.simulate(source)
await weft.run(source, { units: Math.ceil(units * 1.1) })

weft.deploy(source, options?)

Writes the woven script once to its script account, the address derived from the hash of its bytecode, so it can be called by that hash from anywhere. A script already deployed at that hash is found, not written again.

Options

lang'js' | 'py' | 'lua' | 'lisp'
the source language; read from the source when left out

Returns

hashstring
the sha256 of the bytecode, hex: the script’s identity
addressPublicKey
the script account, derived from ["script", hash]
bytesnumber
the size of the bytecode
signaturesstring[]
create, each write, finalize; empty when it was already there
TypeScript · @weft/sdk
const { hash, bytes } = await weft.deploy(readFileSync('tally.lua', 'utf8'), { lang: 'lua' })
console.log(hash, bytes)

weft.call(hash, options?)

Runs a deployed script by its hash. Its storage is the same one the script uses inline.

Options

accountsRecord<string, PublicKey>
accounts the script may touch, by the names it uses
unitsnumber
the compute budget, 200,000 by default, up to 1,400,000

Returns

valueunknown
what the script returned, decoded from return data
logsstring[]
the transaction’s program logs
unitsnumber
compute units the run spent
signaturestring
the transaction signature
slotnumber
the slot it landed in
TypeScript · @weft/sdk
const { value } = await weft.call(hash, { accounts: { alice } })

weave(source, lang)

The compiler on its own: source in, Weft bytecode out, with no network. Useful for checking a script or reading its size before it runs.

Returns

bytesUint8Array
the bytecode as posted on-chain
programProgram
its constants and functions, for disassembly
TypeScript · @weft/sdk
import { weave } from '@weft/sdk'

const { bytes } = weave('print("gm")', 'py')
bytes.length // 26