Skip to content

Instantly share code, notes, and snippets.

@prajwolrg
prajwolrg / sample_crowdsale.py
Created June 10, 2020 00:52
Boilerplate code generated from tbears
from iconservice import *
TAG = 'SampleCrowdSale'
class SampleCrowdSale(IconScoreBase):
def __init__(self, db: IconScoreDatabase) -> None:
super().__init__(db)
def on_install(self) -> None:
@prajwolrg
prajwolrg / sample_crowdsale.py
Last active June 10, 2020 04:30
Full implementation of CrowdSale
from iconservice import *
#Only package allowed to import is iconservice. You can also write up your own classes and then use it by importing it.
TAG = 'SampleCrowdsale' #TAG is basically the same as your main Class name.
# An interface of token to give a reward to anyone who contributes
class TokenInterface(InterfaceScore):
@interface
def transfer(self, _to: Address, _value: int, _data: bytes=None):
pass
@prajwolrg
prajwolrg / sample_crowdsale.py
Created June 10, 2020 05:28
Import necessary packages
from iconsdk.exception import JSONRPCException
from iconsdk.libs.in_memory_zip import gen_deploy_data_content
from iconsdk.icon_service import IconService
from iconsdk.providers.http_provider import HTTPProvider
from iconsdk.builder.transaction_builder import CallTransactionBuilder, TransactionBuilder, DeployTransactionBuilder
from iconsdk.builder.call_builder import CallBuilder
from iconsdk.signed_transaction import SignedTransaction
from iconsdk.wallet.wallet import KeyWallet
from iconsdk.utils.convert_type import convert_hex_str_to_int
from repeater import retry
@prajwolrg
prajwolrg / sample_crowdsale.py
Last active June 10, 2020 05:41
Select the environment
#Uncommnet the line you want to use
# Mainnet | NID = 1
# icon_service = IconService(HTTPProvider("https://ctz.solidwallet.io", 1))
# Euljiro | NID = 2
# icon_service = IconService(HTTPProvider("https://test-ctz.solidwallet.io",2))
# Yeouido | NID = 3
icon_service = IconService(HTTPProvider("https://bicon.net.solidwallet.io", 3))
# Pagoda | NID = 80
# icon_service = IconService(HTTPProvider("https://zicon.net.solidwallet.io", 80))
@prajwolrg
prajwolrg / sample_crowdsale.py
Created June 10, 2020 05:46
Creating, Loading and Storing the wallet
# Generates a wallet
wallet = KeyWallet.create()
# Stores a key store file on the file path with password - abcd1234*
file_path = "./test/test_keystore"
wallet.store(file_path, "abcd1234*")
# Loads a wallet from a keystore file
wallet = KeyWallet.load(file_path, "abcd1234*")
@prajwolrg
prajwolrg / sample_crowdsale.py
Last active June 10, 2020 06:16
4. Set required parameters and send the signed DeployTransaction
GOVERNANCE_ADDRESS = "cx0000000000000000000000000000000000000000"
#Deploy parameters are defined in the SCORE in on_install method
DEPLOY_PARAMS = {
"_fundingGoalInIcx": "0x64",
"_tokenScore": "cx5afaf001f490f494ddeba5bcf4dd05fa47df7589",
"_durationInBlocks": "0x32" }
deploy_transaction = DeployTransactionBuilder()\
.from_(wallet.get_address())\
@prajwolrg
prajwolrg / sample_crowdsale.py
Last active June 10, 2020 06:34
5. Check the transaction result for any errors and save the Score Address
tx_result = icon_service.get_transaction_result(tx_hash)
tx_result
#This is the result displayed in the Jupyter Notebook
{'txHash': '0x724a5c1828464a27239fea7eefc41edab3796c792d84d29f5281a83791498be6',
'blockHeight': 37802,
'blockHash': '0x73080fadb4a3f44bce2bf406802cbbc01451838c2997dfd3d7c4e61620538de2',
'txIndex': 0,
'to': 'cx0000000000000000000000000000000000000000',
'scoreAddress': 'cxf11414ea548ae2d8eac40b5d488212c139718264', # <- This is the SCORE Address
@prajwolrg
prajwolrg / sample_crowdsale.py
Created June 10, 2020 11:30
6. Interact with the SCORE
#if params; define as dictionary as in the case of DEPLOY_PARAMS
params = {}
# Enters transaction information.
call_transaction = CallTransactionBuilder()\
.from_(wallet.get_address())\
.to(SCORE_ADDRESS) \
.nid(NID) \
.nonce(100) \
.value(1000000000) \ # only required if ICX is to be sent
@prajwolrg
prajwolrg / sample_crowdsale.py
Created June 10, 2020 11:32
7. Interact with the blockchain
# Returns block information by block height
block = icon_service.get_block(1000)
# Returns block information by block hash
block = icon_service.get_block("0x000...000")
# Returns the last block information
block = icon_service.get_block("latest")
# Returns the balance of the account of given address
@prajwolrg
prajwolrg / helloworld.sol
Last active April 20, 2022 06:01
Hello World Smart Contract
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/// @title Hello World Smart Contract
/// @author Prajwol Gyawali
contract HelloWorld {
uint256 no;