Skip to content

Commit

Permalink
Merge pull request #1252 from demergent-labs/functional_syntax_bitcoin
Browse files Browse the repository at this point in the history
bitcoin example completed
  • Loading branch information
lastmjs authored Sep 24, 2023
2 parents c9cce30 + 335b821 commit c709c3c
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 118 deletions.
151 changes: 55 additions & 96 deletions canisters/management/bitcoin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import {
blob,
candid,
nat32,
nat64,
Null,
Expand All @@ -11,7 +10,7 @@ import {
text,
Variant,
Vec
} from '../../src/lib_new';
} from '../../src/lib_functional';

export type BitcoinAddress = text;
export const BitcoinAddress = text;
Expand All @@ -24,97 +23,57 @@ export const MillisatoshiPerByte = nat64;
export type Satoshi = nat64;
export const Satoshi = nat64;

export class BitcoinNetwork extends Variant {
@candid(Null)
Mainnet?: Null;

@candid(Null)
Regtest?: Null;

@candid(Null)
Testnet?: Null;
}

export class Outpoint extends Record {
@candid(blob)
txid: blob;

@candid(nat32)
vout: nat32;
}

export class Utxo extends Record {
@candid(nat32)
height: nat32;

@candid(Outpoint)
outpoint: Outpoint;

@candid(Satoshi)
value: Satoshi;
}

export class UtxosFilter extends Variant {
@candid(nat32)
MinConfirmations?: nat32;

@candid(Page)
Page?: Page;
}

export class GetBalanceArgs extends Record {
@candid(BitcoinAddress)
address: BitcoinAddress;

@candid(Opt(nat32))
min_confirmations: Opt<nat32>;

@candid(BitcoinNetwork)
network: BitcoinNetwork;
}

export class GetCurrentFeePercentilesArgs extends Record {
@candid(BitcoinNetwork)
network: BitcoinNetwork;
}

export class GetUtxosArgs extends Record {
@candid(BitcoinAddress)
address: BitcoinAddress;

@candid(Opt(UtxosFilter))
filter: Opt<UtxosFilter>;

@candid(BitcoinNetwork)
network: BitcoinNetwork;
}

export class GetUtxosResult extends Record {
@candid(Opt(Page))
next_page: Opt<Page>;

@candid(BlockHash)
tip_block_hash: BlockHash;

@candid(nat32)
tip_height: nat32;

@candid(Vec(Utxo))
utxos: Vec<Utxo>;
}

export class SendTransactionArgs extends Record {
@candid(blob)
transaction: blob;

@candid(BitcoinNetwork)
network: BitcoinNetwork;
}

export class SendTransactionError extends Variant {
@candid(Null)
MalformedTransaction?: Null;

@candid(Null)
QueueFull?: Null;
}
export const BitcoinNetwork = Variant({
Mainnet: Null,
Regtest: Null,
Testnet: Null
});

export const Outpoint = Record({
txid: blob,
vout: nat32
});

export const Utxo = Record({
height: nat32,
outpoint: Outpoint,
value: Satoshi
});

export const UtxosFilter = Variant({
MinConfirmations: nat32,
Page: Page
});

export const GetBalanceArgs = Record({
address: BitcoinAddress,
min_confirmations: Opt(nat32),
network: BitcoinNetwork
});

export const GetCurrentFeePercentilesArgs = Record({
network: BitcoinNetwork
});

export const GetUtxosArgs = Record({
address: BitcoinAddress,
filter: Opt(UtxosFilter),
network: BitcoinNetwork
});

export const GetUtxosResult = Record({
next_page: Opt(Page),
tip_block_hash: BlockHash,
tip_height: nat32,
utxos: Vec(Utxo)
});

export const SendTransactionArgs = Record({
transaction: blob,
network: BitcoinNetwork
});

export const SendTransactionError = Variant({
MalformedTransaction: Null,
QueueFull: Null
});
27 changes: 26 additions & 1 deletion canisters/management/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import { blob, Principal, Service, update } from '../../src/lib_functional';
import {
blob,
Principal,
Service,
update,
Vec,
Void
} from '../../src/lib_functional';
import {
GetBalanceArgs,
GetCurrentFeePercentilesArgs,
GetUtxosArgs,
GetUtxosResult,
MillisatoshiPerByte,
Satoshi,
SendTransactionArgs
} from './bitcoin';

export * from './bitcoin';

export const managementCanister = Service({
bitcoin_get_balance: update([GetBalanceArgs], Satoshi),
bitcoin_get_current_fee_percentiles: update(
[GetCurrentFeePercentilesArgs],
Vec(MillisatoshiPerByte)
),
bitcoin_get_utxos: update([GetUtxosArgs], GetUtxosResult),
bitcoin_send_transaction: update([SendTransactionArgs], Void),
raw_rand: update([], blob)
})(Principal.fromText('aaaaa-aa'));
8 changes: 4 additions & 4 deletions examples/bitcoin/src/index.did
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
type rec_2 = record {txid:vec nat8; vout:nat32};
type rec_1 = record {height:nat32; value:nat64; outpoint:rec_2};
type rec_0 = record {next_page:opt vec nat8; tip_height:nat32; tip_block_hash:vec nat8; utxos:vec rec_1};
type rec_14 = record {txid:vec nat8; vout:nat32};
type rec_13 = record {height:nat32; value:nat64; outpoint:rec_14};
type rec_12 = record {next_page:opt vec nat8; tip_height:nat32; tip_block_hash:vec nat8; utxos:vec rec_13};
service: () -> {
getBalance: (text) -> (nat64);
getCurrentFeePercentiles: () -> (vec nat64);
getUtxos: (text) -> (rec_0);
getUtxos: (text) -> (rec_12);
sendTransaction: (vec nat8) -> (bool);
}
27 changes: 10 additions & 17 deletions examples/bitcoin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ const BITCOIN_API_CYCLE_COST = 100_000_000n;
const BITCOIN_BASE_TRANSACTION_COST = 5_000_000_000n;
const BITCOIN_CYCLE_COST_PER_TRANSACTION_BYTE = 20_000_000n;

export default class extends Service {
@update([text], Satoshi)
async getBalance(address: string): Promise<Satoshi> {
export default Service({
getBalance: update([text], Satoshi, async (address) => {
return await ic.call(managementCanister.bitcoin_get_balance, {
args: [
{
Expand All @@ -23,10 +22,8 @@ export default class extends Service {
],
cycles: BITCOIN_API_CYCLE_COST
});
}

@update([], Vec(MillisatoshiPerByte))
async getCurrentFeePercentiles(): Promise<Vec<MillisatoshiPerByte>> {
}),
getCurrentFeePercentiles: update([], Vec(MillisatoshiPerByte), async () => {
return await ic.call(
managementCanister.bitcoin_get_current_fee_percentiles,
{
Expand All @@ -38,10 +35,8 @@ export default class extends Service {
cycles: BITCOIN_API_CYCLE_COST
}
);
}

@update([text], GetUtxosResult)
async getUtxos(address: text): Promise<GetUtxosResult> {
}),
getUtxos: update([text], GetUtxosResult, async (address) => {
return await ic.call(managementCanister.bitcoin_get_utxos, {
args: [
{
Expand All @@ -52,10 +47,8 @@ export default class extends Service {
],
cycles: BITCOIN_API_CYCLE_COST
});
}

@update([blob], bool)
async sendTransaction(transaction: blob): Promise<bool> {
}),
sendTransaction: update([blob], bool, async (transaction) => {
const transactionFee =
BITCOIN_BASE_TRANSACTION_COST +
BigInt(transaction.length) *
Expand All @@ -72,5 +65,5 @@ export default class extends Service {
});

return true;
}
}
})
});
1 change: 1 addition & 0 deletions src/lib_functional/candid/reference/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function Record<T>(obj: T): {
const name = v4();

return {
...obj,
getIDL(parents: any) {
const idl = IDL.Rec();
idl.fill(
Expand Down
1 change: 1 addition & 0 deletions src/lib_functional/candid/reference/variant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export function Variant<T>(obj: T): RequireExactlyOne<{
const name = v4();

return {
...obj,
getIDL(parents: any) {
const idl = IDL.Rec();
idl.fill(
Expand Down

0 comments on commit c709c3c

Please sign in to comment.