-
Notifications
You must be signed in to change notification settings - Fork 12
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
xCheddar impl #14
base: master
Are you sure you want to change the base?
xCheddar impl #14
Changes from 1 commit
84d9a0e
1b4a637
0bbc253
1183212
5b3d5a5
976df7e
428f503
b03eecb
1fad008
d2d86b3
c43d8d6
1584f72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ members = [ | |
# "./p1-staking-pool-dyn", | ||
"./p2-token-staking-fixed", | ||
"./p3-farm", | ||
"./xcheddar" | ||
] | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,10 +11,10 @@ crate-type = ["cdylib", "rlib"] | |||||
[dependencies] | ||||||
serde = { version = "*", features = ["derive"] } | ||||||
serde_json = "*" | ||||||
near-sdk = { git = "https://github.com/near/near-sdk-rs", tag="3.1.0" } | ||||||
near-contract-standards = { git = "https://github.com/near/near-sdk-rs", tag="3.1.0" } | ||||||
near-sdk = "4.0.0-pre.7" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. v4 is already released, let's don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can already use v4.1.0-pre |
||||||
near-sys = "0.1.0" | ||||||
near-contract-standards = "4.0.0-pre.7" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
uint = { version = "0.9.0", default-features = false } | ||||||
|
||||||
[dev-dependencies] | ||||||
# near-primitives = { git = "https://github.com/nearprotocol/nearcore.git" } | ||||||
# near-sdk-sim = { git = "https://github.com/near/near-sdk-rs.git", version="v3.1.0" } | ||||||
near-sdk-sim = "4.0.0-pre.7" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
/// Cheddar Token | ||
/// | ||
/// Functionality: | ||
/// - No account storage complexity - Since NEAR slashed storage price by 10x | ||
/// it does not make sense to add that friction (storage backup per user). | ||
|
@@ -16,19 +15,11 @@ use near_contract_standards::fungible_token::{ | |
}; | ||
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | ||
use near_sdk::collections::{LazyOption, LookupMap}; | ||
use near_sdk::json_types::{ValidAccountId, U128}; | ||
use near_sdk::json_types::U128; | ||
use near_sdk::{ | ||
assert_one_yocto, env, ext_contract, log, near_bindgen, AccountId, Balance, Gas, | ||
assert_one_yocto, env, ext_contract, log, near_bindgen, AccountId, Balance, | ||
PanicOnDefault, PromiseOrValue, | ||
}; | ||
|
||
const TGAS: Gas = 1_000_000_000_000; | ||
const GAS_FOR_RESOLVE_TRANSFER: Gas = 5 * TGAS; | ||
const GAS_FOR_FT_TRANSFER_CALL: Gas = 25 * TGAS + GAS_FOR_RESOLVE_TRANSFER; | ||
const NO_DEPOSIT: Balance = 0; | ||
|
||
near_sdk::setup_alloc!(); | ||
|
||
mod internal; | ||
mod migrations; | ||
mod storage; | ||
|
@@ -165,9 +156,14 @@ impl Contract { | |
self.metadata.set(&m); | ||
} | ||
|
||
pub fn set_owner(&mut self, owner_id: ValidAccountId) { | ||
pub fn set_owner(&mut self, owner_id: AccountId) { | ||
self.assert_owner(); | ||
self.owner_id = owner_id.as_ref().clone(); | ||
assert!( | ||
env::is_valid_account_id(owner_id.as_bytes()), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we don't need to validate, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFIAK, v4 validates account automatically |
||
"Account @{} is invalid!", | ||
owner_id.clone() | ||
); | ||
self.owner_id = owner_id.clone(); | ||
} | ||
|
||
/// Get the owner of this account. | ||
|
@@ -240,40 +236,40 @@ impl Contract { | |
#[near_bindgen] | ||
impl FungibleTokenCore for Contract { | ||
#[payable] | ||
fn ft_transfer(&mut self, receiver_id: ValidAccountId, amount: U128, memo: Option<String>) { | ||
fn ft_transfer(&mut self, receiver_id: AccountId, amount: U128, memo: Option<String>) { | ||
robert-zaremba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_one_yocto(); | ||
let sender_id = env::predecessor_account_id(); | ||
let amount: Balance = amount.into(); | ||
self.internal_transfer(&sender_id, receiver_id.as_ref(), amount, memo); | ||
self.internal_transfer(&sender_id, &receiver_id, amount, memo); | ||
} | ||
|
||
#[payable] | ||
fn ft_transfer_call( | ||
&mut self, | ||
receiver_id: ValidAccountId, | ||
receiver_id: AccountId, | ||
amount: U128, | ||
memo: Option<String>, | ||
msg: String, | ||
) -> PromiseOrValue<U128> { | ||
assert_one_yocto(); | ||
let sender_id = env::predecessor_account_id(); | ||
let amount: Balance = amount.into(); | ||
self.internal_transfer(&sender_id, receiver_id.as_ref(), amount, memo); | ||
self.internal_transfer(&sender_id, &receiver_id, amount, memo); | ||
// Initiating receiver's call and the callback | ||
// ext_fungible_token_receiver::ft_on_transfer( | ||
ext_ft_receiver::ft_on_transfer( | ||
sender_id.clone(), | ||
amount.into(), | ||
msg, | ||
receiver_id.as_ref(), | ||
receiver_id.clone(), | ||
NO_DEPOSIT, | ||
env::prepaid_gas() - GAS_FOR_FT_TRANSFER_CALL, | ||
) | ||
.then(ext_self::ft_resolve_transfer( | ||
sender_id, | ||
receiver_id.into(), | ||
receiver_id, | ||
amount.into(), | ||
&env::current_account_id(), | ||
env::current_account_id(), | ||
NO_DEPOSIT, | ||
GAS_FOR_RESOLVE_TRANSFER, | ||
)) | ||
|
@@ -284,8 +280,8 @@ impl FungibleTokenCore for Contract { | |
self.total_supply.into() | ||
} | ||
|
||
fn ft_balance_of(&self, account_id: ValidAccountId) -> U128 { | ||
self._balance_of(account_id.as_ref()).into() | ||
fn ft_balance_of(&self, account_id: AccountId) -> U128 { | ||
self._balance_of(&account_id).into() | ||
} | ||
} | ||
|
||
|
@@ -297,8 +293,8 @@ impl FungibleTokenResolver for Contract { | |
#[private] | ||
fn ft_resolve_transfer( | ||
&mut self, | ||
sender_id: ValidAccountId, | ||
receiver_id: ValidAccountId, | ||
sender_id: AccountId, | ||
receiver_id: AccountId, | ||
amount: U128, | ||
) -> U128 { | ||
let sender_id: AccountId = sender_id.into(); | ||
|
@@ -347,7 +343,7 @@ mod tests { | |
|
||
const OWNER_SUPPLY: Balance = 1_000_000_000_000_000_000_000_000_000_000; | ||
|
||
fn get_context(predecessor_account_id: ValidAccountId) -> VMContextBuilder { | ||
fn get_context(predecessor_account_id: AccountId) -> VMContextBuilder { | ||
let mut builder = VMContextBuilder::new(); | ||
builder | ||
.current_account_id(accounts(0)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,59 +1,52 @@ | ||
//! Implement all the relevant logic for smart contract upgrade. | ||
|
||
use crate::*; | ||
|
||
#[cfg(target_arch = "wasm32")] | ||
mod upgrade { | ||
use near_sdk::env::BLOCKCHAIN_INTERFACE; | ||
use near_sdk::env; | ||
use near_sdk::Gas; | ||
use crate::Contract; | ||
use near_sys as sys; | ||
|
||
use super::*; | ||
|
||
const BLOCKCHAIN_INTERFACE_NOT_SET_ERR: &str = "Blockchain interface not set."; | ||
|
||
/// Gas for calling migration call. | ||
pub const GAS_FOR_MIGRATE_CALL: Gas = 5_000_000_000_000; | ||
use crate::util::*; | ||
|
||
/// Self upgrade and call migrate, optimizes gas by not loading into memory the code. | ||
/// Takes as input non serialized set of bytes of the code. | ||
/// After upgrade we call *pub fn migrate()* on the NEW CONTRACT CODE | ||
#[no_mangle] | ||
pub extern "C" fn upgrade() { | ||
pub fn upgrade() { | ||
/// Gas for calling migration call. One Tera - 1 TGas | ||
/// 20 Tgas | ||
pub const GAS_FOR_UPGRADE: Gas = Gas(20_000_000_000_000); | ||
const BLOCKCHAIN_INTERFACE_NOT_SET_ERR: &str = "Blockchain interface not set."; | ||
|
||
env::setup_panic_hook(); | ||
env::set_blockchain_interface(Box::new(near_blockchain::NearBlockchain {})); | ||
|
||
///assert ownership | ||
#[allow(unused_doc_comments)] | ||
let contract: Contract = env::state_read().expect("ERR_CONTRACT_IS_NOT_INITIALIZED"); | ||
contract.assert_owner(); | ||
let current_id = env::current_account_id().into_bytes(); | ||
let method_name = "migrate".as_bytes().to_vec(); | ||
|
||
let current_id = env::current_account_id(); | ||
let migrate_method_name = "migrate".as_bytes().to_vec(); | ||
let attached_gas = env::prepaid_gas() - env::used_gas() - GAS_FOR_UPGRADE; | ||
unsafe { | ||
BLOCKCHAIN_INTERFACE.with(|b| { | ||
// Load input into register 0. | ||
b.borrow() | ||
.as_ref() | ||
.expect(BLOCKCHAIN_INTERFACE_NOT_SET_ERR) | ||
.input(0); | ||
let promise_id = b | ||
.borrow() | ||
.as_ref() | ||
.expect(BLOCKCHAIN_INTERFACE_NOT_SET_ERR) | ||
.promise_batch_create(current_id.len() as _, current_id.as_ptr() as _); | ||
b.borrow() | ||
.as_ref() | ||
.expect(BLOCKCHAIN_INTERFACE_NOT_SET_ERR) | ||
.promise_batch_action_deploy_contract(promise_id, u64::MAX as _, 0); | ||
let attached_gas = env::prepaid_gas() - env::used_gas() - GAS_FOR_MIGRATE_CALL; | ||
b.borrow() | ||
.as_ref() | ||
.expect(BLOCKCHAIN_INTERFACE_NOT_SET_ERR) | ||
.promise_batch_action_function_call( | ||
promise_id, | ||
method_name.len() as _, | ||
method_name.as_ptr() as _, | ||
0 as _, | ||
0 as _, | ||
0 as _, | ||
attached_gas, | ||
); | ||
}); | ||
// Load input (NEW CONTRACT CODE) into register 0. | ||
sys::input(0); | ||
// prepare self-call promise | ||
let promise_id = sys::promise_batch_create(current_id.as_bytes().len() as _, current_id.as_bytes().as_ptr() as _); | ||
|
||
// #Action_1 - deploy/upgrade code from register 0 | ||
sys::promise_batch_action_deploy_contract(promise_id, u64::MAX as _, 0); | ||
// #Action_2 - schedule a call for migrate | ||
// Execute on NEW CONTRACT CODE | ||
sys::promise_batch_action_function_call( | ||
promise_id, | ||
migrate_method_name.len() as _, | ||
migrate_method_name.as_ptr() as _, | ||
0 as _, | ||
0 as _, | ||
0 as _, | ||
u64::from(attached_gas), | ||
); | ||
robert-zaremba marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dev-1654515440352-14631167054094 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CONTRACT_NAME=dev-1654515440352-14631167054094 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you add a links to CRV and veCRV?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ping
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robert-zaremba yep, what’s required? I update all with ahead commits
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
links to explain CRV and veCRV
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1b4a637