Skip to content

Instantly share code, notes, and snippets.

@vikiival
Created August 5, 2021 11:08
Show Gist options
  • Save vikiival/12d9eb8f7a7c8fed3d9723ce2017d49c to your computer and use it in GitHub Desktop.
Save vikiival/12d9eb8f7a7c8fed3d9723ce2017d49c to your computer and use it in GitHub Desktop.
Smart contracts for NFT in Polkadot ecosystem
@contract
export class ERC721 {
storage: ERC721Storage;
constructor() {
this.storage = new ERC721Storage();
}
@constructor
default(name: string = "", symbol: string = ""): void {
this.storage._name = name;
this.storage._symbol = symbol;
this.storage._owner = msg.sender;
this.storage._counter = u128.Zero;
}
}
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
@event
export class Transfer {
@topic from: AccountId = AccountId0;
@topic to: AccountId = AccountId0;
@topic tokenId: u128 = u128.Zero;
constructor(from: AccountId, to: AccountId, tokenId: u128) {
this.from = from;
this.to = to;
this.tokenId = tokenId;
}
};
@message(payable)
send(to: AccountId): void {
this._send(to);
}
protected _create(_tokenURI: string): void {
assert(!this._maxReached(), "ERC721: Max reached");
assert(msg.sender.eq(this.storage._owner) , "KODA: Not owner");
let counter = this.storage._counter;
this._mint(msg.sender, counter);
this._setTokenURI(counter, _tokenURI);
counter= u128.add(this.storage._counter, u128.One);
this.storage._counter = counter;
}
protected _mint(to: AccountId, tokenId: u128): void {
assert(to.notEq(AccountId0), "ERC721: mint to the zero address");
assert(!this._exists(tokenId), "ERC721: token already minted");
this._getHolderTokens(to).push(new UInt128(tokenId));
this.storage._tokenOwners.set(new UInt128(tokenId), to);
(new Transfer(AccountId0, to, tokenId));
}
/**
* @dev See {IERC721-ownerOf}.
*/
@message(mutates = false)
ownerOf(tokenId: u128): AccountId {
assert(this._exists(tokenId), "ERC721: owner query for nonexistent token")
return this.storage._tokenOwners.get(new UInt128(tokenId));
}
/**
* @dev See {IERC721Metadata-name}.
*/
@message(mutates = false)
name(): string {
return this.storage._name;
}
@storage
class ERC721Storage {
// Mapping from holder address to their (enumerable) set of owned tokens
_holderTokens: SpreadStorableMap<AccountId, SpreadStorableArray<UInt128>>;
// Enumerable mapping from token ids to their owners
_tokenOwners: SpreadStorableMap<UInt128, AccountId>;
// Mapping from token ID to approved address
_tokenApprovals: SpreadStorableMap<UInt128, AccountId>;
// Mapping from owner to operator approvals
_operatorApprovals: SpreadStorableMap<AccountId, SpreadStorableMap<AccountId, Bool>>;
// Token name
_name: string;
// Token symbol
_symbol: string;
// Optional mapping for token URIs
_tokenURIs: SpreadStorableMap<UInt128, ScaleString>;
// Base URI
_baseURI: string;
// Owner of Contract
_owner: AccountId;
// Mapping from token ID to approved address
_tokenRoyalty: SpreadStorableMap<UInt128, UInt8>;
// auto-counter
_counter: u128;
// Mapping from token ID to mapping from address to the emote string
_emotes: SpreadStorableMap<UInt128, SpreadStorableMap<AccountId, ScaleString>>;
// Mapping from token ID to balace (token price)
_balances: SpreadStorableMap<UInt128, BalanceType>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment