From f424d9ac6961d79cbd56d63383a273bb3c65dbd3 Mon Sep 17 00:00:00 2001 From: "Paul H. Liu" Date: Sat, 19 Jun 2021 08:51:09 -0700 Subject: [PATCH] Try fetch candid from canister itself, and then from canlista --- Makefile | 9 +- src/bare-agent.js | 65 +++++++++--- src/governance.did | 250 --------------------------------------------- src/ledger.did | 85 --------------- 4 files changed, 52 insertions(+), 357 deletions(-) delete mode 100644 src/governance.did delete mode 100644 src/ledger.did diff --git a/Makefile b/Makefile index 527dc55..2d40d5d 100644 --- a/Makefile +++ b/Makefile @@ -1,19 +1,14 @@ SRC=src/index.js src/bare-agent.js src/index.html -DID_SRC=src/ledger.did src/governance.did NIX_SRC=default.nix ic-qr-scanner.nix node-env.nix node-packages.nix shell.nix NPM_SRC=webpack.config.js src/simple.min.css OTHER_SRC=src/manifest.json src/favicon.ico src/logo.png -ALL_SRC=$(SRC) $(DID_SRC) $(NIX_SRC) $(NPM_SRC) $(OTHER_SRC) +ALL_SRC=$(SRC) $(NIX_SRC) $(NPM_SRC) $(OTHER_SRC) all: fmt build build: dist/main.bundle.js -#NNS_IFACES ?= $(shell nix-prefetch-url --print-path https://codeload.github.com/dfinity/nns-ifaces/tar.gz/refs/tags/v0.8.1 | tail -n1) -#$(DID_SRC) &: -# cd src && cat $(NNS_IFACES) |tar zx --wildcards --strip-components=1 */ledger.did */governance.did - -dist/index.html dist/main.bundle.js &: $(SRC) $(DID_SRC) $(NPM_SRC) +dist/index.html dist/main.bundle.js &: $(SRC) $(NPM_SRC) npm run-script build fmt: $(SRC) webpack.config.js diff --git a/src/bare-agent.js b/src/bare-agent.js index 0c085b9..8fd3b62 100644 --- a/src/bare-agent.js +++ b/src/bare-agent.js @@ -1,6 +1,7 @@ import { RequestStatusResponseStatus, HttpAgent, + Actor, Cbor, Certificate, requestIdOf, @@ -8,14 +9,6 @@ import { } from "@dfinity/agent"; import { IDL, blobFromText } from "@dfinity/candid"; import { Principal } from "@dfinity/principal"; -import ledger_did from "./ledger.did"; -import governance_did from "./governance.did"; - -/* -import fs from "fs"; -const ledger_did = await fs.readFileSync('./src/ledger.did'); -const governance_did = await fs.readFileSync("./src/governance.did", "utf-8"); -*/ function fromHexString(hexString) { return new Uint8Array( @@ -200,11 +193,6 @@ export async function send_message(message, update_status, sleep) { } } -const canister_did_files = { - "ryjl3-tyaaa-aaaaa-aaaba-cai": ledger_did, - "rrkah-fqaaa-aaaaa-aaaaq-cai": governance_did, -}; - function lookup(dict, name) { for (var i = 0; i < dict.length; i++) { if (dict[i][0] == name) return dict[i][1]; @@ -212,11 +200,56 @@ function lookup(dict, name) { } const CANDID_UI_CANISTER_ID = "a4gq6-oaaaa-aaaab-qaa4q-cai"; +const CANLISTA_CANISTER_ID = "kyhgh-oyaaa-aaaae-qaaha-cai"; + +const getCandidHack_interface = ({ IDL }) => + IDL.Service({ + __get_candid_interface_tmp_hack: IDL.Func([], [IDL.Text], ["query"]), + }); + +const getCandid_interface = ({ IDL }) => + IDL.Service({ + getCandid: IDL.Func( + [IDL.Principal], + [ + IDL.Variant({ + ok: IDL.Text, + err: IDL.Variant({ noCandidFound: IDL.null }), + }), + ], + ["query"] + ), + }); // Try to decode reply using known did files async function try_decode(canister_id, method_name, reply) { try { - let did = canister_did_files[canister_id]; + var did; + // Try fetch i using __get_candid_interface_tmp_hack. + try { + did = await Actor.createActor(getCandidHack_interface, { + agent, + canisterId: canister_id, + }).__get_candid_interface_tmp_hack(); + } catch (e) { + console.log(e); + } + if (!did) { + // Try fetch i from canlista kyhgh-oyaaa-aaaae-qaaha-cai + try { + did = await Actor.createActor(getCandid_interface, { + agent, + canisterId: CANLISTA_CANISTER_ID, + }).getCandid(Principal.fromText(canister_id)); + } catch (e) { + console.log(e); + } + if (did.ok) { + did = did.ok; + } else { + did = null; + } + } if (did) { let result = await agent.query(CANDID_UI_CANISTER_ID, { methodName: "did_to_js", @@ -246,6 +279,8 @@ async function try_decode(canister_id, method_name, reply) { } } } - } catch (err) {} + } catch (err) { + console.log(err); + } return reply; } diff --git a/src/governance.did b/src/governance.did deleted file mode 100644 index 8d850d5..0000000 --- a/src/governance.did +++ /dev/null @@ -1,250 +0,0 @@ -type AccountIdentifier = record { hash : vec nat8 }; -type Action = variant { - ManageNeuron : ManageNeuron; - ExecuteNnsFunction : ExecuteNnsFunction; - RewardNodeProvider : RewardNodeProvider; - SetDefaultFollowees : SetDefaultFollowees; - ManageNetworkEconomics : NetworkEconomics; - ApproveGenesisKyc : ApproveGenesisKyc; - AddOrRemoveNodeProvider : AddOrRemoveNodeProvider; - Motion : Motion; -}; -type AddHotKey = record { new_hot_key : opt principal }; -type AddOrRemoveNodeProvider = record { change : opt Change }; -type Amount = record { e8s : nat64 }; -type ApproveGenesisKyc = record { principals : vec principal }; -type Ballot = record { vote : int32; voting_power : nat64 }; -type BallotInfo = record { vote : int32; proposal_id : opt NeuronId }; -type Change = variant { ToRemove : NodeProvider; ToAdd : NodeProvider }; -type ClaimOrRefreshNeuronFromAccount = record { - controller : opt principal; - memo : nat64; -}; -type ClaimOrRefreshNeuronFromAccountResponse = record { result : opt Result_1 }; -type Command = variant { - Spawn : Spawn; - Split : Split; - Follow : Follow; - Configure : Configure; - RegisterVote : RegisterVote; - DisburseToNeuron : DisburseToNeuron; - MakeProposal : Proposal; - Disburse : Disburse; -}; -type Command_1 = variant { - Error : GovernanceError; - Spawn : SpawnResponse; - Split : SpawnResponse; - Follow : record {}; - Configure : record {}; - RegisterVote : record {}; - DisburseToNeuron : SpawnResponse; - MakeProposal : MakeProposalResponse; - Disburse : DisburseResponse; -}; -type Command_2 = variant { - Spawn : Spawn; - Split : Split; - ClaimOrRefresh : ClaimOrRefreshNeuronFromAccount; - DisburseToNeuron : DisburseToNeuron; - Disburse : Disburse; -}; -type Configure = record { operation : opt Operation }; -type Disburse = record { - to_account : opt AccountIdentifier; - amount : opt Amount; -}; -type DisburseResponse = record { transfer_block_height : nat64 }; -type DisburseToNeuron = record { - dissolve_delay_seconds : nat64; - kyc_verified : bool; - amount_e8s : nat64; - new_controller : opt principal; - nonce : nat64; -}; -type DissolveState = variant { - DissolveDelaySeconds : nat64; - WhenDissolvedTimestampSeconds : nat64; -}; -type ExecuteNnsFunction = record { nns_function : int32; payload : vec nat8 }; -type Follow = record { topic : int32; followees : vec NeuronId }; -type Followees = record { followees : vec NeuronId }; -type Governance = record { - default_followees : vec record { int32; Followees }; - wait_for_quiet_threshold_seconds : nat64; - node_providers : vec NodeProvider; - economics : opt NetworkEconomics; - latest_reward_event : opt RewardEvent; - to_claim_transfers : vec NeuronStakeTransfer; - short_voting_period_seconds : nat64; - proposals : vec record { nat64; ProposalData }; - in_flight_commands : vec record { nat64; NeuronInFlightCommand }; - neurons : vec record { nat64; Neuron }; - genesis_timestamp_seconds : nat64; -}; -type GovernanceError = record { error_message : text; error_type : int32 }; -type IncreaseDissolveDelay = record { - additional_dissolve_delay_seconds : nat32; -}; -type ListNeurons = record { - neuron_ids : vec nat64; - include_neurons_readable_by_caller : bool; -}; -type ListNeuronsResponse = record { - neuron_infos : vec record { nat64; NeuronInfo }; - full_neurons : vec Neuron; -}; -type ListProposalInfo = record { - include_reward_status : vec int32; - before_proposal : opt NeuronId; - limit : nat32; - exclude_topic : vec int32; - include_status : vec int32; -}; -type ListProposalInfoResponse = record { proposal_info : vec ProposalInfo }; -type MakeProposalResponse = record { proposal_id : opt NeuronId }; -type ManageNeuron = record { id : opt NeuronId; command : opt Command }; -type ManageNeuronResponse = record { command : opt Command_1 }; -type Motion = record { motion_text : text }; -type NetworkEconomics = record { - neuron_minimum_stake_e8s : nat64; - max_proposals_to_keep_per_topic : nat32; - neuron_management_fee_per_proposal_e8s : nat64; - reject_cost_e8s : nat64; - transaction_fee_e8s : nat64; - neuron_spawn_dissolve_delay_seconds : nat64; - minimum_icp_xdr_rate : nat64; - maximum_node_provider_rewards_e8s : nat64; -}; -type Neuron = record { - id : opt NeuronId; - controller : opt principal; - recent_ballots : vec BallotInfo; - kyc_verified : bool; - not_for_profit : bool; - maturity_e8s_equivalent : nat64; - cached_neuron_stake_e8s : nat64; - created_timestamp_seconds : nat64; - aging_since_timestamp_seconds : nat64; - hot_keys : vec principal; - account : vec nat8; - dissolve_state : opt DissolveState; - followees : vec record { int32; Followees }; - neuron_fees_e8s : nat64; - transfer : opt NeuronStakeTransfer; -}; -type NeuronId = record { id : nat64 }; -type NeuronInFlightCommand = record { - command : opt Command_2; - timestamp : nat64; -}; -type NeuronInfo = record { - dissolve_delay_seconds : nat64; - recent_ballots : vec BallotInfo; - created_timestamp_seconds : nat64; - state : int32; - retrieved_at_timestamp_seconds : nat64; - voting_power : nat64; - age_seconds : nat64; -}; -type NeuronStakeTransfer = record { - to_subaccount : vec nat8; - neuron_stake_e8s : nat64; - from : opt principal; - memo : nat64; - from_subaccount : vec nat8; - transfer_timestamp : nat64; - block_height : nat64; -}; -type NodeProvider = record { id : opt principal }; -type Operation = variant { - RemoveHotKey : RemoveHotKey; - AddHotKey : AddHotKey; - StopDissolving : record {}; - StartDissolving : record {}; - IncreaseDissolveDelay : IncreaseDissolveDelay; - SetDissolveTimestamp : SetDissolveTimestamp; -}; -type Proposal = record { url : text; action : opt Action; summary : text }; -type ProposalData = record { - id : opt NeuronId; - failure_reason : opt GovernanceError; - ballots : vec record { nat64; Ballot }; - proposal_timestamp_seconds : nat64; - reward_event_round : nat64; - failed_timestamp_seconds : nat64; - reject_cost_e8s : nat64; - latest_tally : opt Tally; - decided_timestamp_seconds : nat64; - proposal : opt Proposal; - proposer : opt NeuronId; - executed_timestamp_seconds : nat64; -}; -type ProposalInfo = record { - id : opt NeuronId; - status : int32; - topic : int32; - failure_reason : opt GovernanceError; - ballots : vec record { nat64; Ballot }; - proposal_timestamp_seconds : nat64; - reward_event_round : nat64; - failed_timestamp_seconds : nat64; - reject_cost_e8s : nat64; - latest_tally : opt Tally; - reward_status : int32; - decided_timestamp_seconds : nat64; - proposal : opt Proposal; - proposer : opt NeuronId; - executed_timestamp_seconds : nat64; -}; -type RegisterVote = record { vote : int32; proposal : opt NeuronId }; -type RemoveHotKey = record { hot_key_to_remove : opt principal }; -type Result = variant { Ok; Err : GovernanceError }; -type Result_1 = variant { Error : GovernanceError; NeuronId : NeuronId }; -type Result_2 = variant { Ok : Neuron; Err : GovernanceError }; -type Result_3 = variant { Ok : NeuronInfo; Err : GovernanceError }; -type RewardEvent = record { - day_after_genesis : nat64; - actual_timestamp_seconds : nat64; - distributed_e8s_equivalent : nat64; - settled_proposals : vec NeuronId; -}; -type RewardMode = variant { - RewardToNeuron : RewardToNeuron; - RewardToAccount : RewardToAccount; -}; -type RewardNodeProvider = record { - node_provider : opt NodeProvider; - reward_mode : opt RewardMode; - amount_e8s : nat64; -}; -type RewardToAccount = record { to_account : opt AccountIdentifier }; -type RewardToNeuron = record { dissolve_delay_seconds : nat64 }; -type SetDefaultFollowees = record { - default_followees : vec record { int32; Followees }; -}; -type SetDissolveTimestamp = record { dissolve_timestamp_seconds : nat64 }; -type Spawn = record { new_controller : opt principal }; -type SpawnResponse = record { created_neuron_id : opt NeuronId }; -type Split = record { amount_e8s : nat64 }; -type Tally = record { - no : nat64; - yes : nat64; - total : nat64; - timestamp_seconds : nat64; -}; -service : (Governance) -> { - claim_gtc_neurons : (principal, vec NeuronId) -> (Result); - claim_or_refresh_neuron_from_account : (ClaimOrRefreshNeuronFromAccount) -> ( - ClaimOrRefreshNeuronFromAccountResponse, - ); - get_full_neuron : (nat64) -> (Result_2) query; - get_neuron_ids : () -> (vec nat64) query; - get_neuron_info : (nat64) -> (Result_3) query; - get_pending_proposals : () -> (vec ProposalInfo) query; - get_proposal_info : (nat64) -> (opt ProposalInfo) query; - list_neurons : (ListNeurons) -> (ListNeuronsResponse) query; - list_proposals : (ListProposalInfo) -> (ListProposalInfoResponse) query; - manage_neuron : (ManageNeuron) -> (ManageNeuronResponse); - transfer_gtc_neuron : (NeuronId, NeuronId) -> (Result); -} diff --git a/src/ledger.did b/src/ledger.did deleted file mode 100644 index 1c1de60..0000000 --- a/src/ledger.did +++ /dev/null @@ -1,85 +0,0 @@ -type AccountBalanceArgs = record { - account: AccountIdentifier; -}; -type AccountIdentifier = text; -type ArchiveOptions = record { - node_max_memory_size_bytes: opt nat32; - max_message_size_bytes: opt nat32; - controller_id: principal; -}; -type CanisterId = principal; -type BlockHeight = nat64; -type Duration = record { - secs: nat64; - nanos: nat32; -}; -type ICPTs = record { - e8s : nat64; -}; -type LedgerCanisterInitPayload = record { - minting_account: AccountIdentifier; - initial_values: vec record {AccountIdentifier; ICPTs}; - max_message_size_bytes: opt nat32; - transaction_window: opt Duration; - archive_options: opt ArchiveOptions; - send_whitelist: vec record {principal}; -}; -type Memo = nat64; -type NotifyCanisterArgs = record { - block_height: BlockHeight; - max_fee: ICPTs; - from_subaccount: opt SubAccount; - to_canister: principal; - to_subaccount: opt SubAccount; -}; -type SendArgs = record { - memo: Memo; - amount: ICPTs; - fee: ICPTs; - from_subaccount: opt SubAccount; - to: AccountIdentifier; - created_at_time: opt TimeStamp; -}; -type SubAccount = vec nat8; -type TimeStamp = record { - timestamp_nanos: nat64; -}; -type Transaction = record { - transfer: Transfer; - memo: Memo; - created_at: BlockHeight; -}; -type Transfer = variant { - Burn: record { - from: AccountIdentifier; - amount: ICPTs; - }; - Mint: record { - to: AccountIdentifier; - amount: ICPTs; - }; - Send: record { - from: AccountIdentifier; - to: AccountIdentifier; - amount: ICPTs; - }; -}; -type HeaderField = record {text; text}; -type HttpRequest = record { - url: text; - method: text; - body: vec nat8; - headers: vec HeaderField; -}; -type HttpResponse = record { - body: vec nat8; - headers: vec HeaderField; - status_code: nat16; -}; -service: (LedgerCanisterInitPayload) -> { - send_dfx : (SendArgs) -> (BlockHeight); - notify_dfx: (NotifyCanisterArgs) -> (); - account_balance_dfx : (AccountBalanceArgs) -> (ICPTs) query; - get_nodes : () -> (vec CanisterId) query; - http_request: (HttpRequest) -> (HttpResponse) query; -}