Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/chat/src/StartChat.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function StartChat({ computer }) {
console.log('creating chat')
let chat
try {
if (await computer.getBalance() < 100) {
if ((await computer.getBalance()).balance < 100) {
await computer.faucet(1e7)
}
chat = await computer.new(ChatSc, [publicKey])
Expand Down
2 changes: 1 addition & 1 deletion packages/chat/src/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function Wallet({ computer, chain }) {

useInterval(() => {
const getBalance = async () => {
if (computer) setBalance(await computer.getBalance())
if (computer) setBalance((await computer.getBalance()).balance)
}
getBalance()
}, 3000)
Expand Down
4 changes: 2 additions & 2 deletions packages/components/built/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ var Balance = function (_a) {
return [4 /*yield*/, computer.getBalance()];
case 5:
availableWalletBalance = _b.sent();
setBalance(availableWalletBalance + amountsInPaymentToken);
setBalance(availableWalletBalance.balance + amountsInPaymentToken);
setChain(computer.getChain());
_b.label = 6;
case 6:
Expand All @@ -111,7 +111,7 @@ var Balance = function (_a) {
_a = setBalance;
return [4 /*yield*/, computer.getBalance()];
case 2:
_a.apply(void 0, [_b.sent()]);
_a.apply(void 0, [(_b.sent()).balance]);
return [2 /*return*/];
}
});
Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Balance = ({
: 0

const availableWalletBalance = await computer.getBalance()
setBalance(availableWalletBalance + amountsInPaymentToken)
setBalance(availableWalletBalance.balance + amountsInPaymentToken)
setChain(computer.getChain())
}
showLoader(false)
Expand All @@ -42,7 +42,7 @@ const Balance = ({

const fund = async () => {
await computer.faucet(1e8)
setBalance(await computer.getBalance())
setBalance((await computer.getBalance()).balance)
}

useEffect(() => {
Expand Down
4 changes: 2 additions & 2 deletions packages/docs/Lib/getBalance.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# getBalance

Returns the current balance in satoshi.
Returns an object with the current balance in Satoshi, the confirmed balance in Satoshi (at least 1 confirmation), and the unconfirmed balance in Satoshi.

### Type
```ts
() => Promise<string>
() => Promise<{balance: number, confirmed: number, unconfirmed: number}>
```

### Syntax
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/Lib/lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ The wallet functionality within a `Computer` instance can be accessed using the
| [send](./send.md) | Sends satoshis to an address |
| [rpcCall](./rpcCall.md) | Access Bitcoin's RPC interface |
| [getAddress](./getAddress.md) | Returns the Bitcoin address of the computer wallet |
| [getBalance](./getBalance.md) | Returns the balance in satoshi |
| [getBalance](./getBalance.md) | Confirmed, unconfirmed and total balance in sats |
| [getChain](./getChain.md) | Returns the blockchain |
| [getNetwork](./getNetwork.md) | Returns the network |
| [getMnemonic](./getMnemonic.md) | Returns a BIP39 mnemonic sentence |
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/node.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ const { txId, vout } = await computer.faucet(1e4)
expect(await new Computer(conf).getUtxos(address)).deep.eq([`${txId}:${vout}`])

// Return the balance
expect(await new Computer(conf).getBalance(address)).eq(1e4)
expect(await new Computer(conf).getBalance(address).balance).eq(1e4)

// Return the transactions
expect(await new Computer(conf).listTxs(address)).deep.eq({
Expand Down
2 changes: 1 addition & 1 deletion packages/ft/src/MintToken.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const MintToken: React.FC<IMintTokenProps> = ({ computer }) => {

const clicked = () => {
setVisible(true)
if (computer.getBalance() === 0) {
if (computer.getBalance().balance === 0) {
alert('Wallet has zero balance, please fund the wallet first.')
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/ft/src/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const Wallet: React.FC<IWalletProps> = ({ computer, chain }) => {

const refresh = async () => {
try {
if (computer) setBalance(await computer.getBalance())
if (computer) setBalance((await computer.getBalance()).balance)
} catch (err) {
console.log(err)
console.log("error occurred while fetching wallet details: ", err)
Expand Down
6 changes: 3 additions & 3 deletions packages/lib/computer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ declare class Wallet {
readonly restClient: RestClient;
constructor(params?: ComputerOptions);
derive(subpath?: string): Wallet;
getBalance(address?: string): Promise<number>;
getBalance(address?: string): Promise<{balance: number, confirmed: number, unconfirmed: number}>;
getUtxos(address?: string): Promise<_Unspent[]>;
getDustThreshold(isWitnessProgram: boolean, script?: Buffer): number;
getAmountThreshold(isWitnessProgram: boolean, script: Buffer): number;
Expand Down Expand Up @@ -109,7 +109,7 @@ declare class RestClient {
constructor({ chain, network, mnemonic, path, passphrase, addressType, url, satPerByte, dustRelayFee }?: ComputerOptions);
rpc(method: string, params: string): Promise<any>;
broadcast(txHex: string): Promise<string>;
getBalance(address: string): Promise<number>;
getBalance(address: string): Promise<{balance: number, confirmed: number, unconfirmed: number}>;
listTxs(address: string): Promise<_Transaction>;
getUtxos(address: string): Promise<_Unspent[]>;
getFormattedUtxos(address: string): Promise<_Unspent[]>;
Expand Down Expand Up @@ -332,7 +332,7 @@ declare class Computer {
load(rev: string): Promise<ModuleExportsNamespace>;
listTxs(address?: string): Promise<import("./types")._Transaction>;
getUtxos(address?: string): Promise<string[]>;
getBalance(address?: string): Promise<number>;
getBalance(address?: string): Promise<{balance: number, confirmed: number, unconfirmed: number}>;
sign(transaction: Transaction, opts?: SigOptions): Promise<void>;
fund(tx: Transaction, opts?: Fee & FundOptions): Promise<void>;
send(satoshis: number, address: string): Promise<string>;
Expand Down
4 changes: 2 additions & 2 deletions packages/lib/dist/bc-lib.browser.min.mjs

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions packages/lib/dist/bc-lib.commonjs.min.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/lib/dist/bc-lib.main.es.mjs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/lib/dist/bc-lib.module.es.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/nft/scripts/deploy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Network \x1b[2m${network}\x1b[0m
Node Url \x1b[2m${url}\x1b[0m
Address \x1b[2m${computer.wallet.address}\x1b[0m
Mnemonic \x1b[2m${mnemonic}\x1b[0m
Balance \x1b[2m${balance / 1e8}\x1b[0m`)
Balance \x1b[2m${balance.balance / 1e8}\x1b[0m`)

const answer = await rl.question('\nDo you want to deploy the contracts? \x1b[2m(y/n)\x1b[0m')
if (answer === 'n') {
Expand Down
4 changes: 0 additions & 4 deletions packages/node/.package.paths

This file was deleted.

3 changes: 2 additions & 1 deletion packages/node/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,8 @@ const { txId, vout } = await computer.faucet(1e4)
expect(await new Computer(conf).getUtxos(address)).deep.eq([`${txId}:${vout}`])

// Return the balance
expect(await new Computer(conf).getBalance(address)).eq(1e4)
const res = await new Computer(conf).getBalance(address)
expect(res.balance).eq(1e4)

// Return the transactions
expect(await new Computer(conf).listTxs(address)).deep.eq({
Expand Down
29 changes: 28 additions & 1 deletion packages/node/chain-setup/ltc-regtest/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,31 @@ services:
- CHAIN=LTC
- BCN_URL=http://127.0.0.1:1031
restart: always

sync:
command: npm run sync
image: bitcoin-computer-node
env_file: .env
restart: always
environment:
- BCN_ENV=dev
- RPC_HOST=node
- RPC_PORT=19332
- NETWORK=regtest
- CHAIN=LTC
- ZMQ_URL=tcp://node:28332
- BCN_URL=http://127.0.0.1:1031
- POSTGRES_PORT=5432
- POSTGRES_HOST=db
- RPC_PROTOCOL=http
- RPC_USER=${RPC_USER}
- RPC_PASSWORD=${RPC_PASSWORD}
- THREADS=${THREADS}
volumes:
- ./logs:/dist/packages/node/logs
depends_on:
- db
- node
networks:
- bitcoin
- bcn

34 changes: 31 additions & 3 deletions packages/node/db/db_schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,44 @@ CREATE TABLE IF NOT EXISTS
"mod" VARCHAR(70),
"previous" VARCHAR(70),
"hash" VARCHAR(64),
"blockHash" VARCHAR(64),
"timestamp" timestamp default CURRENT_TIMESTAMP not null
);

CREATE TABLE IF NOT EXISTS
"Input" (
"outputSpent" VARCHAR(70) NOT NULL PRIMARY KEY,
"spendingInput" VARCHAR(70) NOT NULL
"spendingInput" VARCHAR(70) NOT NULL,
"blockHash" VARCHAR(64)
);

CREATE TABLE IF NOT EXISTS
"Block" (
"hash" VARCHAR(64) NOT NULL PRIMARY KEY,
"height" INTEGER NOT NULL,
"previousHash" VARCHAR(64) NOT NULL
);

CREATE TABLE IF NOT EXISTS
"Orphan" (
"hash" VARCHAR(64) NOT NULL PRIMARY KEY
);

CREATE INDEX "BlockIndex"
ON "Block"("height");

CREATE INDEX "OutputAddressIndex"
ON "Output"("address");

CREATE INDEX "OutputPreviousIndex"
ON "Output"("previous");

CREATE INDEX "OutputBlockHashIndex"
ON "Output"("blockHash");

CREATE INDEX "InputOutputSpentIndex"
ON "Input"("blockHash");

CREATE TABLE IF NOT EXISTS
"User" (
"publicKey" VARCHAR(66) NOT NULL PRIMARY KEY,
Expand All @@ -37,13 +60,18 @@ CREATE TABLE IF NOT EXISTS
);

CREATE TABLE IF NOT EXISTS
"SyncStatus" (
"TxStatus" (
"blockToSync" INTEGER NOT NULL,
"workerId" INTEGER NOT NULL PRIMARY KEY
);

CREATE TABLE IF NOT EXISTS
"BlockStatus" (
"blockToSync" INTEGER NOT NULL PRIMARY KEY
);

CREATE VIEW "Utxos" AS
SELECT "rev", "address", "satoshis", "scriptPubKey", "publicKeys", "timestamp"
SELECT "rev", "address", "satoshis", "scriptPubKey", "publicKeys", "timestamp", "blockHash"
FROM "Output" WHERE "isTbcOutput" = false AND NOT EXISTS
(SELECT 1 FROM "Input" ip WHERE "ip"."outputSpent" = "Output"."rev")

2 changes: 1 addition & 1 deletion packages/node/dist/bcn.es.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/node/dist/bcn.sync.es.mjs

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions packages/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,8 @@
"lint-fix": "eslint --fix",
"node-clean": "./scripts/node-clean.sh",
"reset": "yes | ./scripts/reset.sh",
"start": "node $(grep SRC_ENTRY .package.paths | cut -d '=' -f2)",
"start-test": "node --loader ts-node/esm ./scripts/dev-setup.ts",
"sync": "$(grep SYNC_START .package.paths | cut -d '=' -f2)",
"start": "node dist/bcn.es.mjs",
"sync": "node dist/bcn.sync.es.mjs",
"setup": "./scripts/setup.py",
"types": "tsc -p tsconfig.json",
"test": "node --loader ts-node/esm ./scripts/test.ts",
Expand All @@ -52,11 +51,7 @@
"@bitcoin-computer/lib": "^0.20.1-beta.0",
"@bitcoin-computer/nakamotojs": "^0.20.1-beta.0",
"@bitcoin-computer/secp256k1": "^0.20.1-beta.0",
"@types/chai": "^4.3.1",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.20",
"@types/sinon": "^10.0.11",
"@types/sinon-chai": "^3.2.8",
"argparse": "^2.0.1",
"axios-mock-adapter": "^1.22.0",
"bitcoind-rpc": "^0.9.1",
Expand Down Expand Up @@ -85,6 +80,12 @@
"winston-daily-rotate-file": "^5.0.0",
"zeromq": "6.0.0-beta.6"
},
"devDependencies": {
"@types/chai": "^4.3.1",
"@types/mocha": "^10.0.6",
"@types/sinon": "^10.0.11",
"@types/sinon-chai": "^3.2.8"
},
"keywords": [
"Bitcoin",
"Litecoin",
Expand Down
65 changes: 0 additions & 65 deletions packages/node/scripts/dev-setup.ts

This file was deleted.

5 changes: 4 additions & 1 deletion packages/node/scripts/reset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ HOSTNAME=localhost
psql "postgresql://$USERNAME:$PASSWORD@$HOSTNAME/$DATABASE" << EOF
TRUNCATE TABLE "Input";
TRUNCATE TABLE "Output";
TRUNCATE TABLE "Block";
TRUNCATE TABLE "Orphan";
TRUNCATE TABLE "User";
TRUNCATE TABLE "OffChain";
TRUNCATE TABLE "SyncStatus";
TRUNCATE TABLE "TxStatus";
TRUNCATE TABLE "BlockStatus";
EOF

# stop all docker containers and networks
Expand Down
3 changes: 3 additions & 0 deletions packages/node/scripts/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const testTypeGroup = parser.add_mutually_exclusive_group()
testTypeGroup.add_argument('-s', '--single', { action: 'store' })
testTypeGroup.add_argument('-i', '--integration', { action: 'store_true' })
testTypeGroup.add_argument('-u', '--unit', { action: 'store_true', default: true })
testTypeGroup.add_argument('-sync', '--synchronize', { action: 'store_true' })

const args = parser.parse_args()

Expand Down Expand Up @@ -50,6 +51,8 @@ if (args.integration) {
command = `${command} .mocharc-async.json`
} else if (args.single) {
command = `${command} .mocharc-single.json ${args.single}`
} else if (args.synchronize) {
command = `${command} .mocharc-sync.json`
} else {
command = `${command} .mocharc-unit.json`
}
Expand Down
Loading