Game
TypeScript SDK Quick Start
TypeScript SDK Quick Start
1 Purpose
In this article, we will use an in-game scenario to describe how to use the move language to develop a contract and deploy it to the Endless Blockchain. After the contract is successfully published, we will use TS-SDK to interact with the published contract and call the functions provided by the contract.
1.1 Introduction Features
In game development, core elements such as character management, equipment systems, and item mechanics form fundamental building blocks of the creative process. Moreover, their design and implementation account for approximately 50% of development workloads, making them essential components that define the game's operational framework. If we want to introduce blockchain into traditional game development, we need to use blockchain-supported contracts to implement these basic game functions, which can enable players to have true ownership of their digital assets, thereby further increasing their engagement.
Next we will develop a contract that provides the creation of characters, equipment and item. They have different properties according to the design. Equipment can be enhanced with items to improve its properties. Characters can use equipment to improve properties. There is a clear subordinate relationship between them.
The characters, equipment, and items in the game are all unique and valuable. NFTs on the blockchain has the characteristics of uniqueness, indivisibility, irreplaceability, and scarcity. Therefore, it is very suitable to use NFTs to realize characters, equipment, and items.
1.2 Move Contract's Function Description
We use the NFT functions provided by endless chain to implement a sample contract called endless-hero. The design functions of this contract are as follows:
Use NFT to implement hero, weapon, armor, shield and gem.
Hero can equip a weapon, an armor, and a shield, and has its own attributes gender and race.
Weapon can be inlaid with a gem, with weapon_type, attack and weight
Armor can be inlaid with a gem, with defense and weight
Shield can be inlaid with a gem, with defense and weight
The contract creator acts as the sole administrator. This role is responsible for minting all NFTs (heroes, equipment, gems) and transferring them to ordinary players, such as Alice. Players can then inlay and remove gems from their equipment, as well as equip or unequip items on their heroes.
2 Endless Move Smart Contract Develop
2.1 Install Endless CLI
Download endless-cli in https://github.com/endless-labs/endless-release/releases
2.1.1 Windows
Unzip the downloaded endless-cli-Windows-x86_64.zip to a specified folder, and it is recommended to add the folder to the system PATH variable.
After installation, you can use endless --help to view detailed commands.
2.1.2 Ubuntu
Unzip the downloaded endless-cli-Ubuntu-x86_64.zip to a specified folder, and it is recommended to add the folder to the system PATH variable.
2.2 Install endless-vscode-plugin With VSCode
Flow https://github.com/endless-labs/endless-vscode-plugin 's introduction,install endless-vscode-plugin.
2.3 Create endless-hero Move Contract
2.3.1 create move directory
create move directory in endless-hero
2.3.2 initialization move contract
This will create some directories and Move.toml
2.3.3 Use endless to initialize a account used for deploy contract
select testnet
Enter
This will create default account and saved in .endless\config.yaml file.This accout is auto faucet 1 EDS.
.endless\config.yaml
2.3.4 Modify Move.toml
2.3.4.1 add contract address
add contract address under [addresses]
2.3.4.2 add dependencies
delete
add
EndlessFramework: move contract develop necessary dependencies. EndlessToken:Providing NFT functionality
2.3.5 Develop hero.move
2.3.5.1 Create hero.move file in endless-hero\move\sources
hero.move provided interactive functions as follow
entry fun mint_hero : mint hero nft.
entry fun mint_weapon : mint weapon nft.
entry fun mint_gem : mint gem nft.
entry fun transfer_nft_to_player : transfer nft to player as alice. include hero weapon gem etc.
entry fun hero_equip_weapon : hero equip weapon.
entry fun hero_unequip_weapon : hero remove weapon.
entry fun weapon_equip_gem : weapon inlaid gem.
entry fun weapon_unequip_gem : weapon remove gem.
2.3.5.2 Develop hero.move
2.3.5.2.1 Dependencies Required For Developing NFT
In addition to the Move standard library, Endless also provides the Endless standard library and framework. The following is an introduction to the basic functions of each library.
[std] Provides error definition, basic encoding, hash, string, container and other functions.
[endless_std] Provides basic data types, encryption algorithms, container encapsulation, etc.
[endless_framework] Provides chain-related functions such as accounts, object, asset, pledges, signatures, voting, etc.
[endless_token] New digital asset standard implementation, providing the functionality of both fungible and non-fungible asset.
We will use all the libraries mentioned above.
2.3.5.2.2 Object and Resource
Resource and object are two core state data structures.
Definition
Defined as struct with abilities like key, store
Created via object module API (e.g., create_object)
Lifecycle
Bound to an account, stored under an account address
Independent of accounts, can be owned or referenced
Address Ownership
Tied to a specific account address
Has its own object address, can be transferred or nested
Access Method
Accessed via account address and resource path
Accessed via object ID and standard library functions
Nesting Support
Not supported (stored directly under accounts)
Supports nesting, can be a child of another object
Transferability
Not directly transferable (requires custom logic)
Transferable via object::transfer
Type Safety
Strongly typed, checked at compile time
Also strongly typed, with more flexible structure
Typical Use Case
Account-owned state like balances
Flexible data like NFTs, game characters, composable assets
Gas Cost
Typically lower (simple structure)
Slightly higher (requires object storage management)
Composability
Not supported
Supports composability and nesting
Underlying Storage
Stored directly in the global state under accounts
Managed by the object module in global state
So we can define some resource to store their properties as as follows.
2.3.5.2.3 Object and NFT
Object is the underlying mechanism, providing the ability to nest, transfer, and combine;
NFT is a specific asset model based on Object, adding information such as TokenId and metadata.
In hero.move, we can provide a basic mint nft function, through which heroes, equipment and items are mint.
Mint a hero use function create_hero.
2.3.5.2.4 NFT Transfer
Equipment uses items, heroes use equipment, which can essentially be seen as the transfer of NFT.
Transfer a nft can call object::transfer();
The complete hero.move implementation can be found in the code repository.
2.3.6 Compile endless-hero
endless move compile
2.3.7 Deploy endless-hero
3 Create A TypeScript Project
3.1 Install development environment
Install nodejs v20.19.2.
3.2 Initialize TypeScript Project
This will create a package.json file, allowing you to install dependencies and run scripts.
3.3 Install Dependencies
You will need the Endless TypeScript SDK and dotenv to manage environment variables:
3.4 Create tsconfig.json
Create a tsconfig.json file with the following:
This configuration ensures TypeScript properly recognizes Node.js types and provides appropriate type checking.
3.5 Configure Environment Variables
Create a .env file with the following:
By default, weβll use testnet, but you can also choose mainnet depending on your needs.
3.6 Create index.ts
3.7 Create web3 dir
This folder is used to store code related to interactions with the Endless chain, including creating accounts and faucet EDS on testnet.
4 Create account and get testnet EDS for development
4.1 create account.ts in web3 and import "@endlesslab/endless-ts-sdk
4.2 create account key and save in a file
Save a account as AccountData type, include private_key and address as the following. If AccountName's file is not exist, use sdk's Account generate a key and save it, otherwise load it.
4.3 faucet EDS on testnet
When faucet account on testnet, we wait some seconds to check it blances.
4.4 Verification function
4.4.1 account.ts
Now,account.ts is as the following.
4.4.2 index.ts
Modify index.ts as follow.
4.4.3 Create a account with name alice
craete alice
alice.json After craete alice,will create a alice.json.
faucet alice
Account can faucet one times per day,now alice balance is 1 EDS.
5 Implementing interact with contracts use @endlesslab/endless-ts-sdk
5.1 Configure contract address
open .env file. add CONTRACT_ADDRESS=d4a2aa8290cde4791919c6f0b74d00464e598104dcc2cd3158ae1d7d6e5b91b3
5.2 Mint a hero nft
Create nft.ts and event.ts files in web3 directory.
Define a MintEvent Data type in event.ts.
Implement mint a hero nft function in nft.ts.
Add mint hero entry function in index.ts.
Configure contract Creator's file. create mgr.json . "address" is account field in move.endless\config.yaml "private_key" is private_key field in move.endless\config.yaml
Mint a hero nft. H9ZVsV1rME62Fr8yYGbb2DxekDFcUm9H2p3j2QFE8mTJ is hero nft's object ID(Address).
5.3 Complete the remaining nft interaction functions
According to the process of 5.2, we can complete the remaining functions of web3/nft.ts and index.ts. The complete code implementation can be obtained in the code repository.
6 Interact with contracts (Exapmle)
6.1 Prepare
According to the above steps, you will have two json files and a hero nft:
mgr.json: representing the nft contract
alice.json: representing a player.
hero nft: H9ZVsV1rME62Fr8yYGbb2DxekDFcUm9H2p3j2QFE8mTJ,You can check it out at: https://scan.endless.link/nft/H9ZVsV1rME62Fr8yYGbb2DxekDFcUm9H2p3j2QFE8mTJ?network=testnet
You can find it's attribute and resources in https://scan.endless.link/object/H9ZVsV1rME62Fr8yYGbb2DxekDFcUm9H2p3j2QFE8mTJ/resources?network=testnet 's Resources section.
new hero's armor,shield,weapon is null.
6.2 Mint a weapon and gem
6.2.1 Mint a weapon
Mint weapon NFT address is AAsAGWsPCLyn3Gx5b6hPixYWc1vM67YogwDNgx3mXyon
You can find it's Attribute in https://scan.endless.link/object/AAsAGWsPCLyn3Gx5b6hPixYWc1vM67YogwDNgx3mXyon/resources?network=testnet 's Resources section.
new weapon's gem is null.
6.2.2 Mint a gem
Mint gem NFT address is EiCX3rtKrhcXZsbeey1dqD5jeDPodcvqaWxG7JDKePoP.
You can find it's Attribute in https://scan.endless.link/object/EiCX3rtKrhcXZsbeey1dqD5jeDPodcvqaWxG7JDKePoP/resources?network=testnet 's Resources section.
6.3 Transfer nft
6.3.1 NFT Owner
NFTs mentioned in 5.1 and 5.2(hero,weapon and gem),You can see in the blockchain browser that they all belong to the hero contract FK3J64indQEVV42Qz1a1s38o4bx6DaZxyWL4WygKvc7c (bs58 format of mgr address)γThe initial owner of the minted NFT is it. Now we can transfer the minted NFT to alice(8wTNScjbYY4Gz4CbRa3oYmjBGCbTRekWDHJr4sHTYahW)γ
6.3.2 Transfer NFT to Alice
Transfer hero(H9ZVsV1rME62Fr8yYGbb2DxekDFcUm9H2p3j2QFE8mTJ),weapon(AAsAGWsPCLyn3Gx5b6hPixYWc1vM67YogwDNgx3mXyon) and gem(EiCX3rtKrhcXZsbeey1dqD5jeDPodcvqaWxG7JDKePoP) to alice
Now you can see on the browser that the owner of these three nfts has become alice(8wTNScjbYY4Gz4CbRa3oYmjBGCbTRekWDHJr4sHTYahW).
6.4 Equip and unequip
6.4.1 Weapon equip gem
After this transaction is executed, gem's owner is changed to weapon.
Weaponβs resources data is also changed."gem" filed is fill's gem nft's address EiCX3rtKrhcXZsbeey1dqD5jeDPodcvqaWxG7JDKePoP.
6.4.2 Hero equip weapon
Weapon's owner change to hero
Hero's weapon is "AAsAGWsPCLyn3Gx5b6hPixYWc1vM67YogwDNgx3mXyon"
6.4.3 Hero unequip weapon
weapon's owner is change again alice and hero's weapon is empty.
6.4.4 Weapon unquip gem
gem's owner is change again alice and weapon's gem is empty.
7 Summary
Through the example above, we have demonstrated from scratch how to interact with Endless using TypeScript. The content covers account creation, contract development and deployment, as well as how to call contracts. In developing these contracts, we leveraged Endless' NFT functionalities, including the processes of minting, transferring, and nesting NFTs. We hope this provides helpful insights into understanding Endless contracts and their interactions. Thank you.
Last updated