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(NFT): add events to standard #627

Merged
merged 48 commits into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from 47 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
a6e16e8
feat(NFT): add events to standard
willemneal Nov 4, 2021
3238e4a
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Nov 8, 2021
b65081a
feat: add last two events and add tests
willemneal Nov 8, 2021
19151a1
fix: fmt
willemneal Nov 8, 2021
4b8f0aa
fix: use near indexer's types
willemneal Nov 10, 2021
d27974f
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Nov 30, 2021
c32de7a
feat: add new methods; address feedback and move file up a directory
willemneal Dec 1, 2021
a32852b
chore: update wasm binaries
willemneal Dec 1, 2021
1ddc497
fix: fmt
willemneal Dec 1, 2021
d4a4c8b
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Dec 8, 2021
344d483
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Dec 13, 2021
30a2be5
feat: use Cow and generics to avoid copying strings & remove Display
willemneal Dec 13, 2021
00e0bf5
feat: rename to emit
willemneal Dec 13, 2021
bc403d5
feat: refactor emit functions to use &str instead of generic
willemneal Dec 13, 2021
b6bc5f3
fix: fmt
willemneal Dec 13, 2021
8fc2c80
feat: remove emit_* and add must_use attribute
willemneal Dec 14, 2021
62694c9
add the annoying wasm files
willemneal Dec 14, 2021
0726e8e
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Dec 14, 2021
f9fb90f
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Dec 16, 2021
2f2c0a2
feat: make nft data constructors generic & add from method to AccountId
willemneal Dec 16, 2021
7202200
feat: use event logs in standard
willemneal Dec 16, 2021
0917ab5
fix: fmt
willemneal Dec 16, 2021
62112ad
fix: duplicate import
willemneal Dec 16, 2021
4d494e8
fix: emit event for internal_mint
willemneal Dec 17, 2021
a01075c
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Dec 17, 2021
63beb49
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Dec 20, 2021
c184d9e
Merge branch 'master' into feat/event_logs
willemneal Dec 22, 2021
79a319e
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Dec 23, 2021
3524ffd
feat: ensure log when `nft_transfer_call`s receiver fails
willemneal Jan 12, 2022
c0ef525
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Jan 12, 2022
db7d5d4
fix: fmt
willemneal Jan 12, 2022
83a3b75
fix:wasm
willemneal Jan 12, 2022
38656b0
Merge branch 'master' into feat/event_logs
willemneal Jan 13, 2022
740a594
feat: add test to show that gas check is sufficient
willemneal Jan 13, 2022
9d7474c
fix: test correctly failed; fixed to actually not have enough gas
willemneal Jan 13, 2022
9c561e0
Merge branch 'master' into feat/event_logs
willemneal Jan 13, 2022
7187af3
Merge branch 'master' into feat/event_logs
willemneal Jan 18, 2022
3459d27
Merge branch 'master' into feat/event_logs
willemneal Jan 18, 2022
1404f55
Update near-contract-standards/src/event.rs
willemneal Jan 20, 2022
1746487
chore: add more documentation and edit CHANGELOG
willemneal Jan 20, 2022
9d0d28d
feat: switch to `&'a str` since the event will have shorter lifetime
willemneal Jan 20, 2022
1ce258b
fix: update wasm
willemneal Jan 20, 2022
64c1c6f
chore: add more documentation to core_impl
willemneal Jan 20, 2022
d004203
Update near-contract-standards/src/event.rs
willemneal Jan 20, 2022
179faf1
fix: annoying wasm files
willemneal Jan 20, 2022
bce9abe
Merge branch 'master' into feat/event_logs
willemneal Jan 20, 2022
ae405da
Merge remote-tracking branch 'origin/master' into feat/event_logs
willemneal Jan 21, 2022
173f6f7
Merge branch 'feat/event_logs'
austinabell Jan 25, 2022
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## [unreleased]
- Added event logs to `near-contract-standards`. Currently only NFT related events. [PR 627](https://github.com/near/near-sdk-rs/pull/627)

## `4.0.0-pre.6` [01-21-2021]

Expand Down
44 changes: 38 additions & 6 deletions examples/fungible-token/Cargo.lock

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

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.
44 changes: 38 additions & 6 deletions examples/non-fungible-token/Cargo.lock

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

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 @@ -10,4 +10,3 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
near-sdk = { path = "../../../near-sdk" }
near-contract-standards = { path = "../../../near-contract-standards" }

9 changes: 2 additions & 7 deletions examples/non-fungible-token/test-token-receiver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,8 @@ 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, NO_DEPOSIT, prepaid_gas - GAS_FOR_NFT_ON_TRANSFER)
.into()
}
"keep-it-now" => PromiseOrValue::Value(false),
"keep-it-later" => {
Expand Down
12 changes: 4 additions & 8 deletions examples/non-fungible-token/tests/sim/test_approval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ fn simulate_simple_approve() {

// check nft_is_approved, with approval_id=1
let alice_approval_id_is_1: bool =
view!(nft.nft_is_approved(TOKEN_ID.into(), alice.account_id(), Some(1)))
.unwrap_json();
view!(nft.nft_is_approved(TOKEN_ID.into(), alice.account_id(), Some(1))).unwrap_json();
assert!(alice_approval_id_is_1);

// check nft_is_approved, with approval_id=2
let alice_approval_id_is_2: bool =
view!(nft.nft_is_approved(TOKEN_ID.into(), alice.account_id(), Some(2)))
.unwrap_json();
view!(nft.nft_is_approved(TOKEN_ID.into(), alice.account_id(), Some(2))).unwrap_json();
assert!(!alice_approval_id_is_2);

// alternatively, one could check the data returned by nft_token
Expand All @@ -43,8 +41,7 @@ fn simulate_simple_approve() {
.assert_success();

let alice_approval_id_is_2: bool =
view!(nft.nft_is_approved(TOKEN_ID.into(), alice.account_id(), Some(2)))
.unwrap_json();
view!(nft.nft_is_approved(TOKEN_ID.into(), alice.account_id(), Some(2))).unwrap_json();
assert!(alice_approval_id_is_2);

// approving another account gives different approval_id
Expand Down Expand Up @@ -145,8 +142,7 @@ fn simulate_revoke() {
.assert_success();

// root revokes alice
call!(root, nft.nft_revoke(TOKEN_ID.into(), alice.account_id()), deposit = 1)
.assert_success();
call!(root, nft.nft_revoke(TOKEN_ID.into(), alice.account_id()), deposit = 1).assert_success();

// alice is revoked...
let alice_approved: bool =
Expand Down
118 changes: 118 additions & 0 deletions examples/non-fungible-token/tests/sim/test_core.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::{init, TOKEN_ID};
use near_contract_standards::non_fungible_token::Token;
use near_sdk::serde_json::{self, json};
use near_sdk_sim::{call, view};

#[test]
Expand Down Expand Up @@ -124,3 +125,120 @@ fn simulate_transfer_call_receiver_panics() {
let token: Token = view!(nft.nft_token(TOKEN_ID.into())).unwrap_json();
assert_eq!(token.owner_id, root.account_id());
}

#[test]
fn simulate_transfer_call_receiver_panics_and_nft_resolve_transfer_produces_log() {
let (root, nft, _, receiver, _) = init();
let args = json!({
"receiver_id": receiver.account_id(),
"token_id": TOKEN_ID,
"memo": Some("transfer & call"),
"msg": "incorrect message"

});
let args = serde_json::to_vec(&args).unwrap();
let res = root
.create_transaction(nft.account_id())
.function_call("nft_transfer_call".to_string(), args, 35_000_000_000_000 + 1, 1)
.submit();

// Prints final log
assert_eq!(res.logs().len(), 1);

let token: Token = view!(nft.nft_token(TOKEN_ID.into())).unwrap_json();
assert_eq!(token.owner_id, root.account_id());
}

#[test]
fn simulate_transfer_call_receiver_panics_and_nft_resolve_transfer_produces_no_log_if_not_enough_gas() {
let (root, nft, _, receiver, _) = init();
let args = json!({
"receiver_id": receiver.account_id(),
"token_id": TOKEN_ID,
"memo": Some("transfer & call"),
"msg": "incorrect message"

});
let args = serde_json::to_vec(&args).unwrap();
let res = root
.create_transaction(nft.account_id())
.function_call("nft_transfer_call".to_string(), args, 35_000_000_000_000, 1)
.submit();

// Prints final log
assert_eq!(res.logs().len(), 0);

let token: Token = view!(nft.nft_token(TOKEN_ID.into())).unwrap_json();
assert_eq!(token.owner_id, root.account_id());
}

#[test]
fn simulate_transfer_call_no_extra_log() {
let (root, nft, _, receiver, _) = init();

let args = json!({
"receiver_id": receiver.account_id(),
"token_id": TOKEN_ID,
"memo": Some("transfer & call"),
"msg": "keep-it-now"

});
let args = serde_json::to_vec(&args).unwrap();
let res = root
.create_transaction(nft.account_id())
.function_call("nft_transfer_call".to_string(), args, 200_000_000_000_000, 1)
.submit();

assert_eq!(res.logs().len(), 0);
let token: Token = view!(nft.nft_token(TOKEN_ID.into())).unwrap_json();
assert_eq!(token.owner_id, receiver.account_id());
}

#[test]
fn simulate_simple_transfer_logs() {
let (root, nft, alice, _, _) = init();

let token: Token = view!(nft.nft_token(TOKEN_ID.into())).unwrap_json();
assert_eq!(token.owner_id, root.account_id());

let args = json!({
"receiver_id": alice.account_id(),
"token_id": TOKEN_ID,
"memo": Some("simple transfer"),
});
let args = serde_json::to_vec(&args).unwrap();
let res = root
.create_transaction(nft.account_id())
.function_call("nft_transfer".to_string(), args, 200_000_000_000_000, 1)
.submit();

assert_eq!(res.logs().len(), 1);

let token: Token = view!(nft.nft_token(TOKEN_ID.into())).unwrap_json();
assert_eq!(token.owner_id, alice.account_id());
}

#[test]
fn simulate_simple_transfer_no_logs_on_failure() {
let (root, nft, _, _, _) = init();

let token: Token = view!(nft.nft_token(TOKEN_ID.into())).unwrap_json();
assert_eq!(token.owner_id, root.account_id());

let args = json!({
// transfer to the current owner should fail and not print log
"receiver_id": root.account_id(),
"token_id": TOKEN_ID,
"memo": Some("simple transfer"),
});
let args = serde_json::to_vec(&args).unwrap();
let res = root
.create_transaction(nft.account_id())
.function_call("nft_transfer".to_string(), args, 200_000_000_000_000, 1)
.submit();

assert_eq!(res.logs().len(), 0);

let token: Token = view!(nft.nft_token(TOKEN_ID.into())).unwrap_json();
assert_eq!(token.owner_id, root.account_id());
}
Loading