Skip to content

Commit

Permalink
refactor: move stream creation methods to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed May 28, 2020
1 parent 8256165 commit 1c8a294
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 65 deletions.
23 changes: 14 additions & 9 deletions packages/react-app/src/components/modals/CreateStreamModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -214,21 +214,26 @@ export default function CreateStreamDialog({
</Button>
<Button
onClick={async (): Promise<void> => {
const startTime = parseInt(
moment().add(5, 'minutes').format('X'),
10,
);
const stopTime = parseInt(
moment()
.add(days, 'days')
.add(hours, 'hours')
.add(minutes + 5, 'minutes')
.format('X'),
10,
);
await createStream(
parseInt(streamAmount, 10),
streamContract,
userAddress,
recipient,
zkAsset,
parseInt(moment().add(5, 'minutes').format('X'), 10),
parseInt(
moment()
.add(days, 'days')
.add(hours, 'hours')
.add(minutes + 5, 'minutes')
.format('X'),
10,
),
startTime,
stopTime,
);
handleClose();
}}
Expand Down
30 changes: 30 additions & 0 deletions packages/react-app/src/utils/proofs/deposit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Address } from '../../types/types';

async function buildDepositProof(
streamContractAddress: Address,
asset: any,
payerAddress: Address,
payeeAddress: Address,
sendAmount: number,
): Promise<object> {
const { proof } = await asset.send(
[
{
to: streamContractAddress,
amount: sendAmount,
aztecAccountNotRequired: true, // contract can't have an AZTEC account
numberOfOutputNotes: 1, // only want to track a single note per stream
},
],
{
userAccess: [payerAddress, payeeAddress], // Give view access to sender and recipient
returnProof: true,
sender: streamContractAddress, // proof is being submitted by contract rather than directly
},
);

console.log(proof);
return proof;
}

export default buildDepositProof;
1 change: 1 addition & 0 deletions packages/react-app/src/utils/proofs/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import buildProofs from './proofs';
import { Stream, Address } from '../../types/types';

export { default as buildDepositProof } from './deposit';
// On a withdrawal the change note remains on the contract
const buildWithdrawalProofs = (
aztec: any,
Expand Down
64 changes: 8 additions & 56 deletions packages/react-app/src/utils/stream/creation.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,6 @@
import { Contract } from 'ethers';
import { Address, Hash, Note } from '../../types/types';

function initialiseStream(
streamContract: Contract,
payeeAddress: Address,
noteForStreamContract: Note,
zkAssetAddress: Address,
startTime: number,
endTime: number,
): number {
return streamContract
.createStream(
payeeAddress,
noteForStreamContract.noteHash,
zkAssetAddress,
startTime,
endTime,
)
.then((err: any, txHash: Hash) => {
if (err) {
console.log(err);
return null;
}
console.log('transaction hash', txHash);
return txHash;
});
}

async function fundStream(
streamContractAddress: Address,
payerAddress: Address,
payeeAddress: Address,
sendAmount: number,
asset: any,
): Promise<Note> {
const { outputNotes } = await asset.send(
[
{
to: streamContractAddress,
amount: sendAmount,
aztecAccountNotRequired: true,
numberOfOutputNotes: 1, // contract has one
},
],
{ userAccess: [payerAddress, payeeAddress] }, // Give view access to sender and recipient
);
const noteForStreamContract = outputNotes[0];
console.log('noteForStreamContract', noteForStreamContract);
return noteForStreamContract;
}
import { buildDepositProof } from '../proofs';
import { Address, Hash } from '../../types/types';

async function createStream(
sendAmount: number,
Expand All @@ -58,18 +10,18 @@ async function createStream(
zkAsset: any,
startTime: number,
endTime: number,
): Promise<number> {
const streamNote = await fundStream(
): Promise<Hash> {
const depositProof = await buildDepositProof(
streamContract.address,
zkAsset,
payerAddress,
payeeAddress,
sendAmount,
zkAsset,
);
return initialiseStream(
streamContract,

return streamContract.createStream(
payeeAddress,
streamNote,
depositProof,
zkAsset.address,
startTime,
endTime,
Expand Down

0 comments on commit 1c8a294

Please sign in to comment.