Skip to content

Commit

Permalink
Merge pull request #55 from sCrypt-Inc/build-transfer-script
Browse files Browse the repository at this point in the history
Add buildTransferScript. Close #47
  • Loading branch information
zhfnjust authored Dec 6, 2023
2 parents 42d723f + f8a8c0f commit 7eb320a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
20 changes: 14 additions & 6 deletions src/contracts/bsv20V1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,20 @@ export abstract class BSV20V1 extends SmartContract {
tick: ByteString,
amt: bigint
): ByteString {
const transferScript =
BSV20V1.createTransferInsciption(tick, amt) +
Utils.buildPublicKeyHashScript(address)
const transferScript = BSV20V1.buildTransferScript(address, tick, amt)
return Utils.buildOutput(transferScript, 1n)
}

@method()
static buildTransferScript(
address: Addr,
tick: ByteString,
amt: bigint
): ByteString {
return BSV20V1.createTransferInsciption(tick, amt) +
Utils.buildPublicKeyHashScript(address)
}

@method()
static createMintInsciption(tick: ByteString, amt: bigint): ByteString {
const amtByteString = Ordinal.int2Str(amt)
Expand Down Expand Up @@ -187,9 +195,9 @@ export abstract class BSV20V1 extends SmartContract {
| FTReceiver
const tokenChangeAmt = Array.isArray(recipients)
? current.getAmt() -
recipients.reduce((acc, receiver) => {
return (acc += receiver.amt)
}, 0n)
recipients.reduce((acc, receiver) => {
return (acc += receiver.amt)
}, 0n)
: current.getAmt() - recipients.amt
if (tokenChangeAmt < 0n) {
throw new Error(`Not enough tokens`)
Expand Down
20 changes: 14 additions & 6 deletions src/contracts/bsv20V2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,20 @@ export abstract class BSV20V2 extends SmartContract {
id: ByteString,
amt: bigint
): ByteString {
const transferScript =
BSV20V2.createTransferInsciption(id, amt) +
Utils.buildPublicKeyHashScript(address)
const transferScript = BSV20V2.buildTransferScript(address, id, amt)
return Utils.buildOutput(transferScript, 1n)
}

@method()
static buildTransferScript(
address: Addr,
id: ByteString,
amt: bigint
): ByteString {
return BSV20V2.createTransferInsciption(id, amt) +
Utils.buildPublicKeyHashScript(address)
}

@method()
static createTransferInsciption(id: ByteString, amt: bigint): ByteString {
const amtByteString = Ordinal.int2Str(amt)
Expand Down Expand Up @@ -178,9 +186,9 @@ export abstract class BSV20V2 extends SmartContract {
| FTReceiver
const tokenChangeAmt = Array.isArray(recipients)
? current.getAmt() -
recipients.reduce((acc, receiver) => {
return (acc += receiver.amt)
}, 0n)
recipients.reduce((acc, receiver) => {
return (acc += receiver.amt)
}, 0n)
: current.getAmt() - recipients.amt
if (tokenChangeAmt < 0n) {
throw new Error(`Not enough tokens`)
Expand Down

0 comments on commit 7eb320a

Please sign in to comment.