From 32a35dc739290ba04637f423d0000bd7459734cb Mon Sep 17 00:00:00 2001 From: Gauravc_GauravChauhan Date: Thu, 28 Apr 2022 19:32:33 +0530 Subject: [PATCH 1/5] XDC SDK --- .DS_Store | Bin 6148 -> 0 bytes .env | 1 + .gitattributes | 2 - .gitignore | 50 ++++ .vscode/settings.json | 5 + Base_API_Call.py | 47 --- EXECUTE_Test.py | 97 ------ LICENCE.txt | 7 + MANIFEST.in | 1 + README.md | 252 ++++------------ XDC3PYTHON/__init__.py | 0 XDC3PYTHON/createAccount.py | 9 + XDC3PYTHON/xrc20.py | 327 ++++++++++++++++++++ XDC3PYTHON/xrc721.py | 290 ++++++++++++++++++ __pycache__/Base_API_Call.cpython-37.pyc | Bin 753 -> 0 bytes __pycache__/Base_API_Call.cpython-39.pyc | Bin 1459 -> 0 bytes __pycache__/eth.cpython-39.pyc | Bin 10414 -> 0 bytes __pycache__/net.cpython-39.pyc | Bin 1221 -> 0 bytes __pycache__/tatum.cpython-39.pyc | Bin 6317 -> 0 bytes __pycache__/web3.cpython-37.pyc | Bin 1189 -> 0 bytes __pycache__/web3.cpython-39.pyc | Bin 994 -> 0 bytes common/xrc20abi.json | 222 ++++++++++++++ common/xrc721abi.json | 366 +++++++++++++++++++++++ eth.py | 213 ------------- net.py | 27 -- setup.py | 15 + tatum.py | 195 ------------ web3.py | 17 -- 28 files changed, 1345 insertions(+), 798 deletions(-) delete mode 100644 .DS_Store create mode 100644 .env delete mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 .vscode/settings.json delete mode 100644 Base_API_Call.py delete mode 100644 EXECUTE_Test.py create mode 100644 LICENCE.txt create mode 100644 MANIFEST.in create mode 100644 XDC3PYTHON/__init__.py create mode 100644 XDC3PYTHON/createAccount.py create mode 100644 XDC3PYTHON/xrc20.py create mode 100644 XDC3PYTHON/xrc721.py delete mode 100644 __pycache__/Base_API_Call.cpython-37.pyc delete mode 100644 __pycache__/Base_API_Call.cpython-39.pyc delete mode 100644 __pycache__/eth.cpython-39.pyc delete mode 100644 __pycache__/net.cpython-39.pyc delete mode 100644 __pycache__/tatum.cpython-39.pyc delete mode 100644 __pycache__/web3.cpython-37.pyc delete mode 100644 __pycache__/web3.cpython-39.pyc create mode 100644 common/xrc20abi.json create mode 100644 common/xrc721abi.json delete mode 100644 eth.py delete mode 100644 net.py create mode 100644 setup.py delete mode 100644 tatum.py delete mode 100644 web3.py diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 6aa4635bd3c01c98a46bb9c409a04f2f02cf6599..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKF-`+P474FdP@0sK`vN3>utb-Ff(AZ-Ldu{>Nc30bU9`L^%-9wpI%tq+&{(o( z*Xy~>O>vIR%vZ15YqO=9&EZ5lFief-^ogBS#({7=w z@Rh%R+U)ilobso=!(WbNSj(h<6p#W^Knh5K-zvb~3!5(z6{Ua_kOH3y@cYo<#9lZi z#-{@zMgZUp=`gHgmH;*;fW2@`LR+`j)_S(kDKwFy4h<&@wgrN z7UkwWQBev=fl~#}bGu~!U&DWx|4&KUNdYPFuN3gv@Gz|KO4VB@FUMZn;E! balance: + return "amount exceeds balance" + + amount = w3.toWei(amount, 'ether') + + Transfer = contractInstance.functions.transfer(spender, amount) + + hexData = Transfer._encode_transaction_data() + + data = hexstr_if_str(to_bytes, hexData) + + estimateGas = Transfer.estimateGas( + { + 'from': owner, + } + ) + + nonce = w3.eth.getTransactionCount(owner) + gasPrice = w3.eth.gas_price + + tx = { + 'nonce': nonce, + 'to': tokenAddr, + 'data': data, + 'gas': estimateGas, + 'gasPrice': gasPrice, + } + + signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + + txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) + return w3.toHex(txHash) + + # Approve the passed address to spend the specified amount of tokens on behalf of owner. + # This function required arguments. + # ownerAddress, ownerPrivateKey, spenderAddress, tokenAddr, amount. + + def approve(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + + owner = Web3.toChecksumAddress(ownerAddress) + spender = Web3.toChecksumAddress(spenderAddress) + + balance = contractInstance.functions.balanceOf(owner).call() + allowanceAmount = contractInstance.functions.allowance( + owner, spender).call() + + if amount > balance and allowanceAmount > balance: + return "amount exceeds balance" + + amount = w3.toWei(amount, 'ether') + + approveData = contractInstance.functions.approve(spender, amount) + + hexData = approveData._encode_transaction_data() + + data = hexstr_if_str(to_bytes, hexData) + + estimateGas = approveData.estimateGas() + + nonce = w3.eth.getTransactionCount(owner) + + gasPrice = w3.eth.gas_price + + tx = { + 'nonce': nonce, + 'to': tokenAddr, + 'data': data, + 'gas': estimateGas, + 'gasPrice': gasPrice, + } + + signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + + txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) + return w3.toHex(txHash) + + # increase the amount of tokens that an owner allowed to a spender. + # This function required arguments. + # owner address, ownerPrivateKey, spender address, token address, amount. + + def increaseAllowance(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): + + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + + owner = Web3.toChecksumAddress(ownerAddress) + spender = Web3.toChecksumAddress(spenderAddress) + + balance = contractInstance.functions.balanceOf(owner).call() + + allowanceAmount = contractInstance.functions.allowance( + owner, spender).call() + + allowanceAmount = w3.fromWei(allowanceAmount, 'ether') + + totalAmount = allowanceAmount + amount + + if totalAmount > balance: + return "amount exceeds balance" + + totalAmount = w3.toWei(totalAmount, 'ether') + + Transfer = contractInstance.functions.approve(spender, totalAmount) + + hexData = Transfer._encode_transaction_data() + + data = hexstr_if_str(to_bytes, hexData) + + estimateGas = Transfer.estimateGas() + + nonce = w3.eth.getTransactionCount(owner) + gasPrice = w3.eth.gas_price + + tx = { + 'nonce': nonce, + 'to': tokenAddr, + 'data': data, + 'gas': estimateGas, + 'gasPrice': gasPrice, + } + signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + + txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) + return w3.toHex(txHash) + + # decrease the amount of tokens that an owner allowed to a spender. + # This function required arguments. + # owner address, ownerPrivateKey, spender address, token address, amount. + + def decreaseAllowance(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + + owner = Web3.toChecksumAddress(ownerAddress) + spender = Web3.toChecksumAddress(spenderAddress) + + allowanceAmount = contractInstance.functions.allowance( + owner, spender).call() + allowanceAmount = (w3.fromWei(allowanceAmount, 'ether')) + + if allowanceAmount >= amount: + totalAmount = allowanceAmount - amount + else: + totalAmount = amount - allowanceAmount + + totalAmount = w3.toWei(totalAmount, 'ether') + + approveData = contractInstance.functions.approve(spender, totalAmount) + + hexData = approveData._encode_transaction_data() + + data = hexstr_if_str(to_bytes, hexData) + + estimateGas = approveData.estimateGas() + + nonce = w3.eth.getTransactionCount(owner) + gasPrice = w3.eth.gas_price + + tx = { + 'nonce': nonce, + 'to': tokenAddr, + 'data': data, + 'gas': estimateGas, + 'gasPrice': gasPrice, + } + signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + + txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) + return w3.toHex(txHash) + + # Transfer tokens from one address to another. + # This function requires following arguments. + # owner address, spenderPrivateKey, spender address, receiver address, token address, amount. + + def transferFrom(tokenAddr, ownerAddress, spenderPrivateKey, spenderAddress, receiver, amount): + + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + + owner = Web3.toChecksumAddress(ownerAddress) + receiverAddres = Web3.toChecksumAddress(receiver) + spender = Web3.toChecksumAddress(spenderAddress) + + amount = w3.toWei(amount, 'ether') + + transferData = contractInstance.functions.transferFrom( + owner, receiverAddres, amount) + + estimateGas = transferData.estimateGas({ + 'from': spender, + }) + + hexData = transferData._encode_transaction_data() + + data = hexstr_if_str(to_bytes, hexData) + + nonce = w3.eth.getTransactionCount(spender) + gasPrice = w3.eth.gas_price + + tx = { + 'nonce': nonce, + 'to': tokenAddr, + 'data': data, + 'gas': estimateGas, + 'gasPrice': gasPrice, + } + signedTx = w3.eth.account.signTransaction(tx, spenderPrivateKey) + + txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) + return w3.toHex(txHash) diff --git a/XDC3PYTHON/xrc721.py b/XDC3PYTHON/xrc721.py new file mode 100644 index 0000000..4852467 --- /dev/null +++ b/XDC3PYTHON/xrc721.py @@ -0,0 +1,290 @@ +from web3 import Web3 +from web3._utils.encoding import ( + hexstr_if_str, + to_bytes, +) +from decouple import config +import json + + +rpcUrl = config('NETWORK_URL') + +# HTTPProvider: +w3 = Web3(Web3.HTTPProvider(rpcUrl)) + +# path of abi file +with open('./common/xrc721abi.json') as abiJson: + xrc721abi = json.load(abiJson) + +# This is a class which consists all the methods as per XRC721 standards. + +class XRC721: + + # Gets the Name of the specified address. + # token address required as an argument. + + def name(tokenAddr): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + result = contractInstance.functions.name().call() + return result + + # Gets the Symbol of the specified address. + # token address required as an argument. + + def symbol(tokenAddr): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + result = contractInstance.functions.symbol().call() + return result + + # Gets the owner of an NFT. + # required arguments. + # token address, token id. + + def ownerOf(tokenAddr, tokenId): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + result = contractInstance.functions.ownerOf(tokenId).call() + return result + + # Gets the Totalsupply of the specified address. + # token address required as an argument. + + def totalSupply(tokenAddr): + token = Web3.toChecksumAddress(tokenAddr) + contractInstance = w3.eth.contract(address=token, abi=xrc721abi) + result = contractInstance.functions.totalSupply().call() + resultt = str(result) + return resultt + + # Gets the balance of the specified address. + # reuired arguments + # token address, owner address. + + def balanceOf(tokenAddr, ownerAddress): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + owner = Web3.toChecksumAddress(ownerAddress) + result = contractInstance.functions.balanceOf(owner).call() + return result + + # A distinct Uniform Resource Identifier (URI) for a given asset. + # Gets URI of a token. + # required arguments + # tokenId The identifier for an NFT. + # address of the token. + + def tokenURI(tokenAddr, tokenId): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + result = contractInstance.functions.tokenURI(tokenId).call() + return result + + # Enumerate NFTs assigned to an owner. + # tokenAddress An address for whom to query. + # IndexNO A counter less than `totalSupply()`. + # The token identifier for the `index`th NFT assigned to `owner`. + + def tokenByIndex(tokenAddr, index): + token = Web3.toChecksumAddress(tokenAddr) + contractInstance = w3.eth.contract(address=token, abi=xrc721abi) + result = contractInstance.functions.tokenByIndex(index).call() + return result + + # Enumerate NFTs assigned to an owner. + # The token identifier for the `index`th NFT assigned to `owner`. + # required arguments. + # owner address, token address, token index. + + def tokenofOwnerByIndex(tokenAddr, ownerAddress, index): + token = Web3.toChecksumAddress(tokenAddr) + contractInstance = w3.eth.contract(address=token, abi=xrc721abi) + owner = Web3.toChecksumAddress(ownerAddress) + result = contractInstance.functions.tokenOfOwnerByIndex( + owner, index).call() + return result + + # Query if a contract implements an interface. + # tokenAddress An address for whom to query and x_bytes The interface identifier. + # `true` if the contract implements `interfaceID` andinterfaceID` is not 0xffffffff, `false` otherwise. + # required arguments. + # token address, interface id. + + def supportInterface(tokenAddr, interfaceId): + token = Web3.toChecksumAddress(tokenAddr) + contractInstance = w3.eth.contract(address=token, abi=xrc721abi) + result = contractInstance.functions.supportsInterface( + interfaceId).call() + return result + + # The approved address for a token ID, or zero if no address set Reverts if the token ID does not exist. + # required arguments. + # token address, tokenId. + + def getApproved(tokenAddr, tokenId): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + result = contractInstance.functions.getApproved(tokenId).call() + return result + + # Tells whether an operator is approved by a given owner. + # required arguments. + # owner address, spender address, token address. + + def isApprovedForAll(tokenAddr, ownerAddress, spenderAddress): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + owner = Web3.toChecksumAddress(ownerAddress) + spender = Web3.toChecksumAddress(spenderAddress) + result = contractInstance.functions.isApprovedForAll( + owner, spender).call() + return result + + # Change or reaffirm the approved address for an NFT. + # The zero address indicates there is no approved address. + # Throws unless `owner` is the current NFT owner, or an authorized. + # required arguments. + # tokenAddress, owner address, ownerPrivateKey, spenderAddress, tokenID. + + def approve(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, tokenId): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + + owner = Web3.toChecksumAddress(ownerAddress) + spender = Web3.toChecksumAddress(spenderAddress) + + approveData = contractInstance.functions.approve(spender, tokenId) + + hexData = approveData._encode_transaction_data() + + data = hexstr_if_str(to_bytes, hexData) + + estimateGas = approveData.estimateGas({ + 'from': owner, + }) + + nonce = w3.eth.getTransactionCount(owner) + + gasPrice = w3.eth.gas_price + + tx = { + 'nonce': nonce, + 'to': tokenAddr, + 'data': data, + 'gas': estimateGas, + 'gasPrice': gasPrice, + } + + signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + + txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) + return w3.toHex(txHash) + + # Enable or disable approval for a third party ("operator") to manage all of `Owner`'s assets + # Emits the ApprovalForAll event. The contract MUST allow multiple operators per owner. + # required arguments. + # token address, owner address, ownerPrivateKey, sepnder address, tokenId. + + def setApprovalForAll(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, boolValue): + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + + owner = Web3.toChecksumAddress(ownerAddress) + spender = Web3.toChecksumAddress(spenderAddress) + + approveData = contractInstance.functions.setApprovalForAll( + spender, boolValue) + + hexData = approveData._encode_transaction_data() + + data = hexstr_if_str(to_bytes, hexData) + + estimateGas = approveData.estimateGas({ + 'from': owner, + }) + + nonce = w3.eth.getTransactionCount(owner) + + gasPrice = w3.eth.gas_price + + tx = { + 'nonce': nonce, + 'to': tokenAddr, + 'data': data, + 'gas': estimateGas, + 'gasPrice': gasPrice, + } + + signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + + txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) + return w3.toHex(txHash) + + # Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE + # to confirm that `reciever Address` is capable of receiving NFTs or else they may be permanently lost. + # required arguments. + # token address, owner address, spender address, spenderPrivateKey, receiver address, tokenId. + + def transferFrom(tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey, receiver, tokenId): + + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + + owner = Web3.toChecksumAddress(ownerAddress) + receiverAddres = Web3.toChecksumAddress(receiver) + spender = Web3.toChecksumAddress(spenderAddress) + + transferData = contractInstance.functions.transferFrom( + owner, receiverAddres, tokenId) + + estimateGas = transferData.estimateGas({ + 'from': spender, + }) + + hexData = transferData._encode_transaction_data() + + data = hexstr_if_str(to_bytes, hexData) + + nonce = w3.eth.getTransactionCount(spender) + gasPrice = w3.eth.gas_price + + tx = { + 'nonce': nonce, + 'to': tokenAddr, + 'data': data, + 'gas': estimateGas, + 'gasPrice': gasPrice, + } + signedTx = w3.eth.account.signTransaction(tx, spenderPrivateKey) + + txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) + return w3.toHex(txHash) + + # Transfers the ownership of an NFT from one address to another address. + # required arguments. + # token address, owner address, spender address, spenderPrivateKey, receiver address, tokenId. + + def safeTransferFrom(tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey, receiver, tokenId): + + contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + + owner = Web3.toChecksumAddress(ownerAddress) + receiverAddres = Web3.toChecksumAddress(receiver) + spender = Web3.toChecksumAddress(spenderAddress) + + transferData = contractInstance.functions.safeTransferFrom( + owner, receiverAddres, tokenId) + + estimateGas = transferData.estimateGas({ + 'from': spender, + }) + + hexData = transferData._encode_transaction_data() + + data = hexstr_if_str(to_bytes, hexData) + + nonce = w3.eth.getTransactionCount(spender) + gasPrice = w3.eth.gas_price + + tx = { + 'nonce': nonce, + 'to': tokenAddr, + 'data': data, + 'gas': estimateGas, + 'gasPrice': gasPrice, + } + signedTx = w3.eth.account.signTransaction(tx, spenderPrivateKey) + + txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) + return w3.toHex(txHash) diff --git a/__pycache__/Base_API_Call.cpython-37.pyc b/__pycache__/Base_API_Call.cpython-37.pyc deleted file mode 100644 index 82704d88d406c0d8437c3fadefd500f370ef2615..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 753 zcmY*X&u`N(6t)wm4O_=1#EBEitc% z+9P-Vnp`>YFCY#)w+szSets|C_dUNnzxKw)8iM7&|NY@RM(BqPu9geV0qo)q7>+p3 zP>dfV?r`@KareTB9gh2`Nqz#3x{0+{%K_&Ac5xew03An`&7DgWyWF*^%6-&(`cLEm zuLiqgMbE$(bc*L7S>PF5Im6uLB*imlisml&cx{TO&VpbBF z1-`%-%7&}_Zh1@RN%$p|C#ejjFy~5tF56#&(?Ur=g8krLxD#v#BVmS$LwGJe-ls{H z3TcjnE>a}{&6#GSBG`ZXZaYYUwL2>tiIN6LEi=hQxyAB4OA}^*AKBbxy>)!I_p(1I zH>2pkbfq^oC#}7vUy-3Pd0bne@#d4kpx5sxDMez_#C0vs#-cFs`X^zuC~_qWQ4uW| zue@BR(o{7r62(Q+iwTqilAKL4#dusF0t?EIT@Y4;GsC_i?L&8e?L}`2K%!UC3ze{p zz81NPjt@JuH-Q#N+CO|wk9NbIsLcvN;iYJYWm%X{DqbZbN7mam6B`xDp>3 zZhA^WOXaH$w66jcLU+`X>Ou!<8J>I2ySwoyMUvk}fK>-`9kaRxK@P`!E^BX>)vOjZ zujN8=hCN@fxnKLc0=iRob`f;ZBs`k+(jvDyw>Rugsb~G9EVFT%*m02$-&I9E^QZRB zjXMJw1xCN0>dHpTZ?!eLDvP|*P8yvk*HJnxlpX|;Op`2&f->33ibO@hzIP|F$fIBz z*4k8&2M127#3p;p5RxxiC_P@dUJR$ zsdS8D#OsK(TJAVW3!kdcbd!go&qK&r2&+{(OgT;u23b;Lo4Q@a!m?dnvjv~?nk#;u z%~3fepgm|0TGo=9J8}V4#FVKMV_*h^X=Vq)N&4)30$dGzCm(M<8%6Sw)e}P*nPn0^ z5`?rBAy)3xi?*cko5D;IYqdFn&-*aC1cG3kAs9bdri9~Q5D&g_;a_ZO5b_^k))WT{ zsRY(nTAW~osJ#UceZQak*o)dHUgWp-?`(Zn$k_fpn6})42SxfSm_T;m*RU`U8TAkV{mp z4+`|WR?oUrzt-^Bm)Q`3+GFvAK5&i~zRC8)XciLHF5&hbcYbNcxjhoUX=T&J-pj;J zCvh8LK&cRH#%FyBeDv`5!yAHR&XG9e%wfp0t=9umw4t@R4*P@P7Y>8}Xf_&zE{x+m zndmrn-8h~UYMRly7sv0XN!Fe;J!$%5&QK!fNnH3vN=>Wj_tdZ#yj=>z@O0=k-@$|x h9J%FfYs=M^2~N_cWyo<5!@t_B{-(EEk!N}gUjhBlLZtuz diff --git a/__pycache__/eth.cpython-39.pyc b/__pycache__/eth.cpython-39.pyc deleted file mode 100644 index dfeaf509acc4b21620a35b6f2664de124dfe93c2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10414 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 diff --git a/__pycache__/net.cpython-39.pyc b/__pycache__/net.cpython-39.pyc deleted file mode 100644 index 5ca6a8ec2bff04568622c463296b599a6f11540b..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1221 zcmb_c&2H2%5Vjp>lWn^c3iQY!x0R}-AVj6As(`Kd*LGnGLP5wxaY|FN&eq8SU3O3O z#8b4#J_+yOD_nR5#DQ^ki9%Ji2XN$xXENjYzVSFgqcMYEeE;(6%~u1VZ(6Aiu%X*9 zGZzF$94DxUeT?;*B*Zt0H}&a##0^dl5T~DsZ*tN_P4frDpe9zvozBB{sFms{w{C+q zLO$lmCy}vB4^57bFelrj*b@7-6IPSg4zO>Xuv**(tIg|>dPdEPC}+>2y+M@a!v)z7 z-H)-@j)e>M52<{o=%emtJ0ulECaf)(EtuIg5FC#QCu4&fW16EwJR&1HGDc{Gk4#8H z+lqEJyIaG$=e>_)7N=sk_Q~1JQUUp#Rp*Ag>?}FGDBnprs2b7K56t~Q2EEK#ef4_D ziQ)6DmGLo=8jq^oOJy&}k4;6xR0u`4L07aDB#E+GsmLRdE2|UiCuzWsk-PvwKV9#6 z1|Ynr-jg&865!rXy_Xv;*4c;rf^|0@vlrLhWe;Gu{e5Lkq0}^68wA2sKV*2hQaX3) z^^b*;y0*hv&r9MA(8uEKaQ&Q0m7b^9N^0Cv<1Xk=#5|~~UPd-R{hQj=Dz(z@f064) zk!+;{k)KPh^gM;!%FXc^wpV)njpIn3xMT@JWx2bGhWGW8UUYkH)$A6N{VH?}zH^H_iLjV8( diff --git a/__pycache__/tatum.cpython-39.pyc b/__pycache__/tatum.cpython-39.pyc deleted file mode 100644 index 2c5f65ea650a44589a0c9ecd209595efde1c6e2a..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6317 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 WxS@Bw=Y1b4=)Gg1O;|{q)&2u>m%_*Z diff --git a/__pycache__/web3.cpython-37.pyc b/__pycache__/web3.cpython-37.pyc deleted file mode 100644 index 611f8516f5275d0b46c3a054384648cbe34dc311..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1189 zcmc&zO>fgc5Z$%ciD?u1!L8zee2dh^=?7_5Ayl=MK!8dip{i0LW4s&UlGyHgqc$cv zwLc^|@|X6?iNAn2Fq5=Za_SLB`#hVOc{4k2yLi$!>Akybs5X=+5QjbJdGppIJ?e9 zx#Fxk9idt=hwZq>pdE@xod}tRaRlDPmwqR8cF#VqIw3@@53)iKM+$=0R4)IPW5>;fM0W?vb|kxZf}6S<=kpt-Fi^` z1{*7%b6sS=7hIY!HHib1cA_C4^E+C+U6IcREGc17C zog~JTBug`rVB`_p6x;J6zau}puBeWQ=qog=?x^`)6 zoD-FJjrN545WL75x9iUT|S;;0uL*h|M zJQfluFiPlakc4%28?d3~9;AG4(3ahBkA$dJukG6|cj+{V>h)YiblU4hL~> z5I5p5J4nTc(AuKPJu=^%;owJ*+Yn#9o0X*zcsE6%br46}YB!G+G^JP*PnU-`q-X}){E>i?|QR2ScXo=1d^p8b%nK9dp`s zTiZg!{xB3m&kHe3gM)#rwdmWhd~DKLtVl4i!%(D z(I_O9BN$XRU8+nXb Date: Wed, 11 May 2022 16:45:50 +0530 Subject: [PATCH 2/5] python xdc sdk xrc20 and xrc721 methods --- .env | 1 - README.md | 100 +++++------- XDC3PYTHON/xrc20.py | 186 ++++++++++----------- XDC3PYTHON/xrc721.py | 145 ++++++++--------- common/xrc20abi.json | 222 ------------------------- common/xrc721abi.json | 366 ------------------------------------------ setup.py | 4 +- 7 files changed, 202 insertions(+), 822 deletions(-) delete mode 100644 .env delete mode 100644 common/xrc20abi.json delete mode 100644 common/xrc721abi.json diff --git a/.env b/.env deleted file mode 100644 index 1d00c4e..0000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -NETWORK_URL = "https://rpc.apothem.network" \ No newline at end of file diff --git a/README.md b/README.md index 0cc0d3c..4b64951 100644 --- a/README.md +++ b/README.md @@ -1,86 +1,72 @@ # XDC3PYTHON -``` -XDC3PYTHON SDK with support for smart contracts, XRC20 & XRC721. -``` -# Usage -``` -run this command pip install XDC3PYTHON to add dependecies. -``` +XDC3PYTHON SDK with support for smart contracts, XDC20 & XRC721. -[dependencies] -``` -XDC3PYTHON = "1.0.0" -``` -This SDK supports following Read & Write operations:- +## Usage +**pip install XDC3PYTHON** -``` +### This SDK supports following Read & Write operations:- - | XRC20 Token: Read methods | XRC20 Token: Write methods | - | | | - | name() | approve(receiverAddress , amount) | - | symbol() | transfer(recipient, amount) | - | decimal() | transferFrom(sender, recipient, amount) | - | totalSupply() | increaseAllowance(spender, addedValue) | - | balanceOf(account) | decreaseAllowance(spender, subtractedValue) | - | allowance(owner, spender) | | - | | | - - | XRC721 Token: Read methods | XRC721 Token: Write methods | - | | | - | name() | setApprovalForAll(spenderAddress, booleanValue) | - | symbol() | approve(spenderAddress , tokenId) | - | totalSupply() | transferFrom(receiver, tokenId) | - | balanceOf(ownerAddress) | safeTransferFrom(receiver, tokenId) | - | ownerOf(tokenId) | | - | tokenURI(tokenId) | | - | tokenByIndex(index) | | - | tokenOfOwnerByIndex(ownerAddress,index) | | - | supportInterface(interfaceId) | | - | getApproved(tokenId) | | - | isApprovedForAll(ownerAddress,spenderAddr) | | - | | | +* xrc20 methods. + * Read methods. + * name(), balanceOf(account), totalSupply(), symbol(), decimals(), allowance(pwner, spender). -``` + * Write methods. + * transferXDC(owner,receiver), approve(spender,amount), transferToken(receiver,amount), increaseAllowance(spender, addedValue), decreaseAllowance(spender, subtractedValue), transferFrom(sender, receiver, amount). -# Environment Variable -``` -Create a .env file in the root directory of the Python project to put the wallet and endpoint information in like so: NETWORK_URL = "https://rpc.apothem.network" -``` +* xrc721 methods. + * Read methods. + * name(), symbol(), totalsupply(), balanceOf(ownerAddr), ownerOf(tokenId), tokenURI(tokenId), tokenByIndex(index), tokenOfOwnerByIndex(ownerAddress,index), supportInterface(interfaceId), getApproved(tokenId), isApprovedForAll(ownerAddress,spenderAddress). + + * Write methods. + * setApprovalForAll(spenderAddress, booleanValue), approve(sepnderAddress , tokenId), transferFrom(recipient, tokenId), safeTransferFrom(spender, tokenId). -Example for XRC20. -``` -from XDC3PYTHON import XRC20 +### Example for XRC20. +``` +from XDC3PYTHON.xrc20 import XRC20 + if __name__=="__main__": - token = input('Enter token address: ') - a = XRC20.name(token) - print(a) + NETWORK_URL = "Your endpoint Url" + + obj = XRC20(NETWORK_URL) + + tokenAddr = input('Enter Token Address: ') + + tokenSymbol = obj.name(tokenAddr) + print(tokenSymbol) ``` This example returns name of the specified address. -Example for XRC721. +### Example for XRC721. + ``` +from XDC3PYTHON.xrc721 import XRC721 -from XDC3PYTHON import XRC721 +if __name__=="__main__": + NETWORK_URL = "Your endpoint Url" -if __name__=="__main__": + obj = XRC721(NETWORK_URL) - token = input('Enter token address: ') - a = XRC721.symbol(token) - print(a) + tokenAddr = input('Enter Token Address: ') + + tokenSymbol = obj.name(tokenAddr) + print(tokenSymbol) ``` This example returns symbol of the specified address. # Transports -``` - HTTP transport -``` + +**HTTP transport** + +### Author +[XDCFoundation](https://github.com/XDCFoundation/XDC_Python_SDK_V1) + diff --git a/XDC3PYTHON/xrc20.py b/XDC3PYTHON/xrc20.py index eab168f..af78d2f 100644 --- a/XDC3PYTHON/xrc20.py +++ b/XDC3PYTHON/xrc20.py @@ -1,91 +1,93 @@ +from lib2to3.pgen2 import token from web3 import Web3 from web3._utils.encoding import ( hexstr_if_str, to_bytes, ) -from decouple import config -import json +# xrc20 abi.json +xrc20abi = "[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"balance\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"_owner\",\"type\":\"address\"},{\"name\":\"_spender\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"fallback\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"spender\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"}]" -rpcUrl = config('NETWORK_URL') - -# HTTPProvider: -w3 = Web3(Web3.HTTPProvider(rpcUrl)) - -# path of abi file -with open('./common/xrc20abi.json') as abiJson: - xrc20abi = json.load(abiJson) # This is a class which consists all the methods as per XRC20 standards. class XRC20: + def __init__(self,rpcUrl): + self.rpcUrl = rpcUrl + + # connection to network. + + def getConnection(self): + w3 = Web3(Web3.HTTPProvider(self.rpcUrl)) + return w3 + + # get contract Instance. + + def getContractInstance(self, tokenAddr): + contractInstance = self.getConnection().eth.contract(address=tokenAddr, abi=xrc20abi) + return contractInstance + # Gets the Name of the specified address. # Token address is required as argument. - def name(tokenAddr): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) - name = contractInstance.functions.name().call() + def name(self, tokenAddr): + name = self.getContractInstance(tokenAddr).functions.name().call() return name # Gets method returns total supply of token. # Token address is required as argument. - def totalSupply(tokenAddr): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) - totalSupply = contractInstance.functions.totalSupply().call() - totalSupply = w3.fromWei(totalSupply, 'ether') + def totalSupply(self, tokenAddr): + totalSupply = self.getContractInstance(tokenAddr).functions.totalSupply().call() + totalSupply = self.getConnection().fromWei(totalSupply, 'ether') return totalSupply # Gets the Decimal of the specified address. # Token address is required as argument. - def decimal(tokenAddr): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) - decimal = contractInstance.functions.decimals().call() + def decimal(self, tokenAddr): + decimal = self.getContractInstance(tokenAddr).functions.decimals().call() return decimal # Gets the Symbol of the specified address. # Token address is required as argument. - def symbol(tokenAddr): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) - symbol = contractInstance.functions.symbol().call() + def symbol(self, tokenAddr): + symbol = self.getContractInstance(tokenAddr).functions.symbol().call() return symbol # Gets the balance of the specified address. # token address, owner address are required as arguments. - def balanceOf(tokenAddr, ownerAddress): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + def balanceOf(self, tokenAddr, ownerAddress): owner = Web3.toChecksumAddress(ownerAddress) - balance = contractInstance.functions.balanceOf(owner).call() - return w3.fromWei(balance, 'ether') + balance = self.getContractInstance(tokenAddr).functions.balanceOf(owner).call() + return self.getConnection().fromWei(balance, 'ether') # This method returns how much allowance spender have from owner. # This function required three arguments. # owner address, spender address, token address. - def allowance(tokenAddr, ownerAddress, spenderAddress): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + def allowance(self, tokenAddr, ownerAddress, spenderAddress): owner = Web3.toChecksumAddress(ownerAddress) spender = Web3.toChecksumAddress(spenderAddress) - Allowance = contractInstance.functions.allowance(owner, spender).call() - return w3.fromWei(Allowance, 'ether') + Allowance = self.getContractInstance(tokenAddr).functions.allowance(owner, spender).call() + return self.getConnection().fromWei(Allowance, 'ether') # Transfer XDC for a specified address. # This function requires following arguments. # private key, recipient address, amount. - def transferXDC(ownerAddress, receiverAddress, ownerPrivateKey, amount): + def transferXDC(self, ownerAddress, receiverAddress, ownerPrivateKey, amount): owner = Web3.toChecksumAddress(ownerAddress) spender = Web3.toChecksumAddress(receiverAddress) - amount = w3.toWei(amount, 'ether') + amount = self.getConnection().toWei(amount, 'ether') - nonce = w3.eth.getTransactionCount(owner) - gasPrice = w3.eth.gas_price - estimateGas = w3.eth.estimateGas({ + nonce = self.getConnection().eth.getTransactionCount(owner) + gasPrice = self.getConnection().eth.gas_price + estimateGas = self.getConnection().eth.estimateGas({ 'to': spender, 'from': owner, 'value': amount @@ -99,30 +101,28 @@ def transferXDC(ownerAddress, receiverAddress, ownerPrivateKey, amount): 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, ownerPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) # Transfer token for a specified address. # This function requires following arguments. # ownerAddress, ownerPrivateKey, receiver address, token address, amount. - def transferToken(tokenAddr, ownerAddress, ownerPrivateKey, receiverAddress, amount): - - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + def transferToken(self, tokenAddr, ownerAddress, ownerPrivateKey, receiverAddress, amount): owner = Web3.toChecksumAddress(ownerAddress) spender = Web3.toChecksumAddress(receiverAddress) - balance = contractInstance.functions.balanceOf(owner).call() + balance = self.balanceOf(tokenAddr,owner) if amount > balance: return "amount exceeds balance" - amount = w3.toWei(amount, 'ether') + amount = self.getConnection().toWei(amount, 'ether') - Transfer = contractInstance.functions.transfer(spender, amount) + Transfer = self.getContractInstance(tokenAddr).functions.transfer(spender, amount) hexData = Transfer._encode_transaction_data() @@ -134,8 +134,8 @@ def transferToken(tokenAddr, ownerAddress, ownerPrivateKey, receiverAddress, am } ) - nonce = w3.eth.getTransactionCount(owner) - gasPrice = w3.eth.gas_price + nonce = self.getConnection().eth.getTransactionCount(owner) + gasPrice = self.getConnection().eth.gas_price tx = { 'nonce': nonce, @@ -145,31 +145,29 @@ def transferToken(tokenAddr, ownerAddress, ownerPrivateKey, receiverAddress, am 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, ownerPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) # Approve the passed address to spend the specified amount of tokens on behalf of owner. # This function required arguments. # ownerAddress, ownerPrivateKey, spenderAddress, tokenAddr, amount. - def approve(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + def approve(self, tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): owner = Web3.toChecksumAddress(ownerAddress) spender = Web3.toChecksumAddress(spenderAddress) - balance = contractInstance.functions.balanceOf(owner).call() - allowanceAmount = contractInstance.functions.allowance( - owner, spender).call() + balance = self.balanceOf(tokenAddr, owner) + allowanceAmount = self.allowance(tokenAddr, owner, spender) if amount > balance and allowanceAmount > balance: return "amount exceeds balance" - amount = w3.toWei(amount, 'ether') + amount = self.getConnection().toWei(amount, 'ether') - approveData = contractInstance.functions.approve(spender, amount) + approveData = self.getContractInstance(tokenAddr).functions.approve(spender, amount) hexData = approveData._encode_transaction_data() @@ -177,9 +175,9 @@ def approve(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): estimateGas = approveData.estimateGas() - nonce = w3.eth.getTransactionCount(owner) + nonce = self.getConnection().eth.getTransactionCount(owner) - gasPrice = w3.eth.gas_price + gasPrice = self.getConnection().eth.gas_price tx = { 'nonce': nonce, @@ -189,37 +187,35 @@ def approve(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, ownerPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) # increase the amount of tokens that an owner allowed to a spender. # This function required arguments. # owner address, ownerPrivateKey, spender address, token address, amount. - def increaseAllowance(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): + def increaseAllowance(self, tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) owner = Web3.toChecksumAddress(ownerAddress) spender = Web3.toChecksumAddress(spenderAddress) - balance = contractInstance.functions.balanceOf(owner).call() + balance = self.balanceOf(tokenAddr, owner) - allowanceAmount = contractInstance.functions.allowance( - owner, spender).call() + allowanceAmount = self.allowance(tokenAddr, owner , spender) - allowanceAmount = w3.fromWei(allowanceAmount, 'ether') + allowanceAmount = self.getConnection().fromWei(allowanceAmount, 'ether') totalAmount = allowanceAmount + amount if totalAmount > balance: return "amount exceeds balance" - totalAmount = w3.toWei(totalAmount, 'ether') + totalAmount = self.getConnection().toWei(totalAmount, 'ether') - Transfer = contractInstance.functions.approve(spender, totalAmount) + Transfer = self.getContractInstance(tokenAddr).functions.approve(spender, totalAmount) hexData = Transfer._encode_transaction_data() @@ -227,8 +223,8 @@ def increaseAllowance(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, estimateGas = Transfer.estimateGas() - nonce = w3.eth.getTransactionCount(owner) - gasPrice = w3.eth.gas_price + nonce = self.getConnection().eth.getTransactionCount(owner) + gasPrice = self.getConnection().eth.gas_price tx = { 'nonce': nonce, @@ -237,33 +233,31 @@ def increaseAllowance(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, 'gas': estimateGas, 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, ownerPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) # decrease the amount of tokens that an owner allowed to a spender. # This function required arguments. # owner address, ownerPrivateKey, spender address, token address, amount. - def decreaseAllowance(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + def decreaseAllowance(self, tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, amount): owner = Web3.toChecksumAddress(ownerAddress) spender = Web3.toChecksumAddress(spenderAddress) - allowanceAmount = contractInstance.functions.allowance( - owner, spender).call() - allowanceAmount = (w3.fromWei(allowanceAmount, 'ether')) + allowanceAmount = self.allowance(tokenAddr, owner, spender) + allowanceAmount = self.getConnection().fromWei(allowanceAmount, 'ether') if allowanceAmount >= amount: totalAmount = allowanceAmount - amount else: totalAmount = amount - allowanceAmount - totalAmount = w3.toWei(totalAmount, 'ether') + totalAmount = self.getConnection().toWei(totalAmount, 'ether') - approveData = contractInstance.functions.approve(spender, totalAmount) + approveData = self.getContractInstance(tokenAddr).functions.approve(spender, totalAmount) hexData = approveData._encode_transaction_data() @@ -271,8 +265,8 @@ def decreaseAllowance(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, estimateGas = approveData.estimateGas() - nonce = w3.eth.getTransactionCount(owner) - gasPrice = w3.eth.gas_price + nonce = self.getConnection().eth.getTransactionCount(owner) + gasPrice = self.getConnection().eth.gas_price tx = { 'nonce': nonce, @@ -281,26 +275,24 @@ def decreaseAllowance(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, 'gas': estimateGas, 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, ownerPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) # Transfer tokens from one address to another. # This function requires following arguments. # owner address, spenderPrivateKey, spender address, receiver address, token address, amount. - def transferFrom(tokenAddr, ownerAddress, spenderPrivateKey, spenderAddress, receiver, amount): - - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc20abi) + def transferFrom(self, tokenAddr, ownerAddress, spenderPrivateKey, spenderAddress, receiver, amount): owner = Web3.toChecksumAddress(ownerAddress) receiverAddres = Web3.toChecksumAddress(receiver) spender = Web3.toChecksumAddress(spenderAddress) - amount = w3.toWei(amount, 'ether') + amount = self.getConnection().toWei(amount, 'ether') - transferData = contractInstance.functions.transferFrom( + transferData = self.getContractInstance(tokenAddr).functions.transferFrom( owner, receiverAddres, amount) estimateGas = transferData.estimateGas({ @@ -311,8 +303,8 @@ def transferFrom(tokenAddr, ownerAddress, spenderPrivateKey, spenderAddress, r data = hexstr_if_str(to_bytes, hexData) - nonce = w3.eth.getTransactionCount(spender) - gasPrice = w3.eth.gas_price + nonce = self.getConnection().eth.getTransactionCount(spender) + gasPrice = self.getConnection().eth.gas_price tx = { 'nonce': nonce, @@ -321,7 +313,7 @@ def transferFrom(tokenAddr, ownerAddress, spenderPrivateKey, spenderAddress, r 'gas': estimateGas, 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, spenderPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, spenderPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) diff --git a/XDC3PYTHON/xrc721.py b/XDC3PYTHON/xrc721.py index 4852467..2740422 100644 --- a/XDC3PYTHON/xrc721.py +++ b/XDC3PYTHON/xrc721.py @@ -3,55 +3,59 @@ hexstr_if_str, to_bytes, ) -from decouple import config -import json -rpcUrl = config('NETWORK_URL') +# xrc721 abi.json +xrc721abi = "[{\"constant\":true,\"inputs\":[{\"name\":\"interfaceId\",\"type\":\"bytes4\"}],\"name\":\"supportsInterface\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"getApproved\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"to\",\"type\":\"address\"},{\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"from\",\"type\":\"address\"},{\"name\":\"to\",\"type\":\"address\"},{\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"owner\",\"type\":\"address\"},{\"name\":\"index\",\"type\":\"uint256\"}],\"name\":\"tokenOfOwnerByIndex\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"from\",\"type\":\"address\"},{\"name\":\"to\",\"type\":\"address\"},{\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"safeTransferFrom\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"index\",\"type\":\"uint256\"}],\"name\":\"tokenByIndex\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"ownerOf\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"owner\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"to\",\"type\":\"address\"},{\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"setApprovalForAll\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"from\",\"type\":\"address\"},{\"name\":\"to\",\"type\":\"address\"},{\"name\":\"tokenId\",\"type\":\"uint256\"},{\"name\":\"_data\",\"type\":\"bytes\"}],\"name\":\"safeTransferFrom1\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"tokenURI\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_tokenId\",\"type\":\"uint256\"},{\"name\":\"_uri\",\"type\":\"string\"}],\"name\":\"mint\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"owner\",\"type\":\"address\"},{\"name\":\"operator\",\"type\":\"address\"}],\"name\":\"isApprovedForAll\",\"outputs\":[{\"name\":\"\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"symbol\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"approved\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"tokenId\",\"type\":\"uint256\"}],\"name\":\"Approval\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"owner\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"operator\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"approved\",\"type\":\"bool\"}],\"name\":\"ApprovalForAll\",\"type\":\"event\"}]" -# HTTPProvider: -w3 = Web3(Web3.HTTPProvider(rpcUrl)) - -# path of abi file -with open('./common/xrc721abi.json') as abiJson: - xrc721abi = json.load(abiJson) # This is a class which consists all the methods as per XRC721 standards. class XRC721: + def __init__(self,rpcUrl): + self.rpcUrl = rpcUrl + + # connection to network. + + def getConnection(self): + w3 = Web3(Web3.HTTPProvider(self.rpcUrl)) + return w3 + + # get contract Instance. + + def getContractInstance(self, tokenAddr): + contractInstance = self.getConnection().eth.contract(address=tokenAddr, abi=xrc721abi) + return contractInstance + # Gets the Name of the specified address. # token address required as an argument. - def name(tokenAddr): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) - result = contractInstance.functions.name().call() + def name(self, tokenAddr): + result = self.getContractInstance(tokenAddr).functions.name().call() return result # Gets the Symbol of the specified address. # token address required as an argument. - def symbol(tokenAddr): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) - result = contractInstance.functions.symbol().call() + def symbol(self, tokenAddr): + result = self.getContractInstance(tokenAddr).functions.symbol().call() return result # Gets the owner of an NFT. # required arguments. # token address, token id. - def ownerOf(tokenAddr, tokenId): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) - result = contractInstance.functions.ownerOf(tokenId).call() + def ownerOf(self, tokenAddr, tokenId): + result = self.getContractInstance(tokenAddr).functions.ownerOf(tokenId).call() return result # Gets the Totalsupply of the specified address. # token address required as an argument. - def totalSupply(tokenAddr): + def totalSupply(self, tokenAddr): token = Web3.toChecksumAddress(tokenAddr) - contractInstance = w3.eth.contract(address=token, abi=xrc721abi) - result = contractInstance.functions.totalSupply().call() + result = self.getContractInstance(tokenAddr).functions.totalSupply().call() resultt = str(result) return resultt @@ -59,10 +63,9 @@ def totalSupply(tokenAddr): # reuired arguments # token address, owner address. - def balanceOf(tokenAddr, ownerAddress): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + def balanceOf(self, tokenAddr, ownerAddress): owner = Web3.toChecksumAddress(ownerAddress) - result = contractInstance.functions.balanceOf(owner).call() + result = self.getContractInstance(tokenAddr).functions.balanceOf(owner).call() return result # A distinct Uniform Resource Identifier (URI) for a given asset. @@ -71,9 +74,8 @@ def balanceOf(tokenAddr, ownerAddress): # tokenId The identifier for an NFT. # address of the token. - def tokenURI(tokenAddr, tokenId): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) - result = contractInstance.functions.tokenURI(tokenId).call() + def tokenURI(self, tokenAddr, tokenId): + result = self.getContractInstance(tokenAddr).functions.tokenURI(tokenId).call() return result # Enumerate NFTs assigned to an owner. @@ -81,10 +83,9 @@ def tokenURI(tokenAddr, tokenId): # IndexNO A counter less than `totalSupply()`. # The token identifier for the `index`th NFT assigned to `owner`. - def tokenByIndex(tokenAddr, index): + def tokenByIndex(self, tokenAddr, index): token = Web3.toChecksumAddress(tokenAddr) - contractInstance = w3.eth.contract(address=token, abi=xrc721abi) - result = contractInstance.functions.tokenByIndex(index).call() + result = self.getContractInstance(tokenAddr).functions.tokenByIndex(index).call() return result # Enumerate NFTs assigned to an owner. @@ -92,11 +93,10 @@ def tokenByIndex(tokenAddr, index): # required arguments. # owner address, token address, token index. - def tokenofOwnerByIndex(tokenAddr, ownerAddress, index): + def tokenofOwnerByIndex(self, tokenAddr, ownerAddress, index): token = Web3.toChecksumAddress(tokenAddr) - contractInstance = w3.eth.contract(address=token, abi=xrc721abi) owner = Web3.toChecksumAddress(ownerAddress) - result = contractInstance.functions.tokenOfOwnerByIndex( + result = self.getContractInstance(token).functions.tokenOfOwnerByIndex( owner, index).call() return result @@ -106,10 +106,9 @@ def tokenofOwnerByIndex(tokenAddr, ownerAddress, index): # required arguments. # token address, interface id. - def supportInterface(tokenAddr, interfaceId): + def supportInterface(self, tokenAddr, interfaceId): token = Web3.toChecksumAddress(tokenAddr) - contractInstance = w3.eth.contract(address=token, abi=xrc721abi) - result = contractInstance.functions.supportsInterface( + result = self.getContractInstance(token).functions.supportsInterface( interfaceId).call() return result @@ -117,20 +116,18 @@ def supportInterface(tokenAddr, interfaceId): # required arguments. # token address, tokenId. - def getApproved(tokenAddr, tokenId): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) - result = contractInstance.functions.getApproved(tokenId).call() + def getApproved(self, tokenAddr, tokenId): + result = self.getContractInstance(tokenAddr).functions.getApproved(tokenId).call() return result # Tells whether an operator is approved by a given owner. # required arguments. # owner address, spender address, token address. - def isApprovedForAll(tokenAddr, ownerAddress, spenderAddress): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + def isApprovedForAll(self, tokenAddr, ownerAddress, spenderAddress): owner = Web3.toChecksumAddress(ownerAddress) spender = Web3.toChecksumAddress(spenderAddress) - result = contractInstance.functions.isApprovedForAll( + result = self.getContractInstance(tokenAddr).functions.isApprovedForAll( owner, spender).call() return result @@ -140,13 +137,12 @@ def isApprovedForAll(tokenAddr, ownerAddress, spenderAddress): # required arguments. # tokenAddress, owner address, ownerPrivateKey, spenderAddress, tokenID. - def approve(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, tokenId): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + def approve(self, tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, tokenId): owner = Web3.toChecksumAddress(ownerAddress) spender = Web3.toChecksumAddress(spenderAddress) - approveData = contractInstance.functions.approve(spender, tokenId) + approveData = self.getContractInstance(tokenAddr).functions.approve(spender, tokenId) hexData = approveData._encode_transaction_data() @@ -156,9 +152,9 @@ def approve(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, tokenId): 'from': owner, }) - nonce = w3.eth.getTransactionCount(owner) + nonce = self.getConnection().eth.getTransactionCount(owner) - gasPrice = w3.eth.gas_price + gasPrice = self.getConnection().eth.gas_price tx = { 'nonce': nonce, @@ -168,23 +164,22 @@ def approve(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, tokenId): 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, ownerPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) # Enable or disable approval for a third party ("operator") to manage all of `Owner`'s assets # Emits the ApprovalForAll event. The contract MUST allow multiple operators per owner. # required arguments. # token address, owner address, ownerPrivateKey, sepnder address, tokenId. - def setApprovalForAll(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, boolValue): - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + def setApprovalForAll(self, tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, boolValue): owner = Web3.toChecksumAddress(ownerAddress) spender = Web3.toChecksumAddress(spenderAddress) - approveData = contractInstance.functions.setApprovalForAll( + approveData = self.getContractInstance(tokenAddr).functions.setApprovalForAll( spender, boolValue) hexData = approveData._encode_transaction_data() @@ -195,9 +190,9 @@ def setApprovalForAll(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, 'from': owner, }) - nonce = w3.eth.getTransactionCount(owner) + nonce = self.getConnection().eth.getTransactionCount(owner) - gasPrice = w3.eth.gas_price + gasPrice = self.getConnection().eth.gas_price tx = { 'nonce': nonce, @@ -207,25 +202,23 @@ def setApprovalForAll(tokenAddr, ownerAddress, ownerPrivateKey, spenderAddress, 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, ownerPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, ownerPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) # Transfer ownership of an NFT -- THE CALLER IS RESPONSIBLE # to confirm that `reciever Address` is capable of receiving NFTs or else they may be permanently lost. # required arguments. # token address, owner address, spender address, spenderPrivateKey, receiver address, tokenId. - def transferFrom(tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey, receiver, tokenId): - - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + def transferFrom(self, tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey, receiver, tokenId): owner = Web3.toChecksumAddress(ownerAddress) receiverAddres = Web3.toChecksumAddress(receiver) spender = Web3.toChecksumAddress(spenderAddress) - transferData = contractInstance.functions.transferFrom( + transferData = self.getContractInstance(tokenAddr).functions.transferFrom( owner, receiverAddres, tokenId) estimateGas = transferData.estimateGas({ @@ -236,8 +229,8 @@ def transferFrom(tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey, re data = hexstr_if_str(to_bytes, hexData) - nonce = w3.eth.getTransactionCount(spender) - gasPrice = w3.eth.gas_price + nonce = self.getConnection().eth.getTransactionCount(spender) + gasPrice = self.getConnection().eth.gas_price tx = { 'nonce': nonce, @@ -246,27 +239,25 @@ def transferFrom(tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey, re 'gas': estimateGas, 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, spenderPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, spenderPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) # Transfers the ownership of an NFT from one address to another address. # required arguments. # token address, owner address, spender address, spenderPrivateKey, receiver address, tokenId. - def safeTransferFrom(tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey, receiver, tokenId): - - contractInstance = w3.eth.contract(address=tokenAddr, abi=xrc721abi) + def safeTransferFrom(self, tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey, receiver, tokenId): owner = Web3.toChecksumAddress(ownerAddress) receiverAddres = Web3.toChecksumAddress(receiver) spender = Web3.toChecksumAddress(spenderAddress) - transferData = contractInstance.functions.safeTransferFrom( + transferData = self.getContractInstance(tokenAddr).functions.safeTransferFrom( owner, receiverAddres, tokenId) - estimateGas = transferData.estimateGas({ + estimateGas = transferData.estimateGas({ 'from': spender, }) @@ -274,8 +265,8 @@ def safeTransferFrom(tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey data = hexstr_if_str(to_bytes, hexData) - nonce = w3.eth.getTransactionCount(spender) - gasPrice = w3.eth.gas_price + nonce = self.getConnection().eth.getTransactionCount(spender) + gasPrice = self.getConnection().eth.gas_price tx = { 'nonce': nonce, @@ -284,7 +275,7 @@ def safeTransferFrom(tokenAddr, ownerAddress, spenderAddress, spenderPrivateKey 'gas': estimateGas, 'gasPrice': gasPrice, } - signedTx = w3.eth.account.signTransaction(tx, spenderPrivateKey) + signedTx = self.getConnection().eth.account.signTransaction(tx, spenderPrivateKey) - txHash = w3.eth.sendRawTransaction(signedTx.rawTransaction) - return w3.toHex(txHash) + txHash = self.getConnection().eth.sendRawTransaction(signedTx.rawTransaction) + return self.getConnection().toHex(txHash) diff --git a/common/xrc20abi.json b/common/xrc20abi.json deleted file mode 100644 index 668d697..0000000 --- a/common/xrc20abi.json +++ /dev/null @@ -1,222 +0,0 @@ -[ - { - "constant": true, - "inputs": [], - "name": "name", - "outputs": [ - { - "name": "", - "type": "string" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - }, - { - "constant": false, - "inputs": [ - { - "name": "_spender", - "type": "address" - }, - { - "name": "_value", - "type": "uint256" - } - ], - "name": "approve", - "outputs": [ - { - "name": "", - "type": "bool" - } - ], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" - }, - { - "constant": true, - "inputs": [], - "name": "totalSupply", - "outputs": [ - { - "name": "", - "type": "uint256" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - }, - { - "constant": false, - "inputs": [ - { - "name": "_from", - "type": "address" - }, - { - "name": "_to", - "type": "address" - }, - { - "name": "_value", - "type": "uint256" - } - ], - "name": "transferFrom", - "outputs": [ - { - "name": "", - "type": "bool" - } - ], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" - }, - { - "constant": true, - "inputs": [], - "name": "decimals", - "outputs": [ - { - "name": "", - "type": "uint8" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - }, - { - "constant": true, - "inputs": [ - { - "name": "_owner", - "type": "address" - } - ], - "name": "balanceOf", - "outputs": [ - { - "name": "balance", - "type": "uint256" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - }, - { - "constant": true, - "inputs": [], - "name": "symbol", - "outputs": [ - { - "name": "", - "type": "string" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - }, - { - "constant": false, - "inputs": [ - { - "name": "_to", - "type": "address" - }, - { - "name": "_value", - "type": "uint256" - } - ], - "name": "transfer", - "outputs": [ - { - "name": "", - "type": "bool" - } - ], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" - }, - { - "constant": true, - "inputs": [ - { - "name": "_owner", - "type": "address" - }, - { - "name": "_spender", - "type": "address" - } - ], - "name": "allowance", - "outputs": [ - { - "name": "", - "type": "uint256" - } - ], - "payable": false, - "stateMutability": "view", - "type": "function" - }, - { - "payable": true, - "stateMutability": "payable", - "type": "fallback" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "owner", - "type": "address" - }, - { - "indexed": true, - "name": "spender", - "type": "address" - }, - { - "indexed": false, - "name": "value", - "type": "uint256" - } - ], - "name": "Approval", - "type": "event" - }, - { - "anonymous": false, - "inputs": [ - { - "indexed": true, - "name": "from", - "type": "address" - }, - { - "indexed": true, - "name": "to", - "type": "address" - }, - { - "indexed": false, - "name": "value", - "type": "uint256" - } - ], - "name": "Transfer", - "type": "event" - } -] \ No newline at end of file diff --git a/common/xrc721abi.json b/common/xrc721abi.json deleted file mode 100644 index b0c0a3f..0000000 --- a/common/xrc721abi.json +++ /dev/null @@ -1,366 +0,0 @@ -[{ - "constant": true, - "inputs": [{ - "name": "interfaceId", - "type": "bytes4" - }], - "name": "supportsInterface", - "outputs": [{ - "name": "", - "type": "bool" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": true, - "inputs": [], - "name": "name", - "outputs": [{ - "name": "", - "type": "string" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": true, - "inputs": [{ - "name": "tokenId", - "type": "uint256" - }], - "name": "getApproved", - "outputs": [{ - "name": "", - "type": "address" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": false, - "inputs": [{ - "name": "to", - "type": "address" - }, - { - "name": "tokenId", - "type": "uint256" - } - ], - "name": "approve", - "outputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" -}, -{ - "constant": true, - "inputs": [], - "name": "totalSupply", - "outputs": [{ - "name": "", - "type": "uint256" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": false, - "inputs": [{ - "name": "from", - "type": "address" - }, - { - "name": "to", - "type": "address" - }, - { - "name": "tokenId", - "type": "uint256" - } - ], - "name": "transferFrom", - "outputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" -}, -{ - "constant": true, - "inputs": [{ - "name": "owner", - "type": "address" - }, - { - "name": "index", - "type": "uint256" - } - ], - "name": "tokenOfOwnerByIndex", - "outputs": [{ - "name": "", - "type": "uint256" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": false, - "inputs": [{ - "name": "from", - "type": "address" - }, - { - "name": "to", - "type": "address" - }, - { - "name": "tokenId", - "type": "uint256" - } - ], - "name": "safeTransferFrom", - "outputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" -}, -{ - "constant": true, - "inputs": [{ - "name": "index", - "type": "uint256" - }], - "name": "tokenByIndex", - "outputs": [{ - "name": "", - "type": "uint256" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": true, - "inputs": [{ - "name": "tokenId", - "type": "uint256" - }], - "name": "ownerOf", - "outputs": [{ - "name": "", - "type": "address" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": true, - "inputs": [{ - "name": "owner", - "type": "address" - }], - "name": "balanceOf", - "outputs": [{ - "name": "", - "type": "uint256" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": true, - "inputs": [], - "name": "symbol", - "outputs": [{ - "name": "", - "type": "string" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": false, - "inputs": [{ - "name": "to", - "type": "address" - }, - { - "name": "approved", - "type": "bool" - } - ], - "name": "setApprovalForAll", - "outputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" -}, -{ - "constant": false, - "inputs": [{ - "name": "from", - "type": "address" - }, - { - "name": "to", - "type": "address" - }, - { - "name": "tokenId", - "type": "uint256" - }, - { - "name": "_data", - "type": "bytes" - } - ], - "name": "safeTransferFrom1", - "outputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" -}, -{ - "constant": true, - "inputs": [{ - "name": "tokenId", - "type": "uint256" - }], - "name": "tokenURI", - "outputs": [{ - "name": "", - "type": "string" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "constant": false, - "inputs": [{ - "name": "_to", - "type": "address" - }, - { - "name": "_tokenId", - "type": "uint256" - }, - { - "name": "_uri", - "type": "string" - } - ], - "name": "mint", - "outputs": [], - "payable": false, - "stateMutability": "nonpayable", - "type": "function" -}, -{ - "constant": true, - "inputs": [{ - "name": "owner", - "type": "address" - }, - { - "name": "operator", - "type": "address" - } - ], - "name": "isApprovedForAll", - "outputs": [{ - "name": "", - "type": "bool" - }], - "payable": false, - "stateMutability": "view", - "type": "function" -}, -{ - "inputs": [{ - "name": "name", - "type": "string" - }, - { - "name": "symbol", - "type": "string" - } - ], - "payable": false, - "stateMutability": "nonpayable", - "type": "constructor" -}, -{ - "anonymous": false, - "inputs": [{ - "indexed": true, - "name": "from", - "type": "address" - }, - { - "indexed": true, - "name": "to", - "type": "address" - }, - { - "indexed": true, - "name": "tokenId", - "type": "uint256" - } - ], - "name": "Transfer", - "type": "event" -}, -{ - "anonymous": false, - "inputs": [{ - "indexed": true, - "name": "owner", - "type": "address" - }, - { - "indexed": true, - "name": "approved", - "type": "address" - }, - { - "indexed": true, - "name": "tokenId", - "type": "uint256" - } - ], - "name": "Approval", - "type": "event" -}, -{ - "anonymous": false, - "inputs": [{ - "indexed": true, - "name": "owner", - "type": "address" - }, - { - "indexed": true, - "name": "operator", - "type": "address" - }, - { - "indexed": false, - "name": "approved", - "type": "bool" - } - ], - "name": "ApprovalForAll", - "type": "event" -} -] \ No newline at end of file diff --git a/setup.py b/setup.py index 69516d0..28e3abe 100644 --- a/setup.py +++ b/setup.py @@ -4,8 +4,8 @@ setup( name = 'XDC3PYTHON', - version = '1.0.0', - description = 'XDC RUST SDK with support for smart contracts, XRC20 & XRC721', + version = '1.0.1', + description = 'XDC PYTHON SDK with support for smart contracts, XRC20 & XRC721', long_description=open('README.md').read(), url='https://github.com/XDCFoundation/XDC_Python_SDK_V1.git', author='XDC Foundation', From 4584574c70bd0c856de12971b44cf7c5c4909888 Mon Sep 17 00:00:00 2001 From: lhtsaurabhsingh Date: Wed, 11 May 2022 17:40:42 +0530 Subject: [PATCH 3/5] python xdc sdk xrc20 and xrc721 methods --- XDC3PYTHON/xrc20.py | 1 - 1 file changed, 1 deletion(-) diff --git a/XDC3PYTHON/xrc20.py b/XDC3PYTHON/xrc20.py index af78d2f..03fe905 100644 --- a/XDC3PYTHON/xrc20.py +++ b/XDC3PYTHON/xrc20.py @@ -1,4 +1,3 @@ -from lib2to3.pgen2 import token from web3 import Web3 from web3._utils.encoding import ( hexstr_if_str, From e063a177a63012b11487a8c6bfaf6d71fbf5f50d Mon Sep 17 00:00:00 2001 From: lhtsaurabhsingh Date: Thu, 12 May 2022 18:25:17 +0530 Subject: [PATCH 4/5] Readme updated --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 4b64951..ebd8600 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,11 @@ This example returns symbol of the specified address. **HTTP transport** +## Required Data Types + **Token Address** - `str type` + **Account Address** - `str type` + **amount** & **tokenId** - `int type` + ### Author [XDCFoundation](https://github.com/XDCFoundation/XDC_Python_SDK_V1) From 66fa7e5dea1afa40be77f29200876e9ab0088403 Mon Sep 17 00:00:00 2001 From: Jon McBee <86977117+WalterBlueu@users.noreply.github.com> Date: Wed, 7 Sep 2022 14:14:45 -0400 Subject: [PATCH 5/5] Rename LICENCE.txt to LICENSE.txt --- LICENCE.txt => LICENSE.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename LICENCE.txt => LICENSE.txt (98%) diff --git a/LICENCE.txt b/LICENSE.txt similarity index 98% rename from LICENCE.txt rename to LICENSE.txt index 1ef92da..7fc6409 100644 --- a/LICENCE.txt +++ b/LICENSE.txt @@ -4,4 +4,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.