From 2b5c1f061e731dd3d3f9e8edc2095d5ac4e40329 Mon Sep 17 00:00:00 2001 From: shaojunda Date: Wed, 26 Feb 2020 15:08:28 +0800 Subject: [PATCH] feat: add udt account model --- app/models/udt_account.rb | 29 +++++++++++++++++++ .../20200226063458_create_udt_accounts.rb | 14 +++++++++ db/schema.rb | 14 ++++++++- test/factories/udt_account.rb | 10 +++++++ test/models/udt_account_test.rb | 7 +++++ 5 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 app/models/udt_account.rb create mode 100644 db/migrate/20200226063458_create_udt_accounts.rb create mode 100644 test/factories/udt_account.rb create mode 100644 test/models/udt_account_test.rb diff --git a/app/models/udt_account.rb b/app/models/udt_account.rb new file mode 100644 index 000000000..598e7c30c --- /dev/null +++ b/app/models/udt_account.rb @@ -0,0 +1,29 @@ +class UdtAccount < ApplicationRecord + enum udt_type: { sudt: 0 } + + belongs_to :address + + validates_presence_of :full_name, :symbol, :decimal, :amount + validates_length_of :symbol, minimum: 1, maximum: 16 + validates :decimal, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 39 } + validates :amount, numericality: { greater_than_or_equal_to: 0 } +end + +# == Schema Information +# +# Table name: udt_accounts +# +# id :bigint not null, primary key +# udt_type :integer +# full_name :string +# symbol :string +# decimal :integer +# amount :decimal(40, ) +# address_id :bigint +# created_at :datetime not null +# updated_at :datetime not null +# +# Indexes +# +# index_udt_accounts_on_address_id (address_id) +# diff --git a/db/migrate/20200226063458_create_udt_accounts.rb b/db/migrate/20200226063458_create_udt_accounts.rb new file mode 100644 index 000000000..aefa6eea9 --- /dev/null +++ b/db/migrate/20200226063458_create_udt_accounts.rb @@ -0,0 +1,14 @@ +class CreateUdtAccounts < ActiveRecord::Migration[6.0] + def change + create_table :udt_accounts do |t| + t.integer :udt_type + t.string :full_name + t.string :symbol + t.integer :decimal + t.decimal :amount, precision: 40 + t.references :address + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c4b54f25a..f347d79b7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_01_22_060907) do +ActiveRecord::Schema.define(version: 2020_02_26_063458) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -300,6 +300,18 @@ t.index ["cell_output_id"], name: "index_type_scripts_on_cell_output_id" end + create_table "udt_accounts", force: :cascade do |t| + t.integer "udt_type" + t.string "full_name" + t.string "symbol" + t.integer "decimal" + t.decimal "amount", precision: 40 + t.bigint "address_id" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.index ["address_id"], name: "index_udt_accounts_on_address_id" + end + create_table "uncle_blocks", force: :cascade do |t| t.binary "block_hash" t.decimal "number", precision: 30 diff --git a/test/factories/udt_account.rb b/test/factories/udt_account.rb new file mode 100644 index 000000000..58d86cdcc --- /dev/null +++ b/test/factories/udt_account.rb @@ -0,0 +1,10 @@ +FactoryBot.define do + factory :udt_account do + address + udt_type { "sudt" } + full_name { "kingdom fat coin" } + symbol { "kfc" } + decimal { 6 } + amount { 100000000000 * 10**6 } + end +end diff --git a/test/models/udt_account_test.rb b/test/models/udt_account_test.rb new file mode 100644 index 000000000..9e49179a8 --- /dev/null +++ b/test/models/udt_account_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class UdtAccountTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end