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

feat: refactor ext API and add new NEP264 functionality #742

Merged
merged 20 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
update ext_contract API
  • Loading branch information
austinabell committed Mar 9, 2022
commit f8bde2c0cedf8247a28c5e2294cc9a1d8d1aee53
14 changes: 4 additions & 10 deletions examples/cross-contract-high-level/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,23 +95,17 @@ impl CrossContract {
// }

pub fn simple_call(&mut self, account_id: AccountId, message: String) {
ext_status_message::set_status(message, account_id, 0, env::prepaid_gas() / 2);
ext_status_message::ext(account_id).set_status(message);
}
pub fn complex_call(&mut self, account_id: AccountId, message: String) -> Promise {
// 1) call status_message to record a message from the signer.
// 2) call status_message to retrieve the message of the signer.
// 3) return that message as its own result.
// Note, for a contract to simply call another contract (1) is sufficient.
let prepaid_gas = env::prepaid_gas();
log!("complex_call");
ext_status_message::set_status(message, account_id.clone(), 0, prepaid_gas / 3).then(
ext_status_message::get_status(
env::signer_account_id(),
account_id,
0,
prepaid_gas / 3,
),
)
ext_status_message::ext(account_id.clone())
.set_status(message)
.then(ext_status_message::ext(account_id).get_status(env::signer_account_id()))
}

pub fn transfer_money(&mut self, account_id: AccountId, amount: u64) {
Expand Down
40 changes: 10 additions & 30 deletions near-contract-standards/src/fungible_token/core_impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::fungible_token::core::FungibleTokenCore;
use crate::fungible_token::events::FtTransfer;
use crate::fungible_token::resolver::FungibleTokenResolver;
use crate::fungible_token::resolver::{fungible_token_resolver_ext, FungibleTokenResolver};
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::LookupMap;
use near_sdk::json_types::U128;
Expand All @@ -12,18 +12,6 @@ use near_sdk::{
const GAS_FOR_RESOLVE_TRANSFER: Gas = Gas(5_000_000_000_000);
const GAS_FOR_FT_TRANSFER_CALL: Gas = Gas(25_000_000_000_000 + GAS_FOR_RESOLVE_TRANSFER.0);

const NO_DEPOSIT: Balance = 0;

#[ext_contract(ext_self)]
trait FungibleTokenResolver {
fn ft_resolve_transfer(
&mut self,
sender_id: AccountId,
receiver_id: AccountId,
amount: U128,
) -> U128;
}

#[ext_contract(ext_fungible_token_receiver)]
pub trait FungibleTokenReceiver {
fn ft_on_transfer(
Expand Down Expand Up @@ -179,23 +167,15 @@ impl FungibleTokenCore for FungibleToken {
let amount: Balance = amount.into();
self.internal_transfer(&sender_id, &receiver_id, amount, memo);
// Initiating receiver's call and the callback
ext_fungible_token_receiver::ft_on_transfer(
sender_id.clone(),
amount.into(),
msg,
receiver_id.clone(),
NO_DEPOSIT,
env::prepaid_gas() - GAS_FOR_FT_TRANSFER_CALL,
)
.then(ext_self::ft_resolve_transfer(
sender_id,
receiver_id,
amount.into(),
env::current_account_id(),
NO_DEPOSIT,
GAS_FOR_RESOLVE_TRANSFER,
))
.into()
ext_fungible_token_receiver::ext(receiver_id.clone())
.with_static_gas(env::prepaid_gas() - GAS_FOR_FT_TRANSFER_CALL)
.ft_on_transfer(sender_id.clone(), amount.into(), msg)
.then(
fungible_token_resolver_ext::ext(env::current_account_id())
.with_static_gas(GAS_FOR_RESOLVE_TRANSFER)
.ft_resolve_transfer(sender_id, receiver_id, amount.into()),
)
.into()
}

fn ft_total_supply(&self) -> U128 {
Expand Down
3 changes: 2 additions & 1 deletion near-contract-standards/src/fungible_token/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use near_sdk::{json_types::U128, AccountId};
use near_sdk::{ext_contract, json_types::U128, AccountId};

#[ext_contract(fungible_token_resolver_ext)]
pub trait FungibleTokenResolver {
fn ft_resolve_transfer(
&mut self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ use crate::non_fungible_token::utils::{
refund_approved_account_ids_iter, refund_deposit,
};
use crate::non_fungible_token::NonFungibleToken;
use near_sdk::{assert_one_yocto, env, ext_contract, require, AccountId, Balance, Gas, Promise};
use near_sdk::{assert_one_yocto, env, ext_contract, require, AccountId, Gas, Promise};

const GAS_FOR_NFT_APPROVE: Gas = Gas(10_000_000_000_000);
const NO_DEPOSIT: Balance = 0;

fn expect_token_found<T>(option: Option<T>) -> T {
option.unwrap_or_else(|| env::panic_str("Token not found"))
Expand Down Expand Up @@ -69,15 +68,9 @@ impl NonFungibleTokenApproval for NonFungibleToken {

// if given `msg`, schedule call to `nft_on_approve` and return it. Else, return None.
msg.map(|msg| {
ext_approval_receiver::nft_on_approve(
token_id,
owner_id,
approval_id,
msg,
account_id,
NO_DEPOSIT,
env::prepaid_gas() - GAS_FOR_NFT_APPROVE,
)
ext_approval_receiver::ext(account_id)
.with_static_gas(env::prepaid_gas() - GAS_FOR_NFT_APPROVE)
.nft_on_approve(token_id, owner_id, approval_id, msg)
})
}

Expand Down
32 changes: 10 additions & 22 deletions near-contract-standards/src/non_fungible_token/core/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::collections::{LookupMap, TreeMap, UnorderedSet};
use near_sdk::json_types::Base64VecU8;
use near_sdk::{
assert_one_yocto, env, ext_contract, require, AccountId, Balance, BorshStorageKey, CryptoHash,
assert_one_yocto, env, ext_contract, require, AccountId, BorshStorageKey, CryptoHash,
Gas, IntoStorageKey, PromiseOrValue, PromiseResult, StorageUsage,
};
use std::collections::HashMap;

const GAS_FOR_RESOLVE_TRANSFER: Gas = Gas(5_000_000_000_000);
const GAS_FOR_NFT_TRANSFER_CALL: Gas = Gas(25_000_000_000_000 + GAS_FOR_RESOLVE_TRANSFER.0);

const NO_DEPOSIT: Balance = 0;

#[ext_contract(ext_self)]
trait NFTResolver {
fn nft_resolve_transfer(
Expand Down Expand Up @@ -434,25 +432,15 @@ impl NonFungibleTokenCore for NonFungibleToken {
let (old_owner, old_approvals) =
self.internal_transfer(&sender_id, &receiver_id, &token_id, approval_id, memo);
// Initiating receiver's call and the callback
ext_receiver::nft_on_transfer(
sender_id,
old_owner.clone(),
token_id.clone(),
msg,
receiver_id.clone(),
NO_DEPOSIT,
env::prepaid_gas() - GAS_FOR_NFT_TRANSFER_CALL,
)
.then(ext_self::nft_resolve_transfer(
old_owner,
receiver_id,
token_id,
old_approvals,
env::current_account_id(),
NO_DEPOSIT,
GAS_FOR_RESOLVE_TRANSFER,
))
.into()
ext_receiver::ext(receiver_id.clone())
.with_static_gas(env::prepaid_gas() - GAS_FOR_NFT_TRANSFER_CALL)
.nft_on_transfer(sender_id, old_owner.clone(), token_id.clone(), msg)
.then(
ext_self::ext(env::current_account_id())
.with_static_gas(GAS_FOR_RESOLVE_TRANSFER)
.nft_resolve_transfer(old_owner, receiver_id, token_id, old_approvals),
)
.into()
}

fn nft_token(&self, token_id: TokenId) -> Option<Token> {
Expand Down
Loading