2021-01-07 20:34:24 +00:00
|
|
|
import
|
|
|
|
chronos, nimcrypto, options, json, stint,
|
|
|
|
test_utils,
|
|
|
|
web3
|
2021-02-04 19:30:00 +00:00
|
|
|
|
|
|
|
# the address of Ethereum client (ganache-cli for now)
|
|
|
|
const ethClient = "ws://localhost:8540/"
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
const membershipContractCode = "0x6080604052600060085560006009553480156200001b57600080fd5b506200002662002457565b6040518060600160405280600081526020017f2ff267fd23782a5625e6d804f0a7fa700b8dc6084e2e7a5aff7cd4b1c506d30b81526020017f07191352b37e1257205f09eaba526667cf18cb12e2cfeb2abc38e64bc0f0112e81525090506000600190506000600390506002600311620000ec576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382526029815260200180620052356029913960400191505060405180910390fd5b82600160038110620000fa57fe5b60200201516200010f6200040560201b60201c565b1462000167576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252602c8152602001806200525e602c913960400191505060405180910390fd5b808210620001c1576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260248152602001806200528a6024913960400191505060405180910390fd5b80600081905550816001819055508067ffffffffffffffff81118015620001e757600080fd5b50604051908082528060200260200182016040528015620002175781602001602082028036833780820191505090505b50600290805190602001906200022f92919062002479565b508067ffffffffffffffff811180156200024857600080fd5b50604051908082528060200260200182016040528015620002785781602001602082028036833780820191505090505b50600390805190602001906200029092919062002479565b5081810367ffffffffffffffff81118015620002ab57600080fd5b50604051908082528060200260200182016040528015620002db5781602001602082028036833780820191505090505b5060049080519060200190620002f392919062002479565b5082600290600362000307929190620024cb565b508260039060036200031b929190620024cb565b5060005b828203811015620003685783838201600381106200033957fe5b6020020151600482815481106200034c57fe5b906000526020600020018190555080806001019150506200031f565b50620003b660405180604001604052808560018503600381106200038857fe5b60200201518152602001856001850360038110620003a257fe5b60200201518152506200042d60201b60201c565b600681905550600654600581905550620003ee604051806040016040528060055481526020016006548152506200042d60201b60201c565b6007819055505050506001600b819055506200253c565b60007f2ff267fd23782a5625e6d804f0a7fa700b8dc6084e2e7a5aff7cd4b1c506d30b905090565b60007f30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f00000016040517f165d45ae851912f9a33800b04cc6617b184bf67db11ce904dc82601244551ae281527f10fc284d0af588165f4fc650fe7c53b1d80fbaac16d30518bf142117f42f820460208201527f06b687bd3c688aa9a03545d0835bca75ae82c434bf7d5fb065a2818b5c74814f60408201527f01057eb8e4bba26f12f4ea819251708d72e0605e6de827e990c3ba4ae13f5ecd60608201527e23779a38eb9ef4a9beaf4dc0a2ab5233a28ce6d10ad2512230a275b83017c360808201527f012e5dfdd4f34081753b70c897773f5d2987c8bbae32ad202a27cd61d7fba2fb7f0d1807f022a8d80d9304a1522087b8692dc0acf7b43fea28782d2ae672c0b11f7f17d468d0e6541de501481931453ed1e4a250e5e301f27dc91fe3b4bd522aa53c7f1ea09a4bd33f14eafd75e775d85e568fa668938fdd2f16fad1d4d2d2b9862b007f061f2e832c23bee84c2f09d63d371cc5eda2f749cdbe9a6a6d20469e9fa36e8b8851017f061f2e832c23bee84c2f09d63d371cc5eda2f749cdbe9a6a6d20469e9fa36e8b60208a0151017f061f2e832c23bee84c2f09d63d371cc5eda2f749cdbe9a6a6d20469e9fa36e8b8883840989848b83840909935089838409905089838b83840909925089828309905089828b8384090991507f0e4d154ca9b7f5111958289f43ed5bbc4d4f6118d45d9aefeb778179d921a59b9050808a60408b015184098b60208c015186098c8c518809010101818b8a85098c60808d015187098d60608e01518909010101828c8886098d8a88098e8c8a0901010193508b82830992508b828d8586090991508b81820992508b818d8586090990508b84850992508b848d8586090993507f298d683000ab71c72fe4371cf6cd37bb584b6d816a653ee4bfea62518a337e079250828c60408d015186098d60208e015184098e8e5186090101019550828c8b86098d60808e015184098e60608f015186090101019450828c8886098d8a84098e8c860901010193508b86870992508b868d8586090995508b85860992508b858d8586090994508b84850992508b848d8586090993507f2f860295bc93d694e74905913ddcae47290b9b5abb43a537fe40d9305bd1e1679250828c60408d015186098d60208e015188098e8e518a090101019150828c8b86098d60808e015188098e60608f01518a090101019050828c8886098d8a88098e8c8a0901010193508b82830992508b828d8586090991508b81820992508b818d8586090990508b84850992508b848d8586090993507f1dd8b95
|
|
|
|
|
2021-02-04 19:30:00 +00:00
|
|
|
# the contract interface
|
2021-01-07 20:34:24 +00:00
|
|
|
contract(MembershipContract):
|
|
|
|
proc registerSingle(newMember: Uint256): Uint256 # payable call
|
|
|
|
proc isValidMembershipRoot(membershipRoot: Uint256): Bool
|
|
|
|
|
|
|
|
proc membershipTest() {.async.} =
|
2021-02-04 19:30:00 +00:00
|
|
|
# connect to the eth client
|
|
|
|
let web3 = await newWeb3(ethClient)
|
|
|
|
echo "web3 connected"
|
|
|
|
|
|
|
|
# fetch the list of registered accounts
|
2021-01-07 20:34:24 +00:00
|
|
|
let accounts = await web3.provider.eth_accounts()
|
|
|
|
web3.defaultAccount = accounts[1]
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "Account address ", web3.defaultAccount
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
var balance = await web3.provider.eth_getBalance(web3.defaultAccount , "latest")
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "Initial account balance: ", balance
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
let receipt = await web3.deployContract(membershipContractCode)
|
|
|
|
var contractAddress = receipt.contractAddress.get
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "Address of the deployed contract: ", contractAddress
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
balance = await web3.provider.eth_getBalance(web3.defaultAccount , "latest")
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "Account balance after the contract deployment: ", balance
|
2021-01-07 20:34:24 +00:00
|
|
|
|
2021-02-04 19:30:00 +00:00
|
|
|
# creates a Sender object with a web3 field and contract address of type Address
|
|
|
|
var sender = web3.contractSender(MembershipContract, contractAddress)
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
# send takes three parameters, c: ContractCallBase, value = 0.u256, gas = 3000000'u64 gasPrice = 0
|
|
|
|
# should use send proc for the contract functions that update the state of the contract
|
|
|
|
echo "this is the registration result ", await sender.registerSingle(20.u256).send(value = 1.u256) # value is the membership fee
|
|
|
|
|
|
|
|
await web3.close()
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "web3 closed"
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
|
2021-02-04 19:30:00 +00:00
|
|
|
# This is only a test contract for the experimentation, will be removed on the final merge
|
2021-01-07 20:34:24 +00:00
|
|
|
#[
|
|
|
|
pragma solidity >=0.4.22 <0.8.0;
|
|
|
|
|
|
|
|
contract NumberContract {
|
|
|
|
uint256 public number;
|
|
|
|
|
|
|
|
function setNumber(uint256 newNumber) public {
|
|
|
|
number = newNumber;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNumber() public view returns (uint256) {
|
|
|
|
return number;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]#
|
|
|
|
|
2021-02-04 19:30:00 +00:00
|
|
|
# the contract interface
|
2021-01-07 20:34:24 +00:00
|
|
|
contract(NumberContract):
|
|
|
|
proc setNumber(number: Uint256)
|
|
|
|
proc getNumber(): Uint256 {.view.}
|
|
|
|
|
|
|
|
const getNumberContractCode = "608060405234801561001057600080fd5b5060e18061001f6000396000f30060806040526004361060525763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416633fb5c1cb811460575780638381f58a14606e578063f2c9ecd8146092575b600080fd5b348015606257600080fd5b50606c60043560a4565b005b348015607957600080fd5b50608060a9565b60408051918252519081900360200190f35b348015609d57600080fd5b50608060af565b600055565b60005481565b600054905600a165627a7a72305820fe643610ce4d15c255ba5670a02aa3e86919cc099c10e31e2c8633926e31970e0029"
|
|
|
|
|
|
|
|
proc getNumberTest() {.async.} =
|
2021-02-04 19:30:00 +00:00
|
|
|
let web3 = await newWeb3(ethClient)
|
|
|
|
echo "web3 connected"
|
2021-01-07 20:34:24 +00:00
|
|
|
let accounts = await web3.provider.eth_accounts()
|
|
|
|
web3.defaultAccount = accounts[0]
|
|
|
|
|
|
|
|
var balance = await web3.provider.eth_getBalance(web3.defaultAccount , "latest")
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "The account's initial balance: ", balance
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
let receipt = await web3.deployContract(getNumberContractCode)
|
|
|
|
var contractAddress = receipt.contractAddress.get
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "The address of the deployed contract: ", contractAddress
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
balance = await web3.provider.eth_getBalance(web3.defaultAccount , "latest")
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "Account balance after the contract deployment: ", balance
|
2021-01-07 20:34:24 +00:00
|
|
|
|
2021-02-04 19:30:00 +00:00
|
|
|
var ns = web3.contractSender(NumberContract, contractAddress)
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
discard await ns.setNumber(50.u256).send()
|
|
|
|
balance = await web3.provider.eth_getBalance(web3.defaultAccount , "latest")
|
|
|
|
echo "balance after sent: ", balance
|
|
|
|
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "this is the number fetched from the contract: ", await ns.getNumber().call()
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
await web3.close()
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "closed"
|
2021-01-07 20:34:24 +00:00
|
|
|
|
|
|
|
waitFor membershipTest()
|
2021-02-04 19:30:00 +00:00
|
|
|
echo "rln-relay test finished"
|
|
|
|
# this is only for the experimentation
|
|
|
|
#waitFor getNumberTest()
|