Installation

npm install @treasure-dev/tdk-core

Quick Start

Connect to Treasure Account

When using the core package, applications must implement their own interfaces for logging in to the Treasure Account, including the multistep verification code flow for logging in with email.

Connect with email

import {
  createTreasureConnectClient,
  logInWithEmail,
  sendEmailVerificationCode,
} from "@treasure-dev/tdk-core";

const client = createTreasureConnectClient({ clientId: "..." });

const email = "example@treasure.lol";
await sendEmailVerificationCode({ client, email });

// Check email...

const { user, tdk } = await logInWithEmail({
  client,
  email,
  verificationCode: "123456",
  sessionOptions: {
    backendWallet: "...",
    approvedTargets: ["0x55d0cf68a1afe0932aff6f36c87efa703508191c"],
  },
});

See the logInWithEmail docs for more configuration options.

Connect with social

import {
  createTreasureConnectClient,
  logInWithSocial,
} from "@treasure-dev/tdk-core";

const client = createTreasureConnectClient({ clientId: "..." });

const { user, tdk } = await logInWithSocial({
  client,
  network: "google",
  sessionOptions: {
    backendWallet: "...",
    approvedTargets: ["0x55d0cf68a1afe0932aff6f36c87efa703508191c"],
  },
});

See the logInWithSocial docs for more configuration options.

Ecosystem game partners should contact Treasure to obtain values for clientId and backendWallet.

Call contract write

After starting a user session with approved target contract addresses, you can now use the TDKAPI object to call contract writes:

import type { AddressString } from "@treasure-dev/tdk-core";

// const { user, tdk } = await logInWithEmail({ ... });

await tdk.transaction.create(
  {
    address: "0x55d0cf68a1afe0932aff6f36c87efa703508191c",
    abi: [
      {
        inputs: [
          {
            internalType: "address",
            name: "_to",
            type: "address",
          },
          {
            internalType: "uint256",
            name: "_amount",
            type: "uint256",
          },
        ],
        name: "mint",
        outputs: [],
        stateMutability: "nonpayable",
        type: "function",
      },
    ] as const,
    functionName: "mint",
    args: [
      user.address as AddressString,
      1000000000000000000000n, // 1,000
    ],
  },
  { includeAbi: true },
);

See the connect-core example application for the full end-to-end source code.

Current Features