ContractTest.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. <?php
  2. namespace Test\Unit;
  3. use Test\TestCase;
  4. use Web3\Providers\HttpProvider;
  5. use Web3\RequestManagers\RequestManager;
  6. use Web3\RequestManagers\HttpRequestManager;
  7. use Web3\Contract;
  8. use Web3\Utils;
  9. use Web3\Contracts\Ethabi;
  10. use Web3\Formatters\IntegerFormatter;
  11. use phpseclib\Math\BigInteger as BigNumber;
  12. class ContractTest extends TestCase
  13. {
  14. /**
  15. * contract
  16. *
  17. * @var \Web3\Contract
  18. */
  19. protected $contract;
  20. /**
  21. * testAbi
  22. * GameToken abi from https://github.com/sc0Vu/GameToken
  23. *
  24. * @var string
  25. */
  26. protected $testAbi = '[
  27. {
  28. "constant": true,
  29. "inputs": [],
  30. "name": "name",
  31. "outputs": [
  32. {
  33. "name": "",
  34. "type": "string"
  35. }
  36. ],
  37. "payable": false,
  38. "stateMutability": "view",
  39. "type": "function"
  40. },
  41. {
  42. "constant": false,
  43. "inputs": [
  44. {
  45. "name": "_spender",
  46. "type": "address"
  47. },
  48. {
  49. "name": "_value",
  50. "type": "uint256"
  51. }
  52. ],
  53. "name": "approve",
  54. "outputs": [
  55. {
  56. "name": "success",
  57. "type": "bool"
  58. }
  59. ],
  60. "payable": false,
  61. "stateMutability": "nonpayable",
  62. "type": "function"
  63. },
  64. {
  65. "constant": true,
  66. "inputs": [],
  67. "name": "totalSupply",
  68. "outputs": [
  69. {
  70. "name": "",
  71. "type": "uint256"
  72. }
  73. ],
  74. "payable": false,
  75. "stateMutability": "view",
  76. "type": "function"
  77. },
  78. {
  79. "constant": false,
  80. "inputs": [
  81. {
  82. "name": "_from",
  83. "type": "address"
  84. },
  85. {
  86. "name": "_to",
  87. "type": "address"
  88. },
  89. {
  90. "name": "_value",
  91. "type": "uint256"
  92. }
  93. ],
  94. "name": "transferFrom",
  95. "outputs": [
  96. {
  97. "name": "success",
  98. "type": "bool"
  99. }
  100. ],
  101. "payable": false,
  102. "stateMutability": "nonpayable",
  103. "type": "function"
  104. },
  105. {
  106. "constant": true,
  107. "inputs": [],
  108. "name": "decimals",
  109. "outputs": [
  110. {
  111. "name": "",
  112. "type": "uint8"
  113. }
  114. ],
  115. "payable": false,
  116. "stateMutability": "view",
  117. "type": "function"
  118. },
  119. {
  120. "constant": true,
  121. "inputs": [],
  122. "name": "standard",
  123. "outputs": [
  124. {
  125. "name": "",
  126. "type": "string"
  127. }
  128. ],
  129. "payable": false,
  130. "stateMutability": "view",
  131. "type": "function"
  132. },
  133. {
  134. "constant": true,
  135. "inputs": [
  136. {
  137. "name": "",
  138. "type": "address"
  139. }
  140. ],
  141. "name": "balanceOf",
  142. "outputs": [
  143. {
  144. "name": "",
  145. "type": "uint256"
  146. }
  147. ],
  148. "payable": false,
  149. "stateMutability": "view",
  150. "type": "function"
  151. },
  152. {
  153. "constant": true,
  154. "inputs": [],
  155. "name": "symbol",
  156. "outputs": [
  157. {
  158. "name": "",
  159. "type": "string"
  160. }
  161. ],
  162. "payable": false,
  163. "stateMutability": "view",
  164. "type": "function"
  165. },
  166. {
  167. "constant": false,
  168. "inputs": [
  169. {
  170. "name": "_to",
  171. "type": "address"
  172. },
  173. {
  174. "name": "_value",
  175. "type": "uint256"
  176. }
  177. ],
  178. "name": "transfer",
  179. "outputs": [],
  180. "payable": false,
  181. "stateMutability": "nonpayable",
  182. "type": "function"
  183. },
  184. {
  185. "constant": true,
  186. "inputs": [
  187. {
  188. "name": "",
  189. "type": "address"
  190. },
  191. {
  192. "name": "",
  193. "type": "address"
  194. }
  195. ],
  196. "name": "allowance",
  197. "outputs": [
  198. {
  199. "name": "",
  200. "type": "uint256"
  201. }
  202. ],
  203. "payable": false,
  204. "stateMutability": "view",
  205. "type": "function"
  206. },
  207. {
  208. "inputs": [
  209. {
  210. "name": "initialSupply",
  211. "type": "uint256"
  212. },
  213. {
  214. "name": "tokenName",
  215. "type": "string"
  216. },
  217. {
  218. "name": "decimalUnits",
  219. "type": "uint8"
  220. },
  221. {
  222. "name": "tokenSymbol",
  223. "type": "string"
  224. }
  225. ],
  226. "payable": false,
  227. "stateMutability": "nonpayable",
  228. "type": "constructor"
  229. },
  230. {
  231. "anonymous": false,
  232. "inputs": [
  233. {
  234. "indexed": true,
  235. "name": "from",
  236. "type": "address"
  237. },
  238. {
  239. "indexed": true,
  240. "name": "to",
  241. "type": "address"
  242. },
  243. {
  244. "indexed": false,
  245. "name": "value",
  246. "type": "uint256"
  247. }
  248. ],
  249. "name": "Transfer",
  250. "type": "event"
  251. },
  252. {
  253. "anonymous": false,
  254. "inputs": [
  255. {
  256. "indexed": true,
  257. "name": "_owner",
  258. "type": "address"
  259. },
  260. {
  261. "indexed": true,
  262. "name": "_spender",
  263. "type": "address"
  264. },
  265. {
  266. "indexed": false,
  267. "name": "_value",
  268. "type": "uint256"
  269. }
  270. ],
  271. "name": "Approval",
  272. "type": "event"
  273. }
  274. ]';
  275. /**
  276. * testBytecode
  277. * GameToken abi from https://github.com/sc0Vu/GameToken
  278. *
  279. * @var string
  280. */
  281. protected $testBytecode = '0x60606040526040805190810160405280600581526020017f45524332300000000000000000000000000000000000000000000000000000008152506000908051906020019061004f92919061012f565b50341561005b57600080fd5b604051610ec5380380610ec58339810160405280805190602001909190805182019190602001805190602001909190805182019190505083600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508360048190555082600190805190602001906100f392919061012f565b50806002908051906020019061010a92919061012f565b5081600360006101000a81548160ff021916908360ff160217905550505050506101d4565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061017057805160ff191683800117855561019e565b8280016001018555821561019e579182015b8281111561019d578251825591602001919060010190610182565b5b5090506101ab91906101af565b5090565b6101d191905b808211156101cd5760008160009055506001016101b5565b5090565b90565b610ce2806101e36000396000f3006060604052600436106100a4576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100a9578063095ea7b31461013757806318160ddd1461019157806323b872dd146101ba578063313ce567146102335780635a3b7e421461026257806370a08231146102f057806395d89b411461033d578063a9059cbb146103cb578063dd62ed3e1461040d575b600080fd5b34156100b457600080fd5b6100bc610479565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100fc5780820151818401526020810190506100e1565b50505050905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561014257600080fd5b610177600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610517565b604051808215151515815260200191505060405180910390f35b341561019c57600080fd5b6101a4610609565b6040518082815260200191505060405180910390f35b34156101c557600080fd5b610219600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061060f565b604051808215151515815260200191505060405180910390f35b341561023e57600080fd5b61024661092a565b604051808260ff1660ff16815260200191505060405180910390f35b341561026d57600080fd5b61027561093d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156102b557808201518184015260208101905061029a565b50505050905090810190601f1680156102e25780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102fb57600080fd5b610327600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506109db565b6040518082815260200191505060405180910390f35b341561034857600080fd5b6103506109f3565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610390578082015181840152602081019050610375565b50505050905090810190601f1680156103bd5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156103d657600080fd5b61040b600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610a91565b005b341561041857600080fd5b610463600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610c91565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561050f5780601f106104e45761010080835404028352916020019161050f565b820191906000526020600020905b8154815290600101906020018083116104f257829003601f168201915b505050505081565b600081600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60045481565b6000808373ffffffffffffffffffffffffffffffffffffffff16141561063457600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561068057600080fd5b600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561070d57600080fd5b600660008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561079657600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600660008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b600360009054906101000a900460ff1681565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156109d35780601f106109a8576101008083540402835291602001916109d3565b820191906000526020600020905b8154815290600101906020018083116109b657829003601f168201915b505050505081565b60056020528060005260406000206000915090505481565b60028054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610a895780601f10610a5e57610100808354040283529160200191610a89565b820191906000526020600020905b815481529060010190602001808311610a6c57829003601f168201915b505050505081565b60008273ffffffffffffffffffffffffffffffffffffffff161415610ab557600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610b0157600080fd5b600560008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011015610b8e57600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b60066020528160005260406000206020528060005260406000206000915091505054815600a165627a7a723058203eb700b31f6d7723be3f4a0dd07fc4ba166a17279e26a437227679b92bacb5a20029';
  282. /**
  283. * testUserAbi
  284. * User abi from https://github.com/BlockHR/contracts
  285. *
  286. * @var string
  287. */
  288. protected $testUserAbi = '[
  289. {
  290. "constant": false,
  291. "inputs": [
  292. {
  293. "name": "ethAddress",
  294. "type": "address"
  295. }
  296. ],
  297. "name": "getUser",
  298. "outputs": [
  299. {
  300. "name": "firstName",
  301. "type": "string"
  302. },
  303. {
  304. "name": "lastName",
  305. "type": "string"
  306. },
  307. {
  308. "name": "age",
  309. "type": "uint256"
  310. }
  311. ],
  312. "payable": false,
  313. "stateMutability": "nonpayable",
  314. "type": "function"
  315. },
  316. {
  317. "constant": false,
  318. "inputs": [
  319. {
  320. "name": "ethAddress",
  321. "type": "address"
  322. },
  323. {
  324. "name": "firstName",
  325. "type": "string"
  326. },
  327. {
  328. "name": "lastName",
  329. "type": "string"
  330. },
  331. {
  332. "name": "age",
  333. "type": "uint256"
  334. }
  335. ],
  336. "name": "addUser",
  337. "outputs": [],
  338. "payable": false,
  339. "stateMutability": "nonpayable",
  340. "type": "function"
  341. },
  342. {
  343. "inputs": [],
  344. "payable": false,
  345. "stateMutability": "nonpayable",
  346. "type": "constructor"
  347. },
  348. {
  349. "payable": false,
  350. "stateMutability": "nonpayable",
  351. "type": "fallback"
  352. },
  353. {
  354. "anonymous": false,
  355. "inputs": [
  356. {
  357. "indexed": true,
  358. "name": "ethAddress",
  359. "type": "address"
  360. },
  361. {
  362. "indexed": false,
  363. "name": "firstName",
  364. "type": "string"
  365. },
  366. {
  367. "indexed": false,
  368. "name": "lastName",
  369. "type": "string"
  370. },
  371. {
  372. "indexed": false,
  373. "name": "age",
  374. "type": "uint256"
  375. }
  376. ],
  377. "name": "AddUser",
  378. "type": "event"
  379. }
  380. ]';
  381. /**
  382. * testUserBytecode
  383. * User bytecode from https://github.com/BlockHR/contracts
  384. *
  385. * @var string
  386. */
  387. protected $testUserBytecode = '0x6060604052341561000f57600080fd5b336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055506108ee8061005e6000396000f30060606040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680636f77926b1461005c578063b3c2583514610181575b341561005757600080fd5b600080fd5b341561006757600080fd5b610093600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610249565b604051808060200180602001848152602001838103835286818151815260200191508051906020019080838360005b838110156100dd5780820151818401526020810190506100c2565b50505050905090810190601f16801561010a5780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015610143578082015181840152602081019050610128565b50505050905090810190601f1680156101705780820380516001836020036101000a031916815260200191505b509550505050505060405180910390f35b341561018c57600080fd5b610247600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091908035906020019091905050610545565b005b6102516107c7565b6102596107c7565b60003373ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff16141580156102e457506000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614155b156102ee5761053e565b6000600160008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060020154141561033e5761053e565b600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104145780601f106103e957610100808354040283529160200191610414565b820191906000526020600020905b8154815290600101906020018083116103f757829003601f168201915b50505050509250600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206001018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156104f15780601f106104c6576101008083540402835291602001916104f1565b820191906000526020600020905b8154815290600101906020018083116104d457829003601f168201915b50505050509150600160008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206002015490505b9193909250565b61054d6107db565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614156107c0576000600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600201541415156105f3576107bf565b8381600001819052508281602001819052508181604001818152505080600160008773ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082015181600001908051906020019061066b929190610809565b506020820151816001019080519060200190610688929190610809565b50604082015181600201559050508473ffffffffffffffffffffffffffffffffffffffff167f2771be550edc032daf255a8987e0164bcfad0a5b97238d8961f9c5e38fa3e4f3858585604051808060200180602001848152602001838103835286818151815260200191508051906020019080838360005b8381101561071b578082015181840152602081019050610700565b50505050905090810190601f1680156107485780820380516001836020036101000a031916815260200191505b50838103825285818151815260200191508051906020019080838360005b83811015610781578082015181840152602081019050610766565b50505050905090810190601f1680156107ae5780820380516001836020036101000a031916815260200191505b509550505050505060405180910390a25b5b5050505050565b602060405190810160405280600081525090565b6060604051908101604052806107ef610889565b81526020016107fc610889565b8152602001600081525090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061084a57805160ff1916838001178555610878565b82800160010185558215610878579182015b8281111561087757825182559160200191906001019061085c565b5b509050610885919061089d565b5090565b602060405190810160405280600081525090565b6108bf91905b808211156108bb5760008160009055506001016108a3565b5090565b905600a165627a7a72305820f4dd7dc4c22792dec463ec68973ad2ed12d5994e96a9e7b34db512d6e38143090029';
  388. /**
  389. * accounts
  390. *
  391. * @var array
  392. */
  393. protected $accounts;
  394. /**
  395. * contractAddress
  396. *
  397. * @var string
  398. */
  399. protected $contractAddress;
  400. /**
  401. * setUp
  402. *
  403. * @return void
  404. */
  405. public function setUp()
  406. {
  407. parent::setUp();
  408. $this->contract = new Contract($this->web3->provider, $this->testAbi);
  409. $this->contract->eth->accounts(function ($err, $accounts) {
  410. if ($err === null) {
  411. if (isset($accounts)) {
  412. $this->accounts = $accounts;
  413. return;
  414. }
  415. }
  416. });
  417. }
  418. /**
  419. * testInstance
  420. *
  421. * @return void
  422. */
  423. public function testInstance()
  424. {
  425. $contract = new Contract($this->testHost, $this->testAbi);
  426. $this->assertTrue($contract->provider instanceof HttpProvider);
  427. $this->assertTrue($contract->provider->requestManager instanceof RequestManager);
  428. }
  429. /**
  430. * testSetProvider
  431. *
  432. * @return void
  433. */
  434. public function testSetProvider()
  435. {
  436. $contract = $this->contract;
  437. $requestManager = new HttpRequestManager('http://localhost:8545');
  438. $contract->provider = new HttpProvider($requestManager);
  439. $this->assertEquals($contract->provider->requestManager->host, 'http://localhost:8545');
  440. $contract->provider = null;
  441. $this->assertEquals($contract->provider->requestManager->host, 'http://localhost:8545');
  442. }
  443. /**
  444. * testDeploy
  445. *
  446. * @return void
  447. */
  448. public function testDeploy()
  449. {
  450. $contract = $this->contract;
  451. if (!isset($this->accounts[0])) {
  452. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  453. } else {
  454. $account = $this->accounts[0];
  455. }
  456. $contract->bytecode($this->testBytecode)->new(1000000, 'Game Token', 1, 'GT', [
  457. 'from' => $account,
  458. 'gas' => '0x200b20'
  459. ], function ($err, $result) use ($contract) {
  460. if ($err !== null) {
  461. return $this->fail($err->getMessage());
  462. }
  463. if ($result) {
  464. echo "\nTransaction has made:) id: " . $result . "\n";
  465. }
  466. $transactionId = $result;
  467. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  468. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  469. if ($err !== null) {
  470. return $this->fail($err);
  471. }
  472. if ($transaction) {
  473. $this->contractAddress = $transaction->contractAddress;
  474. echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n";
  475. }
  476. });
  477. });
  478. }
  479. /**
  480. * testSend
  481. *
  482. * @return void
  483. */
  484. public function testSend()
  485. {
  486. $contract = $this->contract;
  487. if (!isset($this->accounts[0])) {
  488. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  489. } else {
  490. $fromAccount = $this->accounts[0];
  491. }
  492. if (!isset($this->accounts[1])) {
  493. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  494. } else {
  495. $toAccount = $this->accounts[1];
  496. }
  497. $contract->bytecode($this->testBytecode)->new(1000000, 'Game Token', 1, 'GT', [
  498. 'from' => $fromAccount,
  499. 'gas' => '0x200b20'
  500. ], function ($err, $result) use ($contract) {
  501. if ($err !== null) {
  502. return $this->fail($err->getMessage());
  503. }
  504. if ($result) {
  505. echo "\nTransaction has made:) id: " . $result . "\n";
  506. }
  507. $transactionId = $result;
  508. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  509. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  510. if ($err !== null) {
  511. return $this->fail($err);
  512. }
  513. if ($transaction) {
  514. $this->contractAddress = $transaction->contractAddress;
  515. echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n";
  516. }
  517. });
  518. });
  519. if (!isset($this->contractAddress)) {
  520. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  521. }
  522. $contract->at($this->contractAddress)->send('transfer', $toAccount, 16, [
  523. 'from' => $fromAccount,
  524. 'gas' => '0x200b20'
  525. ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
  526. if ($err !== null) {
  527. return $this->fail($err->getMessage());
  528. }
  529. if ($result) {
  530. echo "\nTransaction has made:) id: " . $result . "\n";
  531. }
  532. $transactionId = $result;
  533. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  534. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount, $contract) {
  535. if ($err !== null) {
  536. return $this->fail($err);
  537. }
  538. if ($transaction) {
  539. $topics = $transaction->logs[0]->topics;
  540. echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n";
  541. // validate topics
  542. $this->assertEquals($contract->ethabi->encodeEventSignature($this->contract->events['Transfer']), $topics[0]);
  543. $this->assertEquals('0x' . IntegerFormatter::format($fromAccount), $topics[1]);
  544. $this->assertEquals('0x' . IntegerFormatter::format($toAccount), $topics[2]);
  545. }
  546. });
  547. });
  548. }
  549. /**
  550. * testCall
  551. *
  552. * @return void
  553. */
  554. public function testCall()
  555. {
  556. $contract = $this->contract;
  557. if (!isset($this->accounts[0])) {
  558. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  559. } else {
  560. $fromAccount = $this->accounts[0];
  561. }
  562. if (!isset($this->accounts[1])) {
  563. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  564. } else {
  565. $toAccount = $this->accounts[1];
  566. }
  567. $contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
  568. 'from' => $fromAccount,
  569. 'gas' => '0x200b20'
  570. ], function ($err, $result) use ($contract) {
  571. if ($err !== null) {
  572. return $this->fail($err->getMessage());
  573. }
  574. if ($result) {
  575. echo "\nTransaction has made:) id: " . $result . "\n";
  576. }
  577. $transactionId = $result;
  578. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  579. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  580. if ($err !== null) {
  581. return $this->fail($err);
  582. }
  583. if ($transaction) {
  584. $this->contractAddress = $transaction->contractAddress;
  585. echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n";
  586. }
  587. });
  588. });
  589. if (!isset($this->contractAddress)) {
  590. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  591. }
  592. $contract->at($this->contractAddress)->call('balanceOf', $fromAccount, [
  593. 'from' => $fromAccount
  594. ], function ($err, $result) use ($contract) {
  595. if ($err !== null) {
  596. return $this->fail($err->getMessage());
  597. }
  598. if (isset($result)) {
  599. // $bn = Utils::toBn($result);
  600. // $this->assertEquals($bn->toString(), '10000', 'Balance should be 10000.');
  601. $this->assertTrue($result !== null);
  602. }
  603. });
  604. }
  605. /**
  606. * testEstimateGas
  607. *
  608. * @return void
  609. */
  610. public function testEstimateGas()
  611. {
  612. $contract = $this->contract;
  613. if (!isset($this->accounts[0])) {
  614. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  615. } else {
  616. $fromAccount = $this->accounts[0];
  617. }
  618. if (!isset($this->accounts[1])) {
  619. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  620. } else {
  621. $toAccount = $this->accounts[1];
  622. }
  623. $contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
  624. 'from' => $fromAccount,
  625. 'gas' => '0x200b20'
  626. ], function ($err, $result) use ($contract) {
  627. if ($err !== null) {
  628. return $this->fail($err->getMessage());
  629. }
  630. if ($result) {
  631. echo "\nTransaction has made:) id: " . $result . "\n";
  632. }
  633. $transactionId = $result;
  634. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  635. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  636. if ($err !== null) {
  637. return $this->fail($err);
  638. }
  639. if ($transaction) {
  640. $this->contractAddress = $transaction->contractAddress;
  641. echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n";
  642. }
  643. });
  644. });
  645. $contract->bytecode($this->testBytecode)->estimateGas(10000, 'Game Token', 1, 'GT', [
  646. 'from' => $fromAccount,
  647. 'gas' => '0x200b20'
  648. ], function ($err, $result) use ($contract) {
  649. if ($err !== null) {
  650. return $this->fail($err->getMessage());
  651. }
  652. if (isset($result)) {
  653. echo "\nEstimate gas: " . $result->toString() . "\n";
  654. $this->assertTrue($result !== null);
  655. }
  656. });
  657. if (!isset($this->contractAddress)) {
  658. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  659. }
  660. $contract->at($this->contractAddress)->estimateGas('balanceOf', $fromAccount, [
  661. 'from' => $fromAccount
  662. ], function ($err, $result) use ($contract) {
  663. if ($err !== null) {
  664. // infura api gg
  665. return $this->assertTrue($err !== null);
  666. }
  667. if (isset($result)) {
  668. echo "\nEstimate gas: " . $result->toString() . "\n";
  669. $this->assertTrue($result !== null);
  670. }
  671. });
  672. }
  673. /**
  674. * testGetData
  675. *
  676. * @return void
  677. */
  678. public function testGetData()
  679. {
  680. $contract = $this->contract;
  681. if (!isset($this->accounts[0])) {
  682. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  683. } else {
  684. $fromAccount = $this->accounts[0];
  685. }
  686. if (!isset($this->accounts[1])) {
  687. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  688. } else {
  689. $toAccount = $this->accounts[1];
  690. }
  691. $contract->bytecode($this->testBytecode)->new(10000, 'Game Token', 1, 'GT', [
  692. 'from' => $fromAccount,
  693. 'gas' => '0x200b20'
  694. ], function ($err, $result) use ($contract) {
  695. if ($err !== null) {
  696. return $this->fail($err->getMessage());
  697. }
  698. if ($result) {
  699. echo "\nTransaction has made:) id: " . $result . "\n";
  700. }
  701. $transactionId = $result;
  702. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  703. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  704. if ($err !== null) {
  705. return $this->fail($err);
  706. }
  707. if ($transaction) {
  708. $this->contractAddress = $transaction->contractAddress;
  709. echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n";
  710. }
  711. });
  712. });
  713. $constructorData = $contract->bytecode($this->testBytecode)->getData(10000, 'Game Token', 1, 'GT');
  714. $this->assertEquals('60606040526040805190810160405280600581526020017f45524332300000000000000000000000000000000000000000000000000000008152506000908051906020019061004f92919061012f565b50341561005b57600080fd5b604051610ec5380380610ec58339810160405280805190602001909190805182019190602001805190602001909190805182019190505083600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508360048190555082600190805190602001906100f392919061012f565b50806002908051906020019061010a92919061012f565b5081600360006101000a81548160ff021916908360ff160217905550505050506101d4565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061017057805160ff191683800117855561019e565b8280016001018555821561019e579182015b8281111561019d578251825591602001919060010190610182565b5b5090506101ab91906101af565b5090565b6101d191905b808211156101cd5760008160009055506001016101b5565b5090565b90565b610ce2806101e36000396000f3006060604052600436106100a4576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100a9578063095ea7b31461013757806318160ddd1461019157806323b872dd146101ba578063313ce567146102335780635a3b7e421461026257806370a08231146102f057806395d89b411461033d578063a9059cbb146103cb578063dd62ed3e1461040d575b600080fd5b34156100b457600080fd5b6100bc610479565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100fc5780820151818401526020810190506100e1565b50505050905090810190601f1680156101295780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561014257600080fd5b610177600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610517565b604051808215151515815260200191505060405180910390f35b341561019c57600080fd5b6101a4610609565b6040518082815260200191505060405180910390f35b34156101c557600080fd5b610219600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803590602001909190505061060f565b604051808215151515815260200191505060405180910390f35b341561023e57600080fd5b61024661092a565b604051808260ff1660ff16815260200191505060405180910390f35b341561026d57600080fd5b61027561093d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156102b557808201518184015260208101905061029a565b50505050905090810190601f1680156102e25780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156102fb57600080fd5b610327600480803573ffffffffffffffffffffffffffffffffffffffff169060200190919050506109db565b6040518082815260200191505060405180910390f35b341561034857600080fd5b6103506109f3565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610390578082015181840152602081019050610375565b50505050905090810190601f1680156103bd5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34156103d657600080fd5b61040b600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610a91565b005b341561041857600080fd5b610463600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610c91565b6040518082815260200191505060405180910390f35b60018054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561050f5780601f106104e45761010080835404028352916020019161050f565b820191906000526020600020905b8154815290600101906020018083116104f257829003601f168201915b505050505081565b600081600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925846040518082815260200191505060405180910390a36001905092915050565b60045481565b6000808373ffffffffffffffffffffffffffffffffffffffff16141561063457600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561068057600080fd5b600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101561070d57600080fd5b600660008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482111561079657600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555081600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254019250508190555081600660008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825403925050819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b600360009054906101000a900460ff1681565b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156109d35780601f106109a8576101008083540402835291602001916109d3565b820191906000526020600020905b8154815290600101906020018083116109b657829003601f168201915b505050505081565b60056020528060005260406000206000915090505481565b60028054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610a895780601f10610a5e57610100808354040283529160200191610a89565b820191906000526020600020905b815481529060010190602001808311610a6c57829003601f168201915b505050505081565b60008273ffffffffffffffffffffffffffffffffffffffff161415610ab557600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015610b0157600080fd5b600560008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054011015610b8e57600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b60066020528160005260406000206020528060005260406000206000915091505054815600a165627a7a723058203eb700b31f6d7723be3f4a0dd07fc4ba166a17279e26a437227679b92bacb5a2002900000000000000000000000000000000000000000000000000000000000027100000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000a47616d6520546f6b656e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000024754000000000000000000000000000000000000000000000000000000000000', $constructorData);
  715. if (!isset($this->contractAddress)) {
  716. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  717. }
  718. $balanceOfData = $contract->at($this->contractAddress)->getData('balanceOf', $fromAccount);
  719. $this->assertEquals('70a08231000000000000000000000000' . Utils::stripZero($fromAccount), $balanceOfData);
  720. }
  721. /**
  722. * testDecodeMethodReturn
  723. *
  724. * @return void
  725. */
  726. public function testDecodeMethodReturn()
  727. {
  728. $contract = $this->contract;
  729. $contract->abi($this->testUserAbi);
  730. if (!isset($this->accounts[0])) {
  731. $fromAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  732. } else {
  733. $fromAccount = $this->accounts[0];
  734. }
  735. if (!isset($this->accounts[1])) {
  736. $toAccount = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  737. } else {
  738. $toAccount = $this->accounts[1];
  739. }
  740. // Deploy user contract.
  741. $contract->bytecode($this->testUserBytecode)->new([
  742. 'from' => $fromAccount,
  743. 'gas' => '0x200b20'
  744. ], function ($err, $result) use ($contract) {
  745. if ($err !== null) {
  746. return $this->fail($err->getMessage());
  747. }
  748. if ($result) {
  749. echo "\nTransaction has made:) id: " . $result . "\n";
  750. }
  751. $transactionId = $result;
  752. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  753. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) {
  754. if ($err !== null) {
  755. return $this->fail($err);
  756. }
  757. if ($transaction) {
  758. $this->contractAddress = $transaction->contractAddress;
  759. echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n";
  760. }
  761. });
  762. });
  763. if (!isset($this->contractAddress)) {
  764. $this->contractAddress = '0x407d73d8a49eeb85d32cf465507dd71d507100c2';
  765. }
  766. // Add user.
  767. $contract->at($this->contractAddress)->send('addUser', $toAccount, 'Peter', 'Lai', 18, [
  768. 'from' => $fromAccount,
  769. 'gas' => '0x200b20'
  770. ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
  771. if ($err !== null) {
  772. return $this->fail($err->getMessage());
  773. }
  774. if ($result) {
  775. echo "\nTransaction has made:) id: " . $result . "\n";
  776. }
  777. $transactionId = $result;
  778. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  779. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use ($fromAccount, $toAccount, $contract) {
  780. if ($err !== null) {
  781. return $this->fail($err);
  782. }
  783. if ($transaction) {
  784. $topics = $transaction->logs[0]->topics;
  785. echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n";
  786. // validate topics
  787. $this->assertEquals($contract->ethabi->encodeEventSignature($this->contract->events['AddUser']), $topics[0]);
  788. $this->assertEquals('0x' . IntegerFormatter::format($toAccount), $topics[1]);
  789. }
  790. });
  791. });
  792. // Get user.
  793. $contract->call('getUser', $toAccount, [
  794. 'from' => $fromAccount
  795. ], function ($err, $result) use ($contract, $fromAccount, $toAccount) {
  796. if ($err !== null) {
  797. return $this->fail($err->getMessage());
  798. }
  799. if ($result) {
  800. $this->assertEquals($result['firstName'], 'Peter');
  801. $this->assertEquals($result['lastName'], 'Lai');
  802. $this->assertEquals($result['age']->toString(), '18');
  803. }
  804. });
  805. }
  806. /**
  807. * testIssue85
  808. *
  809. * @return void
  810. */
  811. public function testIssue85()
  812. {
  813. $bytecode = '0x608060405234801561001057600080fd5b50610a36806100206000396000f3006080604052600436106100af576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680630bcd3b33146100b45780631f9030371461014457806325f35c36146101775780632d1be94d146102735780638b84925b146102a2578063a15e05fd146102cd578063a5f807cc1461030f578063d07326681461039f578063ded516d21461043e578063f1d0876a146104ad578063f5c2e88a1461054c575b600080fd5b3480156100c057600080fd5b506100c96105dc565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101095780820151818401526020810190506100ee565b50505050905090810190601f1680156101365780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561015057600080fd5b5061015961061e565b60405180826000191660001916815260200191505060405180910390f35b34801561018357600080fd5b5061018c61064b565b604051808060200180602001838103835285818151815260200191508051906020019080838360005b838110156101d05780820151818401526020810190506101b5565b50505050905090810190601f1680156101fd5780820380516001836020036101000a031916815260200191505b50838103825284818151815260200191508051906020019080838360005b8381101561023657808201518184015260208101905061021b565b50505050905090810190601f1680156102635780820380516001836020036101000a031916815260200191505b5094505050505060405180910390f35b34801561027f57600080fd5b506102886106cd565b604051808215151515815260200191505060405180910390f35b3480156102ae57600080fd5b506102b76106db565b6040518082815260200191505060405180910390f35b3480156102d957600080fd5b506102e26106e9565b60405180836000191660001916815260200182600019166000191681526020019250505060405180910390f35b34801561031b57600080fd5b50610324610741565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610364578082015181840152602081019050610349565b50505050905090810190601f1680156103915780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b3480156103ab57600080fd5b506103b461076a565b60405180806020018360001916600019168152602001828103825284818151815260200191508051906020019080838360005b838110156104025780820151818401526020810190506103e7565b50505050905090810190601f16801561042f5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561044a57600080fd5b506104536107d9565b60405180827effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff19167effffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff1916815260200191505060405180910390f35b3480156104b957600080fd5b506104c2610806565b60405180836000191660001916815260200180602001828103825283818151815260200191508051906020019080838360005b838110156105105780820151818401526020810190506104f5565b50505050905090810190601f16801561053d5780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b34801561055857600080fd5b50610561610875565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156105a1578082015181840152602081019050610586565b50505050905090810190601f1680156105ce5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6060806040805190810160405280600381526020017f616263000000000000000000000000000000000000000000000000000000000081525090508091505090565b6000807f616263000000000000000000000000000000000000000000000000000000000090508091505090565b6060806060806040805190810160405280600381526020017f616263000000000000000000000000000000000000000000000000000000000081525091506040805190810160405280600381526020017f78797a0000000000000000000000000000000000000000000000000000000000815250905081819350935050509091565b600080600190508091505090565b600080606390508091505090565b6000806000807f616263000000000000000000000000000000000000000000000000000000000091507f78797a0000000000000000000000000000000000000000000000000000000000905081819350935050509091565b6060806101606040519081016040528061012c81526020016108df61012c913990508091505090565b60606000606060006040805190810160405280600381526020017f616263000000000000000000000000000000000000000000000000000000000081525091507f78797a0000000000000000000000000000000000000000000000000000000000905081819350935050509091565b6000807f610000000000000000000000000000000000000000000000000000000000000090508091505090565b60006060600060607f78797a000000000000000000000000000000000000000000000000000000000091506040805190810160405280600381526020017f6162630000000000000000000000000000000000000000000000000000000000815250905081819350935050509091565b606080606060405190810160405280603c81526020017f616263616263616263636162636162636162636361626361626361626363616281526020017f6361626361626363616263616263616263636162636162636162636300000000815250905080915050905600616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363616263616263616263636162636162636162636361626361626361626363a165627a7a7230582020ee9e6b05918d0df987776ee24808f4dd1c522806bf9fa8f4336c93f6b0ec050029';
  814. $abi = '[
  815. {
  816. "constant": true,
  817. "inputs": [],
  818. "name": "getBytes",
  819. "outputs": [
  820. {
  821. "name": "",
  822. "type": "bytes"
  823. }
  824. ],
  825. "payable": false,
  826. "stateMutability": "pure",
  827. "type": "function"
  828. },
  829. {
  830. "constant": true,
  831. "inputs": [],
  832. "name": "getBytes32",
  833. "outputs": [
  834. {
  835. "name": "",
  836. "type": "bytes32"
  837. }
  838. ],
  839. "payable": false,
  840. "stateMutability": "pure",
  841. "type": "function"
  842. },
  843. {
  844. "constant": true,
  845. "inputs": [],
  846. "name": "getDynamicData",
  847. "outputs": [
  848. {
  849. "name": "",
  850. "type": "bytes"
  851. },
  852. {
  853. "name": "",
  854. "type": "bytes"
  855. }
  856. ],
  857. "payable": false,
  858. "stateMutability": "pure",
  859. "type": "function"
  860. },
  861. {
  862. "constant": true,
  863. "inputs": [],
  864. "name": "getBoolean",
  865. "outputs": [
  866. {
  867. "name": "",
  868. "type": "bool"
  869. }
  870. ],
  871. "payable": false,
  872. "stateMutability": "pure",
  873. "type": "function"
  874. },
  875. {
  876. "constant": true,
  877. "inputs": [],
  878. "name": "getInteger",
  879. "outputs": [
  880. {
  881. "name": "",
  882. "type": "int256"
  883. }
  884. ],
  885. "payable": false,
  886. "stateMutability": "pure",
  887. "type": "function"
  888. },
  889. {
  890. "constant": true,
  891. "inputs": [],
  892. "name": "getTwoBytes32",
  893. "outputs": [
  894. {
  895. "name": "",
  896. "type": "bytes32"
  897. },
  898. {
  899. "name": "",
  900. "type": "bytes32"
  901. }
  902. ],
  903. "payable": false,
  904. "stateMutability": "pure",
  905. "type": "function"
  906. },
  907. {
  908. "constant": true,
  909. "inputs": [],
  910. "name": "getBytesVeryLong",
  911. "outputs": [
  912. {
  913. "name": "",
  914. "type": "bytes"
  915. }
  916. ],
  917. "payable": false,
  918. "stateMutability": "pure",
  919. "type": "function"
  920. },
  921. {
  922. "constant": true,
  923. "inputs": [],
  924. "name": "getDynamicDataMixedOne",
  925. "outputs": [
  926. {
  927. "name": "",
  928. "type": "bytes"
  929. },
  930. {
  931. "name": "",
  932. "type": "bytes32"
  933. }
  934. ],
  935. "payable": false,
  936. "stateMutability": "pure",
  937. "type": "function"
  938. },
  939. {
  940. "constant": true,
  941. "inputs": [],
  942. "name": "getBytes1",
  943. "outputs": [
  944. {
  945. "name": "",
  946. "type": "bytes1"
  947. }
  948. ],
  949. "payable": false,
  950. "stateMutability": "pure",
  951. "type": "function"
  952. },
  953. {
  954. "constant": true,
  955. "inputs": [],
  956. "name": "getDynamicDataMixedTwo",
  957. "outputs": [
  958. {
  959. "name": "",
  960. "type": "bytes32"
  961. },
  962. {
  963. "name": "",
  964. "type": "bytes"
  965. }
  966. ],
  967. "payable": false,
  968. "stateMutability": "pure",
  969. "type": "function"
  970. },
  971. {
  972. "constant": true,
  973. "inputs": [],
  974. "name": "getBytesLong",
  975. "outputs": [
  976. {
  977. "name": "",
  978. "type": "bytes"
  979. }
  980. ],
  981. "payable": false,
  982. "stateMutability": "pure",
  983. "type": "function"
  984. },
  985. {
  986. "inputs": [],
  987. "payable": false,
  988. "stateMutability": "nonpayable",
  989. "type": "constructor"
  990. }
  991. ]';
  992. $functions = [
  993. [
  994. 'name' => 'getBoolean',
  995. 'test' => function ($value) {
  996. return is_bool($value);
  997. }
  998. ], [
  999. 'name' => 'getInteger',
  1000. 'test' => function ($value) {
  1001. return ($value instanceof BigNumber);
  1002. }
  1003. ], [
  1004. 'name' => 'getBytes1',
  1005. 'test' => function ($value) {
  1006. return Utils::isHex($value) && mb_strlen($value) === 4;
  1007. }
  1008. ], [
  1009. 'name' => 'getBytes32',
  1010. 'test' => function ($value) {
  1011. return Utils::isHex($value) && mb_strlen($value) === 66;
  1012. }
  1013. ], [
  1014. 'name' => 'getBytesLong',
  1015. 'test' => function ($value) {
  1016. return Utils::isHex($value);
  1017. }
  1018. ], [
  1019. 'name' => 'getDynamicData',
  1020. 'test' => function ($value) {
  1021. return Utils::isHex($value);
  1022. }
  1023. ], [
  1024. 'name' => 'getDynamicDataMixedOne',
  1025. 'test' => function ($value) {
  1026. return Utils::isHex($value);
  1027. }
  1028. ], [
  1029. 'name' => 'getDynamicDataMixedTwo',
  1030. 'test' => function ($value) {
  1031. return Utils::isHex($value);
  1032. }
  1033. ], [
  1034. 'name' => 'getTwoBytes32',
  1035. 'test' => function ($value) {
  1036. return Utils::isHex($value) && mb_strlen($value) === 66;
  1037. }
  1038. ]
  1039. ];
  1040. $contractAddress = "";
  1041. if (!isset($this->accounts[0])) {
  1042. $account = '0x407d73d8a49eeb85d32cf465507dd71d507100c1';
  1043. } else {
  1044. $account = $this->accounts[0];
  1045. }
  1046. $contract = new Contract($this->web3->provider, $abi);
  1047. $contract->bytecode($bytecode)->new([
  1048. 'from' => $account,
  1049. 'gas' => '0x200b20'
  1050. ], function ($err, $result) use ($contract, &$contractAddress) {
  1051. if ($err !== null) {
  1052. return $this->fail($err->getMessage());
  1053. }
  1054. if ($result) {
  1055. echo "\nTransaction has made:) id: " . $result . "\n";
  1056. }
  1057. $transactionId = $result;
  1058. $this->assertTrue((preg_match('/^0x[a-f0-9]{64}$/', $transactionId) === 1));
  1059. $contract->eth->getTransactionReceipt($transactionId, function ($err, $transaction) use (&$contractAddress) {
  1060. if ($err !== null) {
  1061. return $this->fail($err);
  1062. }
  1063. if ($transaction) {
  1064. $contractAddress = $transaction->contractAddress;
  1065. echo "\nTransaction has mind:) block number: " . $transaction->blockNumber . "\n";
  1066. }
  1067. });
  1068. });
  1069. $contract->at($contractAddress);
  1070. foreach ($functions as $function) {
  1071. $contract->call($function['name'], [], function ($err, $res) use ($function) {
  1072. if ($err !== null) {
  1073. echo 'Error when call ' . $function['name'] . '. Message: ' . $err->getMessage() . "\n";
  1074. return;
  1075. }
  1076. foreach ($res as $r) {
  1077. if (!call_user_func($function['test'], $r)) {
  1078. var_dump($r);
  1079. }
  1080. $this->assertTrue(call_user_func($function['test'], $r));
  1081. }
  1082. });
  1083. }
  1084. }
  1085. }