Skip to content

Commit

Permalink
append EVENT_JSON to near.log() for proper indexer reading (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ariefbayu authored Sep 30, 2024
1 parent ec35a22 commit 8900975
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 36 deletions.
16 changes: 8 additions & 8 deletions src/nft-contract/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export function royaltyToPayout(royaltyPercentage: number, amountToPay: bigint):
return (BigInt(royaltyPercentage) * BigInt(amountToPay) / BigInt(10000)).toString();
}

//refund the storage taken up by passed in approved account IDs and send the funds to the passed in account ID.
//refund the storage taken up by passed in approved account IDs and send the funds to the passed in account ID.
export function refundApprovedAccountIdsIter(accountId: string, approvedAccountIds: string[]) {
//get the storage total by going through and summing all the bytes for each approved account IDs
let storageReleased = approvedAccountIds.map(e => bytesForApprovedAccountId(e)).reduce((partialSum, a) => partialSum + a, 0);
let amountToTransfer = BigInt(storageReleased) * near.storageByteCost().valueOf();

// Send the money to the beneficiary (TODO: don't use batch actions)
const promise = near.promiseBatchCreate(accountId);
near.promiseBatchActionTransfer(promise, amountToTransfer)
Expand Down Expand Up @@ -86,7 +86,7 @@ export function internalAddTokenToOwner(contract: Contract, accountId: string, t
//we insert the token ID into the set
tokenSet.set(tokenId);

//we insert that set for the given account ID.
//we insert that set for the given account ID.
contract.tokensPerOwner.set(accountId, tokenSet);
}

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

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

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

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

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

//return the previous token object that was transferred.
return token
Expand Down
56 changes: 28 additions & 28 deletions src/nft-contract/nft_core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ const GAS_FOR_NFT_ON_TRANSFER = 35_000_000_000_000;
export function internalNftToken({
contract,
tokenId
}:{
contract: Contract,
tokenId: string
}:{
contract: Contract,
tokenId: string
}) {
let token = contract.tokensById.get(tokenId) as Token;
//if there wasn't a token ID in the tokens_by_id collection, we return None
Expand All @@ -24,7 +24,7 @@ export function internalNftToken({
//if there is some token ID in the tokens_by_id collection
//we'll get the metadata for that token
let metadata = contract.tokenMetadataById.get(tokenId) as TokenMetadata;

//we return the JsonToken
let jsonToken = new JsonToken({
tokenId: tokenId,
Expand All @@ -36,21 +36,21 @@ export function internalNftToken({
return jsonToken;
}

//implementation of the nft_transfer method. This transfers the NFT from the current owner to the receiver.
//implementation of the nft_transfer method. This transfers the NFT from the current owner to the receiver.
export function internalNftTransfer({
contract,
receiverId,
tokenId,
approvalId,
memo,
}:{
contract: Contract,
receiverId: string,
tokenId: string,
contract: Contract,
receiverId: string,
tokenId: string,
approvalId: number
memo: string
}) {
//assert that the user attached exactly 1 yoctoNEAR. This is for security and so that the user will be redirected to the NEAR wallet.
//assert that the user attached exactly 1 yoctoNEAR. This is for security and so that the user will be redirected to the NEAR wallet.
assertOneYocto();
//get the sender to transfer the token from the sender to the receiver
let senderId = near.predecessorAccountId();
Expand Down Expand Up @@ -82,13 +82,13 @@ export function internalNftTransferCall({
msg
}:{
contract: Contract,
receiverId: string,
tokenId: string,
receiverId: string,
tokenId: string,
approvalId: number,
memo: string,
msg: string
msg: string
}) {
//assert that the user attached exactly 1 yocto for security reasons.
//assert that the user attached exactly 1 yocto for security reasons.
assertOneYocto();
//get the sender to transfer the token from the sender to the receiver
let senderId = near.predecessorAccountId();
Expand All @@ -106,30 +106,30 @@ export function internalNftTransferCall({
// Initiating receiver's call and the callback
const promise = near.promiseBatchCreate(receiverId);
near.promiseBatchActionFunctionCall(
promise,
"nft_on_transfer",
bytes(JSON.stringify({
promise,
"nft_on_transfer",
bytes(JSON.stringify({
sender_id: senderId,
previous_owner_id: previousToken.owner_id,
token_id: tokenId,
msg
})),
0, // no deposit
})),
0, // no deposit
GAS_FOR_NFT_ON_TRANSFER
);

// We then resolve the promise and call nft_resolve_transfer on our own contract
near.promiseThen(
promise,
near.currentAccountId(),
"nft_resolve_transfer",
promise,
near.currentAccountId(),
"nft_resolve_transfer",
bytes(JSON.stringify({
owner_id: previousToken.owner_id,
receiver_id: receiverId,
token_id: tokenId,
approved_account_ids: previousToken.approved_account_ids
})),
0, // no deposit
})),
0, // no deposit
GAS_FOR_RESOLVE_TRANSFER
);
return near.promiseReturn(promise);
Expand All @@ -152,7 +152,7 @@ export function internalResolveTransfer({
receiverId: string,
tokenId: string,
approvedAccountIds: { [key: string]: number },
memo: string
memo: string
}) {
assert(near.currentAccountId() === near.predecessorAccountId(), "Only the contract itself can call this method");
// Whether receiver wants to return token back to the sender, based on `nft_on_transfer`
Expand All @@ -162,8 +162,8 @@ export function internalResolveTransfer({
//As per the standard, the nft_on_transfer should return whether we should return the token to it's owner or not
//if we need don't need to return the token, we simply return true meaning everything went fine
if (result === 'false') {
/*
since we've already transferred the token and nft_on_transfer returned false, we don't have to
/*
since we've already transferred the token and nft_on_transfer returned false, we don't have to
revert the original transfer and thus we can just return true since nothing went wrong.
*/
//we refund the owner for releasing the storage used up by the approved account IDs
Expand Down Expand Up @@ -193,7 +193,7 @@ export function internalResolveTransfer({
//we add the token to the original owner
internalAddTokenToOwner(contract, ownerId, tokenId);

//we change the token struct's owner to be the original owner
//we change the token struct's owner to be the original owner
token.owner_id = ownerId

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

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

//return false
return false
Expand Down

0 comments on commit 8900975

Please sign in to comment.