Skip to content

Commit

Permalink
get staked voting power from garden
Browse files Browse the repository at this point in the history
  • Loading branch information
ZenVoich committed Sep 19, 2023
1 parent c6076c0 commit 19ddf2f
Show file tree
Hide file tree
Showing 10 changed files with 369 additions and 10 deletions.
2 changes: 1 addition & 1 deletion dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"candid": "garden/.dfx/local/canisters/main/main.did",
"remote": {
"id": {
"ic": "22cl3-kqaaa-aaaaf-add7q-cai"
"ic": "afcvu-dyaaa-aaaap-qboqq-cai"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"create:btcflower": "dfx canister create btcflower --specified-id pk6rk-6aaaa-aaaae-qaazq-cai",
"create:ethflower": "dfx canister create ethflower --specified-id dhiaa-ryaaa-aaaae-qabva-cai",
"create:icpflower": "dfx canister create icpflower --specified-id 4ggk4-mqaaa-aaaae-qad6q-cai",
"create:garden": "dfx canister create garden --specified-id 22cl3-kqaaa-aaaaf-add7q-cai",
"create:garden": "dfx canister create garden --specified-id afcvu-dyaaa-aaaap-qboqq-cai",
"deploy": "npm run build-power-equalizer && npm run build-garden && npm run deploy:dao && npm run deploy:btcflower && npm run deploy:ethflower && npm run deploy:icpflower && npm run deploy:garden",
"deploy:dao": "DFX_MOC_PATH=\"$(vessel bin)/moc\" dfx build dao && dfx generate dao && dfx deploy dao --argument '(true, vec {principal \"zvkal-dnnsd-syh57-zvwzw-3aa6g-nt4vz-2ncib-dqfd4-oaisq-xhv6y-eae\"})' --mode reinstall --yes && npm run test-proposals",
"deploy:btcflower": "dfx deploy btcflower --argument \"$(cat btcflower-initArgs.did)\" --mode reinstall --yes && dfx canister call btcflower initMint && dfx canister call btcflower shuffleTokensForSale && dfx canister call btcflower airdropTokens && dfx canister call btcflower enableSale",
Expand Down
10 changes: 9 additions & 1 deletion src/backend/Main.mo
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ shared (install) actor class DAO(isLocal : Bool, coreTeamPrincipals : [Principal
if (args.option >= proposal.options.size()) {
return #err("Proposal " # debug_show (args.proposalId) # " does not have an option " # debug_show (args.option));
};

let stakedVotingPower = await getUserStakedVotingPower(caller);

switch (await getFlowersFrom(caller)) {
case (#err(error)) { return #err(error) };
case (#ok({ btcFlowers : [Nat32]; ethFlowers : [Nat32]; icpFlowers : [Nat32] })) {
Expand All @@ -308,7 +311,7 @@ shared (install) actor class DAO(isLocal : Bool, coreTeamPrincipals : [Principal
};

// get the amount of flowers and thus voting power a holder has
let votingPower : Nat = (btcFlowers.size() * 2) + ethFlowers.size() + icpFlowers.size();
let votingPower : Nat = (btcFlowers.size() * 2) + ethFlowers.size() + icpFlowers.size() + stakedVotingPower;

// track flowers that were used to cast a vote
let btcFlowersVoted = List.append(List.fromArray<Nat32>(btcFlowers), proposal.flowersVoted.btcFlowers);
Expand Down Expand Up @@ -449,6 +452,11 @@ shared (install) actor class DAO(isLocal : Bool, coreTeamPrincipals : [Principal
};
};

func getUserStakedVotingPower(principal : Principal) : async Nat {
var garden = actor ("afcvu-dyaaa-aaaap-qboqq-cai") : actor { getUserVotingPower : (Principal) -> async Nat };
await garden.getUserVotingPower(principal);
};

/// Remove expired proposals
func closeExpiredProposals() {
for (kv in Trie.iter(proposalsV3)) {
Expand Down
50 changes: 50 additions & 0 deletions src/declarations/garden/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import type {
ActorSubclass,
HttpAgentOptions,
ActorConfig,
Agent,
} from "@dfinity/agent";
import type { Principal } from "@dfinity/principal";
import type { IDL } from "@dfinity/candid";

import { _SERVICE } from './main.did';

export declare const idlFactory: IDL.InterfaceFactory;
export declare const canisterId: string;

export declare interface CreateActorOptions {
/**
* @see {@link Agent}
*/
agent?: Agent;
/**
* @see {@link HttpAgentOptions}
*/
agentOptions?: HttpAgentOptions;
/**
* @see {@link ActorConfig}
*/
actorOptions?: ActorConfig;
}

/**
* Intializes an {@link ActorSubclass}, configured with the provided SERVICE interface of a canister.
* @constructs {@link ActorSubClass}
* @param {string | Principal} canisterId - ID of the canister the {@link Actor} will talk to
* @param {CreateActorOptions} options - see {@link CreateActorOptions}
* @param {CreateActorOptions["agent"]} options.agent - a pre-configured agent you'd like to use. Supercedes agentOptions
* @param {CreateActorOptions["agentOptions"]} options.agentOptions - options to set up a new agent
* @see {@link HttpAgentOptions}
* @param {CreateActorOptions["actorOptions"]} options.actorOptions - options for the Actor
* @see {@link ActorConfig}
*/
export declare const createActor: (
canisterId: string | Principal,
options?: CreateActorOptions
) => ActorSubclass<_SERVICE>;

/**
* Intialized Actor using default settings, ready to talk to a canister using its candid interface
* @constructs {@link ActorSubClass}
*/
export declare const main: ActorSubclass<_SERVICE>;
47 changes: 47 additions & 0 deletions src/declarations/garden/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Actor, HttpAgent } from "@dfinity/agent";

// Imports and re-exports candid interface
import { idlFactory } from "./main.did.js";
export { idlFactory } from "./main.did.js";

/* CANISTER_ID is replaced by webpack based on node environment
* Note: canister environment variable will be standardized as
* process.env.CANISTER_ID_<CANISTER_NAME_UPPERCASE>
* beginning in dfx 0.15.0
*/
export const canisterId =
process.env.CANISTER_ID_GARDEN ||
process.env.GARDEN_CANISTER_ID;

export const createActor = (canisterId, options = {}) => {
const agent = options.agent || new HttpAgent({ ...options.agentOptions });

if (options.agent && options.agentOptions) {
console.warn(
"Detected both agent and agentOptions passed to createActor. Ignoring agentOptions and proceeding with the provided agent."
);
}

// Fetch root key for certificate validation during development
if (process.env.DFX_NETWORK !== "ic") {
agent.fetchRootKey().catch((err) => {
console.warn(
"Unable to fetch root key. Check to ensure that your local replica is running"
);
console.error(err);
});
}

// Creates an actor with using the candid interface and the HttpAgent
return Actor.createActor(idlFactory, {
agent,
canisterId,
...options.actorOptions,
});
};

/**
* A ready-to-use agent for the garden canister
* @type {import("@dfinity/agent").ActorSubclass<import("./main.did.js")._SERVICE>}
*/
export const main = createActor(canisterId);
77 changes: 77 additions & 0 deletions src/declarations/garden/main.did
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
type anon_class_10_1 =
service {
claimRewards: (NeuronId, Account) -> (Result_1);
disburseNeuron: (NeuronId, Account) -> (Result_1);
dissolveNeuron: (NeuronId) -> (Result_1);
getCallerNeurons: () -> (vec Neuron) query;
getStakingAccount: (nat16) -> (Account) query;
getUserVotingPower: (principal) -> (nat) query;
restake: (NeuronId) -> (Result_1);
stake: (nat16) -> (Result);
};
type TokenIndex = nat;
type Time = int;
type Result_1 =
variant {
err: text;
ok;
};
type Result =
variant {
err: text;
ok: NeuronId;
};
type NeuronId = nat;
type Neuron =
record {
createdAt: Time;
dissolveState: DissolveState;
flowers: vec Flower;
id: nat;
prevRewardTime: Time;
rewards: nat;
stakedAt: Time;
stakingAccount: Account;
totalRewards: nat;
userId: principal;
};
type InitArgs =
record {
dailyRewards: record {
btcFlower: nat;
ethFlower: nat;
icpFlower: nat;
};
rewardInterval: Duration;
stakePeriod: Duration;
};
type Flower =
record {
collection: Collection;
tokenIndex: TokenIndex;
};
type Duration =
variant {
days: nat;
hours: nat;
minutes: nat;
nanoseconds: nat;
seconds: nat;
};
type DissolveState =
variant {
DissolveDelay: Time;
DissolveTimestamp: Time;
};
type Collection =
variant {
BTCFlower;
ETHFlower;
ICPFlower;
};
type Account =
record {
owner: principal;
subaccount: opt vec nat8;
};
service : (principal, InitArgs) -> anon_class_10_1
57 changes: 57 additions & 0 deletions src/declarations/garden/main.did.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type { Principal } from '@dfinity/principal';
import type { ActorMethod } from '@dfinity/agent';

export interface Account {
'owner' : Principal,
'subaccount' : [] | [Uint8Array | number[]],
}
export type Collection = { 'BTCFlower' : null } |
{ 'ICPFlower' : null } |
{ 'ETHFlower' : null };
export type DissolveState = { 'DissolveDelay' : Time } |
{ 'DissolveTimestamp' : Time };
export type Duration = { 'nanoseconds' : bigint } |
{ 'hours' : bigint } |
{ 'days' : bigint } |
{ 'minutes' : bigint } |
{ 'seconds' : bigint };
export interface Flower { 'collection' : Collection, 'tokenIndex' : TokenIndex }
export interface InitArgs {
'rewardInterval' : Duration,
'stakePeriod' : Duration,
'dailyRewards' : {
'btcFlower' : bigint,
'icpFlower' : bigint,
'ethFlower' : bigint,
},
}
export interface Neuron {
'id' : bigint,
'prevRewardTime' : Time,
'stakedAt' : Time,
'userId' : Principal,
'createdAt' : Time,
'totalRewards' : bigint,
'rewards' : bigint,
'dissolveState' : DissolveState,
'stakingAccount' : Account,
'flowers' : Array<Flower>,
}
export type NeuronId = bigint;
export type Result = { 'ok' : NeuronId } |
{ 'err' : string };
export type Result_1 = { 'ok' : null } |
{ 'err' : string };
export type Time = bigint;
export type TokenIndex = bigint;
export interface anon_class_10_1 {
'claimRewards' : ActorMethod<[NeuronId, Account], Result_1>,
'disburseNeuron' : ActorMethod<[NeuronId, Account], Result_1>,
'dissolveNeuron' : ActorMethod<[NeuronId], Result_1>,
'getCallerNeurons' : ActorMethod<[], Array<Neuron>>,
'getStakingAccount' : ActorMethod<[number], Account>,
'getUserVotingPower' : ActorMethod<[Principal], bigint>,
'restake' : ActorMethod<[NeuronId], Result_1>,
'stake' : ActorMethod<[number], Result>,
}
export interface _SERVICE extends anon_class_10_1 {}
82 changes: 82 additions & 0 deletions src/declarations/garden/main.did.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
export const idlFactory = ({ IDL }) => {
const Duration = IDL.Variant({
'nanoseconds' : IDL.Nat,
'hours' : IDL.Nat,
'days' : IDL.Nat,
'minutes' : IDL.Nat,
'seconds' : IDL.Nat,
});
const InitArgs = IDL.Record({
'rewardInterval' : Duration,
'stakePeriod' : Duration,
'dailyRewards' : IDL.Record({
'btcFlower' : IDL.Nat,
'icpFlower' : IDL.Nat,
'ethFlower' : IDL.Nat,
}),
});
const NeuronId = IDL.Nat;
const Account = IDL.Record({
'owner' : IDL.Principal,
'subaccount' : IDL.Opt(IDL.Vec(IDL.Nat8)),
});
const Result_1 = IDL.Variant({ 'ok' : IDL.Null, 'err' : IDL.Text });
const Time = IDL.Int;
const DissolveState = IDL.Variant({
'DissolveDelay' : Time,
'DissolveTimestamp' : Time,
});
const Collection = IDL.Variant({
'BTCFlower' : IDL.Null,
'ICPFlower' : IDL.Null,
'ETHFlower' : IDL.Null,
});
const TokenIndex = IDL.Nat;
const Flower = IDL.Record({
'collection' : Collection,
'tokenIndex' : TokenIndex,
});
const Neuron = IDL.Record({
'id' : IDL.Nat,
'prevRewardTime' : Time,
'stakedAt' : Time,
'userId' : IDL.Principal,
'createdAt' : Time,
'totalRewards' : IDL.Nat,
'rewards' : IDL.Nat,
'dissolveState' : DissolveState,
'stakingAccount' : Account,
'flowers' : IDL.Vec(Flower),
});
const Result = IDL.Variant({ 'ok' : NeuronId, 'err' : IDL.Text });
const anon_class_10_1 = IDL.Service({
'claimRewards' : IDL.Func([NeuronId, Account], [Result_1], []),
'disburseNeuron' : IDL.Func([NeuronId, Account], [Result_1], []),
'dissolveNeuron' : IDL.Func([NeuronId], [Result_1], []),
'getCallerNeurons' : IDL.Func([], [IDL.Vec(Neuron)], ['query']),
'getStakingAccount' : IDL.Func([IDL.Nat16], [Account], ['query']),
'getUserVotingPower' : IDL.Func([IDL.Principal], [IDL.Nat], ['query']),
'restake' : IDL.Func([NeuronId], [Result_1], []),
'stake' : IDL.Func([IDL.Nat16], [Result], []),
});
return anon_class_10_1;
};
export const init = ({ IDL }) => {
const Duration = IDL.Variant({
'nanoseconds' : IDL.Nat,
'hours' : IDL.Nat,
'days' : IDL.Nat,
'minutes' : IDL.Nat,
'seconds' : IDL.Nat,
});
const InitArgs = IDL.Record({
'rewardInterval' : Duration,
'stakePeriod' : Duration,
'dailyRewards' : IDL.Record({
'btcFlower' : IDL.Nat,
'icpFlower' : IDL.Nat,
'ethFlower' : IDL.Nat,
}),
});
return [IDL.Principal, InitArgs];
};
Loading

0 comments on commit 19ddf2f

Please sign in to comment.