-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extend Readme
file
#3
Changes from all commits
cbe0001
d4111b8
3acaf79
6897ba5
b86dc30
3e02250
8ec5ed0
34168ea
3ceae75
ef9c945
c74e90a
d81b193
101facd
ad5f281
3f75dc7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
[config] | ||
[config.simulator] | ||
# server-port paramter specifies the port of the http server | ||
server-port = 8085 | ||
# num-of-shards parameter specifies the number of shard that chain simulator will simulate | ||
num-of-shards = 3 | ||
# round-duration-in-milliseconds parameter specifies the duration of a simulated round. The timestamp between two headers will correspond to the round duration but will not reflect real-time | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
round-duration-in-milliseconds = 6000 | ||
# rounds-per-epoch specifies the number of rounds per epoch | ||
rounds-per-epoch = 20 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import sys | ||
import time | ||
|
||
from multiversx_sdk_network_providers import ProxyNetworkProvider | ||
from multiversx_sdk_network_providers.transactions import TransactionOnNetwork | ||
from multiversx_sdk_core.transaction_factories import TokenManagementTransactionsFactory, TransactionsFactoryConfig | ||
from multiversx_sdk_core import TransactionComputer | ||
from multiversx_sdk_wallet import UserPEM, UserSigner | ||
from pathlib import Path | ||
|
||
SIMULATOR_URL = "http://localhost:8085" | ||
GENERATE_BLOCKS_URL = f"{SIMULATOR_URL}/simulator/generate-blocks" | ||
|
||
|
||
def main(): | ||
# create a network provider | ||
provider = ProxyNetworkProvider(SIMULATOR_URL) | ||
|
||
pem = UserPEM.from_file(Path("../wallets/wallet.pem")) | ||
|
||
# call proxy faucet | ||
address = pem.public_key.to_address("erd") | ||
data = {"receiver": f"{address.to_bech32()}"} | ||
provider.do_post(f"{SIMULATOR_URL}/transaction/send-user-funds", data) | ||
|
||
# generate 20 blocks to pass an epoch and the ESDT contract to be enabled | ||
provider.do_post(f"{GENERATE_BLOCKS_URL}/20", {}) | ||
|
||
# create transaction config and factory | ||
config = TransactionsFactoryConfig(provider.get_network_config().chain_id) | ||
transaction_factory = TokenManagementTransactionsFactory(config) | ||
|
||
# create issue transaction | ||
initial_supply = 100000 | ||
tx = transaction_factory.create_transaction_for_issuing_fungible( | ||
sender=address, | ||
token_name="tttt", | ||
token_ticker="TTTT", | ||
initial_supply=initial_supply, | ||
num_decimals=1, | ||
can_pause=False, | ||
can_wipe=False, | ||
can_freeze=False, | ||
can_upgrade=False, | ||
can_change_owner=False, | ||
can_add_special_roles=False, | ||
) | ||
|
||
# set issue cost and nonce | ||
tx.amount = 5000000000000000000 | ||
tx.nonce = provider.get_account(address).nonce | ||
|
||
# sign transaction | ||
user_signer = UserSigner(pem.secret_key) | ||
tx_computer = TransactionComputer() | ||
tx.signature = user_signer.sign(tx_computer.compute_bytes_for_signing(tx)) | ||
|
||
# send transaction | ||
tx_hash = provider.send_transaction(tx) | ||
print(f"generated tx hash: {tx_hash}") | ||
time.sleep(1) | ||
|
||
# execute 5 block ( transaction needs to be executed on source, block on source has to be finalized...) | ||
provider.do_post(f"{GENERATE_BLOCKS_URL}/5", {}) | ||
|
||
# get transaction with status | ||
tx_from_network = provider.get_transaction(tx_hash, with_process_status=True) | ||
|
||
# verify transaction status and account balance | ||
if not tx_from_network.status.is_successful(): | ||
sys.exit(f"transaction status is not correct, status received->{tx_from_network.status}") | ||
|
||
# verify token balance | ||
token_identifier_string = extract_token_identifier(tx_from_network) | ||
amount = provider.get_fungible_token_of_account(address, token_identifier_string) | ||
if amount.balance != initial_supply: | ||
sys.exit(f"amount of token from balance is no equal with the initial supply: actual-{amount.balance}, expected-{initial_supply}") | ||
|
||
print("transaction was executed and tokens were created") | ||
|
||
|
||
def extract_token_identifier(tx: TransactionOnNetwork) -> str: | ||
for event in tx.logs.events: | ||
if event.identifier != "upgradeProperties": | ||
continue | ||
|
||
decoded_bytes = bytes.fromhex(event.topics[0].hex()) | ||
return decoded_bytes.decode('utf-8') | ||
|
||
return "" | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-----BEGIN PRIVATE KEY for erd1l6xt0rqlyzw56a3k8xwwshq2dcjwy3q9cppucvqsmdyw8r98dz3sae0kxl----- | ||
MWFjYmU1YjhhNTg1M2QzOTRhNDQ0NmU4ODUzMDQ5NzdmMmNlOTdhYzk1OWE1NTJm | ||
NjI0YjBlOGRiM2M2NjZmMmZlOGNiNzhjMWYyMDlkNGQ3NjM2Mzk5Y2U4NWMwYTZl | ||
MjRlMjQ0MDVjMDQzY2MzMDEwZGI0OGUzOGNhNzY4YTM= | ||
-----END PRIVATE KEY for erd1l6xt0rqlyzw56a3k8xwwshq2dcjwy3q9cppucvqsmdyw8r98dz3sae0kxl----- |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package api | |
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
|
@@ -13,6 +14,7 @@ import ( | |
const ( | ||
generateBlocksEndpoint = "/simulator/generate-blocks/:num" | ||
initialWalletsEndpoint = "/simulator/initial-wallets" | ||
setKeyValuesEndpoint = "/simulator/address/:address/set-state" | ||
) | ||
|
||
type endpointsProcessor struct { | ||
|
@@ -33,8 +35,9 @@ func (ep *endpointsProcessor) ExtendProxyServer(httpServer *http.Server) error { | |
return errors.New("cannot cast httpServer.Handler to gin.Engine") | ||
} | ||
|
||
ws.GET(generateBlocksEndpoint, ep.generateBlocks) | ||
ws.POST(generateBlocksEndpoint, ep.generateBlocks) | ||
ws.GET(initialWalletsEndpoint, ep.initialWallets) | ||
ws.POST(setKeyValuesEndpoint, ep.setState) | ||
|
||
return nil | ||
} | ||
|
@@ -66,3 +69,26 @@ func (ep *endpointsProcessor) initialWallets(c *gin.Context) { | |
|
||
shared.RespondWith(c, http.StatusOK, initialWallets, "", data.ReturnCodeSuccess) | ||
} | ||
|
||
func (ep *endpointsProcessor) setState(c *gin.Context) { | ||
address := c.Param("address") | ||
if address == "" { | ||
shared.RespondWithBadRequest(c, "invalid provided address") | ||
return | ||
} | ||
|
||
var keyValueMap = map[string]string{} | ||
err := c.ShouldBindJSON(&keyValueMap) | ||
if err != nil { | ||
shared.RespondWithBadRequest(c, fmt.Sprintf("invalid key value map, error: %s", err.Error())) | ||
return | ||
} | ||
|
||
err = ep.facade.SetState(address, keyValueMap) | ||
if err != nil { | ||
shared.RespondWithInternalError(c, errors.New("cannot set state"), err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing return statement after this line There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
return | ||
} | ||
|
||
shared.RespondWith(c, http.StatusOK, gin.H{}, "", data.ReturnCodeSuccess) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍