Skip to content

Commit 8900975

Browse files
authored
append EVENT_JSON to near.log() for proper indexer reading (#18)
1 parent ec35a22 commit 8900975

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

src/nft-contract/internal.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ export function royaltyToPayout(royaltyPercentage: number, amountToPay: bigint):
1515
return (BigInt(royaltyPercentage) * BigInt(amountToPay) / BigInt(10000)).toString();
1616
}
1717

18-
//refund the storage taken up by passed in approved account IDs and send the funds to the passed in account ID.
18+
//refund the storage taken up by passed in approved account IDs and send the funds to the passed in account ID.
1919
export function refundApprovedAccountIdsIter(accountId: string, approvedAccountIds: string[]) {
2020
//get the storage total by going through and summing all the bytes for each approved account IDs
2121
let storageReleased = approvedAccountIds.map(e => bytesForApprovedAccountId(e)).reduce((partialSum, a) => partialSum + a, 0);
2222
let amountToTransfer = BigInt(storageReleased) * near.storageByteCost().valueOf();
23-
23+
2424
// Send the money to the beneficiary (TODO: don't use batch actions)
2525
const promise = near.promiseBatchCreate(accountId);
2626
near.promiseBatchActionTransfer(promise, amountToTransfer)
@@ -86,7 +86,7 @@ export function internalAddTokenToOwner(contract: Contract, accountId: string, t
8686
//we insert the token ID into the set
8787
tokenSet.set(tokenId);
8888

89-
//we insert that set for the given account ID.
89+
//we insert that set for the given account ID.
9090
contract.tokensPerOwner.set(accountId, tokenSet);
9191
}
9292

@@ -105,7 +105,7 @@ export function internalRemoveTokenFromOwner(contract: Contract, accountId: stri
105105
//if the token set is now empty, we remove the owner from the tokens_per_owner collection
106106
if (tokenSet.isEmpty()) {
107107
contract.tokensPerOwner.remove(accountId);
108-
} else { //if the token set is not empty, we simply insert it back for the account ID.
108+
} else { //if the token set is not empty, we simply insert it back for the account ID.
109109
contract.tokensPerOwner.set(accountId, tokenSet);
110110
}
111111
}
@@ -147,7 +147,7 @@ export function internalTransfer(contract: Contract, senderId: string, receiverI
147147
//we then add the token to the receiver_id's set
148148
internalAddTokenToOwner(contract, receiverId, tokenId);
149149

150-
//we create a new token struct
150+
//we create a new token struct
151151
let newToken = new Token ({
152152
ownerId: receiverId,
153153
//reset the approval account IDs
@@ -157,10 +157,10 @@ export function internalTransfer(contract: Contract, senderId: string, receiverI
157157
royalty: token.royalty,
158158
});
159159

160-
//insert that new token into the tokens_by_id, replacing the old entry
160+
//insert that new token into the tokens_by_id, replacing the old entry
161161
contract.tokensById.set(tokenId, newToken);
162162

163-
//if there was some memo attached, we log it.
163+
//if there was some memo attached, we log it.
164164
if (memo != null) {
165165
near.log(`Memo: ${memo}`);
166166
}
@@ -198,7 +198,7 @@ export function internalTransfer(contract: Contract, senderId: string, receiverI
198198
}
199199

200200
// Log the serialized json.
201-
near.log(JSON.stringify(nftTransferLog));
201+
near.log(`EVENT_JSON:${JSON.stringify(nftTransferLog)}`);
202202

203203
//return the previous token object that was transferred.
204204
return token

src/nft-contract/nft_core.ts

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ const GAS_FOR_NFT_ON_TRANSFER = 35_000_000_000_000;
1111
export function internalNftToken({
1212
contract,
1313
tokenId
14-
}:{
15-
contract: Contract,
16-
tokenId: string
14+
}:{
15+
contract: Contract,
16+
tokenId: string
1717
}) {
1818
let token = contract.tokensById.get(tokenId) as Token;
1919
//if there wasn't a token ID in the tokens_by_id collection, we return None
@@ -24,7 +24,7 @@ export function internalNftToken({
2424
//if there is some token ID in the tokens_by_id collection
2525
//we'll get the metadata for that token
2626
let metadata = contract.tokenMetadataById.get(tokenId) as TokenMetadata;
27-
27+
2828
//we return the JsonToken
2929
let jsonToken = new JsonToken({
3030
tokenId: tokenId,
@@ -36,21 +36,21 @@ export function internalNftToken({
3636
return jsonToken;
3737
}
3838

39-
//implementation of the nft_transfer method. This transfers the NFT from the current owner to the receiver.
39+
//implementation of the nft_transfer method. This transfers the NFT from the current owner to the receiver.
4040
export function internalNftTransfer({
4141
contract,
4242
receiverId,
4343
tokenId,
4444
approvalId,
4545
memo,
4646
}:{
47-
contract: Contract,
48-
receiverId: string,
49-
tokenId: string,
47+
contract: Contract,
48+
receiverId: string,
49+
tokenId: string,
5050
approvalId: number
5151
memo: string
5252
}) {
53-
//assert that the user attached exactly 1 yoctoNEAR. This is for security and so that the user will be redirected to the NEAR wallet.
53+
//assert that the user attached exactly 1 yoctoNEAR. This is for security and so that the user will be redirected to the NEAR wallet.
5454
assertOneYocto();
5555
//get the sender to transfer the token from the sender to the receiver
5656
let senderId = near.predecessorAccountId();
@@ -82,13 +82,13 @@ export function internalNftTransferCall({
8282
msg
8383
}:{
8484
contract: Contract,
85-
receiverId: string,
86-
tokenId: string,
85+
receiverId: string,
86+
tokenId: string,
8787
approvalId: number,
8888
memo: string,
89-
msg: string
89+
msg: string
9090
}) {
91-
//assert that the user attached exactly 1 yocto for security reasons.
91+
//assert that the user attached exactly 1 yocto for security reasons.
9292
assertOneYocto();
9393
//get the sender to transfer the token from the sender to the receiver
9494
let senderId = near.predecessorAccountId();
@@ -106,30 +106,30 @@ export function internalNftTransferCall({
106106
// Initiating receiver's call and the callback
107107
const promise = near.promiseBatchCreate(receiverId);
108108
near.promiseBatchActionFunctionCall(
109-
promise,
110-
"nft_on_transfer",
111-
bytes(JSON.stringify({
109+
promise,
110+
"nft_on_transfer",
111+
bytes(JSON.stringify({
112112
sender_id: senderId,
113113
previous_owner_id: previousToken.owner_id,
114114
token_id: tokenId,
115115
msg
116-
})),
117-
0, // no deposit
116+
})),
117+
0, // no deposit
118118
GAS_FOR_NFT_ON_TRANSFER
119119
);
120120

121121
// We then resolve the promise and call nft_resolve_transfer on our own contract
122122
near.promiseThen(
123-
promise,
124-
near.currentAccountId(),
125-
"nft_resolve_transfer",
123+
promise,
124+
near.currentAccountId(),
125+
"nft_resolve_transfer",
126126
bytes(JSON.stringify({
127127
owner_id: previousToken.owner_id,
128128
receiver_id: receiverId,
129129
token_id: tokenId,
130130
approved_account_ids: previousToken.approved_account_ids
131-
})),
132-
0, // no deposit
131+
})),
132+
0, // no deposit
133133
GAS_FOR_RESOLVE_TRANSFER
134134
);
135135
return near.promiseReturn(promise);
@@ -152,7 +152,7 @@ export function internalResolveTransfer({
152152
receiverId: string,
153153
tokenId: string,
154154
approvedAccountIds: { [key: string]: number },
155-
memo: string
155+
memo: string
156156
}) {
157157
assert(near.currentAccountId() === near.predecessorAccountId(), "Only the contract itself can call this method");
158158
// Whether receiver wants to return token back to the sender, based on `nft_on_transfer`
@@ -162,8 +162,8 @@ export function internalResolveTransfer({
162162
//As per the standard, the nft_on_transfer should return whether we should return the token to it's owner or not
163163
//if we need don't need to return the token, we simply return true meaning everything went fine
164164
if (result === 'false') {
165-
/*
166-
since we've already transferred the token and nft_on_transfer returned false, we don't have to
165+
/*
166+
since we've already transferred the token and nft_on_transfer returned false, we don't have to
167167
revert the original transfer and thus we can just return true since nothing went wrong.
168168
*/
169169
//we refund the owner for releasing the storage used up by the approved account IDs
@@ -193,7 +193,7 @@ export function internalResolveTransfer({
193193
//we add the token to the original owner
194194
internalAddTokenToOwner(contract, ownerId, tokenId);
195195

196-
//we change the token struct's owner to be the original owner
196+
//we change the token struct's owner to be the original owner
197197
token.owner_id = ownerId
198198

199199
//we refund the receiver any approved account IDs that they may have set on the token
@@ -235,7 +235,7 @@ export function internalResolveTransfer({
235235
}
236236

237237
// Log the serialized json.
238-
near.log(JSON.stringify(nftTransferLog));
238+
near.log(`EVENT_JSON:${JSON.stringify(nftTransferLog)}`);
239239

240240
//return false
241241
return false

0 commit comments

Comments
 (0)