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

@anastasia-labs/direct-offer-offchain

v0.2.0

Published

https://docs.github.com/en/packages/quickstart

Downloads

5

Readme

Table of Contents

Direct Offer Offchain

Introduction

The Direct Offer Offchain project provides a typescript based SDK to convenietly interact with Plutarch-based implementation of a smart contract enabling peer-to-peer trading, in a trustless manner, for the Cardano blockchain. Without the need for a trusted third party or a Decentralized Exchange (DEX), a user can put up any Cardano native asset(s) for sale in exchange for any user-specified native asset(s).

This project is funded by the Cardano Treasury in Catalyst Fund 10

Documentation

What is Peer-to-Peer (P2P) trading?

P2P trading in the context of this project refers to the direct buying and selling of Cardano Native Tokens (both Fungible & Non-Fungible Tokens) among users, without a third party or an intermediary. This is unlike buying and selling digital assets using a Centralized Exchange (CEX), where you cannot transact directly with counterparties or a DEX where you trade against a fixed Liquidity Pool.

Trading on a CEX requires you to give custody of your tokens to them, so they can execute the trades you enter based on their charts and market order aggregators. A CEX provides access to their order book and facilitates trades and takes fees in exchange.

Depending on the type of order you use, effects such as slippage may mean you don’t get the exact price you want. P2P trading, on the other hand, gives you full control over pricing, settlement time, and whom you choose to sell to and buy from. What is even better you don't need to give custody of your assets to a centralized entity, they are locked in a contract from which you can reclaim them up until the point they are bought.

How can this project facilitate P2P trading?

This project fulfills the cornerstone requirement of a trusted Escrow, over seeing the trade in the form of a smart contract. It locks the seller's assets in the contract until a buyer provides the required ask price or the seller wishes to cancel the offer and claim the funds back.

Usage Example

Install package

npm install @anastasia-labs/direct-offer-offchain

or

pnpm install @anastasia-labs/direct-offer-offchain

Setup Lucid & Offer Scripts

// You can get the compiled scripts here: https://github.com/Anastasia-Labs/direct-offer/tree/master/compiled
import spendingValidator from "../directOfferSpending.json" assert { type : "json" };
import stakingValidator from "../directOfferStaking.json" assert { type : "json" };

export const lucid = await Lucid.new(
  new Maestro({
    network : "Preprod",
    apiKey: "your maestro api key",
  }),
  "Preprod"
);

lucid.selectWalletFromPrivateKey(
  "your secret key here e.g. ed25519_...",
);

const offerScripts = {
  spending: spendingValidator.cborHex,
  staking: stakingValidator.cborHex
};

Make Offer

import {
  MakeOfferConfig,
  makeOffer
} from "@anastasia-labs/direct-offer-offchain";

const makeOfferConfig: MakeOfferConfig = {
  offer: {
    ["lovelace"]: 10_000_000n
  },
  toBuy: {
    [toUnit("e16c2dc8ae937e8d3790c7fd7168d7b994621ba14ca11415f39fed72",
    "4d494e")]: 10_000n,
  },
  scripts: offerScripts,
};

const makeOfferUnSigned = await makeOffer(lucid, makeOfferConfig);

if (makeOfferUnSigned.type == "ok") {
  const makeOfferSigned = await makeOfferUnSigned.data.sign().complete();
  const makeOfferHash = await makeOfferSigned.submit();
  await lucid.awaitTx(makeOfferHash);
  console.log(`Made offer: ${makeOfferHash}`)
}

Fetch Offer

import {
  FetchOfferConfig,
  getOfferUTxOs
} from "@anastasia-labs/direct-offer-offchain";

const offerConfig: FetchOfferConfig = {
  scripts: offerScripts
};

const offers = await getOfferUTxOs(lucid, offerConfig);
console.log("Available Offers", offers);

Accept Offer

import {
  AcceptOfferConfig,
  acceptOffer
} from "@anastasia-labs/direct-offer-offchain";

const acceptOfferConfig: AcceptOfferConfig = {
  offerOutRef: offers[0].outRef,
  scripts: offerScripts
};

const acceptOfferUnsigned = await acceptOffer(lucid, acceptOfferConfig);

if (acceptOfferUnsigned.type == "ok"){
  const acceptOfferSigned = await acceptOfferUnsigned.data
  .sign()
  .complete();
  const acceptOfferSignedHash = await acceptOfferSigned.submit();
  await lucid.awaitTx(acceptOfferSignedHash);
  console.log(`Accepted offer: ${acceptOfferSignedHash}`)
}

Local Build

In the main directory

pnpm run build

Test framework

https://github.com/vitest-dev/vitest

Running Tests

pnpm test

direct-offer-offchain