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

(WIP) refactor(promise)!: refactor promises to schedule at end of function call and allow automatically assigning gas #523

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7d53462
fix(env): Update method name parameters in env to string equivalents
austinabell Aug 3, 2021
fe5ba49
revert removal of conversion of params
austinabell Aug 4, 2021
f995bda
fix help doc
austinabell Aug 4, 2021
2effdf8
update changelog
austinabell Aug 5, 2021
78995da
add PR link
austinabell Aug 5, 2021
a8fbad6
wip setting up queued function call API
austinabell Aug 5, 2021
643eaf6
fix: update ext contract codegen function signature
austinabell Aug 5, 2021
24059a5
wip setup private
austinabell Aug 5, 2021
3a90f36
fix: update ext contract codegen function signature
austinabell Aug 5, 2021
b5b5647
fix: ext contract codegen to take AccountId instead of going through …
austinabell Aug 5, 2021
b263ff0
remove unused ToString imports
austinabell Aug 5, 2021
2e5eccb
changelog
austinabell Aug 5, 2021
d81c952
fix more doc tests
austinabell Aug 5, 2021
37a9c80
rebuild contracts from line changes
austinabell Aug 5, 2021
4d6da5c
fix import
austinabell Aug 5, 2021
9d8d6de
Merge branch 'austin/ext_fix_sig' of github.com:near/near-sdk-rs into…
austinabell Aug 5, 2021
8bbec60
schedule function calls at end of methods
austinabell Aug 5, 2021
7baea1a
wip swapping codegen to use new type
austinabell Aug 5, 2021
90eae1b
setup refactor to include all types of promises
austinabell Aug 8, 2021
e4ba335
setup scheduling and scope function return
austinabell Aug 9, 2021
ea8e0eb
Merge branch 'master' of github.com:near/near-sdk-rs into austin/tmp/…
austinabell Aug 9, 2021
b0832c5
very naive and broken gas calc
austinabell Aug 9, 2021
03cae8c
refactor promises from drop pattern
austinabell Aug 9, 2021
e7f0039
update macro gen
austinabell Aug 9, 2021
5876b9b
cleanup and rebuild wasm
austinabell Aug 9, 2021
5e9b600
fix doc tests and clippy
austinabell Aug 9, 2021
5cdda8d
rebuild wasm
austinabell Aug 9, 2021
a74a5e8
swap BTreeMap to try to reduce code size
austinabell Aug 9, 2021
ca68ec4
fix more doc tests
austinabell Aug 9, 2021
c547b7b
fmt
austinabell Aug 9, 2021
19ad8d3
Update examples/cross-contract-high-level/src/lib.rs
austinabell Aug 11, 2021
96b275f
Merge branch 'master' of github.com:near/near-sdk-rs into austin/tmp/…
austinabell Aug 12, 2021
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

## [unreleased]
* Update `panic` and `panic_utf8` syscall signatures to indicate they do not return. [PR 492](https://github.com/near/near-sdk-rs/pull/492)
- Update `panic` and `panic_utf8` syscall signatures to indicate they do not return. [PR 492](https://github.com/near/near-sdk-rs/pull/492)
- Removes `PublicKey` generic on `env` promise batch calls. Functions now just take a reference to the `PublicKey`. [PR 495](https://github.com/near/near-sdk-rs/pull/495)
- fix: Public keys can no longer be borsh deserialized from invalid bytes. [PR 502](https://github.com/near/near-sdk-rs/pull/502)
- Adds `Hash` derive to `PublicKey`
Expand Down
4 changes: 1 addition & 3 deletions HELP.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,12 @@ Let's assume the calculator is deployed on `calc.near`, we can use the following

```rust
const CALCULATOR_ACCOUNT_ID: &str = "calc.near";
const NO_DEPOSIT: Balance = 0;
const BASE_GAS: Gas = 5_000_000_000_000;

#[near_bindgen]
impl Contract {
pub fn sum_a_b(&mut self, a: U128, b: U128) -> Promise {
let calculator_account_id: AccountId = CALCULATOR_ACCOUNT_ID.to_string();
ext_calculator::sum(a, b, &calculator_account_id, NO_DEPOSIT, BASE_GAS)
ext_calculator::sum(a, b, calculator_account_id)
}
}
```
Expand Down
Binary file not shown.
22 changes: 8 additions & 14 deletions examples/cross-contract-high-level/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ impl CrossContract {
let pivot = arr.len() / 2;
let arr0 = arr[..pivot].to_vec();
let arr1 = arr[pivot..].to_vec();
let prepaid_gas = env::prepaid_gas();
let account_id = env::current_account_id();

ext::merge_sort(arr0, account_id.clone(), 0, prepaid_gas / 4)
.and(ext::merge_sort(arr1, account_id.clone(), 0, prepaid_gas / 4))
.then(ext::merge(account_id, 0, prepaid_gas / 4))
ext::merge_sort(arr0, account_id.clone())
.into_promise()
.and(ext::merge_sort(arr1, account_id.clone()).into())
.then(ext::merge(account_id).into())
.into()
}

Expand Down Expand Up @@ -119,23 +119,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::set_status(message, account_id);
}
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::set_status(message, account_id.clone())
.into_promise()
.then(ext_status_message::get_status(env::signer_account_id(), account_id).into())
}

pub fn transfer_money(&mut self, account_id: AccountId, amount: u64) {
Expand Down
Binary file not shown.
Binary file modified examples/fungible-token/res/defi.wasm
Binary file not shown.
Binary file modified examples/fungible-token/res/fungible_token.wasm
Binary file not shown.
15 changes: 4 additions & 11 deletions examples/fungible-token/test-contract-defi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ use near_contract_standards::fungible_token::receiver::FungibleTokenReceiver;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::json_types::U128;
use near_sdk::{
env, ext_contract, log, near_bindgen, AccountId, Balance, Gas, PanicOnDefault,
PromiseOrValue,
env, ext_contract, log, near_bindgen, AccountId, Balance, Gas, PanicOnDefault, PromiseOrValue,
};

const BASE_GAS: u64 = 5_000_000_000_000;
const PROMISE_CALL: u64 = 5_000_000_000_000;
const GAS_FOR_FT_ON_TRANSFER: Gas = Gas(BASE_GAS + PROMISE_CALL);

const NO_DEPOSIT: Balance = 0;

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct DeFi {
Expand Down Expand Up @@ -64,13 +61,9 @@ impl FungibleTokenReceiver for DeFi {
_ => {
let prepaid_gas = env::prepaid_gas();
let account_id = env::current_account_id();
ext_self::value_please(
msg,
account_id,
NO_DEPOSIT,
prepaid_gas - GAS_FOR_FT_ON_TRANSFER,
)
.into()
ext_self::value_please(msg, account_id)
.with_gas(prepaid_gas - GAS_FOR_FT_ON_TRANSFER)
.into()
}
}
}
Expand Down
Binary file modified examples/gas-fee-tester/res/gas_fee_tester.wasm
Binary file not shown.
Binary file not shown.
Binary file modified examples/mission-control/res/mission_control.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/approval_receiver.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/non_fungible_token.wasm
Binary file not shown.
Binary file modified examples/non-fungible-token/res/token_receiver.wasm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ use near_contract_standards::non_fungible_token::approval::NonFungibleTokenAppro
use near_contract_standards::non_fungible_token::TokenId;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{
env, ext_contract, log, near_bindgen, AccountId, Balance, Gas, PanicOnDefault,
PromiseOrValue,
env, ext_contract, log, near_bindgen, AccountId, Gas, PanicOnDefault, PromiseOrValue,
};

const BASE_GAS: u64 = 5_000_000_000_000;
const PROMISE_CALL: u64 = 5_000_000_000_000;
const GAS_FOR_NFT_ON_APPROVE: Gas = Gas(BASE_GAS + PROMISE_CALL);

const NO_DEPOSIT: Balance = 0;

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct ApprovalReceiver {
Expand Down Expand Up @@ -73,7 +70,8 @@ impl NonFungibleTokenApprovalReceiver for ApprovalReceiver {
_ => {
let prepaid_gas = env::prepaid_gas();
let account_id = env::current_account_id();
ext_self::ok_go(msg, account_id, NO_DEPOSIT, prepaid_gas - GAS_FOR_NFT_ON_APPROVE)
ext_self::ok_go(msg, account_id)
.with_gas(prepaid_gas - GAS_FOR_NFT_ON_APPROVE)
.into()
}
}
Expand Down
25 changes: 7 additions & 18 deletions examples/non-fungible-token/test-token-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ use near_contract_standards::non_fungible_token::core::NonFungibleTokenReceiver;
use near_contract_standards::non_fungible_token::TokenId;
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize};
use near_sdk::{
env, ext_contract, log, near_bindgen, AccountId, Balance, Gas, PanicOnDefault,
PromiseOrValue,
env, ext_contract, log, near_bindgen, AccountId, Gas, PanicOnDefault, PromiseOrValue,
};

const BASE_GAS: u64 = 5_000_000_000_000;
const PROMISE_CALL: u64 = 5_000_000_000_000;
const GAS_FOR_NFT_ON_TRANSFER: Gas = Gas(BASE_GAS + PROMISE_CALL);

const NO_DEPOSIT: Balance = 0;

#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize, PanicOnDefault)]
pub struct TokenReceiver {
Expand Down Expand Up @@ -74,25 +71,17 @@ impl NonFungibleTokenReceiver for TokenReceiver {
"return-it-later" => {
let prepaid_gas = env::prepaid_gas();
let account_id = env::current_account_id();
ext_self::ok_go(
true,
account_id,
NO_DEPOSIT,
prepaid_gas - GAS_FOR_NFT_ON_TRANSFER,
)
.into()
ext_self::ok_go(true, account_id)
.with_gas(prepaid_gas - GAS_FOR_NFT_ON_TRANSFER)
.into()
}
"keep-it-now" => PromiseOrValue::Value(false),
"keep-it-later" => {
let prepaid_gas = env::prepaid_gas();
let account_id = env::current_account_id();
ext_self::ok_go(
false,
account_id,
NO_DEPOSIT,
prepaid_gas - GAS_FOR_NFT_ON_TRANSFER,
)
.into()
ext_self::ok_go(false, account_id)
.with_gas(prepaid_gas - GAS_FOR_NFT_ON_TRANSFER)
.into()
}
_ => env::panic_str("unsupported msg"),
}
Expand Down
Binary file not shown.
Binary file modified examples/status-message/res/status_message.wasm
Binary file not shown.
Binary file modified examples/test-contract/res/test_contract.wasm
Binary file not shown.
24 changes: 12 additions & 12 deletions near-contract-standards/src/fungible_token/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,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(
Expand Down Expand Up @@ -176,17 +174,19 @@ impl FungibleTokenCore for FungibleToken {
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,
))
.with_gas(env::prepaid_gas() - GAS_FOR_FT_TRANSFER_CALL)
.into_promise()
.then(
ext_self::ft_resolve_transfer(
sender_id,
receiver_id,
amount.into(),
env::current_account_id(),
)
.with_gas(GAS_FOR_RESOLVE_TRANSFER)
.into_promise(),
)
.into()
}

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, AccountId, Balance, Gas, Promise};
use near_sdk::{assert_one_yocto, env, ext_contract, 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::nft_on_approve(token_id, owner_id, approval_id, msg, account_id)
.with_gas(env::prepaid_gas() - GAS_FOR_NFT_APPROVE)
.into()
})
}

Expand Down
28 changes: 14 additions & 14 deletions near-contract-standards/src/non_fungible_token/core/core_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,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, log, AccountId, Balance, BorshStorageKey, CryptoHash, Gas,
assert_one_yocto, env, ext_contract, log, 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_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 NFTResolver {
fn nft_resolve_transfer(
Expand Down Expand Up @@ -312,18 +310,20 @@ impl NonFungibleTokenCore for NonFungibleToken {
token_id.clone(),
msg,
receiver_id.clone(),
NO_DEPOSIT,
env::prepaid_gas() - GAS_FOR_FT_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,
))
.with_gas(env::prepaid_gas() - GAS_FOR_FT_TRANSFER_CALL)
.into_promise()
.then(
ext_self::nft_resolve_transfer(
old_owner,
receiver_id,
token_id,
old_approvals,
env::current_account_id(),
)
.with_gas(GAS_FOR_RESOLVE_TRANSFER)
.into(),
)
.into()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ impl ImplItemMethodInfo {
};
quote! {
#contract_deser
let result = #method_invocation;
#value_ser
near_sdk::env::value_return(&result);
{
let result = #method_invocation;
#value_ser
near_sdk::env::value_return(&result);
}
#contract_ser
}
}
Expand All @@ -147,6 +149,10 @@ impl ImplItemMethodInfo {
#value
}
});

let schedule_function_calls = quote! {
near_sdk::schedule_queued_promises();
};
quote! {
#non_bindgen_attrs
#[cfg(target_arch = "wasm32")]
Expand All @@ -160,6 +166,7 @@ impl ImplItemMethodInfo {
#callback_deser
#callback_vec_deser
#body
#schedule_function_calls
}
}
}
Expand Down
Loading