Skip to content

Commit

Permalink
fix: add getSuperProposal function
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Kim committed Jun 28, 2022
1 parent 1a70f02 commit 6cdaa3e
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 58 deletions.
2 changes: 1 addition & 1 deletion docs/assets/search.js

Large diffs are not rendered by default.

70 changes: 37 additions & 33 deletions docs/classes/Pod.html

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions docs/classes/Proposal.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ <h2>Test Scripts</h2>
<p>We have some test scripts to test approve/reject proposals and super proposals. They can be executed
by duplicating the <code>env-examples.json</code> with private keys in <code>env.json</code>, and executing the
transactions with <code>npx ts-node ./scripts/reject-superproposal.ts</code>.</p>
</div></div><div class="col-4 col-menu menu-sticky-wrap menu-highlight"><nav class="tsd-navigation primary"><ul><li class="current"><a href="modules.html">Exports</a></li></ul></nav><nav class="tsd-navigation secondary menu-sticky"><ul><li class="tsd-kind-class"><a href="classes/Pod.html" class="tsd-kind-icon">Pod</a></li><li class="tsd-kind-class"><a href="classes/Proposal.html" class="tsd-kind-icon">Proposal</a></li><li class="tsd-kind-type-alias"><a href="modules.html#ProposalStatus" class="tsd-kind-icon">Proposal<wbr/>Status</a></li><li class="tsd-kind-variable"><a href="modules.html#config" class="tsd-kind-icon">config</a></li><li class="tsd-kind-function"><a href="modules.html#getAdminPods" class="tsd-kind-icon">get<wbr/>Admin<wbr/>Pods</a></li><li class="tsd-kind-function"><a href="modules.html#getPod" class="tsd-kind-icon">get<wbr/>Pod</a></li><li class="tsd-kind-function"><a href="modules.html#getUserPods" class="tsd-kind-icon">get<wbr/>User<wbr/>Pods</a></li><li class="tsd-kind-function"><a href="modules.html#init" class="tsd-kind-icon">init</a></li></ul></nav></div></div></div><footer class="with-border-bottom"><div class="container"><h2>Legend</h2><div class="tsd-legend-group"><ul class="tsd-legend"><li class="tsd-kind-constructor tsd-parent-kind-class"><span class="tsd-kind-icon">Constructor</span></li><li class="tsd-kind-property tsd-parent-kind-class"><span class="tsd-kind-icon">Property</span></li><li class="tsd-kind-method tsd-parent-kind-class"><span class="tsd-kind-icon">Method</span></li></ul></div><h2>Settings</h2><p>Theme <select id="theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></p></div></footer><div class="container tsd-generator"><p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></div><div class="overlay"></div><script src="assets/main.js"></script></body></html>
</div></div><div class="col-4 col-menu menu-sticky-wrap menu-highlight"><nav class="tsd-navigation primary"><ul><li class="current"><a href="modules.html">Exports</a></li></ul></nav><nav class="tsd-navigation secondary menu-sticky"><ul><li class="tsd-kind-class"><a href="classes/Pod.html" class="tsd-kind-icon">Pod</a></li><li class="tsd-kind-class"><a href="classes/Proposal.html" class="tsd-kind-icon">Proposal</a></li><li class="tsd-kind-type-alias"><a href="modules.html#ProposalStatus" class="tsd-kind-icon">Proposal<wbr/>Status</a></li><li class="tsd-kind-variable"><a href="modules.html#config" class="tsd-kind-icon">config</a></li><li class="tsd-kind-function"><a href="modules.html#getAdminPods" class="tsd-kind-icon">get<wbr/>Admin<wbr/>Pods</a></li><li class="tsd-kind-function"><a href="modules.html#getPod" class="tsd-kind-icon">get<wbr/>Pod</a></li><li class="tsd-kind-function"><a href="modules.html#getSuperProposal" class="tsd-kind-icon">get<wbr/>Super<wbr/>Proposal</a></li><li class="tsd-kind-function"><a href="modules.html#getUserPods" class="tsd-kind-icon">get<wbr/>User<wbr/>Pods</a></li><li class="tsd-kind-function"><a href="modules.html#init" class="tsd-kind-icon">init</a></li></ul></nav></div></div></div><footer class="with-border-bottom"><div class="container"><h2>Legend</h2><div class="tsd-legend-group"><ul class="tsd-legend"><li class="tsd-kind-constructor tsd-parent-kind-class"><span class="tsd-kind-icon">Constructor</span></li><li class="tsd-kind-property tsd-parent-kind-class"><span class="tsd-kind-icon">Property</span></li><li class="tsd-kind-method tsd-parent-kind-class"><span class="tsd-kind-icon">Method</span></li></ul></div><h2>Settings</h2><p>Theme <select id="theme"><option value="os">OS</option><option value="light">Light</option><option value="dark">Dark</option></select></p></div></footer><div class="container tsd-generator"><p>Generated using <a href="https://typedoc.org/" target="_blank">TypeDoc</a></p></div><div class="overlay"></div><script src="assets/main.js"></script></body></html>
16 changes: 10 additions & 6 deletions docs/modules.html

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions src/Pod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
createRejectTransaction,
getSafeTransactionsBySafe,
populateDataDecoded,
getSafeTransactionByHash,
} from './lib/services/transaction-service';
import { createSafeTransaction } from './lib/services/create-safe-transaction';
import Proposal from './Proposal';
Expand Down Expand Up @@ -264,6 +265,7 @@ export default class Pod {
}
// If the length is 2, that means there is a paired reject transaction.
// The reject transaction is always created after the approve transaction
// TODO: I'm actually not sure if that assumption is true anymore.
// Because safeTx comes in reverse chron order, subTxPair[1] is the approve, [0] is the reject
return new Proposal(this, nonce, subTxPair[1], subTxPair[0]);
});
Expand All @@ -288,6 +290,27 @@ export default class Pod {
.slice(0, limit); // Slice to return the requested limited Proposals.
};

/**
* Gets a specific proposal by either nonce or the safeTxHash.
* @param identifier - Can be either the proposal id/nonce (preferred), or the safeTxHash
*/
getProposal = async (identifier: number | string): Promise<Proposal> => {
let nonce: number;
if (typeof identifier === 'number') nonce = identifier;
else {
const safeTransaction = await getSafeTransactionByHash(identifier);
nonce = safeTransaction.nonce;
}

// All safe transactions that have a given nonce.
const safeTransactions = await getSafeTransactionsBySafe(this.safe, { nonce });
if (safeTransactions.length === 0) throw new Error('Could not find a related safe transaction');
if (safeTransactions.length === 1) return new Proposal(this, this.nonce, safeTransactions[0]);
if (safeTransactions.length === 2)
return new Proposal(this, this.nonce, safeTransactions[1], safeTransactions[0]);
throw new Error('Unexpected number of safe transactions found');
};

/**
* Returns an array of this pod's super pods, i.e., pods that this pod is a member of
*/
Expand Down
17 changes: 17 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { ethers } from 'ethers';
import Pod from './Pod';
import Proposal from './Proposal';
import { Pod as PodType, Proposal as ProposalType, ProposalStatus } from './types';
import { init, config } from './config';
import { checkAddress } from './lib/utils';
import { fetchUserPodIds, fetchAdminPodIds } from './lib/services/subgraph';
import { getSafeTransactionByHash } from './lib/services/transaction-service';

/**
* Gets a pod object.
Expand All @@ -14,6 +16,20 @@ async function getPod(identifier: string | number): Promise<Pod> {
return new Pod(identifier);
}

/**
* Gets the super proposal that the sub proposal relates to
* @throws If this proposal is not a sub proposal
* @throws If the found super proposal does not relate to this sub proposal.
* @returns
*/
async function getSuperProposal(proposal: Proposal): Promise<Proposal> {
if (!proposal.isSubProposal) throw new Error('This proposal is not a sub proposal');
// Getting the safe transaction for the super proposal.
const safeTransaction = await getSafeTransactionByHash(proposal.parameters[0].value);
const pod = await getPod(safeTransaction.safe);
return pod.getProposal(safeTransaction.nonce);
}

/**
* Fetches an array of Pod objects that a user is a member of
*
Expand Down Expand Up @@ -43,6 +59,7 @@ export {
init,
config,
getPod,
getSuperProposal,
getUserPods,
getAdminPods,
PodType as Pod,
Expand Down
Empty file removed src/lib/services/super-proposal.ts
Empty file.
13 changes: 11 additions & 2 deletions test/proposal.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { ethers } from 'ethers';
import { init } from '../src';
import { init, getSuperProposal } from '../src';
import { getPod } from '../src';
import * as txService from '../src/lib/services/transaction-service';
import * as fetchers from '../src/fetchers';
Expand Down Expand Up @@ -51,7 +51,6 @@ beforeAll(async () => {
});

beforeEach(() => {
init({ provider, network: 1 });
jest.restoreAllMocks();
});

Expand Down Expand Up @@ -192,6 +191,16 @@ describe('Proposal details', () => {
});
});

describe('Pod.getProposal()', () => {
// Commenting out because it makes actual calls, which is bad for our CI/CD.
// test('Should be able to fetch a corresponding superProposal from a subProposal', async () => {
// const pod = await getPod('0x4d3ba1AdabA15796CC3d11E48e8EC28e3A5F7C41');
// const proposal = (await pod.getProposals())[0];
// const superProposal = await getSuperProposal(proposal);
// console.log('superProposal', superProposal);
// });
});

describe('Proposal approve/reject', () => {
test('Successful approve', async () => {
standardMock();
Expand Down

0 comments on commit 6cdaa3e

Please sign in to comment.