ContractTest.php 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634
  1. <?php
  2. namespace Test\Unit;
  3. use Test\TestCase;
  4. use Web3\Providers\HttpProvider;
  5. use Web3\Contract;
  6. use Web3\Utils;
  7. use Web3\Contracts\Ethabi;
  8. use Web3\Formatters\IntegerFormatter;
  9. use phpseclib\Math\BigInteger as BigNumber;
  10. class ContractTest extends TestCase
  11. {
  12. /**
  13. * contract
  14. *
  15. * @var \Web3\Contract
  16. */
  17. protected $contract;
  18. /**
  19. * async contract
  20. *
  21. * @var \Web3\Contract
  22. */
  23. protected $asyncContract;
  24. /**
  25. * testAbi
  26. * GameToken abi from https://github.com/sc0Vu/GameToken
  27. *
  28. * @var string
  29. */
  30. protected $testAbi = '[
  31. {
  32. "constant": true,
  33. "inputs": [],
  34. "name": "name",
  35. "outputs": [
  36. {
  37. "name": "",
  38. "type": "string"
  39. }
  40. ],
  41. "payable": false,
  42. "stateMutability": "view",
  43. "type": "function"
  44. },
  45. {
  46. "constant": false,
  47. "inputs": [
  48. {
  49. "name": "_spender",
  50. "type": "address"
  51. },
  52. {
  53. "name": "_value",
  54. "type": "uint256"
  55. }
  56. ],
  57. "name": "approve",
  58. "outputs": [
  59. {
  60. "name": "success",
  61. "type": "bool"
  62. }
  63. ],
  64. "payable": false,
  65. "stateMutability": "nonpayable",
  66. "type": "function"
  67. },
  68. {
  69. "constant": true,
  70. "inputs": [],
  71. "name": "totalSupply",
  72. "outputs": [
  73. {
  74. "name": "",
  75. "type": "uint256"
  76. }
  77. ],
  78. "payable": false,
  79. "stateMutability": "view",
  80. "type": "function"
  81. },
  82. {
  83. "constant": false,
  84. "inputs": [
  85. {
  86. "name": "_from",
  87. "type": "address"
  88. },
  89. {
  90. "name": "_to",
  91. "type": "address"
  92. },
  93. {
  94. "name": "_value",
  95. "type": "uint256"
  96. }
  97. ],
  98. "name": "transferFrom",
  99. "outputs": [
  100. {
  101. "name": "success",
  102. "type": "bool"
  103. }
  104. ],
  105. "payable": false,
  106. "stateMutability": "nonpayable",
  107. "type": "function"
  108. },
  109. {
  110. "constant": true,
  111. "inputs": [],
  112. "name": "decimals",
  113. "outputs": [
  114. {
  115. "name": "",
  116. "type": "uint8"
  117. }
  118. ],
  119. "payable": false,
  120. "stateMutability": "view",
  121. "type": "function"
  122. },
  123. {
  124. "constant": true,
  125. "inputs": [],
  126. "name": "standard",
  127. "outputs": [
  128. {
  129. "name": "",
  130. "type": "string"
  131. }
  132. ],
  133. "payable": false,
  134. "stateMutability": "view",
  135. "type": "function"
  136. },
  137. {
  138. "constant": true,
  139. "inputs": [
  140. {
  141. "name": "",
  142. "type": "address"
  143. }
  144. ],
  145. "name": "balanceOf",
  146. "outputs": [
  147. {
  148. "name": "",
  149. "type": "uint256"
  150. }
  151. ],
  152. "payable": false,
  153. "stateMutability": "view",
  154. "type": "function"
  155. },
  156. {
  157. "constant": true,
  158. "inputs": [],
  159. "name": "symbol",
  160. "outputs": [
  161. {
  162. "name": "",
  163. "type": "string"
  164. }
  165. ],
  166. "payable": false,
  167. "stateMutability": "view",
  168. "type": "function"
  169. },
  170. {
  171. "constant": false,
  172. "inputs": [
  173. {
  174. "name": "_to",
  175. "type": "address"
  176. },
  177. {
  178. "name": "_value",
  179. "type": "uint256"
  180. }
  181. ],
  182. "name": "transfer",
  183. "outputs": [],
  184. "payable": false,
  185. "stateMutability": "nonpayable",
  186. "type": "function"
  187. },
  188. {
  189. "constant": true,
  190. "inputs": [
  191. {
  192. "name": "",
  193. "type": "address"
  194. },
  195. {
  196. "name": "",
  197. "type": "address"
  198. }
  199. ],
  200. "name": "allowance",
  201. "outputs": [
  202. {
  203. "name": "",
  204. "type": "uint256"
  205. }
  206. ],
  207. "payable": false,
  208. "stateMutability": "view",
  209. "type": "function"
  210. },
  211. {
  212. "inputs": [
  213. {
  214. "name": "initialSupply",
  215. "type": "uint256"
  216. },
  217. {
  218. "name": "tokenName",
  219. "type": "string"
  220. },
  221. {
  222. "name": "decimalUnits",
  223. "type": "uint8"
  224. },
  225. {
  226. "name": "tokenSymbol",
  227. "type": "string"
  228. }
  229. ],
  230. "payable": false,
  231. "stateMutability": "nonpayable",
  232. "type": "constructor"
  233. },
  234. {
  235. "anonymous": false,
  236. "inputs": [
  237. {
  238. "indexed": true,
  239. "name": "from",
  240. "type": "address"
  241. },
  242. {
  243. "indexed": true,
  244. "name": "to",
  245. "type": "address"
  246. },
  247. {
  248. "indexed": false,
  249. "name": "value",
  250. "type": "uint256"
  251. }
  252. ],
  253. "name": "Transfer",
  254. "type": "event"
  255. },
  256. {
  257. "anonymous": false,
  258. "inputs": [
  259. {
  260. "indexed": true,
  261. "name": "_owner",
  262. "type": "address"
  263. },
  264. {
  265. "indexed": true,
  266. "name": "_spender",
  267. "type": "address"
  268. },
  269. {
  270. "indexed": false,
  271. "name": "_value",
  272. "type": "uint256"
  273. }
  274. ],
  275. "name": "Approval",
  276. "type": "event"
  277. }
  278. ]';
  279. /**
  280. * testBytecode
  281. * GameToken abi from https://github.com/sc0Vu/GameToken
  282. *
  283. * @var string
  284. */
  285. protected $testBytecode = '0x60606040526040805190810160405280600581526020017f45524332300000000000000000000000000000000000000000000000000000008152506000908051906020019061004f92919061012f565b50341561005b57600080fd5b604051610ec5380380610ec58339810160405280805190602001909190805182019190602001805190602001909190805182019190505083600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508360048190555082600190805190602001906100f392919061012f565b50806002908051906020019061010a92919061012f565b5081600360006101000a81548160ff021916908360ff160217905550505050506101d4565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061017057805160ff191683800117855561019e565b8280016001018555821561019e579182015b8281111561019d578251825591602001919060010190610182565b5b5090506101ab91906101af565b5090565b6101d191905b808211156101cd5760008160009055506001016101b5565b5090565b90565b610ce2806101e36000396000f3006060604052600436106100a4576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100a9578063095ea7b31461013757806318160ddd1461019157806323b872dd146101ba578063313ce567146102335780635a3b7e421461026257806370a08231146102f057806395d89b411461033d578063a9059cbb146103cb578063dd62ed3e1461040d575b600080fd5b34156100b457600080fd5b6100bc610479565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100fc5780820151818401526020810190506100e1565b50505050905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561014257600080fd5b610177600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610517565b604051808215151515815260200191505060405180910390f35b341561019c57600080fd5b6101a4610609565b6040518082815260200191505060405180910390f35b34156101c557600080fd5b610219600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061060f565b604051808215151515815260200191505060405180910390f35b341561023e57600080fd5b61024661092a565b604051808260ff1660ff16815260200191505060405180910390f35b341561026d57600080fd5b61027561093d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156102b557808201518184015260208101905061029a565b50505050905090810190601f1680156102e25780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102fb57600080fd5b610327600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506109db565b6040518082815260200191505060405180910390f35b341561034857600080fd5b6103506109f3565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610390578082015181840152602081019050610375565b50505050905090810190601f1680156103bd5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156103d657600080fd5b61040b600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610a91565b005b341561041857600080fd5b610463600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610c91565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561050f5780601f106104e45761010080835404028352916020019161050f565b820191906000526020600020905b8154815290600101906020018083116104f257829003601f168201915b505050505081565b600081600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60045481565b6000808373ffffffffffffffffffffffffffffffffffffffff16141561063457600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561068057600080fd5b600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561070d57600080fd5b600660008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561079657600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600660008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b600360009054906101000a900460ff1681565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156109d35780601f106109a8576101008083540402835291602001916109d3565b820191906000526020600020905b8154815290600101906020018083116109b657829003601f168201915b505050505081565b60056020528060005260406000206000915090505481565b60028054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610a895780601f10610a5e57610100808354040283529160200191610a89565b820191906000526020600020905b815481529060010190602001808311610a6c57829003601f168201915b505050505081565b60008273ffffffffffffffffffffffffffffffffffffffff161415610ab557600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610b0157600080fd5b600560008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011015610b8e57600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b60066020528160005260406000206020528060005260406000206000915091505054815600a165627a7a723058203eb700b31f6d7723be3f4a0dd07fc4ba166a17279e26a437227679b92bacb5a20029';
  286. /**
  287. * testUserAbi
  288. * User abi from https://github.com/BlockHR/contracts
  289. *
  290. * @var string
  291. */
  292. protected $testUserAbi = '[
  293. {
  294. "constant": false,
  295. "inputs": [
  296. {
  297. "name": "ethAddress",
  298. "type": "address"
  299. }
  300. ],
  301. "name": "getUser",
  302. "outputs": [
  303. {
  304. "name": "firstName",
  305. "type": "string"
  306. },
  307. {
  308. "name": "lastName",
  309. "type": "string"
  310. },
  311. {
  312. "name": "age",
  313. "type": "uint256"
  314. }
  315. ],
  316. "payable": false,
  317. "stateMutability": "nonpayable",
  318. "type": "function"
  319. },
  320. {
  321. "constant": false,
  322. "inputs": [
  323. {
  324. "name": "ethAddress",
  325. "type": "address"
  326. },
  327. {
  328. "name": "firstName",
  329. "type": "string"
  330. },
  331. {
  332. "name": "lastName",
  333. "type": "string"
  334. },
  335. {
  336. "name": "age",
  337. "type": "uint256"
  338. }
  339. ],
  340. "name": "addUser",
  341. "outputs": [],
  342. "payable": false,
  343. "stateMutability": "nonpayable",
  344. "type": "function"
  345. },
  346. {
  347. "inputs": [],
  348. "payable": false,
  349. "stateMutability": "nonpayable",
  350. "type": "constructor"
  351. },
  352. {
  353. "payable": false,
  354. "stateMutability": "nonpayable",
  355. "type": "fallback"
  356. },
  357. {
  358. "anonymous": false,
  359. "inputs": [
  360. {
  361. "indexed": true,
  362. "name": "ethAddress",
  363. "type": "address"
  364. },
  365. {
  366. "indexed": false,
  367. "name": "firstName",
  368. "type": "string"
  369. },
  370. {
  371. "indexed": false,
  372. "name": "lastName",
  373. "type": "string"
  374. },
  375. {
  376. "indexed": false,
  377. "name": "age",
  378. "type": "uint256"
  379. }
  380. ],
  381. "name": "AddUser",
  382. "type": "event"
  383. }
  384. ]';
  385. /**
  386. * testUserBytecode
  387. * User bytecode from https://github.com/BlockHR/contracts
  388. *
  389. * @var string
  390. */
  391. protected $testUserBytecode = '0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506108ee8061005e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636f77926b1461005c578063b3c2583514610181575b341561005757600080fd5b600080fd5b341561006757600080fd5b610093600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610249565b604051808060200180602001848152602001838103835286818151815260200191508051906020019080838360005b838110156100dd5780820151818401526020810190506100c2565b50505050905090810190601f16801561010a5780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015610143578082015181840152602081019050610128565b50505050905090810190601f1680156101705780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b341561018c57600080fd5b610247600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091908035906020019091905050610545565b005b6102516107c7565b6102596107c7565b60003373ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff16141580156102e457506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614155b156102ee5761053e565b6000600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020154141561033e5761053e565b600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104145780601f106103e957610100808354040283529160200191610414565b820191906000526020600020905b8154815290600101906020018083116103f757829003601f168201915b50505050509250600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206001018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104f15780601f106104c6576101008083540402835291602001916104f1565b820191906000526020600020905b8154815290600101906020018083116104d457829003601f168201915b50505050509150600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206002015490505b9193909250565b61054d6107db565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156107c0576000600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600201541415156105f3576107bf565b8381600001819052508281602001819052508181604001818152505080600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082015181600001908051906020019061066b929190610809565b506020820151816001019080519060200190610688929190610809565b50604082015181600201559050508473ffffffffffffffffffffffffffffffffffffffff167f2771be550edc032daf255a8987e0164bcfad0a5b97238d8961f9c5e38fa3e4f3858585604051808060200180602001848152602001838103835286818151815260200191508051906020019080838360005b8381101561071b578082015181840152602081019050610700565b50505050905090810190601f1680156107485780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015610781578082015181840152602081019050610766565b50505050905090810190601f1680156107ae5780820380516001836020036101000a031916815260200191505b509550505050505060405180910390a25b5b5050505050565b602060405190810160405280600081525090565b6060604051908101604052806107ef610889565b81526020016107fc610889565b8152602001600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061084a57805160ff1916838001178555610878565b82800160010185558215610878579182015b8281111561087757825182559160200191906001019061085c565b5b509050610885919061089d565b5090565b602060405190810160405280600081525090565b6108bf91905b808211156108bb5760008160009055506001016108a3565b5090565b905600a165627a7a72305820f4dd7dc4c22792dec463ec68973ad2ed12d5994e96a9e7b34db512d6e38143090029';
  392. /**
  393. * accounts
  394. *
  395. * @var array
  396. */
  397. protected $accounts;
  398. /**
  399. * contractAddress
  400. *
  401. * @var string
  402. */
  403. protected $contractAddress;
  404. /**
  405. * setUp
  406. *
  407. * @return void
  408. */
  409. public function setUp(): void
  410. {
  411. parent::setUp();
  412. $this->contract = new Contract($this->web3->provider, $this->testAbi);
  413. $this->contract->eth->accounts(function ($err, $accounts) {
  414. if ($err === null) {
  415. if (isset($accounts)) {
  416. $this->accounts = $accounts;
  417. return;
  418. }
  419. }
  420. });
  421. $this->asyncContract = new Contract($this->asyncHttpProvider, $this->testAbi);
  422. }
  423. /**
  424. * testInstance
  425. *
  426. * @return void
  427. */
  428. public function testInstance()
  429. {
  430. $contract = new Contract($this->testHost, $this->testAbi);
  431. $this->assertTrue($contract->provider instanceof HttpProvider);
  432. }
  433. /**
  434. * testSetProvider
  435. *
  436. * @return void
  437. */
  438. public function testSetProvider()
  439. {
  440. $contract = $this->contract;
  441. $contract->provider = new HttpProvider('http://localhost:8545');
  442. $this->assertEquals($contract->provider->host, 'http://localhost:8545');
  443. $contract->provider = null;
  444. $this->assertEquals($contract->provider->host, 'http://localhost:8545');
  445. }
  446. /**
  447. * testDeploy
  448. *
  449. * @return void
  450. */
  451. public function testDeploy()
  452. {
  453. $contract = $this->contract;
  454. if (!isset($this->accounts[0])) {
  455. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  456. } else {
  457. $account = $this->accounts[0];
  458. }
  459. $contract->bytecode($this->testBytecode)->new(1000000, 'Game Token', 1, 'GT', [
  460. 'from' => $account,
  461. 'gas' => '0x200b20'
  462. ], function ($err, $result) use ($contract) {
  463. if ($err !== null) {
  464. return $this->fail($err->getMessage());
  465. }
  466. if ($result) {
  467. echo "\nTransaction has made:) id: " . $result . "\n";
  468. }
  469. $transactionId = $result;
  470. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  471. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  472. if ($err !== null) {
  473. return $this->fail($err);
  474. }
  475. if ($transaction) {
  476. $this->contractAddress = $transaction->contractAddress;
  477. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  478. }
  479. });
  480. });
  481. }
  482. /**
  483. * testSend
  484. *
  485. * @return void
  486. */
  487. public function testSend()
  488. {
  489. $contract = $this->contract;
  490. if (!isset($this->accounts[0])) {
  491. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  492. } else {
  493. $fromAccount = $this->accounts[0];
  494. }
  495. if (!isset($this->accounts[1])) {
  496. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  497. } else {
  498. $toAccount = $this->accounts[1];
  499. }
  500. $contract->bytecode($this->testBytecode)->new(1000000, 'Game Token', 1, 'GT', [
  501. 'from' => $fromAccount,
  502. 'gas' => '0x200b20'
  503. ], function ($err, $result) use ($contract) {
  504. if ($err !== null) {
  505. return $this->fail($err->getMessage());
  506. }
  507. if ($result) {
  508. echo "\nTransaction has made:) id: " . $result . "\n";
  509. }
  510. $transactionId = $result;
  511. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  512. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  513. if ($err !== null) {
  514. return $this->fail($err);
  515. }
  516. if ($transaction) {
  517. $this->contractAddress = $transaction->contractAddress;
  518. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  519. }
  520. });
  521. });
  522. if (!isset($this->contractAddress)) {
  523. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  524. }
  525. $contract->at($this->contractAddress)->send('transfer', $toAccount, 16, [
  526. 'from' => $fromAccount,
  527. 'gas' => '0x200b20'
  528. ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
  529. if ($err !== null) {
  530. return $this->fail($err->getMessage());
  531. }
  532. if ($result) {
  533. echo "\nTransaction has made:) id: " . $result . "\n";
  534. }
  535. $transactionId = $result;
  536. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  537. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount, $contract) {
  538. if ($err !== null) {
  539. return $this->fail($err);
  540. }
  541. if ($transaction) {
  542. $topics = $transaction->logs[0]->topics;
  543. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  544. // validate topics
  545. $this->assertEquals($contract->ethabi->encodeEventSignature($this->contract->events['Transfer']), $topics[0]);
  546. $this->assertEquals('0x' . IntegerFormatter::format($fromAccount), $topics[1]);
  547. $this->assertEquals('0x' . IntegerFormatter::format($toAccount), $topics[2]);
  548. }
  549. });
  550. });
  551. }
  552. /**
  553. * testCall
  554. *
  555. * @return void
  556. */
  557. public function testCall()
  558. {
  559. $contract = $this->contract;
  560. if (!isset($this->accounts[0])) {
  561. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  562. } else {
  563. $fromAccount = $this->accounts[0];
  564. }
  565. if (!isset($this->accounts[1])) {
  566. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  567. } else {
  568. $toAccount = $this->accounts[1];
  569. }
  570. $contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
  571. 'from' => $fromAccount,
  572. 'gas' => '0x200b20'
  573. ], function ($err, $result) use ($contract) {
  574. if ($err !== null) {
  575. return $this->fail($err->getMessage());
  576. }
  577. if ($result) {
  578. echo "\nTransaction has made:) id: " . $result . "\n";
  579. }
  580. $transactionId = $result;
  581. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  582. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  583. if ($err !== null) {
  584. return $this->fail($err);
  585. }
  586. if ($transaction) {
  587. $this->contractAddress = $transaction->contractAddress;
  588. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  589. }
  590. });
  591. });
  592. if (!isset($this->contractAddress)) {
  593. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  594. }
  595. $contract->at($this->contractAddress)->call('balanceOf', $fromAccount, [
  596. 'from' => $fromAccount
  597. ], function ($err, $result) use ($contract) {
  598. if ($err !== null) {
  599. return $this->fail($err->getMessage());
  600. }
  601. if (isset($result)) {
  602. // $bn = Utils::toBn($result);
  603. // $this->assertEquals($bn->toString(), '10000', 'Balance should be 10000.');
  604. $this->assertTrue($result !== null);
  605. }
  606. });
  607. // test issue 143
  608. $contract->at($this->contractAddress)->call('balanceOf', $fromAccount, function ($err, $result) use ($contract) {
  609. if ($err !== null) {
  610. return $this->fail($err->getMessage());
  611. }
  612. if (isset($result)) {
  613. // $bn = Utils::toBn($result);
  614. // $this->assertEquals($bn->toString(), '10000', 'Balance should be 10000.');
  615. $this->assertTrue($result !== null);
  616. }
  617. });
  618. }
  619. /**
  620. * testEstimateGas
  621. *
  622. * @return void
  623. */
  624. public function testEstimateGas()
  625. {
  626. $contract = $this->contract;
  627. if (!isset($this->accounts[0])) {
  628. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  629. } else {
  630. $fromAccount = $this->accounts[0];
  631. }
  632. if (!isset($this->accounts[1])) {
  633. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  634. } else {
  635. $toAccount = $this->accounts[1];
  636. }
  637. $contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
  638. 'from' => $fromAccount,
  639. 'gas' => '0x200b20'
  640. ], function ($err, $result) use ($contract) {
  641. if ($err !== null) {
  642. return $this->fail($err->getMessage());
  643. }
  644. if ($result) {
  645. echo "\nTransaction has made:) id: " . $result . "\n";
  646. }
  647. $transactionId = $result;
  648. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  649. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  650. if ($err !== null) {
  651. return $this->fail($err);
  652. }
  653. if ($transaction) {
  654. $this->contractAddress = $transaction->contractAddress;
  655. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  656. }
  657. });
  658. });
  659. $contract->bytecode($this->testBytecode)->estimateGas(10000, 'Game Token', 1, 'GT', [
  660. 'from' => $fromAccount,
  661. 'gas' => '0x200b20'
  662. ], function ($err, $result) use ($contract) {
  663. if ($err !== null) {
  664. return $this->fail($err->getMessage());
  665. }
  666. if (isset($result)) {
  667. echo "\nEstimate gas: " . $result->toString() . "\n";
  668. $this->assertTrue($result !== null);
  669. }
  670. });
  671. if (!isset($this->contractAddress)) {
  672. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  673. }
  674. $contract->at($this->contractAddress)->estimateGas('balanceOf', $fromAccount, [
  675. 'from' => $fromAccount
  676. ], function ($err, $result) use ($contract) {
  677. if ($err !== null) {
  678. // infura api gg
  679. return $this->fail($err->getMessage());
  680. }
  681. if (isset($result)) {
  682. echo "\nEstimate gas: " . $result->toString() . "\n";
  683. $this->assertTrue($result !== null);
  684. }
  685. });
  686. }
  687. /**
  688. * testGetData
  689. *
  690. * @return void
  691. */
  692. public function testGetData()
  693. {
  694. $contract = $this->contract;
  695. if (!isset($this->accounts[0])) {
  696. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  697. } else {
  698. $fromAccount = $this->accounts[0];
  699. }
  700. if (!isset($this->accounts[1])) {
  701. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  702. } else {
  703. $toAccount = $this->accounts[1];
  704. }
  705. $contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
  706. 'from' => $fromAccount,
  707. 'gas' => '0x200b20'
  708. ], function ($err, $result) use ($contract) {
  709. if ($err !== null) {
  710. return $this->fail($err->getMessage());
  711. }
  712. if ($result) {
  713. echo "\nTransaction has made:) id: " . $result . "\n";
  714. }
  715. $transactionId = $result;
  716. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  717. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  718. if ($err !== null) {
  719. return $this->fail($err);
  720. }
  721. if ($transaction) {
  722. $this->contractAddress = $transaction->contractAddress;
  723. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  724. }
  725. });
  726. });
  727. $constructorData = $contract->bytecode($this->testBytecode)->getData(10000, 'Game Token', 1, 'GT');
  728. // $this->assertEquals('60606040526040805190810160405280600581526020017f45524332300000000000000000000000000000000000000000000000000000008152506000908051906020019061004f92919061012f565b50341561005b57600080fd5b604051610ec5380380610ec58339810160405280805190602001909190805182019190602001805190602001909190805182019190505083600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508360048190555082600190805190602001906100f392919061012f565b50806002908051906020019061010a92919061012f565b5081600360006101000a81548160ff021916908360ff160217905550505050506101d4565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061017057805160ff191683800117855561019e565b8280016001018555821561019e579182015b8281111561019d578251825591602001919060010190610182565b5b5090506101ab91906101af565b5090565b6101d191905b808211156101cd5760008160009055506001016101b5565b5090565b90565b610ce2806101e36000396000f3006060604052600436106100a4576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100a9578063095ea7b31461013757806318160ddd1461019157806323b872dd146101ba578063313ce567146102335780635a3b7e421461026257806370a08231146102f057806395d89b411461033d578063a9059cbb146103cb578063dd62ed3e1461040d575b600080fd5b34156100b457600080fd5b6100bc610479565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100fc5780820151818401526020810190506100e1565b50505050905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561014257600080fd5b610177600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610517565b604051808215151515815260200191505060405180910390f35b341561019c57600080fd5b6101a4610609565b6040518082815260200191505060405180910390f35b34156101c557600080fd5b610219600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061060f565b604051808215151515815260200191505060405180910390f35b341561023e57600080fd5b61024661092a565b604051808260ff1660ff16815260200191505060405180910390f35b341561026d57600080fd5b61027561093d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156102b557808201518184015260208101905061029a565b50505050905090810190601f1680156102e25780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102fb57600080fd5b610327600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506109db565b6040518082815260200191505060405180910390f35b341561034857600080fd5b6103506109f3565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610390578082015181840152602081019050610375565b50505050905090810190601f1680156103bd5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156103d657600080fd5b61040b600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610a91565b005b341561041857600080fd5b610463600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610c91565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561050f5780601f106104e45761010080835404028352916020019161050f565b820191906000526020600020905b8154815290600101906020018083116104f257829003601f168201915b505050505081565b600081600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60045481565b6000808373ffffffffffffffffffffffffffffffffffffffff16141561063457600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561068057600080fd5b600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561070d57600080fd5b600660008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561079657600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600660008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b600360009054906101000a900460ff1681565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156109d35780601f106109a8576101008083540402835291602001916109d3565b820191906000526020600020905b8154815290600101906020018083116109b657829003601f168201915b505050505081565b60056020528060005260406000206000915090505481565b60028054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610a895780601f10610a5e57610100808354040283529160200191610a89565b820191906000526020600020905b815481529060010190602001808311610a6c57829003601f168201915b505050505081565b60008273ffffffffffffffffffffffffffffffffffffffff161415610ab557600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610b0157600080fd5b600560008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011015610b8e57600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b60066020528160005260406000206020528060005260406000206000915091505054815600a165627a7a723058203eb700b31f6d7723be3f4a0dd07fc4ba166a17279e26a437227679b92bacb5a200290000000000000000000000000000000000000000000000000000000000002710000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000a47616d6520546f6b656e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024754000000000000000000000000000000000000000000000000000000000000', $constructorData);
  729. if (!isset($this->contractAddress)) {
  730. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  731. }
  732. $balanceOfData = $contract->at($this->contractAddress)->getData('balanceOf', $fromAccount);
  733. $this->assertEquals('70a08231000000000000000000000000' . Utils::stripZero($fromAccount), $balanceOfData);
  734. }
  735. /**
  736. * testDecodeMethodReturn
  737. *
  738. * @return void
  739. */
  740. public function testDecodeMethodReturn()
  741. {
  742. $contract = $this->contract;
  743. $contract->abi($this->testUserAbi);
  744. if (!isset($this->accounts[0])) {
  745. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  746. } else {
  747. $fromAccount = $this->accounts[0];
  748. }
  749. if (!isset($this->accounts[1])) {
  750. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  751. } else {
  752. $toAccount = $this->accounts[1];
  753. }
  754. // Deploy user contract.
  755. $contract->bytecode($this->testUserBytecode)->new([
  756. 'from' => $fromAccount,
  757. 'gas' => '0x200b20'
  758. ], function ($err, $result) use ($contract) {
  759. if ($err !== null) {
  760. return $this->fail($err->getMessage());
  761. }
  762. if ($result) {
  763. echo "\nTransaction has made:) id: " . $result . "\n";
  764. }
  765. $transactionId = $result;
  766. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  767. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  768. if ($err !== null) {
  769. return $this->fail($err);
  770. }
  771. if ($transaction) {
  772. $this->contractAddress = $transaction->contractAddress;
  773. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  774. }
  775. });
  776. });
  777. if (!isset($this->contractAddress)) {
  778. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  779. }
  780. // Add user.
  781. $contract->at($this->contractAddress)->send('addUser', $toAccount, 'Peter', 'Lai', 18, [
  782. 'from' => $fromAccount,
  783. 'gas' => '0x200b20'
  784. ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
  785. if ($err !== null) {
  786. return $this->fail($err->getMessage());
  787. }
  788. if ($result) {
  789. echo "\nTransaction has made:) id: " . $result . "\n";
  790. }
  791. $transactionId = $result;
  792. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  793. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount, $contract) {
  794. if ($err !== null) {
  795. return $this->fail($err);
  796. }
  797. if ($transaction) {
  798. $topics = $transaction->logs[0]->topics;
  799. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  800. // validate topics
  801. $this->assertEquals($contract->ethabi->encodeEventSignature($this->contract->events['AddUser']), $topics[0]);
  802. $this->assertEquals('0x' . IntegerFormatter::format($toAccount), $topics[1]);
  803. }
  804. });
  805. });
  806. // Get user.
  807. $contract->call('getUser', $toAccount, [
  808. 'from' => $fromAccount
  809. ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
  810. if ($err !== null) {
  811. return $this->fail($err->getMessage());
  812. }
  813. if ($result) {
  814. $this->assertEquals($result['firstName'], 'Peter');
  815. $this->assertEquals($result['lastName'], 'Lai');
  816. $this->assertEquals($result['age']->toString(), '18');
  817. }
  818. });
  819. }
  820. /**
  821. * testIssue85
  822. *
  823. * @return void
  824. */
  825. public function testIssue85()
  826. {
  827. $bytecode = '0x608060405234801561001057600080fd5b50610a36806100206000396000f3006080604052600436106100af576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630bcd3b33146100b45780631f9030371461014457806325f35c36146101775780632d1be94d146102735780638b84925b146102a2578063a15e05fd146102cd578063a5f807cc1461030f578063d07326681461039f578063ded516d21461043e578063f1d0876a146104ad578063f5c2e88a1461054c575b600080fd5b3480156100c057600080fd5b506100c96105dc565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101095780820151818401526020810190506100ee565b50505050905090810190601f1680156101365780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561015057600080fd5b5061015961061e565b60405180826000191660001916815260200191505060405180910390f35b34801561018357600080fd5b5061018c61064b565b604051808060200180602001838103835285818151815260200191508051906020019080838360005b838110156101d05780820151818401526020810190506101b5565b50505050905090810190601f1680156101fd5780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561023657808201518184015260208101905061021b565b50505050905090810190601f1680156102635780820380516001836020036101000a031916815260200191505b5094505050505060405180910390f35b34801561027f57600080fd5b506102886106cd565b604051808215151515815260200191505060405180910390f35b3480156102ae57600080fd5b506102b76106db565b6040518082815260200191505060405180910390f35b3480156102d957600080fd5b506102e26106e9565b60405180836000191660001916815260200182600019166000191681526020019250505060405180910390f35b34801561031b57600080fd5b50610324610741565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610364578082015181840152602081019050610349565b50505050905090810190601f1680156103915780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156103ab57600080fd5b506103b461076a565b60405180806020018360001916600019168152602001828103825284818151815260200191508051906020019080838360005b838110156104025780820151818401526020810190506103e7565b50505050905090810190601f16801561042f5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561044a57600080fd5b506104536107d9565b60405180827effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff19167effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916815260200191505060405180910390f35b3480156104b957600080fd5b506104c2610806565b60405180836000191660001916815260200180602001828103825283818151815260200191508051906020019080838360005b838110156105105780820151818401526020810190506104f5565b50505050905090810190601f16801561053d5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561055857600080fd5b50610561610875565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156105a1578082015181840152602081019050610586565b50505050905090810190601f1680156105ce5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6060806040805190810160405280600381526020017f616263000000000000000000000000000000000000000000000000000000000081525090508091505090565b6000807f616263000000000000000000000000000000000000000000000000000000000090508091505090565b6060806060806040805190810160405280600381526020017f616263000000000000000000000000000000000000000000000000000000000081525091506040805190810160405280600381526020017f78797a0000000000000000000000000000000000000000000000000000000000815250905081819350935050509091565b600080600190508091505090565b600080606390508091505090565b6000806000807f616263000000000000000000000000000000000000000000000000000000000091507f78797a0000000000000000000000000000000000000000000000000000000000905081819350935050509091565b6060806101606040519081016040528061012c81526020016108df61012c913990508091505090565b60606000606060006040805190810160405280600381526020017f616263000000000000000000000000000000000000000000000000000000000081525091507f78797a0000000000000000000000000000000000000000000000000000000000905081819350935050509091565b6000807f610000000000000000000000000000000000000000000000000000000000000090508091505090565b60006060600060607f78797a000000000000000000000000000000000000000000000000000000000091506040805190810160405280600381526020017f6162630000000000000000000000000000000000000000000000000000000000815250905081819350935050509091565b606080606060405190810160405280603c81526020017f616263616263616263636162636162636162636361626361626361626363616281526020017f6361626361626363616263616263616263636162636162636162636300000000815250905080915050905600616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363a165627a7a7230582020ee9e6b05918d0df987776ee24808f4dd1c522806bf9fa8f4336c93f6b0ec050029';
  828. $abi = '[
  829. {
  830. "constant": true,
  831. "inputs": [],
  832. "name": "getBytes",
  833. "outputs": [
  834. {
  835. "name": "",
  836. "type": "bytes"
  837. }
  838. ],
  839. "payable": false,
  840. "stateMutability": "pure",
  841. "type": "function"
  842. },
  843. {
  844. "constant": true,
  845. "inputs": [],
  846. "name": "getBytes32",
  847. "outputs": [
  848. {
  849. "name": "",
  850. "type": "bytes32"
  851. }
  852. ],
  853. "payable": false,
  854. "stateMutability": "pure",
  855. "type": "function"
  856. },
  857. {
  858. "constant": true,
  859. "inputs": [],
  860. "name": "getDynamicData",
  861. "outputs": [
  862. {
  863. "name": "",
  864. "type": "bytes"
  865. },
  866. {
  867. "name": "",
  868. "type": "bytes"
  869. }
  870. ],
  871. "payable": false,
  872. "stateMutability": "pure",
  873. "type": "function"
  874. },
  875. {
  876. "constant": true,
  877. "inputs": [],
  878. "name": "getBoolean",
  879. "outputs": [
  880. {
  881. "name": "",
  882. "type": "bool"
  883. }
  884. ],
  885. "payable": false,
  886. "stateMutability": "pure",
  887. "type": "function"
  888. },
  889. {
  890. "constant": true,
  891. "inputs": [],
  892. "name": "getInteger",
  893. "outputs": [
  894. {
  895. "name": "",
  896. "type": "int256"
  897. }
  898. ],
  899. "payable": false,
  900. "stateMutability": "pure",
  901. "type": "function"
  902. },
  903. {
  904. "constant": true,
  905. "inputs": [],
  906. "name": "getTwoBytes32",
  907. "outputs": [
  908. {
  909. "name": "",
  910. "type": "bytes32"
  911. },
  912. {
  913. "name": "",
  914. "type": "bytes32"
  915. }
  916. ],
  917. "payable": false,
  918. "stateMutability": "pure",
  919. "type": "function"
  920. },
  921. {
  922. "constant": true,
  923. "inputs": [],
  924. "name": "getBytesVeryLong",
  925. "outputs": [
  926. {
  927. "name": "",
  928. "type": "bytes"
  929. }
  930. ],
  931. "payable": false,
  932. "stateMutability": "pure",
  933. "type": "function"
  934. },
  935. {
  936. "constant": true,
  937. "inputs": [],
  938. "name": "getDynamicDataMixedOne",
  939. "outputs": [
  940. {
  941. "name": "",
  942. "type": "bytes"
  943. },
  944. {
  945. "name": "",
  946. "type": "bytes32"
  947. }
  948. ],
  949. "payable": false,
  950. "stateMutability": "pure",
  951. "type": "function"
  952. },
  953. {
  954. "constant": true,
  955. "inputs": [],
  956. "name": "getBytes1",
  957. "outputs": [
  958. {
  959. "name": "",
  960. "type": "bytes1"
  961. }
  962. ],
  963. "payable": false,
  964. "stateMutability": "pure",
  965. "type": "function"
  966. },
  967. {
  968. "constant": true,
  969. "inputs": [],
  970. "name": "getDynamicDataMixedTwo",
  971. "outputs": [
  972. {
  973. "name": "",
  974. "type": "bytes32"
  975. },
  976. {
  977. "name": "",
  978. "type": "bytes"
  979. }
  980. ],
  981. "payable": false,
  982. "stateMutability": "pure",
  983. "type": "function"
  984. },
  985. {
  986. "constant": true,
  987. "inputs": [],
  988. "name": "getBytesLong",
  989. "outputs": [
  990. {
  991. "name": "",
  992. "type": "bytes"
  993. }
  994. ],
  995. "payable": false,
  996. "stateMutability": "pure",
  997. "type": "function"
  998. },
  999. {
  1000. "inputs": [],
  1001. "payable": false,
  1002. "stateMutability": "nonpayable",
  1003. "type": "constructor"
  1004. }
  1005. ]';
  1006. $functions = [
  1007. [
  1008. 'name' => 'getBoolean',
  1009. 'test' => function ($value) {
  1010. return is_bool($value);
  1011. }
  1012. ], [
  1013. 'name' => 'getInteger',
  1014. 'test' => function ($value) {
  1015. return ($value instanceof BigNumber);
  1016. }
  1017. ], [
  1018. 'name' => 'getBytes1',
  1019. 'test' => function ($value) {
  1020. return Utils::isHex($value) && mb_strlen($value) === 4;
  1021. }
  1022. ], [
  1023. 'name' => 'getBytes32',
  1024. 'test' => function ($value) {
  1025. return Utils::isHex($value) && mb_strlen($value) === 66;
  1026. }
  1027. ], [
  1028. 'name' => 'getBytesLong',
  1029. 'test' => function ($value) {
  1030. return Utils::isHex($value);
  1031. }
  1032. ], [
  1033. 'name' => 'getDynamicData',
  1034. 'test' => function ($value) {
  1035. return Utils::isHex($value);
  1036. }
  1037. ], [
  1038. 'name' => 'getDynamicDataMixedOne',
  1039. 'test' => function ($value) {
  1040. return Utils::isHex($value);
  1041. }
  1042. ], [
  1043. 'name' => 'getDynamicDataMixedTwo',
  1044. 'test' => function ($value) {
  1045. return Utils::isHex($value);
  1046. }
  1047. ], [
  1048. 'name' => 'getTwoBytes32',
  1049. 'test' => function ($value) {
  1050. return Utils::isHex($value) && mb_strlen($value) === 66;
  1051. }
  1052. ]
  1053. ];
  1054. $contractAddress = "";
  1055. if (!isset($this->accounts[0])) {
  1056. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  1057. } else {
  1058. $account = $this->accounts[0];
  1059. }
  1060. $contract = new Contract($this->web3->provider, $abi);
  1061. $contract->bytecode($bytecode)->new([
  1062. 'from' => $account,
  1063. 'gas' => '0x200b20'
  1064. ], function ($err, $result) use ($contract, &$contractAddress) {
  1065. if ($err !== null) {
  1066. return $this->fail($err->getMessage());
  1067. }
  1068. if ($result) {
  1069. echo "\nTransaction has made:) id: " . $result . "\n";
  1070. }
  1071. $transactionId = $result;
  1072. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1073. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use (&$contractAddress) {
  1074. if ($err !== null) {
  1075. return $this->fail($err);
  1076. }
  1077. if ($transaction) {
  1078. $contractAddress = $transaction->contractAddress;
  1079. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1080. }
  1081. });
  1082. });
  1083. $contract->at($contractAddress);
  1084. foreach ($functions as $function) {
  1085. $contract->call($function['name'], [], 'latest', function ($err, $res) use ($function) {
  1086. if ($err !== null) {
  1087. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1088. return;
  1089. }
  1090. foreach ($res as $r) {
  1091. if (!call_user_func($function['test'], $r)) {
  1092. var_dump($r);
  1093. }
  1094. $this->assertTrue(call_user_func($function['test'], $r));
  1095. }
  1096. });
  1097. }
  1098. }
  1099. /**
  1100. * testIssue125
  1101. *
  1102. * @return void
  1103. */
  1104. public function testIssue125()
  1105. {
  1106. $bytecode = '0x608060405234801561001057600080fd5b506103bb806100206000396000f3fe608060405260043610610046576000357c01000000000000000000000000000000000000000000000000000000009004806373d4a13a1461004b578063ab62f0e1146100db575b600080fd5b34801561005757600080fd5b506100606101a3565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a0578082015181840152602081019050610085565b50505050905090810190601f1680156100cd5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156100e757600080fd5b506101a1600480360360208110156100fe57600080fd5b810190808035906020019064010000000081111561011b57600080fd5b82018360208201111561012d57600080fd5b8035906020019184600183028401116401000000008311171561014f57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610241565b005b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102395780601f1061020e57610100808354040283529160200191610239565b820191906000526020600020905b81548152906001019060200180831161021c57829003601f168201915b505050505081565b80600090805190602001906102579291906102ea565b50806040518082805190602001908083835b60208310151561028e5780518252602082019150602081019050602083039250610269565b6001836020036101000a03801982511681845116808217855250505050505090500191505060405180910390207fae08ae866f3211e692b7cdd30e8b6ec658a153397150437cb873f95239953a5460405160405180910390a250565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061032b57805160ff1916838001178555610359565b82800160010185558215610359579182015b8281111561035857825182559160200191906001019061033d565b5b509050610366919061036a565b5090565b61038c91905b80821115610388576000816000905550600101610370565b5090565b9056fea165627a7a72305820bb836a29a397513a3f2e2199520262ef5978d3a3d7e3d3532da41064d39177e30029';
  1107. $abi = '[
  1108. {
  1109. "constant": false,
  1110. "inputs": [
  1111. {
  1112. "name": "_data",
  1113. "type": "bytes"
  1114. }
  1115. ],
  1116. "name": "setData",
  1117. "outputs": [],
  1118. "payable": false,
  1119. "stateMutability": "nonpayable",
  1120. "type": "function"
  1121. },
  1122. {
  1123. "anonymous": false,
  1124. "inputs": [
  1125. {
  1126. "indexed": true,
  1127. "name": "_data",
  1128. "type": "bytes"
  1129. }
  1130. ],
  1131. "name": "SetData",
  1132. "type": "event"
  1133. },
  1134. {
  1135. "constant": true,
  1136. "inputs": [],
  1137. "name": "data",
  1138. "outputs": [
  1139. {
  1140. "name": "",
  1141. "type": "bytes"
  1142. }
  1143. ],
  1144. "payable": false,
  1145. "stateMutability": "view",
  1146. "type": "function"
  1147. }
  1148. ]';
  1149. $contractAddress = "";
  1150. if (!isset($this->accounts[0])) {
  1151. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  1152. } else {
  1153. $account = $this->accounts[0];
  1154. }
  1155. $contract = new Contract($this->web3->provider, $abi);
  1156. $contract->bytecode($bytecode)->new([
  1157. 'from' => $account,
  1158. 'gas' => '0x200b20'
  1159. ], function ($err, $result) use ($contract, &$contractAddress) {
  1160. if ($err !== null) {
  1161. return $this->fail($err->getMessage());
  1162. }
  1163. if ($result) {
  1164. echo "\nTransaction has made:) id: " . $result . "\n";
  1165. }
  1166. $transactionId = $result;
  1167. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1168. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use (&$contractAddress) {
  1169. if ($err !== null) {
  1170. return $this->fail($err);
  1171. }
  1172. if ($transaction) {
  1173. $contractAddress = $transaction->contractAddress;
  1174. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1175. }
  1176. });
  1177. });
  1178. $contract->at($contractAddress);
  1179. $contract->call('data', function ($err, $res) {
  1180. if ($err !== null) {
  1181. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1182. return;
  1183. }
  1184. $this->assertEquals('0', $res[0]);
  1185. });
  1186. $contract->send('setData', '0x44aec9b900000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000021000000000000000000000000000000000000000000000000000000000000000b', [
  1187. 'from' => $account,
  1188. 'gas' => '0x200b20'
  1189. ], function ($err, $result) use ($contract) {
  1190. if ($err !== null) {
  1191. var_dump('Error' . $err->getMessage());
  1192. return $this->fail($err->getMessage());
  1193. }
  1194. if ($result) {
  1195. echo "\nTransaction has made:) id: " . $result . "\n";
  1196. }
  1197. $transactionId = $result;
  1198. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1199. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  1200. if ($err !== null) {
  1201. return $this->fail($err);
  1202. }
  1203. $this->assertTrue($transaction !== null);
  1204. });
  1205. });
  1206. $contract->call('data', function ($err, $res) {
  1207. if ($err !== null) {
  1208. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1209. return;
  1210. }
  1211. $this->assertEquals('0x44aec9b900000000000000000000000000000000000000000000000000000000000000030000000000000000000000000000000000000000000000000000000000000021000000000000000000000000000000000000000000000000000000000000000b', $res[0]);
  1212. });
  1213. }
  1214. /**
  1215. * testIssue134
  1216. *
  1217. * @return void
  1218. */
  1219. public function testIssue134()
  1220. {
  1221. $bytecode = '0x608060405234801561001057600080fd5b50610487806100206000396000f3fe60806040526004361061005c576000357c01000000000000000000000000000000000000000000000000000000009004806373d4a13a146100615780638381f58a146100f15780639a73c4091461011c578063c3fad957146101ee575b600080fd5b34801561006d57600080fd5b50610076610229565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100b657808201518184015260208101905061009b565b50505050905090810190601f1680156100e35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156100fd57600080fd5b506101066102c7565b6040518082815260200191505060405180910390f35b34801561012857600080fd5b506101ec6004803603604081101561013f57600080fd5b81019080803590602001909291908035906020019064010000000081111561016657600080fd5b82018360208201111561017857600080fd5b8035906020019184600183028401116401000000008311171561019a57600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050506102cd565b005b3480156101fa57600080fd5b506102276004803603602081101561021157600080fd5b810190808035906020019092919050505061037f565b005b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102bf5780601f10610294576101008083540402835291602001916102bf565b820191906000526020600020905b8154815290600101906020018083116102a257829003601f168201915b505050505081565b60015481565b80600090805190602001906102e39291906103b6565b5081600181905550806040518082805190602001908083835b60208310151561032157805182526020820191506020810190506020830392506102fc565b6001836020036101000a0380198251168184511680821785525050505050509050019150506040518091039020827f95d23dc1ab7ea37e46bd697006b873f80ffecb4cb4ae085c7cc60a977ba7f2f560405160405180910390a35050565b80600181905550807f9d9c58c068c2bc9cb27d4e5c437f624ccbf4910ba8893ffd03ee311830becab160405160405180910390a250565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106103f757805160ff1916838001178555610425565b82800160010185558215610425579182015b82811115610424578251825591602001919060010190610409565b5b5090506104329190610436565b5090565b61045891905b8082111561045457600081600090555060010161043c565b5090565b9056fea165627a7a72305820830ee43c52ac89b1fbc68647e1893aea17aa8ca132532723176508a6bc4586c90029';
  1222. $abi = '[
  1223. {
  1224. "constant": true,
  1225. "inputs": [],
  1226. "name": "data",
  1227. "outputs": [
  1228. {
  1229. "name": "",
  1230. "type": "bytes"
  1231. }
  1232. ],
  1233. "payable": false,
  1234. "stateMutability": "view",
  1235. "type": "function"
  1236. },
  1237. {
  1238. "constant": true,
  1239. "inputs": [],
  1240. "name": "number",
  1241. "outputs": [
  1242. {
  1243. "name": "",
  1244. "type": "uint256"
  1245. }
  1246. ],
  1247. "payable": false,
  1248. "stateMutability": "view",
  1249. "type": "function"
  1250. },
  1251. {
  1252. "constant": false,
  1253. "inputs": [
  1254. {
  1255. "name": "_number",
  1256. "type": "uint256"
  1257. },
  1258. {
  1259. "name": "_data",
  1260. "type": "bytes"
  1261. }
  1262. ],
  1263. "name": "say",
  1264. "outputs": [],
  1265. "payable": false,
  1266. "stateMutability": "nonpayable",
  1267. "type": "function"
  1268. },
  1269. {
  1270. "constant": false,
  1271. "inputs": [
  1272. {
  1273. "name": "_number",
  1274. "type": "uint256"
  1275. }
  1276. ],
  1277. "name": "say",
  1278. "outputs": [],
  1279. "payable": false,
  1280. "stateMutability": "nonpayable",
  1281. "type": "function"
  1282. },
  1283. {
  1284. "anonymous": false,
  1285. "inputs": [
  1286. {
  1287. "indexed": true,
  1288. "name": "_number",
  1289. "type": "uint256"
  1290. }
  1291. ],
  1292. "name": "Say",
  1293. "type": "event"
  1294. },
  1295. {
  1296. "anonymous": false,
  1297. "inputs": [
  1298. {
  1299. "indexed": true,
  1300. "name": "_number",
  1301. "type": "uint256"
  1302. },
  1303. {
  1304. "indexed": true,
  1305. "name": "_data",
  1306. "type": "bytes"
  1307. }
  1308. ],
  1309. "name": "Say",
  1310. "type": "event"
  1311. }
  1312. ]';
  1313. $testNumber = 16;
  1314. $testData = "0x01234";
  1315. $contractAddress = "";
  1316. $contract = new Contract($this->web3->provider, $abi);
  1317. if (!isset($this->accounts[0])) {
  1318. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  1319. } else {
  1320. $account = $this->accounts[0];
  1321. }
  1322. $contract = new Contract($this->web3->provider, $abi);
  1323. $contract->bytecode($bytecode)->new([
  1324. 'from' => $account,
  1325. 'gas' => '0x200b20'
  1326. ], function ($err, $result) use ($contract, &$contractAddress) {
  1327. if ($err !== null) {
  1328. return $this->fail($err->getMessage());
  1329. }
  1330. if ($result) {
  1331. echo "\nTransaction has made:) id: " . $result . "\n";
  1332. }
  1333. $transactionId = $result;
  1334. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1335. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use (&$contractAddress) {
  1336. if ($err !== null) {
  1337. return $this->fail($err);
  1338. }
  1339. if ($transaction) {
  1340. $contractAddress = $transaction->contractAddress;
  1341. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1342. }
  1343. });
  1344. });
  1345. $contract->at($contractAddress);
  1346. // test for send transaction and get data
  1347. $contract->send('say', $testNumber, $testData, [
  1348. 'from' => $account,
  1349. 'gas' => '0x200b20'
  1350. ], function ($err, $result) use ($contract, $testNumber, $testData) {
  1351. if ($err !== null) {
  1352. return $this->fail($err->getMessage());
  1353. }
  1354. if ($result) {
  1355. echo "\nTransaction has made:) id: " . $result . "\n";
  1356. }
  1357. $transactionId = $result;
  1358. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1359. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($testNumber, $testData, $contract) {
  1360. if ($err !== null) {
  1361. return $this->fail($err);
  1362. }
  1363. if ($transaction) {
  1364. $topics = $transaction->logs[0]->topics;
  1365. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1366. // validate topics
  1367. $this->assertEquals($contract->ethabi->encodeEventSignature($contract->events['Say']), $topics[0]);
  1368. $this->assertEquals('0x' . IntegerFormatter::format($testNumber), $topics[1], $topics[2]);
  1369. }
  1370. $contract->call('number', function ($err, $res) use ($testNumber) {
  1371. if ($err !== null) {
  1372. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1373. return;
  1374. }
  1375. $this->assertEquals((string) $testNumber, $res[0]->toString());
  1376. });
  1377. });
  1378. });
  1379. $testNumber++;
  1380. $contract->send('say', $testNumber, [
  1381. 'from' => $account,
  1382. 'gas' => '0x200b20'
  1383. ], function ($err, $result) use ($contract, $testNumber) {
  1384. if ($err !== null) {
  1385. return $this->fail($err->getMessage());
  1386. }
  1387. if ($result) {
  1388. echo "\nTransaction has made:) id: " . $result . "\n";
  1389. }
  1390. $transactionId = $result;
  1391. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1392. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($testNumber, $contract) {
  1393. if ($err !== null) {
  1394. return $this->fail($err);
  1395. }
  1396. if ($transaction) {
  1397. $topics = $transaction->logs[0]->topics;
  1398. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1399. // validate topics
  1400. // $this->assertEquals($contract->ethabi->encodeEventSignature($contract->events['Say']), $topics[0]);
  1401. $this->assertEquals('0x' . IntegerFormatter::format($testNumber), $topics[1]);
  1402. }
  1403. $blockNumber = Utils::toBn($transaction->blockNumber);
  1404. $contract->call('number', Utils::toHex($blockNumber, true), function ($err, $res) use ($testNumber) {
  1405. if ($err !== null) {
  1406. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1407. return;
  1408. }
  1409. $this->assertEquals((string) $testNumber, $res[0]->toString());
  1410. });
  1411. $blockNumber = $blockNumber->subtract(Utils::toBn(1));
  1412. $contract->call('number', Utils::toHex($blockNumber, true), function ($err, $res) use ($testNumber) {
  1413. if ($err !== null) {
  1414. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1415. return;
  1416. }
  1417. $this->assertEquals((string) $testNumber-1, $res[0]->toString());
  1418. });
  1419. });
  1420. });
  1421. // test for estimate gas
  1422. $contract->estimateGas("say", $testNumber, $testData, [
  1423. 'from' => $account,
  1424. 'gas' => '0x200b20'
  1425. ], function ($err, $result) {
  1426. if ($err !== null) {
  1427. return $this->fail($err->getMessage());
  1428. }
  1429. if (isset($result)) {
  1430. echo "\nEstimate gas: " . $result->toString() . "\n";
  1431. $this->assertTrue($result !== null);
  1432. }
  1433. });
  1434. $contract->estimateGas("say", $testNumber, [
  1435. 'from' => $account,
  1436. 'gas' => '0x200b20'
  1437. ], function ($err, $result) {
  1438. if ($err !== null) {
  1439. return $this->fail($err->getMessage());
  1440. }
  1441. if (isset($result)) {
  1442. echo "\nEstimate gas: " . $result->toString() . "\n";
  1443. $this->assertTrue($result !== null);
  1444. }
  1445. });
  1446. }
  1447. /**
  1448. * testAsync
  1449. *
  1450. * @return void
  1451. */
  1452. public function testAsync()
  1453. {
  1454. $contract = $this->contract;
  1455. if (!isset($this->accounts[0])) {
  1456. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  1457. } else {
  1458. $fromAccount = $this->accounts[0];
  1459. }
  1460. if (!isset($this->accounts[1])) {
  1461. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  1462. } else {
  1463. $toAccount = $this->accounts[1];
  1464. }
  1465. $contract->bytecode($this->testBytecode)->new(1000000, 'Game Token', 1, 'GT', [
  1466. 'from' => $fromAccount,
  1467. 'gas' => '0x200b20'
  1468. ], function ($err, $result) use ($contract) {
  1469. if ($err !== null) {
  1470. return $this->fail($err->getMessage());
  1471. }
  1472. if ($result) {
  1473. echo "\nTransaction has made:) id: " . $result . "\n";
  1474. }
  1475. $transactionId = $result;
  1476. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1477. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  1478. if ($err !== null) {
  1479. return $this->fail($err);
  1480. }
  1481. if ($transaction) {
  1482. $this->contractAddress = $transaction->contractAddress;
  1483. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1484. }
  1485. });
  1486. });
  1487. if (!isset($this->contractAddress)) {
  1488. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  1489. }
  1490. $asyncContract = $this->asyncContract;
  1491. $promise1 = $asyncContract->at($this->contractAddress)->send('transfer', $toAccount, 16, [
  1492. 'from' => $fromAccount,
  1493. 'gas' => '0x200b20'
  1494. ], function ($err, $result) use ($contract, $fromAccount, $toAccount, $asyncContract) {
  1495. if ($err !== null) {
  1496. return $this->fail($err->getMessage());
  1497. }
  1498. if ($result) {
  1499. echo "\nTransaction has made:) id: " . $result . "\n";
  1500. }
  1501. $transactionId = $result;
  1502. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1503. $promise = $asyncContract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount, $contract) {
  1504. if ($err !== null) {
  1505. return $this->fail($err);
  1506. }
  1507. if ($transaction) {
  1508. $topics = $transaction->logs[0]->topics;
  1509. echo "\nTransaction has mined:) block number: " . $transaction->blockNumber . "\n";
  1510. // validate topics
  1511. $this->assertEquals($contract->ethabi->encodeEventSignature($this->contract->events['Transfer']), $topics[0]);
  1512. $this->assertEquals('0x' . IntegerFormatter::format($fromAccount), $topics[1]);
  1513. $this->assertEquals('0x' . IntegerFormatter::format($toAccount), $topics[2]);
  1514. }
  1515. });
  1516. $this->assertTrue($promise instanceof \React\Promise\PromiseInterface);
  1517. \React\Async\await($promise);
  1518. });
  1519. $this->assertTrue($promise1 instanceof \React\Promise\PromiseInterface);
  1520. $promise2 = $asyncContract->at($this->contractAddress)->call('balanceOf', $fromAccount, [
  1521. 'from' => $fromAccount
  1522. ], function ($err, $result) use ($contract) {
  1523. if ($err !== null) {
  1524. return $this->fail($err->getMessage());
  1525. }
  1526. if (isset($result)) {
  1527. // $bn = Utils::toBn($result);
  1528. // $this->assertEquals($bn->toString(), '10000', 'Balance should be 10000.');
  1529. $this->assertTrue($result !== null);
  1530. }
  1531. });
  1532. $this->assertTrue($promise2 instanceof \React\Promise\PromiseInterface);
  1533. $promise3 = $asyncContract->bytecode($this->testBytecode)->estimateGas('balanceOf', $toAccount, [
  1534. 'from' => $fromAccount,
  1535. 'gas' => '0x200b20'
  1536. ], function ($err, $result) use ($contract) {
  1537. if ($err !== null) {
  1538. return $this->fail($err->getMessage());
  1539. }
  1540. if (isset($result)) {
  1541. echo "\nEstimate gas: " . $result->toString() . "\n";
  1542. $this->assertTrue($result !== null);
  1543. }
  1544. });
  1545. \React\Async\await(\React\Async\parallel([
  1546. function () use ($promise1) { return $promise1; },
  1547. function () use ($promise2) { return $promise2; },
  1548. function () use ($promise3) { return $promise3; },
  1549. ]));
  1550. }
  1551. }