-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: added test for scan() and getBalance()
Signed-off-by: Anmol Sharma <[email protected]>
- Loading branch information
1 parent
e472d68
commit c0f8e3a
Showing
3 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import axios, { AxiosError, AxiosRequestConfig } from 'axios'; | ||
|
||
export class BitcoinRpcClient { | ||
url: string; | ||
config: AxiosRequestConfig; | ||
|
||
constructor() { | ||
const user = process.env.BITCOIN_RPC_USER; | ||
const password = process.env.BITCOIN_RPC_PASSWORD; | ||
const host = process.env.BITCOIN_RPC_HOST; | ||
this.url = `http://${user}:${password}@${host}`; | ||
this.config = { | ||
method: 'POST', | ||
headers: { | ||
'content-type': 'application/json', | ||
}, | ||
}; | ||
} | ||
|
||
async init() { | ||
let loadWallet = false; | ||
try { | ||
await this.createWallet('default'); | ||
} catch (e) { | ||
if ( | ||
e instanceof AxiosError && | ||
e.response?.data.error.message.includes( | ||
'Database already exists.', | ||
) | ||
) { | ||
loadWallet = true; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
try { | ||
const result = await this.getWalletInfo(); | ||
if (result['walletname'] === 'default') { | ||
loadWallet = false; | ||
} | ||
} catch (e) { | ||
if ( | ||
e instanceof AxiosError && | ||
!e.response?.data.error.message.includes('No wallet is loaded.') | ||
) { | ||
throw e; | ||
} | ||
} | ||
try { | ||
if (loadWallet) { | ||
await this.loadWallet('default'); | ||
const address = await this.getNewAddress(); | ||
await this.mineToAddress(150, address); | ||
} | ||
} catch (e) { | ||
if ( | ||
e instanceof AxiosError && | ||
!e.response?.data.error.message.includes( | ||
'Unable to obtain an exclusive lock on the database', | ||
) | ||
) { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
private async request(config: AxiosRequestConfig) { | ||
const response = await axios.request({ | ||
...this.config, | ||
...config, | ||
}); | ||
return response.data?.result; | ||
} | ||
|
||
async createWallet(walletName: string) { | ||
return await this.request({ | ||
url: this.url, | ||
data: { | ||
method: 'createwallet', | ||
params: [walletName], | ||
}, | ||
}); | ||
} | ||
|
||
async getWalletInfo() { | ||
return await this.request({ | ||
url: this.url, | ||
data: { | ||
method: 'getwalletinfo', | ||
params: [], | ||
}, | ||
}); | ||
} | ||
|
||
async loadWallet(walletName: string) { | ||
return await this.request({ | ||
url: this.url, | ||
data: { | ||
method: 'loadwallet', | ||
params: [walletName], | ||
}, | ||
}); | ||
} | ||
|
||
async getNewAddress() { | ||
return await this.request({ | ||
url: this.url, | ||
data: { | ||
method: 'getnewaddress', | ||
params: [], | ||
}, | ||
}); | ||
} | ||
|
||
async mineToAddress(numBlocks: number, address: string) { | ||
return await this.request({ | ||
url: this.url, | ||
data: { | ||
method: 'generatetoaddress', | ||
params: [numBlocks, address], | ||
}, | ||
}); | ||
} | ||
|
||
async sendToAddress(address: string, amount: number) { | ||
return await this.request({ | ||
url: this.url, | ||
data: { | ||
method: 'sendtoaddress', | ||
params: [address, amount], | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters