I am trying to program an erc20 event listener using Rust programming language and trying to get events from a particular block to the latest block. But even though I have specified from the block it just gives an output from the latest block, not from the specified block. Below is my code:
use hex_literal::hex;
use std::time;
use web3::{
contract::{Contract, Options},
futures::{future, StreamExt},
types::{FilterBuilder, Address},
};
use std::str::FromStr;
use web3::types::{BlockNumber, U64};
#[tokio::main]
async fn main() -> web3::contract::Result<()> {
let web3 = web3::Web3::new(web3::transports::WebSocket::new("wss:").await?);
let filter = FilterBuilder::default()
// this is BSC: TokenHub
.from_block(BlockNumber::Number(U64::from(15255440)))
.address(vec![Address::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap()])
/* .topics(
// this is 'Transfer (index_topic_1 address from, index_topic_2 address to, uint256 value)' event
// use https://emn178.github.io/online-tools/keccak_256.html, and type in 'Transfer(address,address,uint256)'
// it will return result hash as used in next line
Some(vec![hex!("d282f389399565f3671145f5916e51652b60eee8e5c759293a2f5771b8ddfd2e").into()]),
None,
None,
None,
)*/
.build();
let sub = web3.eth_subscribe().subscribe_logs(filter).await?;
sub.for_each(|log| {
println!("{:?}", log);
future::ready(())
}).await;
Ok(())
}
Output is like:
Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x000000000000000000000000007933790a4f00000099e9001629d9fe7775b800, 0x000000000000000000000000938625591adb4e865b882377e2c965f9f9b85e34],
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 65, 133, 173, 92, 182, 64, 0]),
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f),
block_number: Some(15256705),
transaction_hash:Some(0xdd603b88c60e5b5a1aaebedb53d38df626b72d6ec2a11530cf50a8957082f404),
transaction_index: Some(0),
log_index: Some(0),
transaction_log_index: None,
log_type: None,
removed: Some(false) })
Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2, topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x0000000000000000000000002dfa8a0413255ecca71
3b6d1c1e28e634e021478, 0x000000000000000000000000938625591adb4e865b882377e2c965f9f9b85e34],
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 76,81, 115, 63, 131, 0, 0]),
block_hash:Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f),
block_number: Some(15256705),
transaction_hash:Some(0xcf4b91c9c465e8c2b22126ad580e92988162bf51be7520e5cc6dfe9f878faa06),
transaction_index: Some(1),
log_index: Some(4),
transaction_log_index: None,
log_type: None,
removed: Some(false) })
Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2,
topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x000000000000000000000000938625591adb4e865b882377e2c965f9f9b85e34, 0x000000000000000000000000007933790a4f00000099e9001629d9fe7775b800],
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 76, 170, 83, 197, 89, 150, 0, 0]),
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f),
block_number: Some(15256705),
transaction_hash:Some(0xd45f5b511cf832b407182751c451c32b27a203d8ab60136f9719cf61523b0e9a),
transaction_index: Some(2),
log_index: Some(9),
transaction_log_index: None,
log_type: None, removed: Some(false) })
Ok(Log { address: 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2,
topics: [0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef, 0x000000000000000000000000000000000035b5e5ad9019092c665357240f594e, 0x0000000000000000000000002f8ac927aa94293461c75406e90ec0ccfb2748d9],
data: Bytes([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 90,
119, 187, 148, 242, 21, 156]),
block_hash: Some(0xa076bddbfd69560de59c9be11cf5ae6f8782ace08d96c4945f3d15d05e91100f),
block_number: Some(15256705),
transaction_hash:Some(0xd36df58ffe146202ffb9ef2e29e64380991a674c820bb8a1f6a776b9468baa86),
transaction_index: Some(3),
log_index: Some(12),
transaction_log_index: None,
log_type: None,
removed: Some(false) })
I got to the conclusion that the above code gives the output of the event that will happen. So when we add from_block: 15255440, the event has already happened. And in that particular block, no other event is going to happen. So you will get the output from the latest block.
To test this in code add this:
let filter = FilterBuilder::default()
// this is BSC: TokenHub
.from_block(BlockNumber::Number(U64::from(15257125)))
.to_block(BlockNumber::Number(U64::from(15257130)))
.address(vec![Address::from_str("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2").unwrap()])
.build();
Keep in mind to replace 15257125(i.e from_block) and 15257130(i.e. to_block) with the future block which has not been mined yet. And when that block will be mined you will get events between that two blocks.
I have a small node script to resize images for a website:
const sharp = require('sharp')
const fsp = require('fs/promises')
const path = require('path')
const FILES = [
'/home/xx/68/ea/68ea8c14c2b655c50cbc560b9f3a5af620882670.jpeg',
'/home/xx/4e/7f/4e7f94103480d8ff231b17310c598470dc10489c.jpeg',
]
const SIZES = [ 300, 500, 800, 1024, 2048 ]
const FORMATS = [ 'jpeg' ]
const dir = '/home/xx/media/temp/'
async function generatePreviews(pathName, name) {
const image = sharp(pathName)
const metadatas = await image.metadata()
console.log('format:', metadatas.format)
console.log('width:', metadatas.width)
console.log('height:', metadatas.height)
console.log('weight:', await fsp.stat(pathName))
for (const size of SIZES) {
for (const format of FORMATS) {
console.log('start create preview', name, format, size)
try {
await image.clone()
.resize(size, size, {fit: 'inside'})
.toFile(path.join(dir, `preview_${name}_${size}.${format}`))
console.log('Preview created', name, format, size)
} catch (e) {
console.log('An error occured')
console.log(e)
}
}
}
}
async function testPreviews() {
console.log('start generating previews')
for (let i = 0; i < FILES.length; i++) {
await generatePreviews(FILES[i], i)
}
console.log('All previews generated')
}
testPreviews()
It works perfectly on my desktop computer (old i5 760 from 2010), but on my server, it fails almost silently when generating the largest previews for the largest file…
Here is the output on the server:
% node ./src/scripts/test_sharp.js
start generating previews
format: jpeg
width: 683
height: 1024
weight: Stats {
dev: 51713,
mode: 33188,
nlink: 1,
uid: 1001,
gid: 1001,
rdev: 0,
blksize: 4096,
ino: 658937,
size: 473209,
blocks: 928,
atimeMs: 1654021875500.3074,
mtimeMs: 1467568028343.9807,
ctimeMs: 1645954831869.6965,
birthtimeMs: 1645954825541.6052,
atime: 2022-05-31T18:31:15.500Z,
mtime: 2016-07-03T17:47:08.344Z,
ctime: 2022-02-27T09:40:31.870Z,
birthtime: 2022-02-27T09:40:25.542Z
}
start create preview 0 jpeg 300
Preview created 0 jpeg 300
start create preview 0 jpeg 500
Preview created 0 jpeg 500
start create preview 0 jpeg 800
Preview created 0 jpeg 800
start create preview 0 jpeg 1024
Preview created 0 jpeg 1024
start create preview 0 jpeg 2048
Preview created 0 jpeg 2048
format: jpeg
width: 3840
height: 5760
weight: Stats {
dev: 51713,
mode: 33188,
nlink: 1,
uid: 1001,
gid: 1001,
rdev: 0,
blksize: 4096,
ino: 658752,
size: 2272667,
blocks: 4440,
atimeMs: 1654021876468.3223,
mtimeMs: 1468007124640.3523,
ctimeMs: 1645954169924.1448,
birthtimeMs: 1645954151475.8787,
atime: 2022-05-31T18:31:16.468Z,
mtime: 2016-07-08T19:45:24.640Z,
ctime: 2022-02-27T09:29:29.924Z,
birthtime: 2022-02-27T09:29:11.476Z
}
start create preview 1 jpeg 300
Preview created 1 jpeg 300
start create preview 1 jpeg 500
Preview created 1 jpeg 500
start create preview 1 jpeg 800
zsh: killed node ./src/scripts/test_sharp.js
It fails at 800px side preview generation for the largest file (5760 x 3840px).
The catch block doesn't trigger…
Server is a 1 CPU VPS with 1GB of RAM (hosted at gandi.net).
% node --version
v16.15.0
% yarn list --pattern sharp 1 Jun 04:06:40
yarn list v1.22.18
└─ sharp#0.30.6
Done in 1.49s.
Any help would be appreciated :)
How to remove decimals digit from linked output
I am using this code
$("#slider_01").noUiSlider({
start: [2000, 24000],
connect: true,
step: 0.01,
range: {
'min': 0,
'max': 28500
},
format: wNumb({
decimals: false,
thousand: ',',
prefix: '$ ',
})
});
$('#slider_01').Link('lower').to($('#value-lower_1'));
$('#slider_01').Link('upper').to($('#value-upper_1'));
I didn't have access to the wNumb library in the environment I was working with.
Had a look under the hood in the library and this also works:
$("#slider_01").noUiSlider({
...
format: {
to: (v) => parseFloat(v).toFixed(0),
from: (v) => parseFloat(v).toFixed(0)
}
});
Decimals decimals: false is invalid, use decimals: 0. Also, you are setting formatting for the .val() method. Use it like this:
$('#slider_01').Link('lower').to($('#value-lower_1'), null, wNumb({
decimals: 0,
thousand: ',',
prefix: '$ ',
}));
Change the step from 0.01 to 1.
I know it's a very old question, but I did not want to include another library Wnumb just to remove the decimal from one place. Here is my solution without using the wnumb.
var slider = document.getElementById('prcsldr');
noUiSlider.create(slider, {
start: [10000],
range: {
min: 1000,
max: 50000
},
step: 1000,
format:{
to: (v) => v | 0,
from: (v) => v | 0
}
});