Skip to content

Commit

Permalink
Merge branch 'main' into metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs authored Feb 12, 2024
2 parents 9af9b2b + 62042e1 commit 5881ce6
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 95 deletions.
6 changes: 3 additions & 3 deletions canisters/management/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export const Satoshi = nat64;
export type Satoshi = nat64;

export const BitcoinNetwork = Variant({
Mainnet: Null,
Regtest: Null,
Testnet: Null
mainnet: Null,
regtest: Null,
testnet: Null
});
export type BitcoinNetwork = typeof BitcoinNetwork.tsType;

Expand Down
1 change: 1 addition & 0 deletions canisters/management/ic.did
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type satoshi = nat64;
type bitcoin_network = variant {
mainnet;
testnet;
regtest;
};

type bitcoin_address = text;
Expand Down
1 change: 1 addition & 0 deletions examples/bitcoin/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"output": "test/dfx_generated/bitcoin",
"node_compatibility": true
},
"env": ["AZLE_TEST_FETCH"],
"metadata": [
{
"name": "candid:service",
Expand Down
86 changes: 51 additions & 35 deletions examples/bitcoin/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { blob, bool, Canister, ic, None, text, update, Vec } from 'azle';
import { blob, bool, Canister, serialize, text, update, Vec } from 'azle';
import {
GetUtxosResult,
managementCanister,
MillisatoshiPerByte,
Satoshi
} from 'azle/canisters/management';
Expand All @@ -12,56 +11,73 @@ const BITCOIN_CYCLE_COST_PER_TRANSACTION_BYTE = 20_000_000n;

export default Canister({
getBalance: update([text], Satoshi, async (address) => {
return await ic.call(managementCanister.bitcoin_get_balance, {
args: [
{
address,
min_confirmations: None,
network: { Regtest: null }
}
],
cycles: BITCOIN_API_CYCLE_COST
});
}),
getCurrentFeePercentiles: update([], Vec(MillisatoshiPerByte), async () => {
return await ic.call(
managementCanister.bitcoin_get_current_fee_percentiles,
{
const response = await fetch(`icp://aaaaa-aa/bitcoin_get_balance`, {
body: serialize({
args: [
{
network: { Regtest: null }
address,
min_confirmations: [],
network: { regtest: null }
}
],
cycles: BITCOIN_API_CYCLE_COST
})
});
const responseJson = await response.json();

return responseJson;
}),
getCurrentFeePercentiles: update([], Vec(MillisatoshiPerByte), async () => {
const response = await fetch(
`icp://aaaaa-aa/bitcoin_get_current_fee_percentiles`,
{
body: serialize({
args: [
{
network: { regtest: null }
}
],
cycles: BITCOIN_API_CYCLE_COST
})
}
);
const responseJson = await response.json();

return responseJson;
}),
getUtxos: update([text], GetUtxosResult, async (address) => {
return await ic.call(managementCanister.bitcoin_get_utxos, {
args: [
{
address,
filter: None,
network: { Regtest: null }
}
],
cycles: BITCOIN_API_CYCLE_COST
const response = await fetch(`icp://aaaaa-aa/bitcoin_get_utxos`, {
body: serialize({
args: [
{
address,
filter: [],
network: { regtest: null }
}
],
cycles: BITCOIN_API_CYCLE_COST
})
});
const responseJson = await response.json();

return responseJson;
}),
sendTransaction: update([blob], bool, async (transaction) => {
const transactionFee =
BITCOIN_BASE_TRANSACTION_COST +
BigInt(transaction.length) *
BITCOIN_CYCLE_COST_PER_TRANSACTION_BYTE;

await ic.call(managementCanister.bitcoin_send_transaction, {
args: [
{
transaction,
network: { Regtest: null }
}
],
cycles: transactionFee
await fetch(`icp://aaaaa-aa/bitcoin_send_transaction`, {
body: serialize({
args: [
{
transaction,
network: { regtest: null }
}
],
cycles: transactionFee
})
});

return true;
Expand Down
2 changes: 0 additions & 2 deletions examples/bitcoin/test/pretest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { execSync } from 'child_process';

async function pretest() {
await new Promise((resolve) => setTimeout(resolve, 5000));

execSync(`rm -rf .bitcoin/regtest`, {
stdio: 'inherit'
});
Expand Down
14 changes: 5 additions & 9 deletions examples/bitcoin/test/test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ok, getCanisterId, runTests, Test } from 'azle/test';
import { getCanisterId, runTests, Test } from 'azle/test';
import { createActor } from './dfx_generated/bitcoin';
import { wallets } from './wallets';
import { impureSetup, whileRunningBitcoinDaemon } from './setup';
Expand Down Expand Up @@ -42,10 +42,6 @@ function testCanisterFunctionality() {
const blocksMinedInSetup = 101n;
const expectedBalance = blockReward * blocksMinedInSetup;

// TODO remove this after testing
console.log('result', result);
console.log('expectedBalance', expectedBalance);

return {
Ok: result === expectedBalance
};
Expand Down Expand Up @@ -76,7 +72,7 @@ function testCanisterFunctionality() {
{
name: 'sendTransaction',
test: async () => {
const balance_before_transaction =
const receivedBeforeTransaction =
bitcoinCli.getReceivedByAddress(wallets.bob.p2wpkh);

const tx_bytes = hex_string_to_bytes(state.signedTxHex);
Expand All @@ -88,14 +84,14 @@ function testCanisterFunctionality() {
// Wait for generated block to be pulled into replica
await new Promise((resolve) => setTimeout(resolve, 5000));

const balance_after_transaction =
const receivedAfterTransaction =
bitcoinCli.getReceivedByAddress(wallets.bob.p2wpkh, 0);

return {
Ok:
result === true &&
balance_before_transaction === 0 &&
balance_after_transaction === 1
receivedBeforeTransaction === 0 &&
receivedAfterTransaction === 1
};
}
}
Expand Down
3 changes: 2 additions & 1 deletion examples/ckbtc/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"node_compatibility": true,
"output": "wallet/frontend/dfx_generated/wallet_backend"
},
"env": ["CK_BTC_PRINCIPAL", "MINTER_PRINCIPAL"],
"env": ["CK_BTC_PRINCIPAL", "MINTER_PRINCIPAL", "AZLE_TEST_FETCH"],
"assets": [["minter/minter.did", "minter/minter.did"]],
"metadata": [
{
"name": "candid:service",
Expand Down
7 changes: 4 additions & 3 deletions examples/ckbtc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"deploy:wallet_frontend": "dfx deploy wallet_frontend --specified-id ryjl3-tyaaa-aaaaa-aaaba-cai",
"frontend": "npx open-cli http://ryjl3-tyaaa-aaaaa-aaaba-cai.localhost:8000/",
"mint": ".bitcoin/bin/bitcoin-cli -conf=$(pwd)/.bitcoin.conf generatetoaddress 1 $npm_config_address",
"pretest": "ts-node --transpile-only --ignore=false test/pretest.ts",
"test": "ts-node --transpile-only --ignore=false test/test.ts",
"build": "cd wallet/frontend && npm run build"
"build": "cd wallet/frontend && npm run build",
"pre_tests": "ts-node --transpile-only --ignore=false test/pretest.ts",
"tests": "npm run pre_tests && ts-node --transpile-only --ignore=false test/test.ts",
"test": "AZLE_TEST_FETCH=false npm run tests && AZLE_TEST_FETCH=true npm run tests"
},
"dependencies": {
"azle": "0.20.2-rc.0"
Expand Down
Loading

0 comments on commit 5881ce6

Please sign in to comment.