Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Udt account of omiga inscription api #1563

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
42 changes: 28 additions & 14 deletions app/serializers/address_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class AddressSerializer
attribute :is_special do |object|
object.special?.to_s
end
attribute :special_address, if: Proc.new { |record| record.special? } do |object|
attribute :special_address, if: Proc.new { |record|
record.special?
} do |object|
Settings.special_addresses[object.address_hash]
end
attribute :live_cells_count do |object|
Expand All @@ -45,7 +47,19 @@ class AddressSerializer
udt_icon_file: udt_account.udt_icon_file,
udt_type: udt_account.udt_type,
display_name: udt_account.display_name,
uan: udt_account.uan
uan: udt_account.uan,
}
elsif udt_account.udt_type == "omiga_inscription"
info = udt_account.udt.omiga_inscription_info
{
symbol: udt_account.symbol,
decimal: udt_account.decimal.to_s,
amount: udt_account.amount.to_s,
type_hash: udt_account.type_hash,
udt_type: udt_account.udt_type,
udt_amount: udt_account.udt.total_amount.to_s,
expected_supply: info.expected_supply.to_s,
mint_status: info.mint_status,
}
elsif udt_account.udt_type == "m_nft_token"
ts = TypeScript.find_by script_hash: udt_account.type_hash
Expand All @@ -59,31 +73,31 @@ class AddressSerializer
amount: udt_account.amount.to_s,
type_hash: udt_account.type_hash,
collection: {
type_hash: coll&.type_script&.script_hash
type_hash: coll&.type_script&.script_hash,
},
udt_icon_file: udt_account.udt_icon_file,
udt_type: udt_account.udt_type
udt_type: udt_account.udt_type,
}
elsif udt_account.udt_type == "nrc_721_token"
udt = udt_account.udt
Sentry.capture_message("Missing nrc_factory_cell", extra: {
address: object.address_hash,
udt: udt.symbol,
full_name: udt.full_name,
code_hash: udt.code_hash,
args: udt.args
})
address: object.address_hash,
udt: udt.symbol,
full_name: udt.full_name,
code_hash: udt.code_hash,
args: udt.args,
})
factory_cell = udt_account.udt.nrc_factory_cell
coll = factory_cell&.token_collection
{
symbol: factory_cell&.symbol || udt.symbol,
amount: udt_account.nft_token_id.to_s,
type_hash: udt_account.type_hash,
collection: {
type_hash: coll&.type_script&.script_hash
type_hash: coll&.type_script&.script_hash,
},
udt_icon_file: "#{udt_account.udt.nrc_factory_cell&.base_token_uri}/#{udt_account.nft_token_id}",
udt_type: udt_account.udt_type
udt_type: udt_account.udt_type,
}
elsif udt_account.udt_type == "spore_cell"
ts = TypeScript.where(script_hash: udt_account.type_hash).first
Expand All @@ -97,10 +111,10 @@ class AddressSerializer
amount: udt_account.nft_token_id.to_s,
type_hash: udt_account.type_hash,
collection: {
type_hash: coll&.type_script&.script_hash
type_hash: coll&.type_script&.script_hash,
},
udt_icon_file: data,
udt_type: udt_account.udt_type
udt_type: udt_account.udt_type,
}
end
end
Expand Down
2 changes: 1 addition & 1 deletion config/settings.mainnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ omiga_inscription_info_code_hash: ""
omiga_inscription_code_hash: ""

# xudt
xudt_code_hash: ""
xudt_code_hash: "0x50bd8d6680b8b9cf98b73f3c08faf8b2a21914311954118ad6609be6e78a1b95"

# hash length of an attribute(especially which comes from bytea column),
# e.g. Block.uncle_block_hashes: "0x587f354162afd133b4a4f7a4b621d11e043c3c08b0af2801f1686b5403b14953", which has a length of 66 ( 2 + 64)
Expand Down
86 changes: 86 additions & 0 deletions lib/tasks/migration/update_omiga_inscription_udt.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
namespace :migration do
desc "Usage: RAILS_ENV=production bundle exec rake migration:update_omiga_inscription_udt"
task update_omiga_inscription_udt: :environment do
info_ts_ids = TypeScript.where(code_hash: CkbSync::Api.instance.omiga_inscription_info_code_hash).pluck(:id)
info_outputs = CellOutput.where(type_script_id: info_ts_ids)
info_outputs.update_all(cell_type: "omiga_inscription_info")

info_outputs.each do |output|
info = CkbUtils.parse_omiga_inscription_info(output.data)
# ignore old version data
if output.data.slice(-2..-1).in?(["00", "01", "02"])
OmigaInscriptionInfo.upsert(info.merge(output.type_script.to_node),
unique_by: :udt_hash)
end
end

xudt_ts_ids = TypeScript.where(code_hash: CkbSync::Api.instance.xudt_code_hash).pluck(:id)
xudt_ts_ids.each do |tid|
xudt_outputs = CellOutput.where(type_script_id: tid)
xudt_outputs.each do |output|
if OmigaInscriptionInfo.where(udt_hash: output.type_hash).exists?
info = OmigaInscriptionInfo.find_by!(udt_hash: output.type_hash)
output.update(cell_type: "omiga_inscription",
udt_amount: info.mint_limit)
if info.udt_id.nil?
nft_token_attr = {}
nft_token_attr[:full_name] = info.name.presence
nft_token_attr[:symbol] = info.symbol.presence
nft_token_attr[:decimal] = info.decimal
nft_token_attr[:published] = true
udt = Udt.create_or_find_by!({
type_hash: output.type_hash,
udt_type: "omiga_inscription",
block_timestamp: output.block.timestamp,
args: output.type_script.args,
code_hash: output.type_script.code_hash,
hash_type: output.type_script.hash_type,
}.merge(nft_token_attr))
info.update!(udt_id: udt.id)
end
else
output.update(cell_type: "xudt")
end
end
end

# udt_transaction
Udt.where(udt_type: "omiga_inscription").each do |udt|
outputs = CellOutput.omiga_inscription.where(type_hash: udt.type_hash).select(
:address_id, :ckb_transaction_id
).distinct
udt_transaction_attrs =
outputs.map do |output|
{ udt_id: udt.id, ckb_transaction_id: output.ckb_transaction_id }
end
UdtTransaction.insert_all(udt_transaction_attrs)

address_udt_transaction_attrs =
outputs.map do |output|
{ address_id: output.address_id,
ckb_transaction_id: output.ckb_transaction_id }
end
AddressUdtTransaction.insert_all(address_udt_transaction_attrs)
end

# udt_account
Udt.where(udt_type: "omiga_inscription").each do |udt|
# {address_id => udt_amount}
results = CellOutput.live.omiga_inscription.where(type_hash: udt.type_hash).select(:address_id).group(:address_id).sum(:udt_amount)
attrs =
results.map do |address_id, udt_amount|
{
address_id:, udt_type: udt.udt_type, full_name: udt.full_name, symbol: udt.symbol, decimal: udt.decimal,
published: udt.published, code_hash: udt.code_hash, type_hash: udt.type_hash, amount: udt_amount, udt_id: udt.id
}
end

unless attrs.empty?
UdtAccount.insert_all(attrs)
udt.update(total_amount: results.sum { |_k, v| v })
end
end

puts "done"
end
end
Loading