Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(contract): refactor airdrop contract and update tests #161

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions contract/src/airdrop.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const compose =
(...fns) =>
args =>
fns.reduceRight((x, f) => f(x), args);

const toAgoricBech = (data, limit) =>
bech32.encode('agoric', bech32.toWords(data), limit);

Expand Down Expand Up @@ -275,10 +276,7 @@ export const start = async (zcf, privateArgs, baggage) => {
});

const divideAmount = divideAmountByTwo(tokenBrand);
const handlePauseOffers = () => {
stateMachine.transitionTo(PAUSED);
void zcf.setOfferFilter([messagesObject.makeClaimInvitationDescription()]);
};

await objectToMap(
{
merkleRoot,
Expand Down Expand Up @@ -334,7 +332,9 @@ export const start = async (zcf, privateArgs, baggage) => {
*/
updateEpochDetails(absTime, epochIdx) {
const { helper } = this.facets;
TT('epoch Ending:::', this.state.currentEpoch);
this.state.currentEpoch = epochIdx;
TT('epoch starting:::', this.state.currentEpoch);
if (this.state.currentEpoch === targetNumberOfEpochs) {
void withdrawAndBurn(
zcf,
Expand Down Expand Up @@ -490,19 +490,20 @@ export const start = async (zcf, privateArgs, baggage) => {
const pauseInvitation = await zcf.makeInvitation(
// Is this UserSeat argument necessary????
/** @type {UserSeat} */
seat => {
(seat, offerArgs) => {
assert(
stateMachine.canTransitionTo(PAUSED),
`Illegal state transition. Can not transition from state: ${stateMachine.getStatus()} to state ${PAUSED}`,
stateMachine.canTransitionTo(offerArgs.nextState),
`Illegal state transition. Can not transition from state: ${stateMachine.getStatus()} to state ${offerArgs.nextState}`,
);
stateMachine.transitionTo(offerArgs.nextState);
seat.exit('Exiting pause invitation');
return handlePauseOffers();
zcf.setOfferFilter(offerArgs.filter);
void depositInvitation(adminDepositFacet);
},
'pause contract',
'set offer filter',
);
E(depositFacet).receive(pauseInvitation);
};

const recievedPause = depositInvitation(adminDepositFacet);
return recievedPause;
},
Expand All @@ -515,7 +516,7 @@ export const start = async (zcf, privateArgs, baggage) => {
public: publicFacet,
} = prepareContract(airdropStatusTracker);

console.log('START TIME', baggage.get('startTime'));
TT('START TIME', baggage.get('startTime'));
void E(timer).setWakeup(
baggage.get('startTime'),
makeWaker('claimWindowOpenWaker', ({ absValue }) => {
Expand Down
106 changes: 101 additions & 5 deletions contract/test/tribbles-airdrop/contract.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { AmountMath } from '@agoric/ertp';
import { Observable, Task } from '../../src/helpers/adts.js';
import { createStore } from '../../src/tribbles/utils.js';
import { head } from '../../src/helpers/objectTools.js';
import { messagesObject, OPEN, PAUSED } from '../../src/airdrop.contract.js';

const reducerFn = (state = [], action) => {
const { type, payload } = action;
Expand Down Expand Up @@ -241,7 +242,7 @@ test.serial('makeClaimTokensInvitation happy path::', async t => {
t.deepEqual(pauseInvitationDetails.brand, zoeBrand);
t.deepEqual(
head(pauseInvitationDetails.value).description,
'pause contract',
'set offer filter',
);
},
});
Expand All @@ -262,6 +263,7 @@ test.serial('makeClaimTokensInvitation happy path::', async t => {
const wallets = {
alice: await walletFactory.makeSmartWallet(accounts[4].address),
bob: await walletFactory.makeSmartWallet(accounts[2].address),
carol: await walletFactory.makeSmartWallet(accounts[10].address),
};
const { faucet, mintBrandedPayment } = makeStableFaucet({
bundleCache,
Expand All @@ -281,7 +283,7 @@ test.serial('makeClaimTokensInvitation happy path::', async t => {
const makeFeeAmount = () => AmountMath.make(brands.Fee, 5n);

const [aliceTier, bobTier] = [0, 2];
const [alice, bob] = [
const [alice, bob, carol] = [
[
E(wallets.alice.offers).executeOffer(
makeOfferSpec({ ...accounts[4], tier: 0 }, makeFeeAmount(), 0),
Expand All @@ -294,10 +296,17 @@ test.serial('makeClaimTokensInvitation happy path::', async t => {
),
E(wallets.bob.peek).purseUpdates(brands.Tribbles),
],
[
E(wallets.carol.offers).executeOffer(
makeOfferSpec({ ...accounts[10], tier: bobTier }, makeFeeAmount(), 0),
),
E(wallets.carol.peek).purseUpdates(brands.Tribbles),
],
];

const [alicesOfferUpdates, alicesPurse] = alice;
const [bobsOfferUpdate, bobsPurse] = bob;
const [carolsOfferUpdate, carolsPurse] = carol;
/**
* @typedef {{value: { updated: string, status: { id: string, invitationSpec: import('../../tools/wallet-tools.js').InvitationSpec, proposal:Proposal, offerArgs: {key: string, proof: []}}}}} OfferResult
*/
Expand Down Expand Up @@ -381,6 +390,40 @@ test.serial('makeClaimTokensInvitation happy path::', async t => {
);
},
});

await makeAsyncObserverObject(
carolsOfferUpdate,
'AsyncGenerator carolsOfferUpdate has fufilled its requirements.',
).subscribe({
next: traceFn('CAROLS_OFFER_UPDATE:::: SUBSCRIBE.NEXT'),
error: traceFn('CAROLS_OFFER_UPDATE:::: SUBSCRIBE.ERROR'),
complete: ({ message, values }) => {
t.deepEqual(
message,
'AsyncGenerator carolsOfferUpdate has fufilled its requirements.',
);
t.deepEqual(values.length, 4);
},
});

await makeAsyncObserverObject(
carolsPurse,
'AsyncGenerator carolPurse has fufilled its requirements.',
1,
).subscribe({
next: traceFn('TRIBBLES_WATCHER ### SUBSCRIBE.NEXT'),
error: traceFn('TRIBBLES_WATCHER #### SUBSCRIBE.ERROR'),
complete: ({ message, values }) => {
t.deepEqual(
message,
'AsyncGenerator carolPurse has fufilled its requirements.',
);
t.deepEqual(
head(values),
AmountMath.make(brands.Tribbles, AIRDROP_TIERS_STATIC[bobTier]),
);
},
});
});

test.serial(
Expand Down Expand Up @@ -443,7 +486,7 @@ test.serial(
t.deepEqual(pauseInvitationDetails.brand, zoeBrand);
t.deepEqual(
head(pauseInvitationDetails.value).description,
'pause contract',
'set offer filter',
);
},
});
Expand Down Expand Up @@ -486,18 +529,71 @@ test.serial(
invitationSpec: {
source: 'purse',
instance,
description: 'pause contract',
description: 'set offer filter',
},
proposal: {},
offerArgs: {
nextState: PAUSED,
filter: [messagesObject.makeClaimInvitationDescription()],
},
};

const pauseOfferUpdater = E(adminWallet.offers).executeOffer(pauseOffer);

await makeAsyncObserverObject(pauseOfferUpdater).subscribe({
next: traceFn('pauseOfferUpdater ## next'),
error: traceFn('pauseOfferUpdater## Error'),
complete: traceFn('pauseOfferUpdater ## complete'),
});

await makeAsyncObserverObject(
adminZoePurse,
'invitation recieved',
1,
).subscribe({
next: traceFn('ADMIN_WALLET::: NEXT'),
error: traceFn('ADMIN WALLET::: ERROR'),
complete: async ({ message, values }) => {
const [pauseInvitationDetails] = values;
t.deepEqual(message, 'invitation recieved');
t.deepEqual(pauseInvitationDetails.brand, zoeBrand);
t.deepEqual(
head(pauseInvitationDetails.value).description,
'set offer filter',
);
},
});

const removePauseOffer = {
id: 'admin-pause-1',
invitationSpec: {
source: 'purse',
instance,
description: 'set offer filter',
},
proposal: {},
offerArgs: {
nextState: OPEN,
filter: [],
},
};
const removePauseOfferUpdater = E(adminWallet.offers).executeOffer(
removePauseOffer,
);

await makeAsyncObserverObject(removePauseOfferUpdater).subscribe({
next: traceFn('removePauseOfferUpdater ## next'),
error: traceFn('removePauseOfferUpdater## Error'),
complete: traceFn('removePauseOfferUpdater ## complete'),
});
const secondPauseOfferUpdater = E(adminWallet.offers).executeOffer(
pauseOffer,
);
await makeAsyncObserverObject(secondPauseOfferUpdater).subscribe({
next: traceFn('secondPauseOfferUpdater ## next'),
error: traceFn('secondPauseOfferUpdater## Error'),
complete: traceFn('secondPauseOfferUpdater ## complete'),
});

const [aliceTier, bobTier] = [0, 2];
const [alice, bob] = [
[
Expand Down