-
Notifications
You must be signed in to change notification settings - Fork 15
Test: Shared sendTransaction tests for both facades #1053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
828150c
ca9c346
e2624ee
6240d5a
9acf2d4
fc3fb68
baf474f
59ab89a
629eed7
89ccc84
2f7804f
9490cee
43059a9
ef3e2b1
a8ad5f7
c1bcc92
3e77f0f
4c027f2
dc321ca
5b978aa
b81a2d0
18bc8e9
fdef446
10e61f1
d200936
a3147fd
4a58909
b556e5c
87ff71d
eef4dad
9619ae2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| */ | ||
|
|
||
| import { HathorWalletServiceWallet } from '../../../src'; | ||
| import { NATIVE_TOKEN_UID } from '../../../src/constants'; | ||
| import config from '../../../src/config'; | ||
| import { WalletTracker } from '../utils/wallet-tracker.util'; | ||
| import type Transaction from '../../../src/models/transaction'; | ||
|
|
@@ -20,16 +21,24 @@ import { | |
| import { GenesisWalletServiceHelper } from '../helpers/genesis-wallet.helper'; | ||
| import { precalculationHelpers } from '../helpers/wallet-precalculation.helper'; | ||
| import type { WalletStopOptions } from '../../../src/new/types'; | ||
| import type { IHistoryTx } from '../../../src/types'; | ||
| import { AuthorityType } from '../../../src/types'; | ||
| import { NETWORK_NAME } from '../configuration/test-constants'; | ||
| import type { FullNodeTxResponse } from '../../../src/wallet/types'; | ||
| import type { | ||
| FuzzyWalletType, | ||
| IWalletTestAdapter, | ||
| WalletCapabilities, | ||
| CreateWalletOptions, | ||
| CreateWalletResult, | ||
| SendTransactionOptions, | ||
| SendTransactionResult, | ||
| CreateTokenOptions, | ||
| CreateTokenResult, | ||
| GetUtxosAdapterOptions, | ||
| GetUtxosResult, | ||
| AdapterOutput, | ||
| SendManyOutputsAdapterOptions, | ||
| AuthorityUtxoResult, | ||
| GetAuthorityUtxosOptions, | ||
| DelegateAuthorityAdapterOptions, | ||
|
|
@@ -216,14 +225,65 @@ export class ServiceWalletTestAdapter implements IWalletTestAdapter { | |
| return fundTx.hash; | ||
| } | ||
|
|
||
| async waitForTx(wallet: FuzzyWalletType, txId: string): Promise<void> { | ||
| async waitForTx( | ||
| wallet: FuzzyWalletType, | ||
| txId: string, | ||
| recvWallet?: FuzzyWalletType | ||
| ): Promise<void> { | ||
| await pollForTx(this.concrete(wallet), txId); | ||
| if (recvWallet) { | ||
| await pollForTx(this.concrete(recvWallet), txId); | ||
| } | ||
| } | ||
|
|
||
| getPrecalculatedWallet(): PrecalculatedWalletData { | ||
| return precalculationHelpers.test!.getPrecalculatedWallet(); | ||
| } | ||
|
|
||
| async sendTransaction( | ||
| wallet: FuzzyWalletType, | ||
| address: string, | ||
| amount: bigint, | ||
| options?: SendTransactionOptions | ||
| ): Promise<SendTransactionResult> { | ||
| const sw = this.concrete(wallet); | ||
| const { recvWallet, ...txOptions } = options ?? {}; | ||
| const result = await sw.sendTransaction(address, amount, { | ||
| pinCode: SERVICE_PIN, | ||
| ...txOptions, | ||
| }); | ||
| if (!result.hash) { | ||
| throw new Error('sendTransaction: transaction had no hash'); | ||
| } | ||
| await this.waitForTx(wallet, result.hash, recvWallet); | ||
| return { hash: result.hash, transaction: result }; | ||
| } | ||
|
|
||
| async getTx(wallet: FuzzyWalletType, txId: string) { | ||
| // HathorWalletServiceWallet.getTx() is not implemented — use fullnode API | ||
| // and map the response to IHistoryTx format (adding token UID to each output/input) | ||
|
Comment on lines
+262
to
+264
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. polemic:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you have the pr that address this guy? I believe it makes sense if you already have the implementation in hand. |
||
| const fullNodeResponse = await this.concrete(wallet).getFullTxById(txId); | ||
| const { tx } = fullNodeResponse; | ||
| const tokenUids = tx.tokens.map(t => t.uid); | ||
| const resolveToken = (tokenData: number) => | ||
| tokenData === 0 ? NATIVE_TOKEN_UID : tokenUids[tokenData - 1]; | ||
| return { | ||
| tx_id: tx.hash, | ||
| version: tx.version, | ||
| timestamp: tx.timestamp, | ||
| inputs: tx.inputs.map(i => ({ ...i, token: resolveToken(i.token_data) })), | ||
| outputs: tx.outputs.map(o => ({ ...o, token: resolveToken(o.token_data) })), | ||
| parents: tx.parents, | ||
| tokens: tx.tokens, | ||
| weight: tx.weight, | ||
| nonce: Number(tx.nonce), | ||
| } as unknown as IHistoryTx; | ||
| } | ||
|
|
||
| async getFullTxById(wallet: FuzzyWalletType, txId: string): Promise<FullNodeTxResponse> { | ||
| return this.concrete(wallet).getFullTxById(txId); | ||
| } | ||
|
|
||
| async createToken( | ||
| wallet: FuzzyWalletType, | ||
| name: string, | ||
|
|
@@ -232,16 +292,46 @@ export class ServiceWalletTestAdapter implements IWalletTestAdapter { | |
| options?: CreateTokenOptions | ||
| ): Promise<CreateTokenResult> { | ||
| const sw = this.concrete(wallet); | ||
| const response = await sw.createNewToken(name, symbol, amount, { | ||
| const result = await sw.createNewToken(name, symbol, amount, { | ||
| ...options, | ||
| pinCode: SERVICE_PIN, | ||
| }); | ||
| if (!response.hash) { | ||
| if (!result?.hash) { | ||
| throw new Error('createToken: transaction had no hash'); | ||
| } | ||
| await pollForTx(sw, response.hash); | ||
| await pollForTokenDetails(sw, response.hash); | ||
| return { hash: response.hash }; | ||
| await pollForTx(sw, result.hash); | ||
| await pollForTokenDetails(sw, result.hash); | ||
| return { hash: result.hash, transaction: result }; | ||
| } | ||
|
|
||
| async getUtxos( | ||
| wallet: FuzzyWalletType, | ||
| options?: GetUtxosAdapterOptions | ||
| ): Promise<GetUtxosResult> { | ||
| const result = await this.concrete(wallet).getUtxos(options); | ||
| return { | ||
| total_amount_available: result.total_amount_available, | ||
| total_utxos_available: result.total_utxos_available, | ||
| utxos: result.utxos, | ||
| }; | ||
| } | ||
|
|
||
| async sendManyOutputsTransaction( | ||
| wallet: FuzzyWalletType, | ||
| outputs: AdapterOutput[], | ||
| options?: SendManyOutputsAdapterOptions | ||
| ): Promise<SendTransactionResult> { | ||
| const sw = this.concrete(wallet); | ||
| const { recvWallet, ...txOptions } = options ?? {}; | ||
| const result = await sw.sendManyOutputsTransaction(outputs, { | ||
| pinCode: SERVICE_PIN, | ||
| ...txOptions, | ||
| }); | ||
| if (!result?.hash) { | ||
| throw new Error('sendManyOutputsTransaction: transaction had no hash'); | ||
| } | ||
| await this.waitForTx(wallet, result.hash, recvWallet); | ||
| return { hash: result.hash, transaction: result }; | ||
| } | ||
|
|
||
| async getAuthorityUtxos( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Usually, we have the "wait for tx received" on both wallets, the sending and receiving.
This is to avoid testing someting related to the wallet on either one and it still has not processed or received the tx.
Maybe we could have an optional
recvWalletand call the wait helpers for it as well.If
recvWalletis not giver we ignore it.What's your opinion on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call. Added a
recvWalletoption toSendTransactionOptions— when provided, the adapter waits for the tx on the receiving wallet too. Backward-compatible since it's optional.Implemented on fdef446.