Giter VIP home page Giter VIP logo

smartweave's Introduction

SmartWeave

Gitopia

Simple, scalable smart contracts on the Arweave protocol

Uses lazy-evaluation to move the burden of contract execution from network nodes to smart contract users. Currently, SmartWeave supports JavaScript, using the client's unmodified execution engine.

Version: 0.4

For information on how the contracts execute, how to write one, and the API, read the Contract Guide and check some of the examples

For information on how to create a new Profit Sharing Token (PST), you can read the PST Creation Guide.

For a description of the SDK methods available, you can check here

CLI Usage

npm install -g smartweave

You can deploy a contract as follows:

smartweave create [SRC LOCATION] [INITIAL STATE FILE] --key-file [YOUR KEYFILE]

Or, using an existing contract source that is already deployed but with a new initial state and contract id:

smartweave [SRC TX] [INITIAL STATE FILE] --key-file [YOUR KEYFILE]

Check its state:

smartweave read [CONTRACT TXID]

Interact with it:

smartweave write [CONTRACT TXID] --key-file [YOUR KEYFILE] \
  --input "[CONTRACT INPUT STRING HERE]"

When interacting with the contract, the value passed to --input must be valid json. Typically an object is used:

--input '{ "function": "transfer", "qty": 1984 }'

To test a contract interaction without writing it to the network, append --dry-run to your --interact call.

License

This project is licensed under the terms of the MIT license.

smartweave's People

Contributors

aidanok avatar alexander-morris avatar artob avatar benya7 avatar cedriking avatar charmful0x avatar dependabot[bot] avatar handlebauer avatar hlolli avatar jespern avatar littledivy avatar luca-arweave avatar nekomeowww avatar ppedziwiatr avatar rosmcmahon avatar samcamwilliams avatar syedghazanferanwar avatar tierralibre avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

smartweave's Issues

Allowing custom tags on interaction transactions

Hey everyone!

First off, just want to give a huge thanks to everyone who made SmartWeave possible. This is an amazing library, and we love it. With the development of Verto, we've come to realize a few things that may be beneficial to add to SmartWeave to improve the way developers interact with PSTs.

One of these realizations is allowing users to input their own tags when creating an interaction transaction with the library. For Verto especially, we like to tag on additional tags that allow us to track statistics of these transfers. Right now, unfortunately, we're having to rewrite some of our code to add this capability (and avoid using SmartWeave library interactions specifically), as seen here: useverto/trading-post#45

I figured I'd make this issue because we think that this would be useful not just for Verto and our team, but for all developers as a whole.

Thanks!

Cache transactions in SWC readContractState

If SmartWeave.contracts.readContractState is used, all transactions of the target contract should be cached (maybe only if a parameter is specified). Currently, transactions up to the block height must be continuously refetched causing a massive slowdown in performance (up to 10 minutes for 100 transactions involoving readContractState).

[Proposal] Smartweave plugins

Hey there, this is Andres from Verto.exchange.

Overview:
Plugins are a way to introduced custom Javascript functions to the execution of smart weave contracts.

Details:
Plugins should be passed in a static call to a setter, for example:

import { setPlugins } from "smartweave";

setPlugins({
   helloWorld: () => console.log("hello world");
});

Or they can be passed in the readContract function.

export declare function readContract(arweave: Arweave, contractId: string, height?: number, returnValidity?: boolean, plugins?: any): Promise<any>
import { readContract } from "smartweave";

readContract(arweaveClient,
            contractId,
            undefined,
            true,
            {
                helloWorld: () => console.log("hello world");
            }
);

Accessing plugins
In order to access the plugins inside the contract, we can do:

SmartWeave.plugins[plugin_name]();

where SmartWeave is the global that all contract has, with a new object, which is call plugins, and contains the functions we passed in our object

SmartWeave.plugins['helloWorld']();

Security Flaws
A contract can override, or modify the plugins object. For this reason, this object should be frozen, and all its properties should also be frozen. A deep Object.freeze if you may.

@t8

SmartWeave Explorer

It would be great if there was a website where you can debug/read/input data into contracts.
I believe this is what it would need.

  1. A way to drop in a wallet
  2. An input for the contract id
  3. A JSON editing tool for inputting data to a contract
  4. An output log

interactWriteDryRun should work with interactWrite

Currently interactWrite will write the transaction even if the input might be faulty. Furthermore, interactWrite only displays the transaction id after submitting.

It would be great if there was the following:

  1. Instead of returning only a tx id. Return a tx id plus the contract result as seen here: https://github.com/ArweaveTeam/SmartWeave/blob/master/SDK.md#interactwritedryrun

  2. If the dry run returns an error. Throw an error instead of writing the bad input.

  3. This is configurable via an opts parameter something like outputResult: boolean. Therefore updating interactWrite to:

async function interactWrite(arweave: Arweave, wallet: JWKInterface, contractId: string, input: any, opts: Object): Promise<ContractInteractionResult | string>

Add a feature to call "interactRead" from other contract

Currently SWC offers only one way of calling other contract - ie. reading its current state:
SmartWeave.contracts.readContractState(contractId: string): Promise<any>

This is somewhat limiting, as it prevents from calling any logic from external contract.

As "interactRead" does not change state (ie. it only returns "result", not new "state"), adding a feature to call it from another contract doesn't seem to introduce any security risks.

Having such feature would allow to split business logic between different contracts (eg. vesting or staking contract and PST contract).

Current workaround forces to firstly call state update on the callee contract (and wait for a transaction to be mined) - only then the caller contract can read updated state from callee contract and perform its logic - that's really inconvenient.

Why do we need to add 4 bytes of random characters when interacting with contracts?

When I looked at the source code, I found the following snippet:

async function createTx(
  arweave: Arweave,
  wallet: JWKInterface | 'use_wallet',
  contractId: string,
  input: any,
  tags: { name: string; value: string }[],
  target: string = '',
  winstonQty: string = '0',
  reward?: string,
): Promise<Transaction> {
  const options: Partial<CreateTransactionInterface> = {
    data: Math.random().toString().slice(-4),                                // Why need this?
    reward,
  };

Does anyone know why we need the random string?

data: Math.random().toString().slice(-4),

check CONTRACT_TXID validity pre-interaction

it would be great to check the validity of the desired contract TXID by decoding the TX tags and check for the existence of the main tags:

As in:
App-Name: SmartWeaveContract
Content-Type: application/json

And the existence of Contract-Src as a tag key

Exceptions thrown from SmartWeaveGlobal should not be ignored.

Currently, any uncaught exception is ignored and contract execution is continued.

Exceptions that originate from the SmartWeave global, in at least some cases, possibly all, should not be ignored. For example, readContractState could fail due a network timeout, contract execution should not continue in this case, it should be retried.

require() for structuring projects

It would be great to have some sort of require() function built in to include files.
Of course, this require would only actually work with SmartWeave compatible files.

I've actually written a simple workaround to this here. However, that solution just simply appends all files together.

An example would be something like:

const Balance = require('./balance.js');

export function handle(state, action) {
  Balance(state, action);
}
...

Contract Migrations

While a contract migration is just copying a state from an old contract to a new one.
Would be great to have that as part of the core SmartWeave library.

An example migration would be something like that.

smartweave migrate [Contract Source] [Existing Contract ID]

Which would then output the new contract id.

Fetch all transection inputs in one go with graphql

Requesting: https://arweave.net:443/info
Response:   info - 200
Requesting: https://arweave.net:443/tx/JY2pKtf5LkhNsdfaJSwA84DAYtEuQWXc-tZEOS3iGpg
Requesting: https://arweave.net:443/graphql
Response:   graphql - 200
Response:   tx/JY2pKtf5LkhNDFKyJSwA84DAYtEuQWXdfdfOS3iGpg - 200
Requesting: https://arweave.net:443/JY2pKtf5LkhNDFKyJSwA84DAYtdfaWXc-tZEOS3iGpg
Response:   JY2pKtf5LkhNaKyJSwA84DsdfuQWXc-tZEOS3iGpg - 200
Requesting: https://arweave.net:443/tx/sGNWwtvvvu4IRdfasdf0WQh18mRf0D4Hc17hjBLVZ2-hGF4
Response:   tx/sGNWwtvvvu4IRY10WQh18mRfsdfHc17hjBLVZ2-hGF4 - 200
Requesting: https://arweave.net:443/sGNWwsfdasf4IRY10WQh18mRf0D4Hc17hjBLVZ2-hGF4
Response:   sGNWwtvvvu4IRY10WQh18sdfsdf0D4Hc17hjBLVZ2-hGF4 - 200
Replaying 3 confirmed interactions

When reading a state of SW contract, according to the logs I found the SDK first fetch the ids of transections, and then fetch them one by one sequentially. Since graphQL is already involved, why not query the inputs together with the ids in the beginning? This could save a lot of time and network traffic : )

Mistake in README?

"To test a contract interaction without writing it to the network, append --dry-run to your --interact call."

From --help and the rest of the README, it seems the only calls possible are read, write and create.

Should this read:

"To test a contract interaction without writing it to the network, append --dry-run to your write call." ?

"interactRead" does not set block timestamp properly

I've noticed that block timestamp is not properly set for "interactRead" operation - ie. SmartWeave.block.timestamp inside contract's handle function returns null for such operations (it works properly for "interactWrite" though).
I'm not sure why timestamp is set to null in

timestamp: null,

Btw. - "timestamp" property does not seem to be documented in https://github.com/ArweaveTeam/SmartWeave/blob/master/CONTRACT-GUIDE.md#smartweave-global-api

Thanks.

Running untrusted Javascript in SmartWeave contracts

Hey,
I think we're going to need a way to run any JavaScript safely. This is going to be needed for certain things that are coming into the ecosystem such as AMM's. If we are to run random PST contracts in the browser we need to sandbox the code so it's 100% harmless.
I wanted this to be the start of the discussion on the topic. I have a few ideas so far. Sandboxing can be done in two ways:

  1. WASM/WASI inherently sandboxes execution. This won't be easy to do though as V8 (NodeJS JavaScript engine) doesn't support compiling to WASM
  2. Running SmartWeave contracts in its own JS interpreter which can box up the code execution. See an example here: https://github.com/jterrace/js.js

Let me know your thoughts on this.

Browser Usage Entry in the Readme

Coming to the project from scratch, it's not 100% apparent that you can use this lib from a browser context.
How do people feel about adding a section called Web Usage that does a really simple pseudo-code

<script> </script> example ?

PST and ERC20

I noticed there is a PST token.
In ERC20, there is an approve method which is very useful.
Is there any other standard token like ERC20?

George Wu

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.