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

Add gameEngine contextualizer in skyoneer #404

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
35 changes: 35 additions & 0 deletions src/contextualizers/protocol/skyoneer/abis/GameEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const abi = [
{
anonymous: false,
inputs: [
{
indexed: true,
internalType: 'address',
name: 'sender',
type: 'address',
},
{
indexed: true,
internalType: 'address',
name: 'receiver',
type: 'address',
},
{
indexed: true,
internalType: 'address',
name: 'tokenAddress',
type: 'address',
},
{
indexed: false,
internalType: 'uint256',
name: 'tokenAmount',
type: 'uint256',
},
],
name: 'SentTokenToAnotherUser',
type: 'event',
},
] as const;

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

export const PACK_ACTIVATION_DESTINATION_CONTRACT =
'0x21170f8bd35d0afa8ad55719ce29d6489a8585db';
Expand All @@ -12,7 +13,10 @@ export const Z_GOLD_CONTRACT_ADDRESS =
'0x387d73bd8682dceb3327b940213d5de50ee2bba2';
export const PLOT_ACTION_CONTRACT_ADDRESS =
'0xb45805566a842efb6329c11e092158f3e0eddaa2';
export const GAME_ENGINE_CONTRACT_ADDRESS =
'0xc1e5e0dc7e94f9167ccf983ba26f7c21c83e0a33';

export const PACK_ACTIVATION_SOURCE_ABI = packActivationSourceAbi;
export const PACK_ACTIVATION_DESTINATION_ABI = packActivationDestinationAbi;
export const PLOT_ACTION_ABI = plotActionAbi;
export const GAME_ENGINE_ABI = gameEngineAbi;
103 changes: 103 additions & 0 deletions src/contextualizers/protocol/skyoneer/gameEngine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
Transaction,
EventLogTopics,
SkyoneerContextActionEnum,
AssetType,
} from '../../../types';
import { GAME_ENGINE_CONTRACT_ADDRESS, GAME_ENGINE_ABI } from './constants';
import { decodeLog } from '../../../helpers/utils';

export function contextualize(transaction: Transaction): Transaction {
const isGameEngine = detect(transaction);
if (!isGameEngine) 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 !== GAME_ENGINE_CONTRACT_ADDRESS) continue;

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

if (decoded && decoded.eventName === 'SentTokenToAnotherUser') {
return true;
}
}

return false;
}

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

// decode ActivatedStarterPackOnSource event
pcowgill marked this conversation as resolved.
Show resolved Hide resolved
let decoded;
for (const log of transaction.logs) {
if (log.address !== GAME_ENGINE_CONTRACT_ADDRESS) continue;

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

if (decoded && decoded.eventName === 'SentTokenToAnotherUser') {
break;
}
}
if (!decoded) return transaction;

// grab variables from decoded event
const sender = decoded.args['sender'] as string;
const receiver = decoded.args['receiver'] as string;
const tokenAddress = decoded.args['tokenAddress'] as string;
const tokenAmount = decoded.args['tokenAmount'];

transaction.context = {
summaries: {
category: 'PROTOCOL_1',
en: {
title: `Skyoneer`,
default: '[[sender]][[contextAction]][[token]][[receiver]]',
pcowgill marked this conversation as resolved.
Show resolved Hide resolved
},
},
variables: {
sender: {
type: 'address',
value: sender,
},
receiver: {
type: 'address',
value: receiver,
},
token: {
type: AssetType.ERC20,
token: tokenAddress,
value: tokenAmount.toString(),
},
contextAction: {
type: 'contextAction',
value: SkyoneerContextActionEnum.TRANSFERRED,
},
},
};

return transaction;
}
4 changes: 3 additions & 1 deletion src/types/contextAction/protocolContextAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export enum SkyoneerContextActionEnum {
CLEARED_GROWING_CROPS = 'CLEARED_GROWING_CROPS',
CLEARED_DEAD_CROPS = 'CLEARED_DEAD_CROPS',
PLANTED = 'PLANTED',
TRANSFERRED = 'TRANSFERRED',
}

export type SkyoneerContextAction =
Expand All @@ -242,7 +243,8 @@ export type SkyoneerContextAction =
| SkyoneerContextActionEnum.HARVESTED
| SkyoneerContextActionEnum.CLEARED_GROWING_CROPS
| SkyoneerContextActionEnum.CLEARED_DEAD_CROPS
| SkyoneerContextActionEnum.PLANTED;
| SkyoneerContextActionEnum.PLANTED
| SkyoneerContextActionEnum.TRANSFERRED;

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