Skip to content

Commit

Permalink
Fix fee to high
Browse files Browse the repository at this point in the history
  • Loading branch information
zhfnjust committed Dec 12, 2023
1 parent 08567f7 commit cdc6b80
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 13 deletions.
15 changes: 11 additions & 4 deletions src/contracts/bsv20V1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ export abstract class BSV20V1 extends SmartContract {
tick: ByteString,
amt: bigint
): ByteString {
return BSV20V1.createTransferInsciption(tick, amt) +
return (
BSV20V1.createTransferInsciption(tick, amt) +
Utils.buildPublicKeyHashScript(address)
)
}

@method()
Expand Down Expand Up @@ -136,6 +138,8 @@ export abstract class BSV20V1 extends SmartContract {
throw new Error(`no utxo found for address: ${address}`)
}

const feePerKb = (await this.provider?.getFeePerKb()) as number

const deployTx = new bsv.Transaction()
.from(utxos)
.addOutput(
Expand All @@ -151,6 +155,7 @@ export abstract class BSV20V1 extends SmartContract {
satoshis: 1,
})
)
.feePerKb(feePerKb)
.change(address)

return this.signer.signAndsendTransaction(deployTx)
Expand Down Expand Up @@ -195,9 +200,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 Expand Up @@ -270,6 +275,8 @@ export abstract class BSV20V1 extends SmartContract {
})
}

const feePerKb = await current.provider?.getFeePerKb()
tx.feePerKb(feePerKb as number)
tx.change(changeAddress)

if (options.sequence !== undefined) {
Expand Down
2 changes: 2 additions & 0 deletions src/contracts/bsv20V1P2PKH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ export class BSV20V1P2PKH extends BSV20V1 {

const bsvAddress = await feeSigner.getDefaultAddress()

const feePerKb = await feeSigner.provider?.getFeePerKb()
tx.feePerKb(feePerKb as number)
tx.change(bsvAddress)

for (let i = 0; i < senders.length; i++) {
Expand Down
12 changes: 8 additions & 4 deletions src/contracts/bsv20V2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ export abstract class BSV20V2 extends SmartContract {
id: ByteString,
amt: bigint
): ByteString {
return BSV20V2.createTransferInsciption(id, amt) +
return (
BSV20V2.createTransferInsciption(id, amt) +
Utils.buildPublicKeyHashScript(address)
)
}

@method()
Expand Down Expand Up @@ -186,9 +188,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 Expand Up @@ -261,6 +263,8 @@ export abstract class BSV20V2 extends SmartContract {
})
}

const feePerKb = await current.provider?.getFeePerKb()
tx.feePerKb(feePerKb as number)
tx.change(changeAddress)

if (options.sequence !== undefined) {
Expand Down
3 changes: 2 additions & 1 deletion src/contracts/bsv20V2P2PKH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ export class BSV20V2P2PKH extends BSV20V2 {
}

const bsvAddress = await feeSigner.getDefaultAddress()

const feePerKb = await feeSigner.provider?.getFeePerKb()
tx.feePerKb(feePerKb as number)
tx.change(bsvAddress)

for (let i = 0; i < senders.length; i++) {
Expand Down
2 changes: 2 additions & 0 deletions src/contracts/ordinalNFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ export abstract class OrdinalNFT extends SmartContract {
throw new Error('No NFTReceiver found!')
}

const feePerKb = await current.provider?.getFeePerKb()
tx.feePerKb(feePerKb as number)
tx.change(changeAddress)

if (options.sequence !== undefined) {
Expand Down
3 changes: 3 additions & 0 deletions tests/contracts/counterNFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ export class CounterNFT extends OrdinalNFT {
const nextInstance = current.next()
nextInstance.incCounter()

const feePerKb = await current.provider?.getFeePerKb()

const tx = new bsv.Transaction()
.addInput(current.buildContractInput())
.addOutput(
Expand All @@ -95,6 +97,7 @@ export class CounterNFT extends OrdinalNFT {
satoshis: current.balance,
})
)
.feePerKb(feePerKb as number)
.change(options.changeAddress || defaultAddress)

return {
Expand Down
6 changes: 6 additions & 0 deletions tests/contracts/ordinalLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ export class OrdinalLock extends OrdinalNFT {
receiver: Addr
): Promise<ContractTransaction> {
const defaultAddress = await current.signer.getDefaultAddress()
const feePerKb = await current.provider?.getFeePerKb()

const tx = new bsv.Transaction()
.addInput(current.buildContractInput())
.addOutput(
Expand All @@ -74,6 +76,7 @@ export class OrdinalLock extends OrdinalNFT {
satoshis: Number(current.amount),
})
)
.feePerKb(feePerKb as number)
.change(options.changeAddress || defaultAddress)
return {
tx,
Expand All @@ -87,6 +90,8 @@ export class OrdinalLock extends OrdinalNFT {
options: OrdiMethodCallOptions<OrdinalLock>
): Promise<ContractTransaction> {
const defaultAddress = await current.signer.getDefaultAddress()
const feePerKb = await current.provider?.getFeePerKb()

const tx = new bsv.Transaction()
.addInput(current.buildContractInput())
.addOutput(
Expand All @@ -97,6 +102,7 @@ export class OrdinalLock extends OrdinalNFT {
satoshis: 1,
})
)
.feePerKb(feePerKb as number)
.change(options.changeAddress || defaultAddress)
return {
tx,
Expand Down
5 changes: 4 additions & 1 deletion tests/contracts/permissionedFT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ export class PermissionedFT extends BSV20V1 {
atOutputIndex: 1,
})
}
const feePerKb = await current.provider?.getFeePerKb()

tx.change(options.changeAddress || defaultAddress)
tx.feePerKb(feePerKb as number).change(
options.changeAddress || defaultAddress
)
return { tx, atInputIndex: 0, nexts }
}
}
5 changes: 4 additions & 1 deletion tests/contracts/permissionedFTV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,11 @@ export class PermissionedFTV2 extends BSV20V2 {
atOutputIndex: 1,
})
}
const feePerKb = await current.provider?.getFeePerKb()

tx.change(options.changeAddress || defaultAddress)
tx.feePerKb(feePerKb as number).change(
options.changeAddress || defaultAddress
)
return { tx, atInputIndex: 0, nexts }
}
}
8 changes: 6 additions & 2 deletions tests/specs/hashLockNFT.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ describe('Test SmartContract `HashLockNFT`', () => {

it('should pass when calling `unlock`, this will burn the NFT', async () => {
const call = async () => await instance.methods.unlock(message)
await expect(call()).not.to.be.rejected
await expect(call()).to.be.rejectedWith(/No NFTReceiver found/)
})

it('should fail when passing incorrect message', async () => {
const call = async () => await instance.methods.unlock(toByteString(''))
const ordAddress = await instance.signer.getDefaultAddress()
const call = async () =>
await instance.methods.unlock(toByteString(''), {
transfer: new OrdiNFTP2PKH(Addr(ordAddress.toByteString())),
})
await expect(call()).to.be.rejectedWith(/hashes are not equal/)
})

Expand Down

0 comments on commit cdc6b80

Please sign in to comment.