Skip to content

Commit

Permalink
chore: wip: write template for contract handler
Browse files Browse the repository at this point in the history
  • Loading branch information
asimm241 committed May 17, 2021
1 parent 8dd45a9 commit 7b0e9bd
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/datastore/postgres-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4033,7 +4033,7 @@ export class PgDataStore
});
}

async getftMetadata(contractId: string): Promise<FoundOrNot<DbAssetMetadata>> {
getftMetadata(contractId: string): Promise<FoundOrNot<DbAssetMetadata>> {
/**
* extract nft information from db here..
*/
Expand All @@ -4046,12 +4046,12 @@ export class PgDataStore
image_canonical_uri: 'canonical image uri',
};

return {
return Promise.resolve({
found: true,
result: metadata,
};
});
}
async getNftMetadata(contractId: string): Promise<FoundOrNot<DbAssetMetadata>> {
getNftMetadata(contractId: string): Promise<FoundOrNot<DbAssetMetadata>> {
/**
* extract nft information from db here..
*/
Expand All @@ -4064,10 +4064,10 @@ export class PgDataStore
image_canonical_uri: 'canonical image uri',
};

return {
return Promise.resolve({
found: true,
result: metadata,
};
});
}

async close(): Promise<void> {
Expand Down
87 changes: 87 additions & 0 deletions src/event-stream/contract-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { DbBnsName, DbBnsNamespace } from './../../src/datastore/common';
import {
BnsContractIdentifier,
nameFunctions,
namespaceReadyFunction,
printTopic,
} from './../../src/bns-constants';
import { SmartContractEvent } from './core-node-message';
import { addressToString } from '@stacks/transactions';
import {
getFunctionName,
parseNameRawValue,
parseNamespaceRawValue,
} from './../../src/bns-helpers';

export interface SmartContractEventExtracts {
event: SmartContractEvent;
functionName: string;
block_height: number;
index_block_hash: string;
}

export function handleContractEvent(extracts: SmartContractEventExtracts) {
const { topic, contract_identifier, raw_value } = extracts.event.contract_event;
const { txid } = extracts.event;

if (
topic === printTopic &&
(contract_identifier === BnsContractIdentifier.mainnet ||
contract_identifier === BnsContractIdentifier.testnet)
) {
const { functionName, block_height, index_block_hash } = extracts;
handleBnsEvent(functionName, raw_value, block_height, index_block_hash, txid);
} else {
// todo add condition for token contract identifiers etc.
handleTokenEvent();
}
}

function handleBnsEvent(
bnsFunctionName: string,
bnsRawValue: string,
block_height: number,
index_block_hash: string,
txid: string
) {
if (nameFunctions.includes(bnsFunctionName)) {
//todo change name functions variable name
// const attachment = parseNameRawValue(event.contract_event.raw_value);
const attachment = parseNameRawValue(bnsRawValue);
const name: DbBnsName = {
name: attachment.attachment.metadata.name.concat(
'.',
attachment.attachment.metadata.namespace
),
namespace_id: attachment.attachment.metadata.namespace,
address: addressToString(attachment.attachment.metadata.tx_sender),
expire_block: 0,
registered_at: block_height,
zonefile_hash: attachment.attachment.hash,
zonefile: '', //zone file will be updated in /attachments/new
latest: true,
tx_id: txid,
status: attachment.attachment.metadata.op,
index_block_hash: index_block_hash,
canonical: true,
atch_resolved: false, //saving an unresoved BNS name
};
// dbTx.names.push(name);
}
if (bnsFunctionName === namespaceReadyFunction) {
//event received for namespaces
const namespace: DbBnsNamespace | undefined = parseNamespaceRawValue(
bnsRawValue,
block_height,
txid,
index_block_hash
);
if (namespace != undefined) {
// dbTx.namespaces.push(namespace);
}
}
}

function handleTokenEvent() {
throw Error('not yet implmented');
}

0 comments on commit 7b0e9bd

Please sign in to comment.