Skip to content

Commit

Permalink
feat: add plotAction contextualizer
Browse files Browse the repository at this point in the history
  • Loading branch information
ponyjackal committed Jul 1, 2024
1 parent 6e8e761 commit 58b50b9
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 1 deletion.
188 changes: 188 additions & 0 deletions src/contextualizers/protocol/cropXYZ/abis/PlotAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
const abi = [
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: 'address',
name: 'player',
type: 'address',
},
{
indexed: true,
internalType: 'uint256',
name: 'plotId',
type: 'uint256',
},
{
indexed: true,
internalType: 'uint256',
name: 'stakedElement',
type: 'uint256',
},
{
indexed: false,
internalType: 'uint256',
name: 'newStakedElement',
type: 'uint256',
},
{
indexed: false,
internalType: 'uint16',
name: 'harvestableAmount',
type: 'uint16',
},
{
indexed: false,
internalType: 'uint16',
name: 'actuallyProducedAmount',
type: 'uint16',
},
{
indexed: false,
internalType: 'uint24',
name: 'plotHarvests',
type: 'uint24',
},
],
name: 'HarvestedPlot',
type: 'event',
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: 'address',
name: 'player',
type: 'address',
},
{
indexed: true,
internalType: 'uint256',
name: 'plotId',
type: 'uint256',
},
{
indexed: true,
internalType: 'uint256',
name: 'diedStakedElement',
type: 'uint256',
},
{
indexed: false,
internalType: 'uint256',
name: 'newStakedElement',
type: 'uint256',
},
{
indexed: false,
internalType: 'uint16',
name: 'diedAmount',
type: 'uint16',
},
{
indexed: false,
internalType: 'uint24',
name: 'plotDeaths',
type: 'uint24',
},
],
name: 'ClearedDiedHarvest',
type: 'event',
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: 'address',
name: 'player',
type: 'address',
},
{
indexed: true,
internalType: 'uint256',
name: 'plotId',
type: 'uint256',
},
{
indexed: true,
internalType: 'uint256',
name: 'clearedStakedElement',
type: 'uint256',
},
{
indexed: false,
internalType: 'uint256',
name: 'newStakedElement',
type: 'uint256',
},
{
indexed: false,
internalType: 'uint16',
name: 'clearedAmount',
type: 'uint16',
},
{
indexed: false,
internalType: 'uint24',
name: 'plotClears',
type: 'uint24',
},
],
name: 'ClearedHarvest',
type: 'event',
},
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: 'address',
name: 'player',
type: 'address',
},
{
indexed: true,
internalType: 'uint256',
name: 'plotId',
type: 'uint256',
},
{
indexed: true,
internalType: 'uint256',
name: 'stakedElement',
type: 'uint256',
},
{
indexed: false,
internalType: 'uint16',
name: 'stakedAmount',
type: 'uint16',
},
{
indexed: false,
internalType: 'uint64',
name: 'timeStartStaked',
type: 'uint64',
},
{
indexed: false,
internalType: 'uint64',
name: 'timeReadyDelta',
type: 'uint64',
},
{
indexed: false,
internalType: 'uint64',
name: 'timeExpiredDelta',
type: 'uint64',
},
],
name: 'StakedCrop',
type: 'event',
},
] as const;

export default abi;
4 changes: 4 additions & 0 deletions src/contextualizers/protocol/cropXYZ/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import packActivationDestinationAbi from './abis/PackActivationDestination';
import packActivationSourceAbi from './abis/PackActivationSource';
import plotActionAbi from './abis/PlotAction';

export const PACK_ACTIVATION_DESTINATION_CONTRACT =
'0x21170f8bd35d0afa8ad55719ce29d6489a8585db';
Expand All @@ -9,6 +10,9 @@ export const PLOT_ERC721_CONTRACT =
'0xe2f275b2a5c376fd10006b67a9be0cc3bd5488e8';
export const Z_GOLD_CONTRACT_ADDRESS =
'0x387d73bd8682dceb3327b940213d5de50ee2bba2';
export const PLOT_ACTION_CONTRACT_ADDRESS =
'0xB45805566A842efB6329c11e092158f3E0eDdaa2';

export const PACK_ACTIVATION_SOURCE_ABI = packActivationSourceAbi;
export const PACK_ACTIVATION_DESTINATION_ABI = packActivationDestinationAbi;
export const PLOT_ACTION_ABI = plotActionAbi;
155 changes: 155 additions & 0 deletions src/contextualizers/protocol/cropXYZ/plotAction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import {
Transaction,
EventLogTopics,
GoldContextActionEnum,
} from '../../../types';
import { PLOT_ACTION_CONTRACT_ADDRESS, PLOT_ACTION_ABI } from './constants';
import { decodeLog } from '../../../helpers/utils';

export function contextualize(transaction: Transaction): Transaction {
const isPackActivationSource = detect(transaction);
if (!isPackActivationSource) return transaction;

const result = generate(transaction);
return result;
}

export function detect(transaction: Transaction): boolean {
/**
* There is a degree of overlap between the 'detect' and 'generateContext' functions,
* and while this might seem redundant, maintaining the 'detect' function aligns with
* established patterns in our other modules. This consistency is beneficial,
* and it also serves to decouple the logic, thereby simplifying the testing process
*/
// check logs
if (!transaction.logs) return false;

for (const log of transaction.logs) {
if (log.address !== PLOT_ACTION_CONTRACT_ADDRESS) continue;

const decoded = decodeLog(PLOT_ACTION_ABI, log.data, [
log.topic0,
log.topic1,
log.topic2,
log.topic3,
] as EventLogTopics);

if (
decoded &&
(decoded.eventName === 'HarvestedPlot' ||
decoded.eventName === 'ClearedDiedHarvest' ||
decoded.eventName === 'ClearedHarvest' ||
decoded.eventName === 'StakedCrop')
) {
return true;
}
}

return false;
}

export function generate(transaction: Transaction): Transaction {
if (!transaction.logs || !transaction.chainId) return transaction;

// decode ActivatedStarterPackOnSource event
let decoded;
for (const log of transaction.logs) {
if (log.address !== PLOT_ACTION_CONTRACT_ADDRESS) continue;

decoded = decodeLog(PLOT_ACTION_ABI, log.data, [
log.topic0,
log.topic1,
log.topic2,
log.topic3,
] as EventLogTopics);

if (decoded && decoded.eventName === 'HarvestedPlot') {
const player = decoded.args['player'];
const plotId = decoded.args['plotId'];
transaction.context = {
summaries: {
category: 'PROTOCOL_1',
en: {
title: `Gold`,
default: '[[player]][[harvested]]plots[[plotId]]',
},
},
variables: {
player: {
type: 'address',
value: player,
},
plotId: {
type: 'number',
value: plotId,
},
harvested: {
type: 'contextAction',
value: GoldContextActionEnum.HARVESTED_PLOT,
},
},
};
return transaction;
}

if (decoded && decoded.eventName === 'ClearedHarvest') {
const player = decoded.args['player'];
const plotId = decoded.args['plotId'];
transaction.context = {
summaries: {
category: 'PROTOCOL_1',
en: {
title: `Gold`,
default: '[[player]][[clearedHarvest]]plots[[plotId]]',
},
},
variables: {
player: {
type: 'address',
value: player,
},
plotId: {
type: 'number',
value: plotId,
},
clearedHarvest: {
type: 'contextAction',
value: GoldContextActionEnum.CLEARED_HARVEST,
},
},
};
return transaction;
}

if (decoded && decoded.eventName === 'StakedCrop') {
const player = decoded.args['player'];
const plotId = decoded.args['plotId'];
transaction.context = {
summaries: {
category: 'PROTOCOL_1',
en: {
title: `Gold`,
default: '[[player]][[stakedCrop]]plots[[plotId]]',
},
},
variables: {
player: {
type: 'address',
value: player,
},
plotId: {
type: 'number',
value: plotId,
},
stakedCrop: {
type: 'contextAction',
value: GoldContextActionEnum.STAKED_CROP,
},
},
};
return transaction;
}
}

return transaction;
}
8 changes: 7 additions & 1 deletion src/types/contextAction/protocolContextAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,11 +230,17 @@ export type BoomboxContextAction =
export enum GoldContextActionEnum {
ACTIVATED_A_STARTER_PACK = 'ACTIVATED_A_STARTER_PACK',
RECEIVED = 'RECEIVED',
HARVESTED_PLOT = 'HARVESTED_PLOT',
CLEARED_HARVEST = 'CLEARED_HARVEST',
STAKED_CROP = 'STAKED_CROP',
}

export type GoldContextAction =
| GoldContextActionEnum.ACTIVATED_A_STARTER_PACK
| GoldContextActionEnum.RECEIVED;
| GoldContextActionEnum.RECEIVED
| GoldContextActionEnum.HARVESTED_PLOT
| GoldContextActionEnum.CLEARED_HARVEST
| GoldContextActionEnum.STAKED_CROP;

export enum Protocols {
WETH = 'WETH',
Expand Down

0 comments on commit 58b50b9

Please sign in to comment.