@dfinity/ledger-icrc
Version:
A library for interfacing with ICRC ledgers on the Internet Computer.
3 lines (2 loc) • 16.6 kB
JavaScript
import{Canister as Re,createServices as Te,toNullable as xe}from"@dfinity/utils";var le=({IDL:e})=>{let s=e.Rec(),t=e.Rec(),c=e.Rec(),i=e.Record({num_blocks_to_archive:e.Opt(e.Nat64),max_transactions_per_response:e.Opt(e.Nat64),trigger_threshold:e.Opt(e.Nat64),more_controller_ids:e.Opt(e.Vec(e.Principal)),max_message_size_bytes:e.Opt(e.Nat64),cycles_for_archive_creation:e.Opt(e.Nat64),node_max_memory_size_bytes:e.Opt(e.Nat64),controller_id:e.Opt(e.Principal)}),_=e.Variant({Int:e.Int,Nat:e.Nat,Blob:e.Vec(e.Nat8),Text:e.Text}),d=e.Vec(e.Nat8),r=e.Record({owner:e.Principal,subaccount:e.Opt(d)}),V=e.Variant({SetTo:r,Unset:e.Null}),N=e.Record({icrc2:e.Bool}),y=e.Record({change_archive_options:e.Opt(i),token_symbol:e.Opt(e.Text),transfer_fee:e.Opt(e.Nat),metadata:e.Opt(e.Vec(e.Tuple(e.Text,_))),change_fee_collector:e.Opt(V),max_memo_length:e.Opt(e.Nat16),token_name:e.Opt(e.Text),feature_flags:e.Opt(N)}),h=e.Record({decimals:e.Opt(e.Nat8),token_symbol:e.Text,transfer_fee:e.Nat,metadata:e.Vec(e.Tuple(e.Text,_)),minting_account:r,initial_balances:e.Vec(e.Tuple(r,e.Nat)),fee_collector_account:e.Opt(r),archive_options:e.Record({num_blocks_to_archive:e.Nat64,max_transactions_per_response:e.Opt(e.Nat64),trigger_threshold:e.Nat64,more_controller_ids:e.Opt(e.Vec(e.Principal)),max_message_size_bytes:e.Opt(e.Nat64),cycles_for_archive_creation:e.Opt(e.Nat64),node_max_memory_size_bytes:e.Opt(e.Nat64),controller_id:e.Principal}),max_memo_length:e.Opt(e.Nat16),token_name:e.Text,feature_flags:e.Opt(N)}),Ne=e.Variant({Upgrade:e.Opt(y),Init:h}),o=e.Nat,b=e.Record({block_range_end:o,canister_id:e.Principal,block_range_start:o}),p=e.Record({start:o,length:e.Nat}),F=e.Vec(e.Tuple(e.Text,c));c.fill(e.Variant({Int:e.Int,Map:F,Nat:e.Nat,Nat64:e.Nat64,Blob:e.Vec(e.Nat8),Text:e.Text,Array:e.Vec(c)}));let O=c,k=e.Record({blocks:e.Vec(O)}),A=e.Func([p],[k],[]),v=e.Record({certificate:e.Opt(e.Vec(e.Nat8)),first_index:o,blocks:e.Vec(O),chain_length:e.Nat64,archived_blocks:e.Vec(e.Record({callback:A,start:o,length:e.Nat}))}),P=e.Record({certificate:e.Opt(e.Vec(e.Nat8)),hash_tree:e.Vec(e.Nat8)}),m=e.Nat,R=e.Record({start:m,length:e.Nat}),a=e.Nat64,B=e.Record({from:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat,spender:e.Opt(r)}),C=e.Record({to:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat}),w=e.Record({fee:e.Opt(e.Nat),from:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat,expected_allowance:e.Opt(e.Nat),expires_at:e.Opt(a),spender:r}),G=e.Record({to:r,fee:e.Opt(e.Nat),from:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat,spender:e.Opt(r)}),T=e.Record({burn:e.Opt(B),kind:e.Text,mint:e.Opt(C),approve:e.Opt(w),timestamp:a,transfer:e.Opt(G)}),M=e.Record({transactions:e.Vec(T)}),E=e.Func([R],[M],[]),q=e.Record({first_index:m,log_length:e.Nat,transactions:e.Vec(T),archived_transactions:e.Vec(e.Record({callback:E,start:m,length:e.Nat}))}),n=e.Nat,U=e.Record({url:e.Text,name:e.Text}),S=e.Record({to:r,fee:e.Opt(n),memo:e.Opt(e.Vec(e.Nat8)),from_subaccount:e.Opt(d),created_at_time:e.Opt(a),amount:n}),z=e.Variant({GenericError:e.Record({message:e.Text,error_code:e.Nat}),TemporarilyUnavailable:e.Null,BadBurn:e.Record({min_burn_amount:n}),Duplicate:e.Record({duplicate_of:o}),BadFee:e.Record({expected_fee:n}),CreatedInFuture:e.Record({ledger_time:a}),TooOld:e.Null,InsufficientFunds:e.Record({balance:n})}),Q=e.Variant({Ok:o,Err:z}),x=e.Record({utc_offset_minutes:e.Opt(e.Int16),language:e.Text}),J=e.Record({metadata:x,device_spec:e.Opt(e.Variant({GenericDisplay:e.Null,LineDisplay:e.Record({characters_per_line:e.Nat16,lines_per_page:e.Nat16})}))}),$=e.Record({arg:e.Vec(e.Nat8),method:e.Text,user_preferences:J}),j=e.Variant({LineDisplayMessage:e.Record({pages:e.Vec(e.Record({lines:e.Vec(e.Text)}))}),GenericDisplayMessage:e.Text}),H=e.Record({metadata:x,consent_message:j}),u=e.Record({description:e.Text}),K=e.Variant({GenericError:e.Record({description:e.Text,error_code:e.Nat}),InsufficientPayment:u,UnsupportedCanisterCall:u,ConsentMessageUnavailable:u}),W=e.Variant({Ok:H,Err:K}),X=e.Record({account:r,spender:r}),Y=e.Record({allowance:e.Nat,expires_at:e.Opt(a)}),Z=e.Record({fee:e.Opt(e.Nat),memo:e.Opt(e.Vec(e.Nat8)),from_subaccount:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat,expected_allowance:e.Opt(e.Nat),expires_at:e.Opt(a),spender:r}),I=e.Variant({GenericError:e.Record({message:e.Text,error_code:e.Nat}),TemporarilyUnavailable:e.Null,Duplicate:e.Record({duplicate_of:o}),BadFee:e.Record({expected_fee:e.Nat}),AllowanceChanged:e.Record({current_allowance:e.Nat}),CreatedInFuture:e.Record({ledger_time:a}),TooOld:e.Null,Expired:e.Record({ledger_time:a}),InsufficientFunds:e.Record({balance:e.Nat})}),D=e.Variant({Ok:o,Err:I}),L=e.Record({to:r,fee:e.Opt(n),spender_subaccount:e.Opt(d),from:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:n}),ee=e.Variant({GenericError:e.Record({message:e.Text,error_code:e.Nat}),TemporarilyUnavailable:e.Null,InsufficientAllowance:e.Record({allowance:n}),BadBurn:e.Record({min_burn_amount:n}),Duplicate:e.Record({duplicate_of:o}),BadFee:e.Record({expected_fee:n}),CreatedInFuture:e.Record({ledger_time:a}),TooOld:e.Null,InsufficientFunds:e.Record({balance:n})}),te=e.Variant({Ok:o,Err:ee}),re=e.Record({from:e.Opt(e.Principal)}),ce=e.Vec(e.Record({end:e.Nat,canister_id:e.Principal,start:e.Nat}));t.fill(e.Variant({Int:e.Int,Map:e.Vec(e.Tuple(e.Text,t)),Nat:e.Nat,Blob:e.Vec(e.Nat8),Text:e.Text,Array:e.Vec(t)})),s.fill(e.Record({log_length:e.Nat,blocks:e.Vec(e.Record({id:e.Nat,block:t})),archived_blocks:e.Vec(e.Record({args:e.Vec(p),callback:e.Func([e.Vec(p)],[s],[])}))}));let ae=e.Record({certificate:e.Vec(e.Nat8),hash_tree:e.Vec(e.Nat8)});return e.Service({archives:e.Func([],[e.Vec(b)],[]),get_blocks:e.Func([p],[v],[]),get_data_certificate:e.Func([],[P],[]),get_transactions:e.Func([R],[q],[]),icrc10_supported_standards:e.Func([],[e.Vec(e.Record({url:e.Text,name:e.Text}))],[]),icrc1_balance_of:e.Func([r],[n],[]),icrc1_decimals:e.Func([],[e.Nat8],[]),icrc1_fee:e.Func([],[n],[]),icrc1_metadata:e.Func([],[e.Vec(e.Tuple(e.Text,_))],[]),icrc1_minting_account:e.Func([],[e.Opt(r)],[]),icrc1_name:e.Func([],[e.Text],[]),icrc1_supported_standards:e.Func([],[e.Vec(U)],[]),icrc1_symbol:e.Func([],[e.Text],[]),icrc1_total_supply:e.Func([],[n],[]),icrc1_transfer:e.Func([S],[Q],[]),icrc21_canister_call_consent_message:e.Func([$],[W],[]),icrc2_allowance:e.Func([X],[Y],[]),icrc2_approve:e.Func([Z],[D],[]),icrc2_transfer_from:e.Func([L],[te],[]),icrc3_get_archives:e.Func([re],[ce],[]),icrc3_get_blocks:e.Func([e.Vec(p)],[s],[]),icrc3_get_tip_certificate:e.Func([],[e.Opt(ae)],[]),icrc3_supported_block_types:e.Func([],[e.Vec(e.Record({url:e.Text,block_type:e.Text}))],[]),is_ledger_ready:e.Func([],[e.Bool],[])})};var _e=({IDL:e})=>{let s=e.Rec(),t=e.Rec(),c=e.Rec(),i=e.Record({num_blocks_to_archive:e.Opt(e.Nat64),max_transactions_per_response:e.Opt(e.Nat64),trigger_threshold:e.Opt(e.Nat64),more_controller_ids:e.Opt(e.Vec(e.Principal)),max_message_size_bytes:e.Opt(e.Nat64),cycles_for_archive_creation:e.Opt(e.Nat64),node_max_memory_size_bytes:e.Opt(e.Nat64),controller_id:e.Opt(e.Principal)}),_=e.Variant({Int:e.Int,Nat:e.Nat,Blob:e.Vec(e.Nat8),Text:e.Text}),d=e.Vec(e.Nat8),r=e.Record({owner:e.Principal,subaccount:e.Opt(d)}),V=e.Variant({SetTo:r,Unset:e.Null}),N=e.Record({icrc2:e.Bool}),y=e.Record({change_archive_options:e.Opt(i),token_symbol:e.Opt(e.Text),transfer_fee:e.Opt(e.Nat),metadata:e.Opt(e.Vec(e.Tuple(e.Text,_))),change_fee_collector:e.Opt(V),max_memo_length:e.Opt(e.Nat16),token_name:e.Opt(e.Text),feature_flags:e.Opt(N)}),h=e.Record({decimals:e.Opt(e.Nat8),token_symbol:e.Text,transfer_fee:e.Nat,metadata:e.Vec(e.Tuple(e.Text,_)),minting_account:r,initial_balances:e.Vec(e.Tuple(r,e.Nat)),fee_collector_account:e.Opt(r),archive_options:e.Record({num_blocks_to_archive:e.Nat64,max_transactions_per_response:e.Opt(e.Nat64),trigger_threshold:e.Nat64,more_controller_ids:e.Opt(e.Vec(e.Principal)),max_message_size_bytes:e.Opt(e.Nat64),cycles_for_archive_creation:e.Opt(e.Nat64),node_max_memory_size_bytes:e.Opt(e.Nat64),controller_id:e.Principal}),max_memo_length:e.Opt(e.Nat16),token_name:e.Text,feature_flags:e.Opt(N)}),Ne=e.Variant({Upgrade:e.Opt(y),Init:h}),o=e.Nat,b=e.Record({block_range_end:o,canister_id:e.Principal,block_range_start:o}),p=e.Record({start:o,length:e.Nat}),F=e.Vec(e.Tuple(e.Text,c));c.fill(e.Variant({Int:e.Int,Map:F,Nat:e.Nat,Nat64:e.Nat64,Blob:e.Vec(e.Nat8),Text:e.Text,Array:e.Vec(c)}));let O=c,k=e.Record({blocks:e.Vec(O)}),A=e.Func([p],[k],["query"]),v=e.Record({certificate:e.Opt(e.Vec(e.Nat8)),first_index:o,blocks:e.Vec(O),chain_length:e.Nat64,archived_blocks:e.Vec(e.Record({callback:A,start:o,length:e.Nat}))}),P=e.Record({certificate:e.Opt(e.Vec(e.Nat8)),hash_tree:e.Vec(e.Nat8)}),m=e.Nat,R=e.Record({start:m,length:e.Nat}),a=e.Nat64,B=e.Record({from:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat,spender:e.Opt(r)}),C=e.Record({to:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat}),w=e.Record({fee:e.Opt(e.Nat),from:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat,expected_allowance:e.Opt(e.Nat),expires_at:e.Opt(a),spender:r}),G=e.Record({to:r,fee:e.Opt(e.Nat),from:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat,spender:e.Opt(r)}),T=e.Record({burn:e.Opt(B),kind:e.Text,mint:e.Opt(C),approve:e.Opt(w),timestamp:a,transfer:e.Opt(G)}),M=e.Record({transactions:e.Vec(T)}),E=e.Func([R],[M],["query"]),q=e.Record({first_index:m,log_length:e.Nat,transactions:e.Vec(T),archived_transactions:e.Vec(e.Record({callback:E,start:m,length:e.Nat}))}),n=e.Nat,U=e.Record({url:e.Text,name:e.Text}),S=e.Record({to:r,fee:e.Opt(n),memo:e.Opt(e.Vec(e.Nat8)),from_subaccount:e.Opt(d),created_at_time:e.Opt(a),amount:n}),z=e.Variant({GenericError:e.Record({message:e.Text,error_code:e.Nat}),TemporarilyUnavailable:e.Null,BadBurn:e.Record({min_burn_amount:n}),Duplicate:e.Record({duplicate_of:o}),BadFee:e.Record({expected_fee:n}),CreatedInFuture:e.Record({ledger_time:a}),TooOld:e.Null,InsufficientFunds:e.Record({balance:n})}),Q=e.Variant({Ok:o,Err:z}),x=e.Record({utc_offset_minutes:e.Opt(e.Int16),language:e.Text}),J=e.Record({metadata:x,device_spec:e.Opt(e.Variant({GenericDisplay:e.Null,LineDisplay:e.Record({characters_per_line:e.Nat16,lines_per_page:e.Nat16})}))}),$=e.Record({arg:e.Vec(e.Nat8),method:e.Text,user_preferences:J}),j=e.Variant({LineDisplayMessage:e.Record({pages:e.Vec(e.Record({lines:e.Vec(e.Text)}))}),GenericDisplayMessage:e.Text}),H=e.Record({metadata:x,consent_message:j}),u=e.Record({description:e.Text}),K=e.Variant({GenericError:e.Record({description:e.Text,error_code:e.Nat}),InsufficientPayment:u,UnsupportedCanisterCall:u,ConsentMessageUnavailable:u}),W=e.Variant({Ok:H,Err:K}),X=e.Record({account:r,spender:r}),Y=e.Record({allowance:e.Nat,expires_at:e.Opt(a)}),Z=e.Record({fee:e.Opt(e.Nat),memo:e.Opt(e.Vec(e.Nat8)),from_subaccount:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:e.Nat,expected_allowance:e.Opt(e.Nat),expires_at:e.Opt(a),spender:r}),I=e.Variant({GenericError:e.Record({message:e.Text,error_code:e.Nat}),TemporarilyUnavailable:e.Null,Duplicate:e.Record({duplicate_of:o}),BadFee:e.Record({expected_fee:e.Nat}),AllowanceChanged:e.Record({current_allowance:e.Nat}),CreatedInFuture:e.Record({ledger_time:a}),TooOld:e.Null,Expired:e.Record({ledger_time:a}),InsufficientFunds:e.Record({balance:e.Nat})}),D=e.Variant({Ok:o,Err:I}),L=e.Record({to:r,fee:e.Opt(n),spender_subaccount:e.Opt(d),from:r,memo:e.Opt(e.Vec(e.Nat8)),created_at_time:e.Opt(a),amount:n}),ee=e.Variant({GenericError:e.Record({message:e.Text,error_code:e.Nat}),TemporarilyUnavailable:e.Null,InsufficientAllowance:e.Record({allowance:n}),BadBurn:e.Record({min_burn_amount:n}),Duplicate:e.Record({duplicate_of:o}),BadFee:e.Record({expected_fee:n}),CreatedInFuture:e.Record({ledger_time:a}),TooOld:e.Null,InsufficientFunds:e.Record({balance:n})}),te=e.Variant({Ok:o,Err:ee}),re=e.Record({from:e.Opt(e.Principal)}),ce=e.Vec(e.Record({end:e.Nat,canister_id:e.Principal,start:e.Nat}));t.fill(e.Variant({Int:e.Int,Map:e.Vec(e.Tuple(e.Text,t)),Nat:e.Nat,Blob:e.Vec(e.Nat8),Text:e.Text,Array:e.Vec(t)})),s.fill(e.Record({log_length:e.Nat,blocks:e.Vec(e.Record({id:e.Nat,block:t})),archived_blocks:e.Vec(e.Record({args:e.Vec(p),callback:e.Func([e.Vec(p)],[s],["query"])}))}));let ae=e.Record({certificate:e.Vec(e.Nat8),hash_tree:e.Vec(e.Nat8)});return e.Service({archives:e.Func([],[e.Vec(b)],["query"]),get_blocks:e.Func([p],[v],["query"]),get_data_certificate:e.Func([],[P],["query"]),get_transactions:e.Func([R],[q],["query"]),icrc10_supported_standards:e.Func([],[e.Vec(e.Record({url:e.Text,name:e.Text}))],["query"]),icrc1_balance_of:e.Func([r],[n],["query"]),icrc1_decimals:e.Func([],[e.Nat8],["query"]),icrc1_fee:e.Func([],[n],["query"]),icrc1_metadata:e.Func([],[e.Vec(e.Tuple(e.Text,_))],["query"]),icrc1_minting_account:e.Func([],[e.Opt(r)],["query"]),icrc1_name:e.Func([],[e.Text],["query"]),icrc1_supported_standards:e.Func([],[e.Vec(U)],["query"]),icrc1_symbol:e.Func([],[e.Text],["query"]),icrc1_total_supply:e.Func([],[n],["query"]),icrc1_transfer:e.Func([S],[Q],[]),icrc21_canister_call_consent_message:e.Func([$],[W],[]),icrc2_allowance:e.Func([X],[Y],["query"]),icrc2_approve:e.Func([Z],[D],[]),icrc2_transfer_from:e.Func([L],[te],[]),icrc3_get_archives:e.Func([re],[ce],["query"]),icrc3_get_blocks:e.Func([e.Vec(p)],[s],["query"]),icrc3_get_tip_certificate:e.Func([],[e.Opt(ae)],["query"]),icrc3_supported_block_types:e.Func([],[e.Vec(e.Record({url:e.Text,block_type:e.Text}))],["query"]),is_ledger_ready:e.Func([],[e.Bool],["query"])})};import{isNullish as Oe,toNullable as l}from"@dfinity/utils";var pe=({from_subaccount:e,fee:s,created_at_time:t,memo:c,...i})=>({...i,fee:l(s),memo:l(c),from_subaccount:l(e),created_at_time:l(t)}),de=({spender_subaccount:e,fee:s,created_at_time:t,memo:c,...i})=>({...i,fee:l(s),memo:l(c),spender_subaccount:l(e),created_at_time:l(t)}),me=({fee:e,created_at_time:s,memo:t,from_subaccount:c,expected_allowance:i,expires_at:_,...d})=>({...d,fee:l(e),memo:l(t),from_subaccount:l(c),created_at_time:l(s),expected_allowance:l(i),expires_at:l(_)}),ue=({userPreferences:{metadata:{utcOffsetMinutes:e,language:s},deriveSpec:t},...c})=>({...c,user_preferences:{metadata:{language:s,utc_offset_minutes:l(e)},device_spec:Oe(t)?l():l("GenericDisplay"in t?{GenericDisplay:null}:{LineDisplay:{characters_per_line:t.LineDisplay.charactersPerLine,lines_per_page:t.LineDisplay.linesPerPage}})}});var g=class extends Error{constructor({msg:s,errorType:t}){super(s),this.errorType=t}},ne=class extends Error{constructor(t,c){super();this.message=t;this.error_code=c}},f=class extends Error{},oe=class extends f{},se=class extends f{},ie=class extends f{},ge=e=>"GenericError"in e?new ne(e.GenericError.description,e.GenericError.error_code):"InsufficientPayment"in e?new oe(e.InsufficientPayment.description):"UnsupportedCanisterCall"in e?new se(e.UnsupportedCanisterCall.description):"ConsentMessageUnavailable"in e?new ie(e.ConsentMessageUnavailable.description):new f(`Unknown error type ${JSON.stringify(e)}`);var fe=class e extends Re{constructor(){super(...arguments);this.metadata=t=>this.caller(t).icrc1_metadata();this.transactionFee=t=>this.caller(t).icrc1_fee();this.balance=t=>this.caller({certified:t.certified}).icrc1_balance_of({owner:t.owner,subaccount:xe(t.subaccount)});this.transfer=async t=>{let c=await this.caller({certified:!0}).icrc1_transfer(pe(t));if("Err"in c)throw new g({errorType:c.Err,msg:"Failed to transfer"});return c.Ok};this.totalTokensSupply=t=>this.caller(t).icrc1_total_supply();this.transferFrom=async t=>{let c=await this.caller({certified:!0}).icrc2_transfer_from(de(t));if("Err"in c)throw new g({errorType:c.Err,msg:"Failed to transfer from"});return c.Ok};this.approve=async t=>{let c=await this.caller({certified:!0}).icrc2_approve(me(t));if("Err"in c)throw new g({errorType:c.Err,msg:"Failed to entitle the spender to transfer the amount"});return c.Ok};this.allowance=async t=>{let{certified:c,...i}=t;return this.caller({certified:c}).icrc2_allowance({...i})};this.consentMessage=async t=>{let{icrc21_canister_call_consent_message:c}=this.caller({certified:!0}),i=await c(ue(t));if("Err"in i)throw ge(i.Err);return i.Ok};this.getBlocks=t=>this.caller({certified:t.certified}).icrc3_get_blocks(t.args)}static create(t){let{service:c,certifiedService:i,canisterId:_}=Te({options:t,idlFactory:_e,certifiedIdlFactory:le});return new e(_,c,i)}};export{pe as a,de as b,me as c,ue as d,g as e,ne as f,f as g,oe as h,se as i,ie as j,ge as k,fe as l};
//# sourceMappingURL=chunk-RQUD6I43.js.map