Skip to content

Commit

Permalink
Bitcoin transfers & tbtc dapp fix
Browse files Browse the repository at this point in the history
  • Loading branch information
cavanmflynn committed Oct 1, 2020
1 parent 52f9289 commit 230ae07
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@femessage/log-viewer": "^1.4.2",
"@iarna/toml": "^2.2.5",
"ant-design-vue": "^1.6.5",
"async-retry": "^1.3.1",
"axios": "^0.19.2",
"bitcoin-core": "^3.0.0",
"dayjs": "^1.8.20",
Expand Down
25 changes: 25 additions & 0 deletions src/components/partials/node-drawer/node-drawer.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,31 @@
>
</a-row>
<a-divider class="partial/node-drawer__divider" />
<a-row :gutter="[16, 20]">
<a-col :span="16"
><a-input placeholder="Bitcoin Address" v-model="bitcoinSendTo"
/></a-col>
<a-col :span="8"
><a-input-number
style="float: right"
placeholder="Amount"
:min="0.00000001"
v-model="bitcoinSendAmount"
/></a-col>
</a-row>
<a-row>
<a-col
><a-button
type="primary"
@click="() => sendBitcoin(node)"
:loading="bitcoinSending"
ghost
block
>Send Bitcoin</a-button
></a-col
>
</a-row>
<a-divider class="partial/node-drawer__divider" />
<a-row class="partial/node-drawer__row">
<a-col :span="10">RPC Host</a-col>
<a-col :span="14">
Expand Down
26 changes: 26 additions & 0 deletions src/components/partials/node-drawer/node-drawer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export class NodeDrawer extends Vue {

public bitcoinMining = false;
public bitcoinMineBlockCount = 1;
public bitcoinSending = false;
public bitcoinSendTo = '';
public bitcoinSendAmount: number | null = null;

get titleCase() {
return this.node?.type === 'ecdsa' ? 'uppercase' : 'startcase';
Expand Down Expand Up @@ -101,10 +104,33 @@ export class NodeDrawer extends Vue {
node,
blocks: this.bitcoinMineBlockCount,
});
this.bitcoinMineBlockCount = 1;
} catch (error) {
system.notify({ message: 'Bitcoin block mining failed', error });
} finally {
this.bitcoinMining = false;
}
}

public async sendBitcoin(node: BitcoinNode) {
try {
this.bitcoinSending = true;
if (!this.bitcoinSendAmount) {
this.$message.error('Please enter a send amount');
return;
}
await bitcoind.sendFunds({
node,
toAddress: this.bitcoinSendTo,
amount: this.bitcoinSendAmount,
});
this.bitcoinSendTo = '';
this.bitcoinSendAmount = null;
this.$message.success('Bitcoin sent!');
} catch (error) {
system.notify({ message: 'Failed to send bitcoin', error });
} finally {
this.bitcoinSending = false;
}
}
}
2 changes: 1 addition & 1 deletion src/lib/utils/bitcoind-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class BitcoindService {
async mine(numBlocks: number, node: BitcoinNode) {
const client = this.createClient(node);
const addr = await client.getNewAddress();
return await client.generateToAddress(numBlocks, addr);
return client.generateToAddress(numBlocks, addr);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/localization/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
}
},
"MINE_ERROR": "The number of blocks to mine must be a positive number",
"SEND_ERROR": "The send amount must be a positive number",

"RANDOM_BEACON_NODES": "Beacon Nodes",
"ECDSA_SIGNING_NODES": "Signing Nodes",
Expand Down
20 changes: 19 additions & 1 deletion src/store/modules/bitcoind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class BitcoindModule extends VuexModule {

@Action({ rawError: true })
async mine({ blocks, node }: { blocks: number; node: BitcoinNode }) {
if (blocks < 0)
if (blocks <= 0)
throw new Error(
languageLibrary[system.language || 'en'].get('MINE_ERROR').toString({}),
);
Expand All @@ -67,4 +67,22 @@ export class BitcoindModule extends VuexModule {
.map(this.getInfo),
);
}

@Action({ rawError: true })
async sendFunds({
toAddress,
amount,
node,
}: {
toAddress: string;
amount: number;
node: BitcoinNode;
}) {
if (amount <= 0)
throw new Error(
languageLibrary[system.language || 'en'].get('SEND_ERROR').toString({}),
);

await bitcoindService.sendFunds(node, toAddress, amount);
}
}
7 changes: 5 additions & 2 deletions src/store/modules/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
import { system, bitcoind, ethereum } from '..';
import { info } from 'electron-log';
import { APP_VERSION } from '@/lib/constants';
import retry from 'async-retry';

@Module({ store, name: 'network', dynamic: true, namespaced: true })
export class NetworkModule extends VuexModule {
Expand Down Expand Up @@ -270,8 +271,10 @@ export class NetworkModule extends VuexModule {
.then(async () => {
this.setStatus({ id, status: Status.Started, only: eth.name });
await ethereum.getInfo(eth);
await ethereumService.triggerKeepBeaconGenesis(eth);
await ethereum.mine({ blocks: 5, node: eth }); // tBTC dapp fails unless blocks are mined on startup
await retry(async () =>
ethereumService.triggerKeepBeaconGenesis(eth),
);
await ethereum.mine({ blocks: 10, node: eth }); // tBTC dapp fails unless blocks are mined on startup
})
.catch((error) =>
this.setStatus({
Expand Down
9 changes: 8 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2516,6 +2516,13 @@ async-limiter@~1.0.0:
resolved "https://registry.yarnpkg.com/async-limiter/-/async-limiter-1.0.0.tgz#78faed8c3d074ab81f22b4e985d79e8738f720f8"
integrity sha512-jp/uFnooOiO+L211eZOoSyzpOITMXx1rBITauYykG3BRYPu8h0UcxsPNB04RR5vo4Tyz3+ay17tR6JVf9qzYWg==

async-retry@^1.3.1:
version "1.3.1"
resolved "https://registry.yarnpkg.com/async-retry/-/async-retry-1.3.1.tgz#139f31f8ddce50c0870b0ba558a6079684aaed55"
integrity sha512-aiieFW/7h3hY0Bq5d+ktDBejxuwR78vRu9hDUdR8rNhSaQ29VzPL4AoIRG7D/c7tdenwOcKvgPM6tIxB3cB6HA==
dependencies:
retry "0.12.0"

async-validator@^3.0.3:
version "3.2.3"
resolved "https://registry.yarnpkg.com/async-validator/-/async-validator-3.2.3.tgz#b38b72f9c08c1d28548df13bb260b6908448ca49"
Expand Down Expand Up @@ -10942,7 +10949,7 @@ ret@~0.1.10:
resolved "https://registry.yarnpkg.com/ret/-/ret-0.1.15.tgz#b8a4825d5bdb1fc3f6f53c2bc33f81388681c7bc"
integrity sha512-TTlYpa+OL+vMMNG24xSlQGEJ3B/RzEfUlLct7b5G/ytav+wPrplCpVMFuwzXbkecJrb6IYo1iFb0S9v37754mg==

retry@^0.12.0:
retry@0.12.0, retry@^0.12.0:
version "0.12.0"
resolved "https://registry.yarnpkg.com/retry/-/retry-0.12.0.tgz#1b42a6266a21f07421d1b0b54b7dc167b01c013b"
integrity sha1-G0KmJmoh8HQh0bC1S33BZ7AcATs=
Expand Down

0 comments on commit 230ae07

Please sign in to comment.