CashScript / cashscript

⚖️ Easily write and interact with Bitcoin Cash smart contracts
https://cashscript.org
MIT License
111 stars 77 forks source link

When manualTokenInputs are not empty, selectAllTokenUtxos() should not be executed. #148

Closed wafeishushu closed 1 year ago

wafeishushu commented 1 year ago

version: "cashscript": "0.8.0-next.4"

path: /node_modules/cashscript/dist/Transaction.js

// ...
async setInputsAndOutputs() {
    if (this.outputs.length === 0) {
        throw Error('Attempted to build a transaction without outputs');
    }
    const allUtxos = await this.provider.getUtxos(this.address);
    const manualTokenInputs = this.inputs.filter((input) => input.token);
    // This will throw if the amount is not enough
    if (manualTokenInputs.length > 0) {
        selectAllTokenUtxos(manualTokenInputs, this.outputs);
    }
    const automaticTokenInputs = selectAllTokenUtxos(allUtxos, this.outputs);
    const tokenInputs = manualTokenInputs.length > 0 ? manualTokenInputs : automaticTokenInputs;
    if (this.tokenChange) {
        const tokenChangeOutputs = createFungibleTokenChangeOutputs(tokenInputs, this.outputs, this.address);
        this.outputs.push(...tokenChangeOutputs);
    }

When doing token transfer, I got the following error:

Error: Insufficient funds for token 2369bfbfdeecabb4070f547b33ee5700e03917f70df6f1bd6dec009907be350d: available (0) < needed (1000).
    at selectTokenUtxos ({path_to_node_modules}/node_modules/cashscript/dist/Transaction.js:371:15)

I think the code:

const automaticTokenInputs = selectAllTokenUtxos(allUtxos, this.outputs);
const tokenInputs = manualTokenInputs.length > 0 ? manualTokenInputs : automaticTokenInputs;

should be const tokenInputs = manualTokenInputs.length > 0 ? manualTokenInputs : selectAllTokenUtxos(allUtxos, this.outputs);

rkalis commented 1 year ago

Thanks for the report, this should be fixed in the full release of v0.8.0. Feel free to re-open this issue if it persists.