Skip to content
This repository has been archived by the owner on Mar 24, 2023. It is now read-only.

Change ckb decimal to 18 #314

Merged
merged 5 commits into from
May 6, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions .github/workflows/docker-publish-indexer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: Docker Build Push (Indexer)

on:
push:
branches: [main, hot-fix, compatibility-breaking-changes, fix-login, refactor-sudt-with-registry-address]
branches: [main, hot-fix, compatibility-breaking-changes, fix-login, refactor-sudt-with-registry-address, change-ckb-decimal-to-18]
# Publish semver tags as releases.
tags: ["v*.*.*"]
pull_request:
branches: [main, hot-fix, compatibility-breaking-changes, fix-login, refactor-sudt-with-registry-address]
branches: [main, hot-fix, compatibility-breaking-changes, fix-login, refactor-sudt-with-registry-address, change-ckb-decimal-to-18]

env:
# Use docker.io for Docker Hub if empty
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ name: Docker Build Push (Web3)

on:
push:
branches: [main, hot-fix, compatibility-breaking-changes, fix-login, refactor-sudt-with-registry-address]
branches: [main, hot-fix, compatibility-breaking-changes, fix-login, refactor-sudt-with-registry-address, change-ckb-decimal-to-18]
# Publish semver tags as releases.
tags: ["v*.*.*"]
pull_request:
branches: [main, hot-fix, compatibility-breaking-changes, fix-login, refactor-sudt-with-registry-address]
branches: [main, hot-fix, compatibility-breaking-changes, fix-login, refactor-sudt-with-registry-address, change-ckb-decimal-to-18]

env:
# Use docker.io for Docker Hub if empty
Expand Down
22 changes: 17 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions crates/indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
gw-types = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "d4cc2a8d6b6b20577ea6e6df1496eba191df6dc6" }
gw-common = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "d4cc2a8d6b6b20577ea6e6df1496eba191df6dc6" }
gw-jsonrpc-types = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "d4cc2a8d6b6b20577ea6e6df1496eba191df6dc6" }
gw-types = { git = "https://github.com/zeroqn/godwoken.git", rev = "448b7c28a49deb9f9a73683be95bd8cacbab2c57" }
gw-common = { git = "https://github.com/zeroqn/godwoken.git", rev = "448b7c28a49deb9f9a73683be95bd8cacbab2c57" }
gw-jsonrpc-types = { git = "https://github.com/zeroqn/godwoken.git", rev = "448b7c28a49deb9f9a73683be95bd8cacbab2c57" }
ckb-hash = "0.100.0"
ckb-types = "0.100.0"
anyhow = "1.0"
smol = "1.2.5"
thiserror = "1.0"
sqlx = { version = "0.5", features = [ "runtime-async-std-native-tls", "postgres", "sqlite", "chrono", "decimal" ] }
sqlx = { version = "0.5", features = [ "runtime-async-std-native-tls", "postgres", "sqlite", "chrono", "decimal", "bigdecimal" ] }
rust_decimal = "1.10.3"
num-bigint = "0.4"
faster-hex = "0.5.0"
Expand Down
38 changes: 19 additions & 19 deletions crates/indexer/src/helper.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use anyhow::{anyhow, Result};
use gw_common::{registry_address::RegistryAddress, H256};
use gw_types::packed::LogItem;
use gw_types::prelude::*;
use gw_types::{packed::LogItem, U256};
use std::{convert::TryInto, usize};

// 128KB
Expand Down Expand Up @@ -62,13 +62,13 @@ pub enum GwLog {
sudt_id: u32,
from_address: RegistryAddress,
to_address: RegistryAddress,
amount: u128,
amount: U256,
},
SudtPayFee {
sudt_id: u32,
from_address: RegistryAddress,
block_producer_address: RegistryAddress,
amount: u128,
amount: U256,
},
PolyjuiceSystem {
gas_used: u64,
Expand All @@ -86,9 +86,9 @@ pub enum GwLog {
// data format should be from_registry_address + to_registry_address + amount
// registry address format: 4 bytes registry id(u32) in little endian, 4 bytes address byte size(u32) in little endian, and 0 or 20 bytes address
// registry address can be 8-bytes(empty address) or 28-bytes(eth address)
// amount is a u128 number in little endian format
// so data can be (8 + 8 + 16) or (8 + 28 + 16) or (28 + 8 + 16) or (28 + 28 + 16) bytes
fn parse_sudt_log_data(data: &[u8]) -> anyhow::Result<(RegistryAddress, RegistryAddress, u128)> {
// amount is a u256 number in little endian format
// so data can be (8 + 8 + 32) or (8 + 28 + 32) or (28 + 8 + 32) or (28 + 28 + 32) bytes
fn parse_sudt_log_data(data: &[u8]) -> anyhow::Result<(RegistryAddress, RegistryAddress, U256)> {
let mut start = 0;
let mut end = start + {
let from_address_byte_size = u32::from_le_bytes(data[4..8].try_into()?);
Expand Down Expand Up @@ -123,9 +123,9 @@ fn parse_sudt_log_data(data: &[u8]) -> anyhow::Result<(RegistryAddress, Registry
}
};

let mut u128_bytes = [0u8; 16];
u128_bytes.copy_from_slice(&data[end..(end + 16)]);
let amount = u128::from_le_bytes(u128_bytes);
let mut u256_bytes = [0u8; 32];
u256_bytes.copy_from_slice(&data[end..(end + 32)]);
let amount = U256::from_little_endian(&u256_bytes);
Ok((from_address, to_address, amount))
}

Expand All @@ -137,11 +137,11 @@ pub fn parse_log(item: &LogItem) -> Result<GwLog> {
GW_LOG_SUDT_TRANSFER => {
let sudt_id: u32 = item.account_id().unpack();
let data_len = data.len();
// 28 + 28 + 16 = 72
// 8 + 28 + 16 = 52
// 28 + 8 + 16 = 52
// 8 + 8 + 16 = 32
if data_len != 72 && data_len != 52 && data_len != 32 {
// 28 + 28 + 32 = 88
// 8 + 28 + 32 = 68
// 28 + 8 + 32 = 68
// 8 + 8 + 32 = 48
if data_len != 88 && data_len != 68 && data_len != 48 {
return Err(anyhow!(
"Invalid data length: {}, data: {}",
data.len(),
Expand All @@ -159,11 +159,11 @@ pub fn parse_log(item: &LogItem) -> Result<GwLog> {
GW_LOG_SUDT_PAY_FEE => {
let sudt_id: u32 = item.account_id().unpack();
let data_len = data.len();
// 28 + 28 + 16 = 72
// 8 + 28 + 16 = 52
// 28 + 8 + 16 = 52
// 8 + 8 + 16 = 32
if data_len != 72 && data_len != 52 && data_len != 32 {
// 28 + 28 + 32 = 88
// 8 + 28 + 32 = 68
// 28 + 8 + 32 = 68
// 8 + 8 + 32 = 48
if data_len != 88 && data_len != 68 && data_len != 48 {
return Err(anyhow!(
"Invalid data length: {}, data: {}",
data.len(),
Expand Down
40 changes: 27 additions & 13 deletions crates/indexer/src/indexer.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::{collections::HashSet, str::FromStr};

use crate::{
helper::{hex, parse_log, GwLog, PolyjuiceArgs, GW_LOG_POLYJUICE_SYSTEM},
Expand All @@ -17,10 +17,14 @@ use gw_types::{
packed::{L2Block, SUDTArgs, SUDTArgsUnion, Script},
prelude::Unpack as GwUnpack,
prelude::*,
U256,
};
use gw_web3_rpc_client::{convertion, godwoken_rpc_client::GodwokenRpcClient};
use rust_decimal::{prelude::ToPrimitive, Decimal};
use sqlx::types::chrono::{DateTime, NaiveDateTime, Utc};
use sqlx::types::{
chrono::{DateTime, NaiveDateTime, Utc},
BigDecimal,
};

const MILLIS_PER_SEC: u64 = 1_000;
pub struct Web3Indexer {
Expand Down Expand Up @@ -96,8 +100,8 @@ impl Web3Indexer {
.bind(hex(web3_block.hash.as_slice())?)
.bind(hex(web3_block.parent_hash.as_slice())?)
.bind(hex(&web3_block.logs_bloom)?)
.bind(Decimal::from(web3_block.gas_limit))
.bind(Decimal::from(web3_block.gas_used))
.bind(u128_to_big_decimal(&web3_block.gas_limit)?)
.bind(u128_to_big_decimal(&web3_block.gas_used)?)
.bind(web3_block.timestamp)
.bind(hex(&web3_block.miner)?)
.bind(Decimal::from(web3_block.size))
Expand All @@ -124,16 +128,16 @@ impl Web3Indexer {
.bind(web3_tx.transaction_index)
.bind(hex(&web3_tx.from_address)?)
.bind(web3_to_address_hex)
.bind(Decimal::from(web3_tx.value))
.bind(u256_to_big_decimal(&web3_tx.value)?)
.bind(Decimal::from(web3_tx.nonce))
.bind(Decimal::from(web3_tx.gas_limit))
.bind(Decimal::from(web3_tx.gas_price))
.bind(u128_to_big_decimal(&web3_tx.gas_limit)?)
.bind(u128_to_big_decimal(&web3_tx.gas_price)?)
.bind(hex(&web3_tx.data)?)
.bind(Decimal::from(web3_tx.v))
.bind(hex(&web3_tx.r)?)
.bind(hex(&web3_tx.s)?)
.bind(Decimal::from(web3_tx.cumulative_gas_used))
.bind(Decimal::from(web3_tx.gas_used))
.bind(u128_to_big_decimal(&web3_tx.cumulative_gas_used)?)
.bind(u128_to_big_decimal(&web3_tx.gas_used)?)
.bind(hex(&web3_tx.logs_bloom)?)
.bind(web3_contract_address_hex)
.bind(web3_tx.status)
Expand Down Expand Up @@ -319,7 +323,7 @@ impl Web3Indexer {
tx_index,
from_address,
to_address,
polyjuice_args.value,
polyjuice_args.value.into(),
nonce,
polyjuice_args.gas_limit.into(),
polyjuice_args.gas_price,
Expand Down Expand Up @@ -398,13 +402,13 @@ impl Web3Indexer {
continue;
}

let amount: u128 = sudt_transfer.amount().unpack();
let fee: u64 = sudt_transfer.fee().amount().unpack();
let amount: U256 = sudt_transfer.amount().unpack();
let fee: u128 = sudt_transfer.fee().amount().unpack();
let value = amount;

// Represent SUDTTransfer fee in web3 style, set gas_price as 1 temporary.
let gas_price = 1;
let gas_limit = fee.into();
let gas_limit = fee;
cumulative_gas_used += gas_limit;

let nonce: u32 = l2_transaction.raw().nonce().unpack();
Expand Down Expand Up @@ -523,3 +527,13 @@ async fn get_script(

Ok(script_opt)
}

fn u128_to_big_decimal(value: &u128) -> Result<BigDecimal> {
let result = BigDecimal::from_str(&value.to_string())?;
Ok(result)
}

fn u256_to_big_decimal(value: &U256) -> Result<BigDecimal> {
let result = BigDecimal::from_str(&value.to_string())?;
Ok(result)
}
5 changes: 3 additions & 2 deletions crates/indexer/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use gw_common::H256;
use gw_types::U256;
use sha3::{Digest, Keccak256};
use sqlx::types::chrono::{DateTime, Utc};

Expand Down Expand Up @@ -26,7 +27,7 @@ pub struct Transaction {
pub transaction_index: u32,
pub from_address: Address,
pub to_address: Option<Address>,
pub value: u128,
pub value: U256,
pub nonce: u32,
pub gas_limit: u128,
pub gas_price: u128,
Expand All @@ -51,7 +52,7 @@ impl Transaction {
transaction_index: u32,
from_address: Address,
to_address: Option<Address>,
value: u128,
value: U256,
nonce: u32,
gas_limit: u128,
gas_price: u128,
Expand Down
6 changes: 3 additions & 3 deletions crates/rpc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ serde_json = "1.0"
reqwest = { version = "0.11", features = ["json", "blocking"] }
ckb-jsonrpc-types = "0.100.0"
ckb-types = "0.100.0"
gw-jsonrpc-types = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "d4cc2a8d6b6b20577ea6e6df1496eba191df6dc6" }
gw-types = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "d4cc2a8d6b6b20577ea6e6df1496eba191df6dc6" }
gw-common = { git = "https://github.com/nervosnetwork/godwoken.git", rev = "d4cc2a8d6b6b20577ea6e6df1496eba191df6dc6" }
gw-jsonrpc-types = { git = "https://github.com/zeroqn/godwoken.git", rev = "448b7c28a49deb9f9a73683be95bd8cacbab2c57" }
gw-types = { git = "https://github.com/zeroqn/godwoken.git", rev = "448b7c28a49deb9f9a73683be95bd8cacbab2c57" }
gw-common = { git = "https://github.com/zeroqn/godwoken.git", rev = "448b7c28a49deb9f9a73683be95bd8cacbab2c57" }
jsonrpc-core = "17"
rand = "0.8"
anyhow = "1.0"