GithubHelp home page GithubHelp logo

slyxby / near-sdk Goto Github PK

View Code? Open in Web Editor NEW

This project forked from 4ire-labs/near-sdk

0.0 0.0 0.0 271 KB

SDK for NEAR Protocol

Home Page: https://npmjs.com/package/@4ire-labs/near-sdk

License: The Unlicense

Shell 0.50% TypeScript 99.50%

near-sdk's Introduction

@4ire-labs/near-sdk

SDK for NEAR Protocol

Coverage Status

Install

npm install @4ire-labs/near-sdk

Use

TL;DR run example

NEAR_ENV=testnet
NEAR_SENDER_ID=name.testnet
NEAR_SENDER_PRIVATE_KEY=ed25519:data
import * as near from '@4ire-labs/near-sdk'
import 'dotenv/config'

NFT

class NFTBasic extends near.NEP4Standard {
    mintToken(owner_id: string, token_id: number): Promise<near.Outcome<void>> {
        return this.callRaw({
            methodName: 'mint_token',
            args: {owner_id, token_id},
        })
    }
}

async function token() {
    const ownerContract = near.custodianAccount(near.accountIdBySlug('nep4'))
    const NFTContract = await near.Contract.connect(
        NFTBasic,
        near.accountIdBySlug('nep4'),
        ownerContract,
    )
    const tokenId = +new Date
    const mintTrx = await NFTContract.mintToken(ownerContract.accountId, tokenId)
    console.log(`Minted NFT #${tokenId}:`, {
        accountId: await NFTContract.getTokenOwner(tokenId),
        transactionId: mintTrx.transactionId,
    })
}
Minted NFT #1622990248520: {
  accountId: 'nep4.local',
  transactionId: 'nep4.local:5d5o65nfmWWdbXXwtfM9mdqyR6E7X2VEPt586JeSHQK4'
}

Account

async function account() {
    const deposit = '0.05'
    const entropy = Buffer.from('0123456789ABCDEF')
    const mnemonic = near.generateMnemonic(entropy)
    console.log('mnemonic:', mnemonic)

    // Implicit Account
    // https://docs.near.org/docs/roles/integrator/implicit-accounts
    const newImplicitAccount = near.mnemonicToAccount(mnemonic)
    console.log('Implicit Account:', {
        accountId: newImplicitAccount.accountId,
        publicKey: newImplicitAccount.keyPair.publicKey.toString(),
    })
    await near.writeUnencryptedFileSystemKeyStore(newImplicitAccount)

    // Getting Sender Account
    const sender = near.parseAccountNetwork()
    console.log('Sender Account:', {
        accountId: sender.accountId,
        publicKey: sender.keyPair.publicKey.toString(),
    })
    let trx: near.Outcome<boolean>
    let newAccount: near.AccountNetwork

    // Normal Account
    newAccount = near.mnemonicToAccount(mnemonic, near.accountIdBySlug(`sample${+new Date}`))
    await near.writeUnencryptedFileSystemKeyStore(newAccount)
    trx = await near.createAccount(sender, newAccount, deposit)
    console.log('Created normal account:', {
        accountId: newAccount.accountId,
        publicKey: newAccount.keyPair.publicKey.toString(),
        transactionId: trx.transactionId,
    })
    trx = await near.deleteAccount(newAccount)
    console.log('Deleted transactionId:', trx.transactionId)

    // Custodial Account
    newAccount = near.custodianAccount(near.accountIdBySlug(`sample${+new Date}`), sender)
    trx = await near.createAccount(sender, newAccount, deposit)
    await near.writeUnencryptedFileSystemKeyStore(newAccount)
    console.log('Created custodial account:', {
        accountId: newAccount.accountId,
        publicKey: newAccount.keyPair.publicKey.toString(),
        transactionId: trx.transactionId,
    })
    trx = await near.deleteAccount(newAccount)
    console.log('Deleted transactionId:', trx.transactionId)
}
mnemonic: coral maze mimic half fat breeze thought choice drastic boss bacon middle
Implicit Account: {
  accountId: '47d322f48bf873ad10c1b6ed2253518d3d3e0cad9a1a72a9c62b311400b72c7a',
  publicKey: 'ed25519:5qNgFf7z5huxn11jgPJBnX2RGdmcmYodhLWnd71oozgH'
}
Sender Account: {
  accountId: 'local',
  publicKey: 'ed25519:7PGseFbWxvYVgZ89K1uTJKYoKetWs7BJtbyXDzfbAcqX'
}
Created normal account: {
  accountId: 'sample1622990251089.local',
  publicKey: 'ed25519:5qNgFf7z5huxn11jgPJBnX2RGdmcmYodhLWnd71oozgH',
  transactionId: 'local:EpGPDCgKUbdGZQsx517bMSfiYSbCEqa49xrw1J6Voobk'
}
Deleted transactionId: sample1622990251089.local:CLnDXM7JAZqiWLWdthdrAB6SdBNmW9S9SFZvPKP8JmNn
Created custodial account: {
  accountId: 'sample1622990256631.local',
  publicKey: 'ed25519:7PGseFbWxvYVgZ89K1uTJKYoKetWs7BJtbyXDzfbAcqX',
  transactionId: 'local:2TCrKv62VeViFdnQB9AknYpVXRuxVp4zA8rccAgNmctq'
}
Deleted transactionId: sample1622990256631.local:3gwSA1hEiZ3rWja3tPifZPbWhr7ok39FTfaNg4VrqNbP

near-sdk's People

Contributors

ilyar avatar slyxby avatar

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.