@ -5,6 +5,7 @@ const { Database } = require('sqlite3');
const fs = require ( 'fs' ) ;
const assetsBase = 'https://art101-assets.s3.us-west-2.amazonaws.com' ;
const db = new Database ( './state/sqlite.db' ) ;
const config = {
apiKey : process . env . MORALIS _KEY
@ -40,6 +41,49 @@ class Scrape {
} ;
}
async postDiscord ( ) {
if ( ! process . env . DISCORD _URL ) return
db . all ( 'SELECT * FROM events WHERE discord_sent = 0 AND contract = ?' , [ this . contractAddress ] , async ( err , r ) => {
r . map ( async row => {
try {
const title = ` Sale of token ${ row . token _id } for ${ this . contractName } ! ` ;
const desc = ` Purchased by ${ row . buyer } in block ${ row . block _number } for ${ Number ( row . sale _price ) / 1000000000000000000.0 } Ξ. [Etherscan](https://etherscan.io/tx/ ${ row . tx _hash } ) ` ;
const url = ` ${ assetsBase } / ${ this . contractAddress } / ${ row . token _id . toString ( ) } .json ` ;
const metadata = await fetch ( url )
. then ( ( m ) => m . json ( ) ) ;
const imageURL = metadata . image . replace ( 'ipfs://' , ` ${ assetsBase } / ${ this . contractAddress } / ` ) + '.fullsize.png' ;
await sleep ( 2 ) ;
await fetch ( process . env . DISCORD _URL , {
method : 'POST' ,
headers : {
'Accept' : 'application/json' ,
'Content-Type' : 'application/json'
} ,
body : JSON . stringify ( {
embeds : [
{
title : title ,
description : desc ,
image : {
url : imageURL
} ,
url : ` https://gallery.art101.io/collection/ ${ this . contractName } / ${ row . token _id } `
}
]
} )
} ) ;
db . run ( ` UPDATE events SET discord_sent = 1 WHERE contract = ? AND token_id = ? and block_number = ? ` , [
row . contract , row . token _id , row . block _number
] ) ;
return ` posted sale info to Discord: ${ title } - ${ desc } - ${ imageURL } ` ;
} catch ( err ) {
throw new Error ( ` [!] Failed to post to Discord: ${ err } ` ) ;
}
} ) ;
} ) ;
}
async scrape ( ) {
const cursor = this . getCursor ( )
if ( cursor === '' ) {
@ -99,6 +143,12 @@ class Scrape {
}
function shortenAddress ( address ) {
const shortAddress = ` ${ address . slice ( 0 , 6 ) } ... ${ address . slice ( address . length - 4 , address . length ) } ` ;
if ( address . startsWith ( '0x' ) ) return shortAddress ;
return address ;
}
( async ( ) => {
const tableExists = await new Promise ( ( resolve ) => {
db . get ( 'SELECT name FROM sqlite_master WHERE type="table" AND name="events"' , [ ] , ( err , row ) => {
@ -136,6 +186,7 @@ class Scrape {
const c = new Scrape ( contract ) ;
try {
await c . scrape ( ) ;
await c . postDiscord ( ) ;
} catch ( e ) {
console . log ( e ) ;
}