Skip to content

Commit

Permalink
Switch to Albatross WASM client
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Nov 15, 2024
1 parent adb43fa commit cc01a5a
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"swap"
],
"dependencies": {
"@nimiq/core-web": "^1.6.3",
"@nimiq/core": "next",
"@nimiq/electrum-client": "https://github.com/nimiq/electrum-client#build",
"@nimiq/oasis-api": "^1.0.1",
"ethers": "^5.7.2",
Expand Down
36 changes: 14 additions & 22 deletions src/NimiqAssetAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@ import { AssetAdapter, SwapAsset } from './IAssetAdapter';

shimPromiseFinally();

type RawTransactionDetails = import('@nimiq/core-web').Client.TransactionDetails;
export type TransactionDetails = ReturnType<import('@nimiq/core-web').Client.TransactionDetails['toPlain']>;
export type ConsensusState = import('@nimiq/core-web').Client.ConsensusState;
export type TransactionDetails = import('@nimiq/core').PlainTransactionDetails;
export type ConsensusState = import('@nimiq/core').ConsensusState;

export interface NimiqClient {
addTransactionListener(
listener: (tx: TransactionDetails | RawTransactionDetails) => any,
addresses: string[],
): number | Promise<number>;
addTransactionListener(listener: (tx: TransactionDetails) => any, addresses: string[]): number | Promise<number>;
getTransactionsByAddress(
address: string,
sinceBlockHeight?: number,
knownTransactions?: TransactionDetails[] | RawTransactionDetails[],
): Promise<TransactionDetails[] | RawTransactionDetails[]>;
removeListener(handle: number): void | Promise<void>;
sendTransaction(
tx: TransactionDetails | RawTransactionDetails | string,
): Promise<TransactionDetails | RawTransactionDetails>;
addConsensusChangedListener(listener: (consensusState: ConsensusState) => any): number | Promise<number>;
knownTransactions?: TransactionDetails[],
): Promise<TransactionDetails[]>;
removeListener(handle: number): Promise<void>;
sendTransaction(tx: TransactionDetails | string): Promise<TransactionDetails>;
addConsensusChangedListener(listener: (consensusState: ConsensusState) => any): Promise<number>;
}

export class NimiqAssetAdapter implements AssetAdapter<SwapAsset.NIM> {
Expand All @@ -36,8 +30,7 @@ export class NimiqAssetAdapter implements AssetAdapter<SwapAsset.NIM> {
): Promise<TransactionDetails> {
// eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve, reject) => {
const listener = (tx: TransactionDetails | RawTransactionDetails) => {
if ('toPlain' in tx) tx = tx.toPlain();
const listener = (tx: TransactionDetails) => {
if (!test(tx)) return false;

cleanup();
Expand All @@ -49,7 +42,7 @@ export class NimiqAssetAdapter implements AssetAdapter<SwapAsset.NIM> {
const transactionListener = await this.client.addTransactionListener(listener, [address]);

// Setup a transaction history check function
let history: TransactionDetails[] | RawTransactionDetails[] = [];
let history: TransactionDetails[] = [];
const checkHistory = async () => {
history = await this.client.getTransactionsByAddress(address, 0, history);
for (const tx of history) {
Expand All @@ -62,7 +55,7 @@ export class NimiqAssetAdapter implements AssetAdapter<SwapAsset.NIM> {

// Re-check transaction history when consensus is re-established
const consensusListener = await this.client.addConsensusChangedListener(
(consensusState: ConsensusState) => consensusState === 'established' && checkHistory(),
(consensusState) => consensusState === 'established' && checkHistory(),
);

// Also re-check transaction history every minute to catch cases where subscription fails
Expand Down Expand Up @@ -99,7 +92,7 @@ export class NimiqAssetAdapter implements AssetAdapter<SwapAsset.NIM> {
if (typeof tx.data.raw !== 'string' || tx.data.raw !== data) return false;

// Must wait until mined
if (tx.state === 'mined' || tx.state === 'confirmed') {
if (tx.state === 'included' || tx.state === 'confirmed') {
if (tx.confirmations! >= confirmations) return true;
}

Expand All @@ -125,7 +118,7 @@ export class NimiqAssetAdapter implements AssetAdapter<SwapAsset.NIM> {
);
await this.findTransaction(proxyTx.recipient, (tx) =>
tx.transactionHash === proxyTx.transactionHash
&& (tx.state === 'mined' || tx.state === 'confirmed')).finally(() =>
&& (tx.state === 'included' || tx.state === 'confirmed')).finally(() =>
window.clearInterval(resendInterval)
);
}
Expand Down Expand Up @@ -185,7 +178,7 @@ export class NimiqAssetAdapter implements AssetAdapter<SwapAsset.NIM> {
return this.findTransaction(address, (tx) => {
if (tx.sender !== address) return false;
if (typeof (tx.proof as any as { preImage: unknown }).preImage !== 'string') return false;
if (tx.state === 'mined' || tx.state === 'confirmed') return true;
if (tx.state === 'included' || tx.state === 'confirmed') return true;
if (typeof onUpdate === 'function') onUpdate(tx);
return false;
});
Expand All @@ -199,7 +192,6 @@ export class NimiqAssetAdapter implements AssetAdapter<SwapAsset.NIM> {
private async sendTransaction(serializedTx: string, throwOnFailure = true): Promise<TransactionDetails> {
if (this.stopped) throw new Error('NimiqAssetAdapter called while stopped');
let tx = await this.client.sendTransaction(serializedTx);
if ('toPlain' in tx) tx = tx.toPlain();
if (throwOnFailure && tx.state === 'new') throw new Error('Failed to send transaction');
return tx;
}
Expand Down
146 changes: 142 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -384,10 +384,13 @@
"@ethersproject/properties" "^5.7.0"
"@ethersproject/strings" "^5.7.0"

"@nimiq/core-web@^1.6.3":
version "1.6.3"
resolved "https://registry.yarnpkg.com/@nimiq/core-web/-/core-web-1.6.3.tgz#b4a8f8c5d289850b20cb4009766af5b1cafd6e20"
integrity sha512-D6RrJi2cRU81odNpmwczhUBvOQ47+/Db1svrTkH/G4xNd72lr9MS5nMdfpUz+rBRnSprljrzW2mdUtZ6W9bPaA==
"@nimiq/core@next":
version "2.0.0-next.rc.4"
resolved "https://registry.yarnpkg.com/@nimiq/core/-/core-2.0.0-next.rc.4.tgz#a353f4507f2f10e3be741f920cff921c9474dea7"
integrity sha512-qSBy/Mg0rbqAstEnrgzGhqeQpmQdOjCZPWmBV5uFxAMAAYl9nrABaXWQXo3Jvemvx5VQE9pz5/ma3I8Z0ROy1g==
dependencies:
comlink "^4.4.1"
websocket "^1.0.34"

"@nimiq/electrum-client@https://github.com/nimiq/electrum-client#build":
version "0.2.1"
Expand Down Expand Up @@ -556,6 +559,13 @@ bs58check@<3.0.0, bs58check@^2.0.0, bs58check@^2.1.1:
create-hash "^1.1.0"
safe-buffer "^5.1.2"

bufferutil@^4.0.1:
version "4.0.8"
resolved "https://registry.yarnpkg.com/bufferutil/-/bufferutil-4.0.8.tgz#1de6a71092d65d7766c4d8a522b261a6e787e8ea"
integrity sha512-4T53u4PdgsXqKaIctwF8ifXlRTTmEPJ8iEPWFdGZvcf7sbwYo6FKFEX9eNNAnzFZ7EzJAQ3CJeOtCRA4rDp7Pw==
dependencies:
node-gyp-build "^4.3.0"

call-bind@^1.0.0, call-bind@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/call-bind/-/call-bind-1.0.2.tgz#b1d4e89e688119c3c9a903ad30abb2f6a919be3c"
Expand All @@ -572,6 +582,11 @@ cipher-base@^1.0.1, cipher-base@^1.0.3:
inherits "^2.0.1"
safe-buffer "^5.0.1"

comlink@^4.4.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/comlink/-/comlink-4.4.1.tgz#e568b8e86410b809e8600eb2cf40c189371ef981"
integrity sha512-+1dlx0aY5Jo1vHy/tSsIGpSkN4tS9rZSW8FIhG0JH/crs9wwweswIo/POr451r7bZww3hFbPAKnTpimzL/mm4Q==

create-hash@^1.1.0, create-hash@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/create-hash/-/create-hash-1.2.0.tgz#889078af11a63756bcfb59bd221996be3a9ef196"
Expand All @@ -595,6 +610,21 @@ create-hmac@^1.1.3, create-hmac@^1.1.7:
safe-buffer "^5.0.1"
sha.js "^2.4.8"

d@1, d@^1.0.1, d@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/d/-/d-1.0.2.tgz#2aefd554b81981e7dccf72d6842ae725cb17e5de"
integrity sha512-MOqHvMWF9/9MX6nza0KgvFH4HpMU0EF5uUDXqX/BtxtU8NfB0QzRtJ8Oe/6SuS4kbhyzVJwjd97EA4PKrzJ8bw==
dependencies:
es5-ext "^0.10.64"
type "^2.7.2"

debug@^2.2.0:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
integrity sha512-bC7ElrdJaJnPbAP+1EotYvqZsb3ecl5wi6Bfi6BJTUcNowp6cvspg0jXznRTKDjm/E7AdgFBVeAPVMNcKGsHMA==
dependencies:
ms "2.0.0"

define-properties@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/define-properties/-/define-properties-1.1.3.tgz#cf88da6cbee26fe6db7094f61d870cbd84cee9f1"
Expand Down Expand Up @@ -664,6 +694,43 @@ es-to-primitive@^1.2.1:
is-date-object "^1.0.1"
is-symbol "^1.0.2"

es5-ext@^0.10.35, es5-ext@^0.10.50, es5-ext@^0.10.62, es5-ext@^0.10.64, es5-ext@~0.10.14:
version "0.10.64"
resolved "https://registry.yarnpkg.com/es5-ext/-/es5-ext-0.10.64.tgz#12e4ffb48f1ba2ea777f1fcdd1918ef73ea21714"
integrity sha512-p2snDhiLaXe6dahss1LddxqEm+SkuDvV8dnIQG0MWjyHpcMNfXKPE+/Cc0y+PhxJX3A4xGNeFCj5oc0BUh6deg==
dependencies:
es6-iterator "^2.0.3"
es6-symbol "^3.1.3"
esniff "^2.0.1"
next-tick "^1.1.0"

es6-iterator@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/es6-iterator/-/es6-iterator-2.0.3.tgz#a7de889141a05a94b0854403b2d0a0fbfa98f3b7"
integrity sha512-zw4SRzoUkd+cl+ZoE15A9o1oQd920Bb0iOJMQkQhl3jNc03YqVjAhG7scf9C5KWRU/R13Orf588uCC6525o02g==
dependencies:
d "1"
es5-ext "^0.10.35"
es6-symbol "^3.1.1"

es6-symbol@^3.1.1, es6-symbol@^3.1.3:
version "3.1.4"
resolved "https://registry.yarnpkg.com/es6-symbol/-/es6-symbol-3.1.4.tgz#f4e7d28013770b4208ecbf3e0bf14d3bcb557b8c"
integrity sha512-U9bFFjX8tFiATgtkJ1zg25+KviIXpgRvRHS8sau3GfhVzThRQrOeksPeT0BWW2MNZs1OEWJ1DPXOQMn0KKRkvg==
dependencies:
d "^1.0.2"
ext "^1.7.0"

esniff@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/esniff/-/esniff-2.0.1.tgz#a4d4b43a5c71c7ec51c51098c1d8a29081f9b308"
integrity sha512-kTUIGKQ/mDPFoJ0oVfcmyJn4iBDRptjNVIzwIFR7tqWXdVI9xfA2RMwY/gbSpJG3lkdWNEjLap/NqVHZiJsdfg==
dependencies:
d "^1.0.1"
es5-ext "^0.10.62"
event-emitter "^0.3.5"
type "^2.7.2"

ethers@^5.7.2:
version "5.7.2"
resolved "https://registry.yarnpkg.com/ethers/-/ethers-5.7.2.tgz#3a7deeabbb8c030d4126b24f84e525466145872e"
Expand Down Expand Up @@ -700,6 +767,21 @@ ethers@^5.7.2:
"@ethersproject/web" "5.7.1"
"@ethersproject/wordlists" "5.7.0"

event-emitter@^0.3.5:
version "0.3.5"
resolved "https://registry.yarnpkg.com/event-emitter/-/event-emitter-0.3.5.tgz#df8c69eef1647923c7157b9ce83840610b02cc39"
integrity sha512-D9rRn9y7kLPnJ+hMq7S/nhvoKwwvVJahBi2BPmx3bvbsEdK3W9ii8cBSGjP+72/LnM4n6fo3+dkCX5FeTQruXA==
dependencies:
d "1"
es5-ext "~0.10.14"

ext@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/ext/-/ext-1.7.0.tgz#0ea4383c0103d60e70be99e9a7f11027a33c4f5f"
integrity sha512-6hxeJYaL110a9b5TEJSj0gojyHQAmA2ch5Os+ySCiA1QGdS697XWY1pzsrSjqA9LDEEgdB/KypIlR59RcLuHYw==
dependencies:
type "^2.7.2"

[email protected]:
version "1.0.0"
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
Expand Down Expand Up @@ -857,6 +939,11 @@ is-symbol@^1.0.2, is-symbol@^1.0.3:
dependencies:
has-symbols "^1.0.2"

is-typedarray@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/is-typedarray/-/is-typedarray-1.0.0.tgz#e479c80858df0c1b11ddda6940f96011fcda4a9a"
integrity sha512-cyA56iCMHAh5CdzjJIa4aohJyeO1YbwLi3Jc35MmRU6poroFjIGZzUzupGiRPOjgHg9TLu43xbpwXk523fMxKA==

is-weakref@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/is-weakref/-/is-weakref-1.0.1.tgz#842dba4ec17fa9ac9850df2d6efbc1737274f2a2"
Expand Down Expand Up @@ -893,11 +980,26 @@ minimalistic-crypto-utils@^1.0.1:
resolved "https://registry.yarnpkg.com/minimalistic-crypto-utils/-/minimalistic-crypto-utils-1.0.1.tgz#f6c00c1c0b082246e5c4d99dfb8c7c083b2b582a"
integrity sha1-9sAMHAsIIkblxNmd+4x8CDsrWCo=

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
integrity sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==

nan@^2.13.2:
version "2.15.0"
resolved "https://registry.yarnpkg.com/nan/-/nan-2.15.0.tgz#3f34a473ff18e15c1b5626b62903b5ad6e665fee"
integrity sha512-8ZtvEnA2c5aYCZYd1cvgdnU6cqwixRoYg70xPLWUws5ORTa/lnw+u4amixRS/Ac5U5mQVgp9pnlSUnbNWFaWZQ==

next-tick@^1.1.0:
version "1.1.0"
resolved "https://registry.yarnpkg.com/next-tick/-/next-tick-1.1.0.tgz#1836ee30ad56d67ef281b22bd199f709449b35eb"
integrity sha512-CXdUiJembsNjuToQvxayPZF9Vqht7hewsvy2sOWafLvi2awflj9mOC6bHIg50orX8IJvWKY9wYQ/zB2kogPslQ==

node-gyp-build@^4.3.0:
version "4.8.0"
resolved "https://registry.yarnpkg.com/node-gyp-build/-/node-gyp-build-4.8.0.tgz#3fee9c1731df4581a3f9ead74664369ff00d26dd"
integrity sha512-u6fs2AEUljNho3EYTJNBfImO5QTo/J/1Etd+NVdCj7qWKUSN/bSLkZwhDv7I+w/MSC6qJ4cknepkAYykDdK8og==

object-inspect@^1.11.0, object-inspect@^1.9.0:
version "1.11.0"
resolved "https://registry.yarnpkg.com/object-inspect/-/object-inspect-1.11.0.tgz#9dceb146cedd4148a0d9e51ab88d34cf509922b1"
Expand Down Expand Up @@ -1033,6 +1135,18 @@ tiny-secp256k1@^1.1.1, tiny-secp256k1@^1.1.3:
elliptic "^6.4.0"
nan "^2.13.2"

type@^2.7.2:
version "2.7.2"
resolved "https://registry.yarnpkg.com/type/-/type-2.7.2.tgz#2376a15a3a28b1efa0f5350dcf72d24df6ef98d0"
integrity sha512-dzlvlNlt6AXU7EBSfpAscydQ7gXB+pPGsPnfJnZpiNJBDj7IaJzQlBZYGdEi4R9HmPdBv2XmWJ6YUtoTa7lmCw==

typedarray-to-buffer@^3.1.5:
version "3.1.5"
resolved "https://registry.yarnpkg.com/typedarray-to-buffer/-/typedarray-to-buffer-3.1.5.tgz#a97ee7a9ff42691b9f783ff1bc5112fe3fca9080"
integrity sha512-zdu8XMNEDepKKR+XYOXAVPtWui0ly0NtohUscw+UmaHiAWT8hrV1rr//H6V+0DvJ3OQ19S979M0laLfX8rm82Q==
dependencies:
is-typedarray "^1.0.0"

typeforce@^1.11.3, typeforce@^1.11.5:
version "1.18.0"
resolved "https://registry.yarnpkg.com/typeforce/-/typeforce-1.18.0.tgz#d7416a2c5845e085034d70fcc5b6cc4a90edbfdc"
Expand All @@ -1053,6 +1167,13 @@ unbox-primitive@^1.0.1:
has-symbols "^1.0.2"
which-boxed-primitive "^1.0.2"

utf-8-validate@^5.0.2:
version "5.0.10"
resolved "https://registry.yarnpkg.com/utf-8-validate/-/utf-8-validate-5.0.10.tgz#d7d10ea39318171ca982718b6b96a8d2442571a2"
integrity sha512-Z6czzLq4u8fPOyx7TU6X3dvUZVvoJmxSQ+IcrlmagKhilxlhZgxPK6C5Jqbkw1IDUmFTM+cz9QDnnLTwDz/2gQ==
dependencies:
node-gyp-build "^4.3.0"

util-deprecate@^1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
Expand All @@ -1065,6 +1186,18 @@ varuint-bitcoin@^1.0.4:
dependencies:
safe-buffer "^5.1.1"

websocket@^1.0.34:
version "1.0.34"
resolved "https://registry.yarnpkg.com/websocket/-/websocket-1.0.34.tgz#2bdc2602c08bf2c82253b730655c0ef7dcab3111"
integrity sha512-PRDso2sGwF6kM75QykIesBijKSVceR6jL2G8NGYyq2XrItNC2P5/qL5XeR056GhA+Ly7JMFvJb9I312mJfmqnQ==
dependencies:
bufferutil "^4.0.1"
debug "^2.2.0"
es5-ext "^0.10.50"
typedarray-to-buffer "^3.1.5"
utf-8-validate "^5.0.2"
yaeti "^0.0.6"

which-boxed-primitive@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/which-boxed-primitive/-/which-boxed-primitive-1.0.2.tgz#13757bc89b209b049fe5d86430e21cf40a89a8e6"
Expand All @@ -1087,3 +1220,8 @@ [email protected]:
version "7.4.6"
resolved "https://registry.yarnpkg.com/ws/-/ws-7.4.6.tgz#5654ca8ecdeee47c33a9a4bf6d28e2be2980377c"
integrity sha512-YmhHDO4MzaDLB+M9ym/mDA5z0naX8j7SIlT8f8z+I0VtzsRbekxEutHSme7NPS2qE8StCYQNUnfWdXta/Yu85A==

yaeti@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/yaeti/-/yaeti-0.0.6.tgz#f26f484d72684cf42bedfb76970aa1608fbf9577"
integrity sha512-MvQa//+KcZCUkBTIC9blM+CU9J2GzuTytsOUwf2lidtvkx/6gnEp1QvJv34t9vdjhFmha/mUiNDbN0D0mJWdug==

0 comments on commit cc01a5a

Please sign in to comment.