-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cobuild: handle fee payment and set change output
I call payFee before filling the witness in the previous version. The fee estimation is smaller because the tx size is smaller than the final one after adding all the witnesses. I set feeRate to 3000 to work around this. In this commit, I refactor code to make it possible to create a dummy tx for fee estimation. It's guaranteed that the dummy tx is not less than the final tx.
- Loading branch information
Showing
13 changed files
with
264 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { bytes } from "@ckb-lumos/codec"; | ||
|
||
import payFeeWithBuildingPacket from "../lumos-adapter/pay-fee-with-building-packet"; | ||
import * as generalLockActions from "./general-lock-actions"; | ||
import { finalizeWitnesses } from "./lock-actions"; | ||
import { groupByLock } from "./script-group"; | ||
|
||
// feePayments: [{address, fee?, feeRate?}] | ||
export async function payFee(buildingPacket, feePayments, config) { | ||
const groups = groupByLock(buildingPacket.value.resolvedInputs.outputs); | ||
|
||
// Rember fields that should be restored | ||
const witnesses = buildingPacket.value.payload.witnesses; | ||
|
||
const buildingPacketWillPayFee = finalizeWitnesses( | ||
Object.entries(groups).reduce( | ||
(acc, [scriptHash, inputs]) => | ||
storeWitnessForFeeEstimation( | ||
acc, | ||
scriptHash, | ||
inputs.map((e) => e[0]), | ||
config.ckbChainConfig, | ||
), | ||
buildingPacket, | ||
), | ||
); | ||
|
||
const buildingPacketHavePaidFee = await payFeeWithBuildingPacket( | ||
buildingPacketWillPayFee, | ||
feePayments, | ||
config, | ||
); | ||
|
||
return { | ||
type: buildingPacketHavePaidFee.type, | ||
value: { | ||
...buildingPacketHavePaidFee.value, | ||
payload: { | ||
...buildingPacketHavePaidFee.value.payload, | ||
witnesses, | ||
}, | ||
}, | ||
}; | ||
} | ||
|
||
function storeWitnessForFeeEstimation( | ||
buildingPacket, | ||
scriptHash, | ||
inputIndices, | ||
ckbChainConfig, | ||
) { | ||
const script = | ||
buildingPacket.value.resolvedInputs.outputs[inputIndices[0]].lock; | ||
if (script.codeHash === ckbChainConfig.SCRIPTS.JOYID_COBUILD_POC.CODE_HASH) { | ||
return generalLockActions.storeWitnessForFeeEstimation( | ||
buildingPacket, | ||
scriptHash, | ||
inputIndices, | ||
// Variable length, but 500 is usually enough. | ||
() => bytes.hexify(new Uint8Array(500)), | ||
); | ||
} | ||
|
||
throw new Error( | ||
`NotSupportedLock: codeHash=${script.codeHash} hashType=${script.hashType}`, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { utils as lumosBaseUtils } from "@ckb-lumos/base"; | ||
const { computeScriptHash } = lumosBaseUtils; | ||
|
||
export function groupByLock(cellOutputs) { | ||
return Object.groupBy(cellOutputs.entries(), ([_i, v]) => | ||
computeScriptHash(v.lock), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.