npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

testweave-sdk

v0.2.2

Published

This is the SDK of TestWeave. TestWeave is the testing environment of the Arweave.

Downloads

351

Readme

TestWeave SDK

This is the SDK of the TestWeave. TestWeave is the testing environment of the Arweave.

MANDATORY PREREQUISITES

To work with the TestWeave, you need to install a local testnet. To do so, follow the instructions here: https://github.com/ArweaveTeam/testweave-docker

Installation

You can install the SDK by means of NPM, as following:

npm install testweave-sdk

Or you can grab the package directly from the arweave, as following:

npm install https://arweave.net/IkZLzAPt8ArujsQL6Y7Rx1CTZx8N0hgUTLvl5_Uokls

and then import it in your project as the following:

import TestWeave from 'testweave-sdk';

Usage

The SDK supplies handlers for testing the followings:

  1. deploying files on the Arweave;
  2. deploying and testing SmartWeave contracts on the Arweave;

Firstly you need to create a TestWeave instance on the top on an Arweave node, as following:

import TestWeave from 'testweave-sdk';

// init arweave as usual
const arweave = Arweave.init({
  host: 'localhost',
  port: 1984,
  protocol: 'http',
  timeout: 20000,
  logging: false,
});

// init TestWeave on the top of arweave
const testWeave = await TestWeave.init(arweave);

And here you go! Now you can use your arweave instance as usual, but every interaction will be performed on the test network!

For a fast bootstrap checkout the examples in the following sections.

To check all the useful helpers that the SDK supplies, checkout the XXX section.

Example 1 - Submitting a data transaction

  1. Initialize the arweave node and the TestWeave on it:
import Arweave from 'arweave';
import TestWeave from 'testweave-sdk';

const arweave = Arweave.init({
  host: 'localhost',
  port: 1984,
  protocol: 'http',
  timeout: 20000,
  logging: false,
}); 

const testWeave = await TestWeave.init(arweave);
  1. Create a data transaction, sign and post it
const data = `
<html>
  <head>
    <meta charset="UTF-8">
    <title>Info about arweave</title>
  </head>
  <body>
    Arweave is the best web3-related thing out there!!!
  </body>
</html>`
const dataTransaction = await arweave.createTransaction({
  data,
}, testWeave.rootJWK)

await arweave.transactions.sign(dataTransaction, testWeave.rootJWK)
const statusBeforePost = await arweave.transactions.getStatus(dataTransaction.id)
console.log(statusBeforePost); // this will return 404
await arweave.transactions.post(dataTransaction)
const statusAfterPost = await arweave.transactions.getStatus(dataTransaction.id)
console.log(statusAfterPost); // this will return 202
  1. Use the TestWeave to instantly mine the block that contains the transaction
await testWeave.mine();
const statusAfterMine = await arweave.transactions.getStatus(dataTransaction.id)
console.log(statusAfterMine); // this will return 200

Thats it!

Example 2 - Publishing and Interacting with SmartWeave Contracts

  1. Initialize the arweave node and the TestWeave on it:
import Arweave from 'arweave';
import TestWeave from 'testweave-sdk';

const arweave = Arweave.init({
  host: 'localhost',
  port: 1984,
  protocol: 'http',
  timeout: 20000,
  logging: false,
}); 

const testWeave = await TestWeave.init(arweave);
  1. Create a SmartWeave PST contract (you can find a sample contract source and its init state, built on the TestWeave root address, here: https://github.com/ArweaveTeam/testweave-sdk/tree/main/tests/fixtures:
import { createContract, readContract, interactWrite, interactWriteDryRun } from 'smartweave';
import fs from 'fs';

// import the sample contract init state
import contractInitState from 'token-pst-contract.json';
// load the contract as a string
const contractSource = fs.readFileSync('token-pst-contract.js').toString();

// create the contract and mine the transaction for creating it
const c = await createContract(arweave, testWeave.rootJWK, contractSource, JSON.stringify(contractInitState));
await testWeave.mine();
  1. Read the contract state, transfer some token to a generated wallet, and read again the contract state
// read the contract before performing any interaction
const beforeTransaction = await readContract(arweave, c);
console.log(`Before interact write: ${JSON.stringify(beforeTransaction)}`)

// generate a wallet
const jkw = await arweave.wallets.generate();
const generatedAddr = await arweave.wallets.getAddress(jkw)

// interact with the contract
const iwt = await interactWrite(arweave, testWeave.rootJWK, c, {
  function: 'transfer',
  target: generatedAddr,
  qty:5000
}, [] , generatedAddr, '23999392')
console.log(`Interact write transaction: ${JSON.stringify(iwt)}`);

// mine the contract interaction transaction
await testWeave.mine();

// get the new balance of the generated address (it should be 23999392)
const generatedAddressBalance = await arweave.wallets.getBalance(generatedAddr)
console.log(generatedAddressBalance)

// read the contract after the interact write transaction (the generated wallet should own 5000 tokens)
const afterTransaction = await readContract(arweave, c);
console.log(`After interact write: ${JSON.stringify(afterTransaction)}`);

SDK helpers

For easily test Arweave applications, the SDK supplies the helpers described in the following sections.

drop(wallet, quantity)

Drops AR from the root wallet to another one. Use it as followings:

const jkw = await arweave.wallets.generate();
const generatedAddr = await arweave.wallets.getAddress(jkw);
await testWeave.drop(generatedAddr, '10000');
const generatedAddressBalance = await arweave.wallets.getBalance(generatedAddr) // returns 10000

mine()

Mines the following block of the testnet and all the transactions contained in it.

await testWeave.mine();

getter rootJWK

Returns the root JWK, it has an initial balance of 10000000 and the address MlV6DeOtRmakDOf6vgOBlif795tcWimgyPsYYNQ8q1Y

const rootJWK = await testWeave.rootJWK;