Skip to content

Commit

Permalink
getSubscriptionResources publicfacet added;
Browse files Browse the repository at this point in the history
  • Loading branch information
Muneeb147 committed Sep 5, 2024
1 parent 7d0049e commit d7e87d1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 11 deletions.
57 changes: 48 additions & 9 deletions contract/src/offer-up.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ import '@agoric/zoe/exported.js';
export const start = async zcf => {
const {
subscriptionPrice,
// subscriptionPeriod = 'MONTHLY',
// serviceToAvail = 'NETFLIX',
subscriptionPeriod = 'MONTHLY',

Check failure on line 50 in contract/src/offer-up.contract.js

View workflow job for this annotation

GitHub Actions / unit (20)

'subscriptionPeriod' is assigned a value but never used. Allowed unused vars must match /^_/u
serviceToAvail = 'NETFLIX',
} = zcf.getTerms();

const subscriptionResources = {
'NETFLIX': ['Movie_1', 'Movie_2']
}

/**
* a new ERTP mint for items, accessed thru the Zoe Contract Facet.
* Note: `makeZCFMint` makes the associated brand and issuer available
Expand Down Expand Up @@ -78,15 +82,18 @@ export const start = async zcf => {
/** a seat for allocating proceeds of sales */
const proceeds = zcf.makeEmptySeatKit().zcfSeat;

const subscriptions = new Map();


/** @type {OfferHandler} */
const tradeHandler = async buyerSeat => {
// Creating a unit of subscription
const subscriptionAmount = AmountMath.make(
brand,
makeCopyBag([[{ expiryTime: '123' }, 1n]]),
);
const tradeHandler = async (buyerSeat, offerArgs) => {

const want = { Items: subscriptionAmount };
// @ts-ignore
const userAddress = offerArgs.userAddress;
console.log("UserAddress", userAddress);

const amountObject = AmountMath.make(brand, makeCopyBag([[{ expiryTime: '123' }, 1n]]))
const want = { Items: amountObject };

const newSubscription = itemMint.mintGains(want);

Expand All @@ -100,6 +107,8 @@ export const start = async zcf => {
]),
);

subscriptions.set(userAddress, want.Items);

buyerSeat.exit(true);
newSubscription.exit();
return 'Subscription Granted';
Expand All @@ -120,9 +129,39 @@ export const start = async zcf => {
proposalShape,
);

const checkUserHasSubscription = (userAddress) => {
const userSubscription = subscriptions.get(userAddress);

if (!userSubscription || !userSubscription.value.payload)
return false

const expiryTime = userSubscription.value.payload[0][0].expiryTime

// Here we'll check with current time from time service. The expiryTime should be greater than current time
if (!expiryTime || expiryTime != '123')

Check failure on line 141 in contract/src/offer-up.contract.js

View workflow job for this annotation

GitHub Actions / unit (20)

Expected '!==' and instead saw '!='
return false
return true;
//
}

const getSubscriptionResources = (userAddress) => {
const userHasSubscription = checkUserHasSubscription(userAddress);

if (userHasSubscription) {
// User has a valid subscription, return the resources
return subscriptionResources[serviceToAvail];
} else {
// User doesn't have a valid subscription
return 'Access denied: You do not have a valid subscription.';
}

};


// Mark the publicFacet Far, i.e. reachable from outside the contract
const publicFacet = Far('Items Public Facet', {
makeTradeInvitation,
getSubscriptionResources,
});
return harden({ publicFacet });
};
Expand Down
10 changes: 8 additions & 2 deletions contract/test/test-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,20 @@ const alice = async (t, zoe, instance, purse) => {

const toTrade = E(publicFacet).makeTradeInvitation();

const seat = E(zoe).offer(toTrade, proposal, { Price: pmt });
const userAddress = 'agoric123456';

const seat = E(zoe).offer(toTrade, proposal, { Price: pmt }, { userAddress });
const items = await E(seat).getPayout('Items');

const actual = await E(issuers.Item).getAmountOf(items);
t.log('Alice payout brand', actual.brand);
t.log('Alice payout value', actual.value);
t.deepEqual(actual, proposal.want.Items);

const actualMovies = ['Movie_1', 'Movie_2']
const subscriptionMovies = await E(publicFacet).getSubscriptionResources(userAddress)

t.deepEqual(actualMovies, subscriptionMovies)
};

test('Alice trades: give some play money, want subscription', async t => {
Expand All @@ -113,7 +120,6 @@ test('Alice trades: give some play money, want subscription', async t => {
const money = makeIssuerKit('PlayMoney');
const issuers = { Price: money.issuer };
const terms = { subscriptionPrice: AmountMath.make(money.brand, 500n) };

/** @type {ERef<Installation<AssetContractFn>>} */
const installation = E(zoe).install(bundle);
const { instance } = await E(zoe).startInstance(installation, issuers, terms);
Expand Down

0 comments on commit d7e87d1

Please sign in to comment.