Skip to content

Instantly share code, notes, and snippets.

@lourou
Created January 9, 2024 16:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lourou/fbfa0abcabc3a219a6056515fc51fe2c to your computer and use it in GitHub Desktop.
Save lourou/fbfa0abcabc3a219a6056515fc51fe2c to your computer and use it in GitHub Desktop.
import {
ContentTypeId,
EncodedContent,
JSContentCodec,
} from "@xmtp/react-native-sdk";
export const ContentTypeTransactionReference: ContentTypeId = {
authorityId: "xmtp.org",
typeId: "transactionReference",
versionMajor: 1,
versionMinor: 0,
};
export type TransactionReference = {
/**
* The namespace for the networkId
*/
namespace?: string;
/**
* The networkId for the transaction, in decimal or hexidecimal format
*/
networkId: number | string;
/**
* The transaction hash
*/
reference: string;
/**
* Optional metadata object
*/
metadata?: {
transactionType: string;
currency: string;
amount: number;
fromAddress: string;
toAddress: string;
};
};
export class TransactionReferenceCodec
implements JSContentCodec<TransactionReference>
{
get contentType(): ContentTypeId {
return ContentTypeTransactionReference;
}
encode(content: TransactionReference): EncodedContent {
const encoded = {
type: ContentTypeTransactionReference,
parameters: {},
content: new TextEncoder().encode(JSON.stringify(content)),
}
return encoded;
}
decode(encodedContent: EncodedContent): TransactionReference {
const uint8Array = encodedContent.content;
const contentReceived = JSON.parse(new TextDecoder().decode(uint8Array));
return contentReceived
}
fallback(content: TransactionReference): string | undefined {
if (content.reference) {
return `[Crypto transaction] Use a blockchain explorer to learn more using the transaction hash: ${content.reference}`;
} else {
return `Crypto transaction`;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment