Skip to content

Commit

Permalink
Merge pull request #31 from hollow-leaf/feat/addReset
Browse files Browse the repository at this point in the history
feat: add reset & remove access
  • Loading branch information
Pianochicken authored Aug 25, 2023
2 parents 8ed4c20 + 107f412 commit e6dda5c
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
16 changes: 14 additions & 2 deletions packages/tezos-contracts/vote.jsligo
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ type storage = {
type parameter =
| ["Vote", int]
| ["Access"]
| ["Reset"]

type return_ = [list <operation>, storage];

/* Two entrypoints */
/* entrypoints */
const voteProposal = ([store, vote] : [storage, int]) : storage => {

let temp_store = store;
Expand All @@ -30,14 +31,25 @@ const accessProposal = (store: storage) : storage => {
return result;
}

const resetProposal = (store: storage) : storage => {
let temp_store = store;

temp_store.yes = 0;
temp_store.no = 0;

const result = temp_store;
return result;
}

/* Main access point that dispatches to the entrypoints according to
the smart contract parameter. */
const main = ([action, store] : [parameter, storage]) : return_ => {
return [
(list([]) as list <operation>), // No operations
(list([]) as list <operation>),
(match (action, {
Vote: (n: int) => voteProposal([store, n]),
Access: () => accessProposal(store),
Reset: () => resetProposal(store),
}))
]
};
61 changes: 33 additions & 28 deletions packages/tezos-interact/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
import { TezosToolkit } from '@taquito/taquito';
import { InMemorySigner, importKey } from '@taquito/signer';
import { InMemorySigner } from '@taquito/signer';

const Tezos = new TezosToolkit('https://ghostnet.tezos.marigold.dev');

Tezos.setProvider({
signer: new InMemorySigner('edskS1zsQPHwdhuQQ9dcvtmrgSC1f7pqMZMrst4xzB4hMgkmAzgnDHbRhHGckG7RXN6yoed1PE31S63z2Y7Pa13kvZcQgcQ8bU'),
});

interface ProposalType {
[index: number]: string;
}

const proposal: ProposalType = {
1: 'KT1ExMeV39XPUEyARJYwjUMfC1Lq1PLwo8bh',
2: 'KT1KnNfPjgnn7jznstBZ7FXvwBEH5RwjLafK',
3: 'KT1S9iHKShcrkJLZMTeeEcqgKZ3gQQgmcKBg'
}

export const accessProposal = async () => {
export const voteProposal = async (proposalNumber: number, vote: number) => {
if(proposalNumber > 3 || proposalNumber < 1) {
return Error
}

await Tezos.contract
.at('KT1KqDtnTZMYiEpe81f6u24Jnmq5Hg2h9z5v')
.at(proposal[proposalNumber])
.then((contract) => {
console.log('Access proposal...')
return contract.methods.access().send();
console.log('Vote proposal...')
return contract.methods.vote(vote).send();
})
.then((op) => {
const result: any = op.results[0]
console.log(`Waiting for ${op.hash} to be confirmed...`);
return op.confirmation(1).then(() => op.hash);
})
.then((hash) => {
console.log(`Access operation finished: https://better-call.dev/ghostnet/opg/${hash}`)
console.log(`Vote finished: https://better-call.dev/ghostnet/opg/${hash}`)
})
.catch((error) => {
console.log(`Access error: ${JSON.stringify(error, null, 2)}`)
})
}
console.log(`Vote error: ${JSON.stringify(error, null, 2)}`)
});
}

export const resetProposal = async (proposalNumber: number) => {
if(proposalNumber > 3 || proposalNumber < 1) {
return Error
}

export const voteProposal = async (vote: number) => {
await Tezos.contract
.at('KT1KqDtnTZMYiEpe81f6u24Jnmq5Hg2h9z5v')
.at(proposal[proposalNumber])
.then((contract) => {
console.log('Vote proposal...')
return contract.methods.vote(vote).send();
console.log('Reset proposal...')
return contract.methods.reset().send();
})
.then((op) => {
console.log(`Waiting for ${op.hash} to be confirmed...`);
return op.confirmation(1).then(() => op.hash);
})
.then((hash) => {
console.log(`Vote finished: https://better-call.dev/ghostnet/opg/${hash}`)
console.log(`Reset operation finished: https://better-call.dev/ghostnet/opg/${hash}`)
})
.catch((error) => {
console.log(`Vote error: ${JSON.stringify(error, null, 2)}`)
});
}

export const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))

const main = async () => {
// await voteProposal(1)
// sleep(15000)
// await accessProposal()
console.log(`Reset error: ${JSON.stringify(error, null, 2)}`)
})
}

main()

6 changes: 3 additions & 3 deletions packages/tezos-interact/src/test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { voteProposal, accessProposal } from './index';
import { voteProposal, resetProposal } from './index';

const main = async () => {
await voteProposal(1)
await accessProposal()
await voteProposal(1, 1)
await resetProposal(1)
}

main()

0 comments on commit e6dda5c

Please sign in to comment.