-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
1,158 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
243 changes: 243 additions & 0 deletions
243
smart-contracts/assembly/contracts/NFT/NFTEnumerable-example.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,243 @@ | ||
/** | ||
* | ||
* This is an example of an NFT contract that uses the NFT-enumerable-internals | ||
* helper functions to implement the ERC721Enumerable standard. | ||
* | ||
* This file does two things: | ||
* 1. It wraps the NFT-enumerable-internals functions, manages the deserialize/serialize of the arguments | ||
* and return values, | ||
* and exposes them to the outside world. | ||
* 2. It implements some custom features that are not part of the ERC721Enumerable standard, | ||
* like mint, burn, or ownership. | ||
* | ||
* The NFT-enumerable-internals functions are not supposed to be re-exported by this file. | ||
*/ | ||
|
||
import { | ||
Args, | ||
boolToByte, | ||
stringToBytes, | ||
u256ToBytes, | ||
} from '@massalabs/as-types'; | ||
import { | ||
_approve, | ||
_balanceOf, | ||
_constructor, | ||
_getApproved, | ||
_isApprovedForAll, | ||
_name, | ||
_ownerOf, | ||
_setApprovalForAll, | ||
_symbol, | ||
_update, | ||
_transferFrom, | ||
_totalSupply, | ||
_tokenByIndex, | ||
_tokenOfOwnerByIndex, | ||
} from './NFTEnumerable-internals'; | ||
import { setOwner, onlyOwner } from '../utils/ownership'; | ||
import { Context, isDeployingContract } from '@massalabs/massa-as-sdk'; | ||
|
||
/** | ||
* @param binaryArgs - serialized strings representing the name and the symbol of the NFT | ||
* | ||
* @remarks This is the constructor of the contract. It can only be called once, when the contract is being deployed. | ||
* It expects two serialized arguments: the name and the symbol of the NFT. | ||
* Once the constructor has handled the deserialization of the arguments, | ||
* it calls the _constructor function from the NFT-enumerable-internals. | ||
* | ||
* Finally, it sets the owner of the contract to the caller of the constructor. | ||
*/ | ||
export function constructor(binaryArgs: StaticArray<u8>): void { | ||
assert(isDeployingContract()); | ||
const args = new Args(binaryArgs); | ||
const name = args.nextString().expect('name argument is missing or invalid'); | ||
const symbol = args | ||
.nextString() | ||
.expect('symbol argument is missing or invalid'); | ||
_constructor(name, symbol); | ||
setOwner(new Args().add(Context.caller().toString()).serialize()); | ||
} | ||
|
||
export function name(): string { | ||
return _name(); | ||
} | ||
|
||
export function symbol(): string { | ||
return _symbol(); | ||
} | ||
|
||
/** | ||
* | ||
* @param binaryArgs - serialized string representing the address whose balance we want to check | ||
* @returns a serialized u256 representing the balance of the address | ||
*/ | ||
export function balanceOf(binaryArgs: StaticArray<u8>): StaticArray<u8> { | ||
const args = new Args(binaryArgs); | ||
const address = args | ||
.nextString() | ||
.expect('address argument is missing or invalid'); | ||
return u256ToBytes(_balanceOf(address)); | ||
} | ||
|
||
/** | ||
* | ||
* @param binaryArgs - serialized u256 representing the tokenId whose owner we want to check | ||
* @returns a serialized string representing the address of owner of the tokenId | ||
*/ | ||
export function ownerOf(binaryArgs: StaticArray<u8>): StaticArray<u8> { | ||
const args = new Args(binaryArgs); | ||
const tokenId = args | ||
.nextU256() | ||
.expect('tokenId argument is missing or invalid'); | ||
return stringToBytes(_ownerOf(tokenId)); | ||
} | ||
|
||
/** | ||
* | ||
* @param binaryArgs - serialized u256 representing the tokenId whose approved address we want to check | ||
* @returns a serialized string representing the address of the approved address of the tokenId | ||
*/ | ||
export function getApproved(binaryArgs: StaticArray<u8>): StaticArray<u8> { | ||
const args = new Args(binaryArgs); | ||
const tokenId = args | ||
.nextU256() | ||
.expect('tokenId argument is missing or invalid'); | ||
return stringToBytes(_getApproved(tokenId)); | ||
} | ||
|
||
/** | ||
* | ||
* @param binaryArgs - serialized strings representing the address of an owner and an operator | ||
* @returns a serialized u8 representing a boolean value indicating if | ||
* the operator is approved for all the owner's tokens | ||
*/ | ||
export function isApprovedForAll(binaryArgs: StaticArray<u8>): StaticArray<u8> { | ||
const args = new Args(binaryArgs); | ||
const owner = args | ||
.nextString() | ||
.expect('owner argument is missing or invalid'); | ||
const operator = args | ||
.nextString() | ||
.expect('operator argument is missing or invalid'); | ||
return boolToByte(_isApprovedForAll(owner, operator)); | ||
} | ||
|
||
/** | ||
* | ||
* @param binaryArgs - serialized strings representing the address of the recipient and the tokenId to approve | ||
* @remarks This function is only callable by the owner of the tokenId or an approved operator. | ||
* Indeed, this will be checked by the _approve function of the NFT-internals. | ||
* | ||
*/ | ||
export function approve(binaryArgs: StaticArray<u8>): void { | ||
const args = new Args(binaryArgs); | ||
const to = args.nextString().expect('to argument is missing or invalid'); | ||
const tokenId = args | ||
.nextU256() | ||
.expect('tokenId argument is missing or invalid'); | ||
_approve(to, tokenId); | ||
} | ||
|
||
/** | ||
* | ||
* @param binaryArgs - serialized arguments representing the address of the operator and a boolean value indicating | ||
* if the operator should be approved for all the caller's tokens | ||
* | ||
*/ | ||
export function setApprovalForAll(binaryArgs: StaticArray<u8>): void { | ||
const args = new Args(binaryArgs); | ||
const to = args.nextString().expect('to argument is missing or invalid'); | ||
const approved = args | ||
.nextBool() | ||
.expect('approved argument is missing or invalid'); | ||
_setApprovalForAll(to, approved); | ||
} | ||
|
||
/** | ||
* | ||
* @param binaryArgs - serialized arguments representing the address of the sender, | ||
* the address of the recipient, and the tokenId to transfer. | ||
* | ||
* @remarks This function is only callable by the owner of the tokenId or an approved operator. | ||
*/ | ||
export function transferFrom(binaryArgs: StaticArray<u8>): void { | ||
const args = new Args(binaryArgs); | ||
const from = args.nextString().expect('from argument is missing or invalid'); | ||
const to = args.nextString().expect('to argument is missing or invalid'); | ||
const tokenId = args | ||
.nextU256() | ||
.expect('tokenId argument is missing or invalid'); | ||
_transferFrom(from, to, tokenId); | ||
} | ||
|
||
/** | ||
* | ||
* @param binaryArgs - serialized arguments representing the address of the recipient and the tokenId to mint | ||
* | ||
* @remarks This function is only callable by the owner of the contract. | ||
*/ | ||
export function mint(binaryArgs: StaticArray<u8>): void { | ||
onlyOwner(); | ||
const args = new Args(binaryArgs); | ||
const to = args.nextString().expect('to argument is missing or invalid'); | ||
const tokenId = args | ||
.nextU256() | ||
.expect('tokenId argument is missing or invalid'); | ||
_update(to, tokenId, ''); | ||
} | ||
|
||
/** | ||
* | ||
* @param binaryArgs - serialized u256 representing the tokenId to burn | ||
* | ||
* @remarks This function is not part of the ERC721 standard. | ||
* It serves as an example of how to use the NFT-enumerable-internals functions to implement custom features. | ||
*/ | ||
export function burn(binaryArgs: StaticArray<u8>): void { | ||
const args = new Args(binaryArgs); | ||
const tokenId = args | ||
.nextU256() | ||
.expect('tokenId argument is missing or invalid'); | ||
_update('', tokenId, ''); | ||
} | ||
|
||
/** | ||
* Returns the total number of tokens in existence. | ||
* @returns a serialized u256 representing the total supply | ||
*/ | ||
export function totalSupply(_: StaticArray<u8>): StaticArray<u8> { | ||
return u256ToBytes(_totalSupply()); | ||
} | ||
|
||
/** | ||
* Returns the token ID at a given index of all the tokens in this contract. | ||
* @param binaryArgs - serialized u256 representing the index | ||
* @returns a serialized u256 representing the token ID | ||
*/ | ||
export function tokenByIndex(binaryArgs: StaticArray<u8>): StaticArray<u8> { | ||
const args = new Args(binaryArgs); | ||
const index = args.nextU256().expect('index argument is missing or invalid'); | ||
return u256ToBytes(_tokenByIndex(index)); | ||
} | ||
|
||
/** | ||
* Returns the token ID at a given index of the tokens list of the requested owner. | ||
* @param binaryArgs - serialized arguments representing the owner address and the index | ||
* @returns a serialized u256 representing the token ID | ||
*/ | ||
export function tokenOfOwnerByIndex( | ||
binaryArgs: StaticArray<u8>, | ||
): StaticArray<u8> { | ||
const args = new Args(binaryArgs); | ||
const owner = args | ||
.nextString() | ||
.expect('owner argument is missing or invalid'); | ||
const index = args.nextU256().expect('index argument is missing or invalid'); | ||
return u256ToBytes(_tokenOfOwnerByIndex(owner, index)); | ||
} | ||
|
||
/** | ||
* Expose the ownerAddress function to allow checking the owner of the contract. | ||
*/ | ||
export { ownerAddress } from '../utils/ownership'; |
Oops, something went wrong.