Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(sdk): implement BRC-20 #89

Draft
wants to merge 17 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/node/brc-20-deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { BRC20Deploy, JsonRpcDatasource, Ordit } from '@sadoprotocol/ordit-sdk'

const network = 'regtest'
const wallet = new Ordit({
bip39: '<mnemonic>',
network
})
wallet.setDefaultAddress('taproot')
const datasource = new JsonRpcDatasource({ network })

async function main() {
const tx = new BRC20Deploy({
address: wallet.selectedAddress,
pubKey: wallet.publicKey,
destinationAddress: wallet.selectedAddress,
feeRate: 2,
network: 'regtest',
tick: 'TEST',
supply: 1000000000,
limit: 100,
})

const revealData = await tx.reveal()
console.log({ revealData })

const hex = await tx.deploy()
const signedTxHex = wallet.signPsbt(hex, { isRevealTx: true })
const txId = await datasource.relay({ hex: signedTxHex })

console.log({ txId })
}

main()
34 changes: 34 additions & 0 deletions examples/node/brc-20-mint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { BRC20Mint, JsonRpcDatasource, Ordit } from '@sadoprotocol/ordit-sdk'

const network = 'regtest'
const wallet = new Ordit({
bip39: '<mnemonic>',
network
})
wallet.setDefaultAddress('taproot')
const datasource = new JsonRpcDatasource({ network })

async function main() {
const tx = new BRC20Mint({
address: wallet.selectedAddress,
pubKey: wallet.publicKey,
destinationAddress: wallet.selectedAddress,
feeRate: 2,
network,
tick: 'TEST',
amount: 100
})

const revealData = await tx.reveal()
console.log({ revealData })

const hex = await tx.mint()
if(!hex) return

const signedTxHex = wallet.signPsbt(hex, { isRevealTx: true })
const txId = await datasource.relay({ hex: signedTxHex })

console.log({ txId })
}

main()
38 changes: 38 additions & 0 deletions examples/node/brc-20-transfer-executor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { BRC20TransferExecutor, JsonRpcDatasource, Ordit } from '@sadoprotocol/ordit-sdk'

const network = 'regtest'
const wallet = new Ordit({
bip39: '<mnemonic>',
network
})

const destWallet = new Ordit({
bip39: '<mnemonic>',
network
})

wallet.setDefaultAddress('taproot')
destWallet.setDefaultAddress('taproot')
const datasource = new JsonRpcDatasource({ network })
const destinationAddress = wallet.selectedAddress

async function main() {
const tx = new BRC20TransferExecutor({
address: wallet.selectedAddress,
pubKey: wallet.publicKey,
destinationAddress,
feeRate: 2,
network,
tick: 'TEST',
amount: 10
})

const hex = await tx.transfer()
if(hex) {
const signedTxHex = wallet.signPsbt(hex, { isRevealTx: true })
const txId = await datasource.relay({ hex: signedTxHex })
console.log({ txId })
}
}

main()
34 changes: 34 additions & 0 deletions examples/node/brc-20-transfer-generator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { BRC20TransferGenerator, JsonRpcDatasource, Ordit } from '@sadoprotocol/ordit-sdk'

const network = 'regtest'
const wallet = new Ordit({
bip39: '<mnemonic>',
network
})
wallet.setDefaultAddress('taproot')
const datasource = new JsonRpcDatasource({ network })

async function main() {
const tx = new BRC20TransferGenerator({
address: wallet.selectedAddress,
pubKey: wallet.publicKey,
feeRate: 3,
network,
tick: 'TEST',
amount: 10
})

const revealData = await tx.reveal()
console.log({ revealData })

// generate transfer inscription
const hex = await tx.generate()

if(hex) {
const signedTxHex = wallet.signPsbt(hex, { isRevealTx: true })
const txId = await datasource.relay({ hex: signedTxHex })
console.log({ txId })
}
}

main()
6 changes: 5 additions & 1 deletion examples/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
"instant-buy": "node instant-buy",
"publish-collection": "node collections",
"build-psbt": "node build-psbt",
"split-utxo": "node split-utxo"
"split-utxo": "node split-utxo",
"brc20-deploy": "node brc-20-deploy",
"brc20-mint": "node brc-20-mint",
"brc20-transfer-generator": "node brc-20-transfer-generator",
"brc20-transfer-executor": "node brc-20-transfer-executor"
},
"author": "",
"license": "ISC",
Expand Down
48 changes: 48 additions & 0 deletions packages/sdk/src/api/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Transaction as BTCTransaction } from "bitcoinjs-lib"

import { Rarity } from "../inscription/types"
import { JsonRpcPagination } from "../modules/types"
import { Transaction, UTXO } from "../transactions/types"
import { RequireAtLeastOne } from "../utils/types"

Expand Down Expand Up @@ -72,3 +73,50 @@ export interface GetSpendablesOptions {
export interface GetBalanceOptions {
address: string
}

export interface GetTokenOptions {
tick: string
}

export interface GetTransfersOptions {
filter: RequireAtLeastOne<{
inscription?: string
tick?: string
from?: string
to?: string
}>
pagination?: JsonRpcPagination
}

export interface GetTransfersResponse {
transfers: Array<{
inscription: string
tick: string
slug: string
amount: number
from: {
address: string
block: number
timestamp: number
}
to: {
address: string
block: number
timestamp: number
}
}>
pagination?: JsonRpcPagination
}

export interface GetAddressTokensOptions {
address: string
}

export interface GetAddressTokensResponse {
address: string
tick: string
slug: string
total: number
available: number
transferable: number
}
117 changes: 117 additions & 0 deletions packages/sdk/src/brc20/BRC20Deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { MINIMUM_AMOUNT_IN_SATS } from "../constants"
import { Inscriber } from "../transactions/Inscriber"
import { BRC20DeployOptions, BRC20DeployPayloadAttributes } from "./types"

export class BRC20Deploy extends Inscriber {
private tick: string
private supply: number
private limit?: number
private decimals?: number

constructor({
address,
pubKey,
destinationAddress,
feeRate,
network,
tick,
supply,
limit,
decimals
}: BRC20DeployOptions) {
super({
network,
address,
changeAddress: address,
destinationAddress: destinationAddress || address,
publicKey: pubKey,
feeRate,
postage: MINIMUM_AMOUNT_IN_SATS,
mediaType: "<temp-type>", // Set on payload creation
mediaContent: "<temp-content>" // Set on payload creation
})

this.tick = tick
this.supply = supply
this.limit = limit
this.decimals = decimals
}

private async validateDeployOptions() {
if (this.decimals && (this.decimals < 0 || this.decimals > 18)) {
throw new Error("Invalid decimals")
}

if (this.limit && this.limit <= 0) {
throw new Error("Invalid limit")
}

if (this.supply <= 0) {
throw new Error("Invalid supply")
}

if (this.tick.length < 4) {
throw new Error("Invalid tick")
}

try {
const token = await this.datasource.getToken({ tick: this.tick })
if (token) {
throw new Error("Token already exists")
}
} catch (error) {
if (error.message !== "Token not found") {
throw error
}
}

this.generatePayload()
}

private generatePayload() {
const payload: BRC20DeployPayloadAttributes = {
p: "brc-20",
op: "deploy",
tick: this.tick,
max: this.supply.toString(),
lim: this.limit?.toString(),
dec: this.decimals?.toString()
}

this.media = {
content: JSON.stringify(payload),
type: "text/plain;charset=utf-8"
}
}

async reveal() {
await this.validateDeployOptions()

// generate deposit address and fee for inscription
const { address, revealFee: amount } = await this.generateCommit()

return { address, amount }
}

async deploy() {
await this.validateDeployOptions()
await this.generateCommit()

const isReady = await this.isReady()
if (isReady) {
await this.build()
return this.toHex()
}
}

async recoverFunds() {
await this.validateDeployOptions()
await this.recover()

const isReady = await this.isReady()
if (isReady) {
await this.build()
return this.toHex()
}
}
}
Loading