Skip to content

Commit

Permalink
test: added test for send functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
theanmolsharma committed Jan 27, 2024
1 parent 5d67fef commit 33af253
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 14 deletions.
27 changes: 13 additions & 14 deletions test/helpers/bitcoin-rpc-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ export class BitcoinRpcClient {
try {
await this.createWallet('default');
} catch (e) {
if (
e instanceof AxiosError &&
e.response?.data.error.message.includes(
'Database already exists.',
)
) {
if ((e as Error).message.includes('Database already exists.')) {
loadWallet = true;
} else {
throw e;
Expand All @@ -39,10 +34,7 @@ export class BitcoinRpcClient {
loadWallet = false;
}
} catch (e) {
if (
e instanceof AxiosError &&
!e.response?.data.error.message.includes('No wallet is loaded.')
) {
if (!(e as Error).message.includes('No wallet is loaded.')) {
throw e;
}
}
Expand All @@ -54,8 +46,7 @@ export class BitcoinRpcClient {
}
} catch (e) {
if (
e instanceof AxiosError &&
!e.response?.data.error.message.includes(
!(e as Error).message.includes(
'Unable to obtain an exclusive lock on the database',
)
) {
Expand All @@ -74,8 +65,6 @@ export class BitcoinRpcClient {
} catch (e) {
if (e instanceof AxiosError) {
if (e.response?.data.error) {
// eslint-disable-next-line no-console
console.log(e.response.data);
throw new Error(e.response.data.error.message);
} else {
throw new Error(e.message);
Expand Down Expand Up @@ -145,4 +134,14 @@ export class BitcoinRpcClient {
},
});
}

async getMempoolEntry(txid: string) {
return await this.request({
url: this.url,
data: {
method: 'getmempoolentry',
params: [txid],
},
});
}
}
2 changes: 2 additions & 0 deletions test/helpers/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ services:
image: blockstream/esplora:5ec810278c737c78ed54e82749581f1a52ce1b54
environment:
ELECTRS_ARGS: --jsonrpc-import --daemon-rpc-addr bitcoin:18443 --cookie alice:password
volumes:
- ./nginx.conf.in:/srv/explorer/source/contrib/nginx.conf.in
ports:
- "50001:50001"
- "8094:80"
Expand Down
112 changes: 112 additions & 0 deletions test/helpers/nginx.conf.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
geo $limit {
default 1;
34.70.28.228/32 0;
127.0.0.1/32 0;
}

map $limit $limit_key {
0 "";
1 $http_x_forwarded_for;
}

limit_req_status 429;
limit_conn_status 429;

upstream backend {
server unix:/var/electrs-rest.sock;
keepalive 300;
}

upstream prerenderer {
server unix:/var/prerender-http.sock;
keepalive 300;
}

upstream electrum_websocket {
server unix:/var/electrum-websocket.sock;
keepalive 300;
}

server {
listen 80;
keepalive_requests 100000;
gzip on;
gzip_types application/json text/plain application/xml application/javascript;
gzip_proxied any;
gzip_vary on;
root {STATIC_DIR};
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 650s;
{NGINX_LOGGING};
server_tokens off;

# client timeouts
client_body_timeout 5s;
client_header_timeout 5s;

add_header X-Frame-Options SAMEORIGIN always;
add_header Content-Security-Policy "{NGINX_CSP}" always;
add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload" always;
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Content-Type-Options "nosniff" always;
add_header Referrer-Policy "no-referrer" always;

location /{NGINX_PATH}api/ {
proxy_pass http://backend/;
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Expose-Headers 'x-total-results';
proxy_http_version 1.1;
proxy_set_header Connection "";
}
location /{NGINX_PATH}nojs/ {
proxy_pass http://prerenderer/;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
location /{NGINX_PATH}electrum-websocket/ {
client_body_timeout 60s;
proxy_pass http://electrum_websocket/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 1d;
proxy_send_timeout 1d;
}

location = /{NGINX_PATH}nojs {
return 301 " /{NGINX_PATH}nojs/";
}
location ~ ^/{NGINX_PATH}((tx|block|block-height|address|assets?)/|[a-zA-Z0-9]+$) {
expires 60m;
if ($args = "nojs") {
{NGINX_REWRITE_NOJS};
}
try_files $uri /index.html;
}
location /{NGINX_PATH} {
expires 60m;
if ($args = "nojs") {
{NGINX_REWRITE_NOJS};
}
{NGINX_REWRITE}
}
location = /{NGINX_NOSLASH_PATH} {
if ($args = "nojs") {
{NGINX_REWRITE_NOJS};
}
return 301 " /{NGINX_PATH}";
}
error_page 404 /{NGINX_PATH}notfound.html;
location = /notfound.html {
internal;
}
location /{NGINX_PATH}api/fee-estimates {
return 200 '{"1": 1.0, "3": 1.0, "6": 1.0}';
add_header Access-Control-Allow-Origin * always;
add_header Access-Control-Expose-Headers 'x-total-results';
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
13 changes: 13 additions & 0 deletions test/wallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ describe('Wallet', () => {
expect(await wallet.getBalance()).toBe(10000000);
});

it('should send a payment to an address', async () => {
const txid = await wallet.send(
await bitcoinRpcClient.getNewAddress(),
5000000,
);

expect(txid).toBeDefined();

// get the transaction from the node and check it has been broadcasted
const tx = await bitcoinRpcClient.getMempoolEntry(txid);
expect(tx).toBeDefined();
});

afterAll(async () => {
await wallet.close();
fs.rmSync('./test/wallet', { recursive: true, force: true });
Expand Down

0 comments on commit 33af253

Please sign in to comment.