diff --git a/canisters/management/bitcoin.ts b/canisters/management/bitcoin.ts index 811263c4cf..057a08e685 100644 --- a/canisters/management/bitcoin.ts +++ b/canisters/management/bitcoin.ts @@ -2,7 +2,6 @@ import { blob, - candid, nat32, nat64, Null, @@ -11,7 +10,7 @@ import { text, Variant, Vec -} from '../../src/lib_new'; +} from '../../src/lib_functional'; export type BitcoinAddress = text; export const BitcoinAddress = text; @@ -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; - - @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; - - @candid(BitcoinNetwork) - network: BitcoinNetwork; -} - -export class GetUtxosResult extends Record { - @candid(Opt(Page)) - next_page: Opt; - - @candid(BlockHash) - tip_block_hash: BlockHash; - - @candid(nat32) - tip_height: nat32; - - @candid(Vec(Utxo)) - utxos: Vec; -} - -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 +}); diff --git a/canisters/management/index.ts b/canisters/management/index.ts index fdbc36c211..4f063f428b 100644 --- a/canisters/management/index.ts +++ b/canisters/management/index.ts @@ -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')); diff --git a/examples/bitcoin/src/index.did b/examples/bitcoin/src/index.did index bc830c1f89..b74fa19ac0 100644 --- a/examples/bitcoin/src/index.did +++ b/examples/bitcoin/src/index.did @@ -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); } diff --git a/examples/bitcoin/src/index.ts b/examples/bitcoin/src/index.ts index 70ee0f4108..89b95f62dd 100644 --- a/examples/bitcoin/src/index.ts +++ b/examples/bitcoin/src/index.ts @@ -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 { +export default Service({ + getBalance: update([text], Satoshi, async (address) => { return await ic.call(managementCanister.bitcoin_get_balance, { args: [ { @@ -23,10 +22,8 @@ export default class extends Service { ], cycles: BITCOIN_API_CYCLE_COST }); - } - - @update([], Vec(MillisatoshiPerByte)) - async getCurrentFeePercentiles(): Promise> { + }), + getCurrentFeePercentiles: update([], Vec(MillisatoshiPerByte), async () => { return await ic.call( managementCanister.bitcoin_get_current_fee_percentiles, { @@ -38,10 +35,8 @@ export default class extends Service { cycles: BITCOIN_API_CYCLE_COST } ); - } - - @update([text], GetUtxosResult) - async getUtxos(address: text): Promise { + }), + getUtxos: update([text], GetUtxosResult, async (address) => { return await ic.call(managementCanister.bitcoin_get_utxos, { args: [ { @@ -52,10 +47,8 @@ export default class extends Service { ], cycles: BITCOIN_API_CYCLE_COST }); - } - - @update([blob], bool) - async sendTransaction(transaction: blob): Promise { + }), + sendTransaction: update([blob], bool, async (transaction) => { const transactionFee = BITCOIN_BASE_TRANSACTION_COST + BigInt(transaction.length) * @@ -72,5 +65,5 @@ export default class extends Service { }); return true; - } -} + }) +}); diff --git a/src/lib_functional/candid/reference/record.ts b/src/lib_functional/candid/reference/record.ts index e6732dd705..c52f3e9de2 100644 --- a/src/lib_functional/candid/reference/record.ts +++ b/src/lib_functional/candid/reference/record.ts @@ -9,6 +9,7 @@ export function Record(obj: T): { const name = v4(); return { + ...obj, getIDL(parents: any) { const idl = IDL.Rec(); idl.fill( diff --git a/src/lib_functional/candid/reference/variant.ts b/src/lib_functional/candid/reference/variant.ts index ab272d5bfb..f530380bb9 100644 --- a/src/lib_functional/candid/reference/variant.ts +++ b/src/lib_functional/candid/reference/variant.ts @@ -9,6 +9,7 @@ export function Variant(obj: T): RequireExactlyOne<{ const name = v4(); return { + ...obj, getIDL(parents: any) { const idl = IDL.Rec(); idl.fill(