-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add dao module in tfchain client and tfgrid client (#1089)
* add dao module in tfchain client and tfgrid client * add dao module * add DaoVoteModel * add dao to tfgrid modules * add dao vote and get scripts * add vote to tfchain module in gridclient * Update config.json * fix naming * remove type any * remove unnecessary file * remove dao as it is on parent class * remove unnecessary import * fix types * fix farmId type * remove hex2a method * remove nonce * add init in vote script * fix hash type, add min(1) to farmId * remove nowblock any type * change hash function parameter * add return types * add patchExtrinsic * fix hashes type * update types casting, add apply() to vote call
- Loading branch information
1 parent
885bada
commit a1a475e
Showing
10 changed files
with
242 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { getClient } from "./client_loader"; | ||
|
||
async function main() { | ||
const grid3 = await getClient(); | ||
const proposals = await grid3.dao.get(); | ||
console.log(proposals); | ||
await grid3.disconnect(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { getClient } from "../client_loader"; | ||
|
||
async function main() { | ||
const grid3 = await getClient(); | ||
await grid3.tfchain.init({ name: "harby", secret: grid3.clientOptions.mnemonic }); | ||
await grid3.tfchain.vote({ | ||
name: "harby", | ||
address: "5FWW1F7XHaiRgPEqJdkv9nVgz94AVKXkTKNyfbLcY4rqpaNM", | ||
farmId: 246, | ||
hash: "0xa539b59dcf7ba10764a49c9effb88aea400d3c20f0071c3b85494423079757fe", | ||
approve: true, | ||
}); | ||
|
||
await grid3.disconnect(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { TFClient } from "../clients/tf-grid/client"; | ||
import { GridClientConfig } from "../config"; | ||
import { expose } from "../helpers/expose"; | ||
import { validateInput } from "../helpers/validator"; | ||
import { DaoVoteModel } from "./models"; | ||
class Dao { | ||
client: TFClient; | ||
constructor(config: GridClientConfig) { | ||
this.client = new TFClient(config.substrateURL, config.mnemonic, config.storeSecret, config.keypairType); | ||
} | ||
@expose | ||
@validateInput | ||
async get() { | ||
return await this.client.dao.get(); | ||
} | ||
@expose | ||
@validateInput | ||
async vote(options: DaoVoteModel) { | ||
return (await this.client.dao.vote(options)).apply(); | ||
} | ||
} | ||
export { Dao as dao }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import moment from "moment"; | ||
|
||
import { Client, QueryClient } from "./client"; | ||
import { checkConnection } from "./utils"; | ||
interface Proposal { | ||
threshold: number; | ||
ayes: AyesAndNayes[]; | ||
nayes: AyesAndNayes[]; | ||
vetos: number; | ||
end: moment.Moment; | ||
hash: string; | ||
action: string; | ||
description: string; | ||
link: string; | ||
ayesProgress: number; | ||
nayesProgress: number; | ||
} | ||
interface AyesAndNayes { | ||
farmId: number; | ||
weight: number; | ||
} | ||
interface ProposalRemark { | ||
args: { remark: string }; | ||
} | ||
interface DaoProposal { | ||
description: string; | ||
link: string; | ||
} | ||
interface ProposalVotes { | ||
ayes: AyesAndNayes[]; | ||
nays: AyesAndNayes[]; | ||
threshold: number; | ||
vetos: number; | ||
end: number; | ||
} | ||
export interface Proposals { | ||
active: Proposal[]; | ||
inactive: Proposal[]; | ||
} | ||
export interface DaoVoteOptions { | ||
address: string; | ||
farmId: number; | ||
hash: string; | ||
approve: boolean; | ||
} | ||
|
||
class QueryDao { | ||
constructor(public client: QueryClient) { | ||
this.client = client; | ||
} | ||
|
||
@checkConnection | ||
async get(): Promise<Proposals> { | ||
const hashesJson = await this.client.api.query.dao.proposalList(); | ||
const hashes = hashesJson.toPrimitive() as string[]; | ||
const activeProposals: Proposal[] = []; | ||
const inactiveProposals: Proposal[] = []; | ||
for await (const hash of hashes) { | ||
const daoProposal = await this.getDaoProposal(hash); | ||
const proposal = await this.getProposal(hash); | ||
const proposalVotes = await this.getProposalVotes(hash); | ||
|
||
const nowBlock: number = +(await this.client.api.query.system.number()); | ||
const timeUntilEnd = (proposalVotes.end - nowBlock) * 6; | ||
if (proposal && daoProposal) { | ||
if (proposalVotes.end < nowBlock) { | ||
inactiveProposals.push({ | ||
threshold: proposalVotes.threshold, | ||
ayes: proposalVotes.ayes, //[{farmId: number, weight: number}] | ||
nayes: proposalVotes.nays, | ||
vetos: proposalVotes.vetos, | ||
end: moment().add(timeUntilEnd, "second"), | ||
hash: hash, | ||
action: proposal.args.remark, | ||
description: daoProposal.description, | ||
link: daoProposal.link, | ||
ayesProgress: this.getProgress(proposalVotes.ayes, proposalVotes.nays, true), | ||
nayesProgress: this.getProgress(proposalVotes.ayes, proposalVotes.nays, false), | ||
}); | ||
} else { | ||
activeProposals.push({ | ||
threshold: proposalVotes.threshold, | ||
ayes: proposalVotes.ayes, //[{farmId: number, weight: number}] | ||
nayes: proposalVotes.nays, | ||
vetos: proposalVotes.vetos, | ||
end: moment().add(timeUntilEnd, "second"), | ||
hash: hash, | ||
action: proposal.args.remark, | ||
description: daoProposal.description, | ||
link: daoProposal.link, | ||
ayesProgress: this.getProgress(proposalVotes.ayes, proposalVotes.nays, true), | ||
nayesProgress: this.getProgress(proposalVotes.ayes, proposalVotes.nays, false), | ||
}); | ||
} | ||
} | ||
} | ||
return { active: activeProposals, inactive: inactiveProposals }; | ||
} | ||
private getVotesWithWeights(votes: AyesAndNayes[]): number { | ||
return votes.reduce((total: number, vote: AyesAndNayes) => { | ||
return total + vote.weight; | ||
}, 0); | ||
} | ||
private getProgress(ayes: AyesAndNayes[], nayes: AyesAndNayes[], typeAye: boolean): number { | ||
const totalAyeWeight = ayes ? this.getVotesWithWeights(ayes) : 0; | ||
const totalNayeWeight = nayes ? this.getVotesWithWeights(nayes) : 0; | ||
const total = totalAyeWeight + totalNayeWeight; | ||
if (total > 0) { | ||
if (typeAye) { | ||
return (totalAyeWeight / total) * 100; | ||
} | ||
return (totalNayeWeight / total) * 100; | ||
} | ||
return 0; | ||
} | ||
@checkConnection | ||
private async getDaoProposal(hash: string): Promise<DaoProposal> { | ||
const proposalJson = await this.client.api.query.dao.proposals(hash); | ||
const proposal: DaoProposal = proposalJson.toPrimitive() as unknown as DaoProposal; | ||
return proposal; | ||
} | ||
|
||
@checkConnection | ||
private async getProposal(hash: string): Promise<ProposalRemark | undefined> { | ||
try { | ||
const proposalJson = await this.client.api.query.dao.proposalOf(hash); | ||
const proposalRemark: ProposalRemark = proposalJson.toPrimitive() as unknown as ProposalRemark; | ||
return proposalRemark; | ||
} catch (error) { | ||
console.warn("Couldn't decode a proposal"); | ||
} | ||
} | ||
@checkConnection | ||
private async getProposalVotes(hash: string): Promise<ProposalVotes> { | ||
const votesJson = await this.client.api.query.dao.voting(hash); | ||
const proposalVotes: ProposalVotes = votesJson.toPrimitive() as unknown as ProposalVotes; | ||
return proposalVotes; | ||
} | ||
} | ||
class Dao extends QueryDao { | ||
constructor(public client: Client) { | ||
super(client); | ||
this.client = client; | ||
} | ||
@checkConnection | ||
async vote(options: DaoVoteOptions) { | ||
const extrinsic = this.client.api.tx.dao.vote(options.farmId, options.hash, options.approve); | ||
return this.client.patchExtrinsic(extrinsic); | ||
} | ||
} | ||
export { Dao, QueryDao }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters