Skip to content
Open
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
15 changes: 14 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
target
.vscode
.vscode
# Generated by Cargo
# will have compiled files and executables
**/target/
# These are backup files generated by rustfmt
**/*.rs.bk

.DS_Store

# The cache for docker container dependency
.cargo

# The cache for chain data in container
.local
18 changes: 17 additions & 1 deletion Cargo.lock

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

8 changes: 8 additions & 0 deletions bonding-curve/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ version = '2.0.0'
default-features = false
version = '2.0.0'

[dependencies.orml-currencies]
default-features = false
git = 'https://github.com/open-web3-stack/open-runtime-module-library'
version = '0.3.0'
rev = 'e5f3be0372e7f2f2e2c9a0079633e92115c8feb4'


[dev-dependencies]

[dev-dependencies.sp-core]
Expand All @@ -72,6 +79,7 @@ std = [
'codec/std',
'frame-support/std',
'frame-system/std',
'orml-currencies/std',
'orml-tokens/std',
'orml-traits/std',
'pallet-balances/std',
Expand Down
6 changes: 4 additions & 2 deletions bonding-curve/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl<
/// Integral when the curve is at point `x`.
pub fn integral(&self, x: u128) -> u128 {
let nexp = self.exponent + 1;
x.pow(nexp) * self.slope / nexp as u128
x.pow(nexp) / 1_000_000_000_000 * self.slope / nexp as u128
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ decl_module! {
/// Creates a new bonding curve.
///
#[weight = 0]
pub fn create(origin, currency_id: CurrencyIdOf<T>, exponent: u32, slope: u128, max_supply: u128) {
pub fn create(origin, currency_id: CurrencyIdOf<T>, exponent: u32, slope: u128, max_supply: u128, initial_supply: u128) {
let sender = ensure_signed(origin)?;

// Requires an amount to be reserved.
Expand All @@ -138,6 +138,8 @@ decl_module! {
// Adds 1 of the token to the module account.
T::Currency::deposit(currency_id, &Self::module_account(), 1.saturated_into())?;

T::Currency::deposit(currency_id, &sender, initial_supply.saturated_into())?;

let new_curve = BondingCurve {
creator: sender.clone(),
currency_id,
Expand Down
45 changes: 42 additions & 3 deletions bonding-curve/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use sp_runtime::{
traits::{BlakeTwo256, IdentityLookup},
testing::Header, Perbill,
};
use orml_currencies::BasicCurrencyAdapter;

pub type AccountId = u128;
pub type Amount = i128;
Expand All @@ -18,7 +19,6 @@ pub const BOB: AccountId = 1;
impl_outer_origin! {
pub enum Origin for Test {}
}

#[derive(Clone, Eq, PartialEq)]
pub struct Test;

Expand Down Expand Up @@ -67,25 +67,54 @@ impl orml_tokens::Trait for Test {
}

parameter_types! {
pub const GetNativeCurrencyId: CurrencyId = 0;
pub const ExistentialDeposit: u64 = 10;
pub const MaxLocks: u32 = 10;
}

impl pallet_balances::Trait for Test {
type Balance = u128;
type Event = ();
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = frame_system::Module<Test>;
type WeightInfo = ();
type MaxLocks = MaxLocks;
}

parameter_types! {
pub const GetNativeCurrencyId: u128 = 0;
}

impl orml_currencies::Trait for Test {
type Event = ();
type MultiCurrency = Tokens;
type NativeCurrency = BasicCurrencyAdapter<Test, Balances, i128, u32>;
type GetNativeCurrencyId = GetNativeCurrencyId;
type WeightInfo = ();
}

parameter_types! {
pub const CurveDeposit: Balance = 10;
pub const BondingCurveModuleId: ModuleId = ModuleId(*b"mtg/bonc");
}

impl Trait for Test {
type Event = ();
type Currency = orml_tokens::Module<Test>;
type Currency = Currencies;
type GetNativeCurrencyId = GetNativeCurrencyId;
type CurveDeposit = CurveDeposit;
type ModuleId = BondingCurveModuleId;
}

pub type Balances = pallet_balances::Module<Test>;
pub type Currencies = orml_currencies::Module<Test>;
pub type System = frame_system::Module<Test>;
pub type Tokens = orml_tokens::Module<Test>;
pub type BondingCurve = Module<Test>;

pub struct ExtBuilder {
endowed_accounts: Vec<(AccountId, CurrencyId, Balance)>,
balances: Vec<(AccountId, Balance)>,
}

impl Default for ExtBuilder {
Expand All @@ -96,6 +125,10 @@ impl Default for ExtBuilder {
(BOB, 0, 1_000),

],
balances: vec![
(ALICE, 1_000_000_000_000),
(BOB, 1_000_000_000_000),
]
}
}
}
Expand All @@ -112,6 +145,12 @@ impl ExtBuilder {
.assimilate_storage(&mut t)
.unwrap();

pallet_balances::GenesisConfig::<Test> {
balances: self.balances,
}
.assimilate_storage(&mut t)
.unwrap();

t.into()
}
}
29 changes: 20 additions & 9 deletions bonding-curve/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{Error, mock::*};
use frame_support::{
assert_noop, assert_ok, traits::OnInitialize,
assert_noop, assert_ok,
};
use orml_traits::MultiCurrency;

Expand All @@ -13,8 +13,9 @@ fn it_creates_a_new_bonding_curve() {
Origin::signed(ALICE),
0, // currency_id | Creator token $ANSON
1,
100,
1,
1000,
0,
),
Error::<Test>::CurrencyAlreadyExists,
);
Expand All @@ -24,16 +25,17 @@ fn it_creates_a_new_bonding_curve() {
Origin::signed(ALICE),
1,
1,
100,
1,
1000,
0
)
);

let bc = BondingCurve::curves(0).unwrap();
assert_eq!(bc.creator, ALICE);
assert_eq!(bc.currency_id, 1);
assert_eq!(bc.exponent, 1);
assert_eq!(bc.slope, 100);
assert_eq!(bc.slope, 1);

// TODO: Ensure funds are reserved.
});
Expand All @@ -48,20 +50,28 @@ fn it_can_buy_from_a_bonding_curve() {
Origin::signed(ALICE),
1,
1,
100,
1000,
1,
1_000_000_000_000_000,
0
)
);

// Bob buys 2 tokens.

let bob_bal_before = Balances::free_balance(&BOB);

// Bob buys 1 tokens.
assert_ok!(
BondingCurve::buy(
Origin::signed(BOB),
0,
2,
1_000_000_000_000,
)
);

let bob_bal_after = Balances::free_balance(&BOB);
assert_eq!(bob_bal_before, 1_000_000_000_000);
assert_eq!(bob_bal_after, 499_999_999_999);

let native_balance = Tokens::free_balance(0, &BOB);
let token_balance = Tokens::free_balance(1, &BOB);
let diff = 1_000 - native_balance;
Expand All @@ -79,8 +89,9 @@ fn it_can_sell_to_a_bonding_curve() {
Origin::signed(ALICE),
1,
1,
100,
1,
1000,
0
)
);

Expand Down
1 change: 1 addition & 0 deletions rust-toolchain
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nightly-2020-10-06
26 changes: 26 additions & 0 deletions substrate-node-template/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "Substrate Node template",
"context": "..",
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"lldb.executable": "/usr/bin/lldb"
},
"extensions": [
"rust-lang.rust",
"bungcip.better-toml",
"vadimcn.vscode-lldb"
],
"forwardPorts": [
3000,
9944
],
"preCreateCommand": "cargo build --release && cargo check",
"postStartCommand": "./target/release/node-template --dev --ws-external",
"menuActions": [
{"id": "polkadotjs",
"label": "Open PolkadotJS Apps",
"type": "external-preview",
"args": ["https://polkadot.js.org/apps/?rpc=wss%3A%2F%2F/$HOST/wss"]}
],
"image": "paritytech/substrate-playground-template-node-template:latest"
}
22 changes: 22 additions & 0 deletions substrate-node-template/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
root = true
[*.rs]
indent_style=tab
indent_size=tab
tab_width=4
end_of_line=lf
charset=utf-8
trim_trailing_whitespace=true
max_line_length=100
insert_final_newline=true

[*.yml]
indent_style=space
indent_size=2
tab_width=8
end_of_line=lf

[*.sh]
indent_style=space
indent_size=2
tab_width=8
end_of_line=lf
13 changes: 13 additions & 0 deletions substrate-node-template/.github/ISSUE_TEMPLATE/ask-a-question.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
name: Ask a Question
about: Ask a question about this template.
title: ""
labels: question
assignees: ""
---

**Question**

_Please include information such as the following: is your question to clarify an existing resource
or are you asking about something new? what are you trying to accomplish? where have you looked for
answers?_
Loading