# Weil SDK

Welcome to Weil SDK for TypeScript / JavaScript.

## Installation

```
npm i @weilliptic/weil-sdk
```

## Usage

Note: all hex-addresses in the SDK won't have `0x` prefix

### Import (with private key)

```typescript
import { WeilWallet } from '@weilliptic/weil-sdk'

const wallet = new WeilWallet({
  privateKey: '...', // hex-encoded private key,
  sentinelEndpoint: 'https://sentinel.weilliptic.ai',
})
```


### Import in DAPP (with wallet integration)

```typescript
import { WeilWalletConnection } from '@weilliptic/weil-sdk'

const wallet = new WeilWalletConnection({
  walletProvider: window.WeilWallet,
})
```

### Deploy contract

```typescript
const binPath = './my-applet.wasm'
const widlPath = './my-applet.widl'

const fileToHex = async (filePath: string) => {
  const buffer = await fs.readFile(filePath)
  return buffer.toString('hex')
}

const filesAsHex = Promise.all([binPath, widlPath].map(fileToHex))
const fileContent = await fs.readFile(filePath)

const result = await wallet.contracts.deploy(
  ...filesAsHex,
  {
    author: 'Me'
  }
)

console.log(result)
```

### Execute contract

```typescript

const yutakaAppletAddress = '' // hex
const transferResult = await wallet.contracts.execute(
  yutakaAppletAddress,
  'transfer',
  {
    to_addr: '...', // hex
    amount: 1000,
  },
)

console.log('-------------')
console.log(transferResult)

const balanceResult = await wallet.contracts.execute(
  yutakaAppletAddress,
  'balance_for',
  {
    addr: '...', // hex
  },
)

console.log('-------------')
console.log(balanceResult)
```

### Execute contract using generated bindings

```typescript
import { Yutaka } from './bindings.ts'

const yutakaAppletAddress = '' // hex
const yutaka = Yutaka(wallet, appletAddress)

const transferResult = await yutaka.transfer({
  to_addr: '...', // hex
  amount: 1000,
})
console.log('-------------')
console.log(transferResult)

const balance = await yutaka.balance_for({
  addr: '...' // hex
})

console.log('-------------')
console.log(balanceResult)
```
