Skip to content

Commit

Permalink
Fix pr
Browse files Browse the repository at this point in the history
  • Loading branch information
scrypt committed Sep 22, 2023
1 parent 2ce192b commit 182478c
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 258 deletions.
47 changes: 22 additions & 25 deletions src/contracts/bsv20V1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,8 @@ import {
import { Ordinal } from './ordinal'
import { OrdP2PKH } from './ordP2PKH'
import { fromByteString } from '../utils'
import { OneSatApis } from './1satApis'

export type TokenReceiver = {
instance: BSV20V1 | OrdP2PKH
amt: bigint
}

export interface OrdMethodCallOptions<T> extends MethodCallOptions<T> {
transfer: Array<TokenReceiver>
tokenChangeAddress?: bsv.Address
skipTokenChange?: boolean
}
import { OneSatApis } from '../1satApis'
import { FTMethodCallOptions, FTReceiver } from '../types'

/**
* A base class implementing the bsv20 v1 protocol
Expand Down Expand Up @@ -173,14 +163,15 @@ export class BSV20V1 extends SmartContract {
): MethodCallTxBuilder<BSV20V1> {
return async function (
current: BSV20V1,
options: OrdMethodCallOptions<BSV20V1>,
options: FTMethodCallOptions<BSV20V1>,
...args
): Promise<ContractTransaction> {
const tokenChangeAmt =
current.getAmt() -
options.transfer.reduce((acc, receiver) => {
return (acc += receiver.amt)
}, 0n)
const tokenChangeAmt = Array.isArray(options.transfer)
? current.getAmt() -
options.transfer.reduce((acc, receiver) => {
return (acc += receiver.amt)
}, 0n)
: options.transfer.amt
if (tokenChangeAmt < 0n) {
throw new Error(`Not enough tokens`)
}
Expand All @@ -193,11 +184,7 @@ export class BSV20V1 extends SmartContract {

tx.addInput(current.buildContractInput())

for (let i = 0; i < options.transfer.length; i++) {
const receiver = options.transfer[i]

await receiver.instance.connect(current.signer)

function addReceiver(receiver: FTReceiver) {
if (receiver.instance instanceof BSV20V1) {
receiver.instance.setAmt(receiver.amt)
} else if (receiver.instance instanceof OrdP2PKH) {
Expand All @@ -222,6 +209,14 @@ export class BSV20V1 extends SmartContract {
atOutputIndex: nexts.length,
})
}
if (Array.isArray(options.transfer)) {
for (let i = 0; i < options.transfer.length; i++) {
const receiver = options.transfer[i]
addReceiver(receiver)
}
} else {
addReceiver(options.transfer)
}

if (tokenChangeAmt > 0n && options.skipTokenChange !== true) {
const tokenChangeAddress = options.tokenChangeAddress
Expand Down Expand Up @@ -265,7 +260,7 @@ export class BSV20V1 extends SmartContract {
static async transfer(
senders: Array<OrdP2PKH | BSV20V1>,
signer: Signer,
receivers: Array<TokenReceiver>
receivers: Array<FTReceiver>
) {
const ordPubKey = await signer.getDefaultPubKey()

Expand Down Expand Up @@ -302,8 +297,10 @@ export class BSV20V1 extends SmartContract {

if (receiver.instance instanceof BSV20V1) {
receiver.instance.setAmt(receiver.amt)
} else {
} else if (receiver.instance instanceof OrdP2PKH) {
receiver.instance.setBSV20(tick, receiver.amt)
} else {
throw new Error('unsupport receiver, only BSV20V1 or OrdP2PKH!')
}

tx.addOutput(
Expand Down
104 changes: 3 additions & 101 deletions src/contracts/onesatNFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,13 @@ import {
SmartContract,
toByteString,
Utils,
UTXO,
assert,
prop,
bsv,
MethodCallOptions,
ContractTransaction,
StatefulNext,
} from 'scrypt-ts'
import { Inscription } from '../types'
import { Ordinal } from './ordinal'
import { signTx } from 'scryptlib'
import { OneSatApis } from './1satApis'
import { OneSatApis } from '../1satApis'

export class OneSatNFT extends SmartContract {
@prop(true)
Expand Down Expand Up @@ -71,100 +66,7 @@ export class OneSatNFT extends SmartContract {
})
}

async transfer(receiver: OneSatNFT, methodName: string, ...args) {
const builder = this['_txBuilders'].has(methodName)

if (!builder) {
this.bindTxBuilder(
methodName,
async (
current: OneSatNFT,
options: MethodCallOptions<OneSatNFT>
): Promise<ContractTransaction> => {
const bsvChangeAddress =
await this.signer.getDefaultAddress()

const nexts: StatefulNext<OneSatNFT>[] = []
const tx = new bsv.Transaction()

tx.addInput(current.buildContractInput())

await receiver.connect(this.signer)

tx.addOutput(
new bsv.Transaction.Output({
script: receiver.lockingScript,
satoshis: 1,
})
)

nexts.push({
instance: receiver,
balance: 1,
atOutputIndex: nexts.length,
})

tx.change(bsvChangeAddress)

return Promise.resolve({
tx,
atInputIndex: 0,
nexts: nexts,
})
}
)
}

return this.methods[methodName](...args)
}

static send2Contract(
ordinalUtxo: UTXO,
ordPk: bsv.PrivateKey,
instance: SmartContract
) {
instance.buildDeployTransaction = (
utxos: UTXO[],
amount: number,
changeAddress?: bsv.Address | string
): Promise<bsv.Transaction> => {
const deployTx = new bsv.Transaction()

deployTx.from(ordinalUtxo).addOutput(
new bsv.Transaction.Output({
script: instance.lockingScript,
satoshis: amount,
})
)

if (changeAddress) {
deployTx.change(changeAddress)
}
const lockingScript = bsv.Script.fromHex(ordinalUtxo.script)

const sig = signTx(
deployTx,
ordPk,
lockingScript,
amount,
0,
bsv.crypto.Signature.ANYONECANPAY_SINGLE
)

deployTx.inputs[0].setScript(
bsv.Script.buildPublicKeyHashIn(
ordPk.publicKey,
bsv.crypto.Signature.fromTxFormat(Buffer.from(sig, 'hex')),
bsv.crypto.Signature.ANYONECANPAY_SINGLE
)
)

return Promise.resolve(deployTx)
}
return instance.deploy(1)
}

public static async getLatestInstanceByOrigin<T extends SmartContract>(
public static async getLatestInstanceByOrigin<T extends OneSatNFT>(
clazz: new (...args: any) => T,
origin: string
): Promise<T> {
Expand All @@ -176,7 +78,7 @@ export class OneSatNFT extends SmartContract {

const insciptionScript = Ordinal.getInsciptionScript(utxo.script)

const instance = (clazz as unknown as typeof SmartContract).fromUTXO(
const instance = (clazz as unknown as typeof OneSatNFT).fromUTXO(
utxo,
{},
bsv.Script.fromHex(insciptionScript)
Expand Down
14 changes: 11 additions & 3 deletions src/contracts/ordP2PKH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
} from 'scrypt-ts'
import { Inscription } from '../types'
import { Ordinal } from './ordinal'
import { OneSatApis } from './1satApis'
import { OneSatApis } from '../1satApis'

export class OrdP2PKH extends SmartContract {
// Address of the recipient.
Expand Down Expand Up @@ -77,8 +77,16 @@ export class OrdP2PKH extends SmartContract {

static fromAddress(address: string | bsv.Address | bsv.PublicKey) {
OrdP2PKH.loadArtifact(desc)
const s = bsv.Script.buildPublicKeyHashOut(address)
return new OrdP2PKH(Addr(toHex(s.chunks[2].buf)))
let addr: Addr

if (typeof address === 'string') {
addr = Addr(bsv.Address.fromString(address).toByteString())
} else if (address instanceof bsv.Address) {
addr = Addr(address.toByteString())
} else {
addr = Addr(bsv.Address.fromPublicKey(address).toByteString())
}
return new OrdP2PKH(addr)
}

static fromP2PKHUTXO(utxo: UTXO): OrdP2PKH {
Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MethodCallOptions, SmartContract, bsv } from 'scrypt-ts'

/** Ordinal Inscription */
export type Inscription = {
/** content in text */
Expand All @@ -11,3 +13,14 @@ export type BSV20Protocol = {
tick: string
amt: string
}

export type FTReceiver = {
instance: SmartContract
amt: bigint
}

export interface FTMethodCallOptions<T> extends MethodCallOptions<T> {
transfer: Array<FTReceiver> | FTReceiver
tokenChangeAddress?: bsv.Address
skipTokenChange?: boolean
}
4 changes: 2 additions & 2 deletions tests/specs/fromTxHashPuzzle.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { HashPuzzle } from '../contracts/hashPuzzle'
import { getDefaultSigner } from '../utils/txHelper'

import chaiAsPromised from 'chai-as-promised'
import { OrdMethodCallOptions, OrdP2PKH, Ordinal } from '../scrypt-ord'
import { FTMethodCallOptions, OrdP2PKH, Ordinal } from '../scrypt-ord'
use(chaiAsPromised)

describe('Test BSV20 fromUTXO', () => {
Expand Down Expand Up @@ -51,7 +51,7 @@ describe('Test BSV20 fromUTXO', () => {
amt: 6n,
},
],
} as OrdMethodCallOptions<HashPuzzle>)
} as FTMethodCallOptions<HashPuzzle>)

console.log('withdraw bsv20 to p2pkh: ', tx.id)
}
Expand Down
Loading

0 comments on commit 182478c

Please sign in to comment.