Skip to content

[BTC] Tx crafting#60

Merged
regnarock merged 21 commits into
feat/btc_balancesfrom
btc_tx_crafting
Aug 15, 2024
Merged

[BTC] Tx crafting#60
regnarock merged 21 commits into
feat/btc_balancesfrom
btc_tx_crafting

Conversation

@regnarock
Copy link
Copy Markdown

@regnarock regnarock commented Jul 30, 2024

This pull request adds support for:

Incoming:

  • Support for useMaxAmount field in the API transaction intent.

h-adamik and others added 11 commits July 20, 2024 12:41
* Align all cosmos packages versions

* Update config: celestia nodeURL & dydx minGasPrice

* Improve logs for all Cosmos node error messages

* Better handle pubKey and other cosmos account data

* Update dYdX node + minor fixes

* Change nonce type from number to bigint + actually include it during cosmos completeTransaction
@vercel
Copy link
Copy Markdown

vercel Bot commented Jul 30, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
adamik ✅ Ready (Inspect) Visit Preview 💬 Add feedback Aug 15, 2024 1:24pm

Comment thread package.json
},
"pnpm": {
"patchedDependencies": {
"coinselect@3.1.13": "patches/coinselect@3.1.13.patch"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have some context about the need for this patch? A link or anything.
Not mandatory just in case, could be useful for future reference.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put those links as context in the description of the PR:

with basic UTXO selection using bitcoinjs coinselect.
⚠️ Known issues:
Because the lib is only published in NPM without its types (bitcoinjs/coinselect#77 (comment)), I've patched the lib using their own definitions.

But your comment makes me think that I should put that context closer to the codebase itself. A recommendation?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose to add the following documentation right next to the patch.

Comment on lines +15 to +17
/**
* add fields for compatibility with coinselect @see {@link UTXO}
*/
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nitpicking)
Single-line comment when possible, for more compact code

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just wanted to have the @see {@link UTXO} picked up as JsDoc. And VS code only parses them when between /** .... */ 🙈

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realised it could be done in one line 🤦

Comment on lines +10 to +16
* This should never happen because the transaction is already validated by zod
* @see {@link transactionDataSchema.refine} at the {@link transactionDataSchema} definition
* TODO: maybe we could make sure that completeTransaction directly receives a multi-recipient transaction ?
*/
if (!isMultiRecipientTransaction(transaction.data)) {
throw new Error(`Something went wrong, the transaction is not a multi-recipient transaction.`);
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be solved after merging MultiRecipientTransaction into the regular TransferTransaction, if I followed correctly 🙂

transaction: TransactionWrapper
): Promise<void> {
throw new Error("Not implemented");
async function completeTransaction(this: BtcService, transaction: TransactionWrapper): Promise<void> {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just noticing useMaxAmount is not handled yet. Is it planned? (not a blocker, Fabrice said many times that this bonus feature would drop if it was any trouble)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just added it! ✔️

mode: TransactionMode.TRANSFER_TO_MANY;
sender: Xpub;
recipients: Recipient[];
inputs?: UtxoWithMetadata[];
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's new though! Would need to figure out if it makes sense in a regular TransferTransaction 🤔

However I don't remember if we discussed it, and sorry if we didn't, but this raises the question: should the client be given the possibility to pick UTXOs?

I am tempted to say 'no', at least for now. The Adamik API should only handle a single amount, both input and output, and manage UTXOs completely internally and behind the scenes from the user.
Even if we do in the future expose the inputs/outputs to the user, this will have to be an additional, bonus feature, on top of the standard single amount. This is important so Bitcoin can still be handled the same as the other chains on client side.

That's just the idea, but do you see any issue proceeding that way?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent points!

  1. I had discussed the introduction of inputs as one of the reasons for a different tx model. Although, those are just for the user's information, they're not use as a input field (paradoxically 🤣) by the API.
    So we could:
  • remove it altogether (and only use an internal model to transit the input data between BTC family Service implementations) => my favourite (but longer to do)
  • keep it as optional for debug purposes on the client side. => my actual recommendation (it's not perfect, but allows for a quick integration of BTC within the API and we could always remove this field later)
  1. Yes we discussed it and decided that the client should not be able to (for now).

=> I'll go for the introduction of an internal model to forward the input data between functions of the BTC service.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😮 I just realised we're already talking about the inner model, the schema doesn't expose the inputs.

So it's only an internal model problem:
-> knowing we already agreed on having a regular TransferTransaction having family dependents optional fields (like gas)
=> I wouldn't see an issue in having an inputs for BTC usage only in here too.


// Enrich inputs from the explorer
await Promise.all(
transaction.data.inputs.map(async (input, inputIndex) => {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm missing something, but aren't you always adding all inputs to the psbt?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right I am. At minimum we always have 2 recipients:

  • the actual recipient of the transaction intent
  • the change recipient

})
);

for (const recipient of transaction.data.recipients) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About recipients, don't you need to add the change output manually? And effectively add a recipient to the input transaction?

If I get it correctly, here you handle every recipient that's in the input transaction, i.e for an actual transfer request to multiple addresses (which by the way doesn't need to be supported, nice-to-have feature if we can have it at no cost)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change output was indeed added manually during the completeTransaction step: https://github.com/AdamikHQ/adamik/pull/60/files#diff-919690c591b9a53acc95310ea442b358e27fb74d4241dc1e1b20423e3d01323cR55

In all the inputs are already setup when we enter encodeTransactionToSign.
I have to handle multiple inputs because it's actually the default case for a BTC tx.

h-adamik and others added 4 commits August 1, 2024 17:01
* Complete Cosmos staking implementation

* Update OpenAPI JSON docs

* Add prevalidation that transaction mode is supported for the chain

* Update OpenAPI JSON docs

* Better handle validation of token balance

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Additionally:
- improved coinselect types and documentation
- moved validation from zod to BTC service prevalidate as handling of
simple transactions is widely different in BTC
- fixed a potential race condition in transaction encoding
@regnarock regnarock changed the title [WIP][BTC] Tx crafting [BTC] Tx crafting Aug 13, 2024
@regnarock
Copy link
Copy Markdown
Author

Should be ready to merge now as soon as 🟢 for you @hakim-adamik 👍

# Conflicts:
#	pnpm-lock.yaml
#	public/openapi.json
#	src/app/api/transaction/broadcast/schema.ts
#	src/app/families/common.ts
#	src/app/model/types_WIP.ts
# Conflicts:
#	package.json
#	pnpm-lock.yaml
#	public/openapi.json
#	src/app/api/transaction/broadcast/schema.ts
#	src/app/families/common.ts
#	src/app/model/types_WIP.ts
# Conflicts:
#	src/app/config/chains.ts
#	src/app/families/bitcoin/backend/explorer.ts
#	src/app/families/bitcoin/backend/types.ts
#	src/app/families/bitcoin/service/getAddressesState.ts
#	src/app/model/types_WIP.ts
@github-actions
Copy link
Copy Markdown

OpenAPI Comparison results: 1 changes: 1 error, 0 warning, 0 info

@regnarock regnarock merged commit e058370 into feat/btc_balances Aug 15, 2024
@regnarock regnarock deleted the btc_tx_crafting branch August 15, 2024 18:21
@regnarock
Copy link
Copy Markdown
Author

Damn! Github misinterpreted a faulty merge on btc_balances and automatically closed this PR and removed the branch 🤦

Reopened here just for the merge back to btc_balances:

@regnarock regnarock mentioned this pull request Aug 15, 2024
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants