Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
bb6a407
add unnamed using flipper template
ltfschoen Jun 10, 2023
231d7ae
wip
ltfschoen Jun 11, 2023
8fdf3bb
wip
ltfschoen Jun 11, 2023
3faf53e
resolve some errors
ltfschoen Jun 11, 2023
e5e7720
fix all errors
ltfschoen Jun 12, 2023
14b04a5
use raw bytes instead of hash type so can access length of block hash
ltfschoen Jun 12, 2023
7d035fe
save and emit entropy of coin1 and coin2
ltfschoen Jun 12, 2023
f1b5c55
wip stuck stubbing blocknumber from ink environment
ltfschoen Jun 13, 2023
56aaa29
use set_block_number from pr to upstream working
ltfschoen Jun 13, 2023
10a08f8
fix contracts using String but already uploaded error
ltfschoen Jun 15, 2023
88782c0
working. allow toggle --skip-dry-run with bulk comment
ltfschoen Jun 15, 2023
aeb12bc
add basic_contract_caller from ink-examples
ltfschoen Jun 15, 2023
ed4290e
add basic_contract_caller all working
ltfschoen Jun 16, 2023
931fe20
remove get message from main contract and do not call it generates error
ltfschoen Jun 16, 2023
4217f6b
restore get method in main contract
ltfschoen Jun 16, 2023
455c222
convert to CreateBuilder and CallBuilder but with error when call via…
ltfschoen Jun 16, 2023
e5f449a
fix basic-contract-caller
ltfschoen Jun 17, 2023
5686da3
call via main but no Builder not support tuple return Decode(Error)
ltfschoen Jun 18, 2023
bf4e6f2
fix Decode(Error) caused by assertion failing since call from contrac…
ltfschoen Jun 18, 2023
3433a89
demo handling errors
ltfschoen Jun 19, 2023
75f5370
fix LangError
ltfschoen Jun 19, 2023
4ae3e63
setup moonbeam MyToken template
ltfschoen Jun 20, 2023
39fcd92
ipsp22
ltfschoen Jun 20, 2023
73750e6
update readme and add ipsp22 script
ltfschoen Jun 21, 2023
768361f
add evm example pending astar response
ltfschoen Jun 22, 2023
e98be60
Merge pull request #17 from ltfschoen/moonbeam
ltfschoen Jun 22, 2023
8887442
update precompile but DeploymentError RandonNumber hit a require or r…
ltfschoen Jun 23, 2023
fd5624c
incorporate feedback from moonbuilders
ltfschoen Jun 23, 2023
d68f6d5
fix so only use randomness after instantiated Randomness
ltfschoen Jun 24, 2023
4962720
try use console log
ltfschoen Jun 24, 2023
2f66366
wip errors due to https://github.com/trufflesuite/truffle/issues/3468…
ltfschoen Jun 24, 2023
4df48da
stuck on error ProviderError 32603 calling requestRandomness
ltfschoen Jun 25, 2023
30db35e
StatusError in tx for requestRandomness
ltfschoen Jun 25, 2023
df8ac88
try change to Websocket endpoint but error Web3WSProviderError: Reque…
ltfschoen Jun 26, 2023
84a6508
MethodNotImplementedError: The method you're trying to call is not im…
ltfschoen Jun 26, 2023
49f7632
timeouts
ltfschoen Jun 26, 2023
2efa2e6
works up to getting requestId but still often get error PollingBlockT…
ltfschoen Jun 26, 2023
6d85e28
tests working enough to generate random number
ltfschoen Jun 27, 2023
5b96de4
update readme
ltfschoen Jun 27, 2023
3b01dd0
add vrf template contracts
ltfschoen Jun 27, 2023
fb3d91f
wip Chainlink VRF Sepolia
ltfschoen Jun 27, 2023
2a2cd51
Uncaught Error: invalid 1st argument: block_number value was not vali…
ltfschoen Jun 27, 2023
dd0d6ab
success deploying chainlink VRF to Sepolia
ltfschoen Jun 27, 2023
f1d3067
Merge pull request #20 from ltfschoen/chainlink
ltfschoen Jun 27, 2023
039630f
trying to interact with vrf to play game
ltfschoen Jun 28, 2023
e8992e4
able to get instance of vrf contract
ltfschoen Jun 28, 2023
3a8541b
fix use of correct alchemy key
ltfschoen Jun 28, 2023
5d3b2c3
fix migration, requires min deposit of 1 eth to not error
ltfschoen Jun 29, 2023
34e8cf5
fix use of alchemy but error when creating chainlink subscription to …
ltfschoen Jul 2, 2023
a7a7971
wait a few blocks before get rolled value for player
ltfschoen Jul 2, 2023
79af150
show current error
ltfschoen Jul 2, 2023
991b0cd
wip
ltfschoen Jul 2, 2023
4da18cf
insufficient funds for gas * price + value
ltfschoen Jul 3, 2023
78c4c52
crashes on requestRandomness. fixes error insufficient funds for intr…
ltfschoen Jul 3, 2023
74eca77
wip
ltfschoen Jul 3, 2023
4e136dc
fix vrf
ltfschoen Jul 5, 2023
5dde11b
wip
ltfschoen Jul 6, 2023
0d6b01a
split flipper from moonbase vrf example
ltfschoen Jul 6, 2023
5f827f9
update readme
ltfschoen Jul 6, 2023
b724f57
rename flipper folder to randomness
ltfschoen Jul 6, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
split flipper from moonbase vrf example
  • Loading branch information
ltfschoen committed Jul 6, 2023
commit 0d6b01a92a4fdc1fccef0fb6726d5944376b2243
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
* [Setup Docker Container](#setup-container)
* [Run Cargo Contracts Node in Docker Container](#run-cargo-contracts-node)
* Build & Upload
* [**Quickstart** Build & Upload ink! Rust Flipper Smart Contract to Local Testnet (using Cargo Contract)](#quick-build-upload)
* [**Quickstart** Build & Upload ink! Rust "Basic Contract Caller" Smart Contract to Local Testnet (using Cargo Contract)](#quick-basic-contract-caller)
* [**Quickstart** Build & Upload ink! Rust "IPSP22" Smart Contract to Local Testnet (using Cargo Contract)](#quick-ipsp22)
* [**Quickstart** Build & Upload ink! Rust "Unnamed" Smart Contract to Local Testnet (using Cargo Contract)](#quick-unnamed)
* [Build & Upload Chainlink VRFD20 Randomness Solidity Smart Contract to Ethereum Sepolia (using Truffle)](#vrfd20)
* [Build & Upload ink! Rust Flipper Smart Contract to Local Testnet (using Cargo Contract)](#build-upload)
* [Build & Upload ink! Rust Flipper Smart Contract to Local Testnet (using Swanky CLI)](#build-upload-swanky)
* [**Quickstart** Build & Upload "Flipper" ink! Rust Smart Contract to Local Testnet (using Cargo Contract)](#quick-build-upload)
* [**Quickstart** Build & Upload "Basic Contract Caller" ink! Rust Smart Contract to Local Testnet (using Cargo Contract)](#quick-basic-contract-caller)
* [**Quickstart** Build & Upload "IPSP22" ink! Rust Smart Contract to Local Testnet (using Cargo Contract)](#quick-ipsp22)
* [**Quickstart** Build & Upload "Unnamed" ink! Rust Smart Contract to Local Testnet (using Cargo Contract)](#quick-unnamed)
* [Build & Upload Moonbeam VRF Randomness Precompile Solidity Smart Contract to Moonbase Alpha Testnet (using Truffle)](#moonbase-vrf)
* [Build & Upload Chainlink VRFD20 Randomness Solidity Smart Contract to Ethereum Sepolia Testnet (using Truffle)](#vrfd20)
* [Build & Upload "Flipper" ink! Rust Smart Contract to Local Testnet (using Cargo Contract)](#build-upload)
* [Build & Upload "Flipper" ink! Rust Smart Contract to Local Testnet (using Swanky CLI)](#build-upload-swanky)
* Interact
* [Interact with ink! Python Smart Contract](#interact-python)
* [Interact with ink! Rust Flipper Smart Contract using Polkadot.js API](#interact-polkadot-js-flipper)
Expand Down Expand Up @@ -193,6 +194,10 @@ docker exec -it ink /app/docker/quickstart.sh
./docker/quickstart-unnamed.sh
```

### Build & Upload Moonbeam VRF Randomness Precompile Solidity Smart Contract to Moonbase Alpha Testnet (using Truffle) <a id="moonbase-vrf"></a>

* Follow the instructions in the [VRF example README](./dapps/evm2/flipper/README.md)

### Build & Upload Chainlink VRFD20 Randomness Solidity Smart Contract to Ethereum Sepolia Testnet (using Truffle) <a id="vrfd20"></a>

* Follow the instructions in the [VRF20 example README](./dapps/evm2/flipper/README.md)
Expand Down
9 changes: 4 additions & 5 deletions dapps/evm2/flipper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ shasum -a 256 moonkey
* Migrate full `truffle migrate --reset --compile-all --network moonbase`
* Migrate full `truffle migrate --reset --compile-all --network sepolia`
* Test
* **Important:** It is necessary to first comment-out the code that is **not being compiled** in 2_deploy_contracts.j
* `truffle test ./test/test_Flipper.js --verbose-rpc --network moonbase`
* `truffle test ./test/test_MoonbaseVRF.js --verbose-rpc --network moonbase`
* `truffle test ./test/test_ChainlinkVRF.js --network sepolia`
* Verify Contract - Moonbase Precompile

Expand Down Expand Up @@ -159,10 +158,10 @@ Verifying contracts on sourcify
* NOTE:
* I had already created a subscription id https://vrf.chain.link/sepolia/3217 to deploy through migrations file but had not added the consumer contract of it deployed, which i later did at https://sepolia.etherscan.io/tx/0xc689e64aca1c531fab582bbbbd86e835bb465c227d6068e001f1692f30eab3f6, and funded it in this tx https://sepolia.etherscan.io/tx/0x23e4464e74fbfb30dfd3a65042e86f37d915e143b63105f5391bcd23d45d93f6, so this is the one to use since that's the subscription id passed to the constructor when deployed
* Fix
* After being unsuccessful in trying to generate a random number with subscription id 3217 and 3350, I got help in #vrf room of Chainlink on Discord from Lucas Archangelo who suggested to use the existing subscription id and redeploy and add the new contract address as the consumer. So I deployed VRFD20 to 0xE265f9a30c72A78C4b89Fc2e2C60e9327704Fa5e that has tx hash https://dashboard.tenderly.co/tx/sepolia/0x0122f8b4efb1f66dea8482edf7d4e9089946cd13a60559504a4dc63fdd0a727b?trace=0.0, so then i went to https://vrf.chain.link/sepolia/3350, and added new consumer address 0xE265f9a30c72A78C4b89Fc2e2C60e9327704Fa5e in this tx 0x397c8aedab55e475d27b87de8d63cc1f866b007698f94cea49ab2ae75f016104, then i imported all the contracts into Remix to access that contract and verified that i was interacting with that new contract. then made transaction rollDice passing my account address 0x1dd907abb024e17d196de0d7fe8eb507b6ccaae7 as a parameter in this tx https://sepolia.etherscan.io/tx/0x558a0567c5c71ec378e6919d75d8cd02b60e97c6c87205f77f5146233a58affa, then finally in Remix i called getRolledValueForPlayer passing my account address 0x1dd907abb024e17d196de0d7fe8eb507b6ccaae7 as a parameter, and that output the following in Remix 0: uint256: 8, so it successfully generated the random number 8. I also modified ./scripts/demo.js to load that new address and to not run rollDice since we'd already done that, and it also returned `8`.
* After being unsuccessful in trying to generate a random number with subscription id 3217 and 3350, I got help in #vrf room of Chainlink on Discord from Lucas Archangelo who suggested to use the existing subscription id and redeploy and add the new contract address as the consumer. So I deployed VRFD20 to 0xE265f9a30c72A78C4b89Fc2e2C60e9327704Fa5e that has tx hash https://dashboard.tenderly.co/tx/sepolia/0x0122f8b4efb1f66dea8482edf7d4e9089946cd13a60559504a4dc63fdd0a727b?trace=0.0, so then i went to https://vrf.chain.link/sepolia/3350, and added new consumer address 0xE265f9a30c72A78C4b89Fc2e2C60e9327704Fa5e in this tx 0x397c8aedab55e475d27b87de8d63cc1f866b007698f94cea49ab2ae75f016104, then i imported all the contracts into Remix to access that contract and verified that i was interacting with that new contract. then made transaction rollDice passing my account address 0x1dd907abb024e17d196de0d7fe8eb507b6ccaae7 as a parameter in this tx https://sepolia.etherscan.io/tx/0x558a0567c5c71ec378e6919d75d8cd02b60e97c6c87205f77f5146233a58affa, then finally in Remix i called getRolledValueForPlayer passing my account address 0x1dd907abb024e17d196de0d7fe8eb507b6ccaae7 as a parameter, and that output the following in Remix 0: uint256: 8, so it successfully generated the random number 8. I also modified ./scripts/demo-chainlink-vrf-on-ethereum-sepolia.js to load that new address and to not run rollDice since we'd already done that, and it also returned `8`.

* Run
* node ./scripts/demo.js
* node ./scripts/demo-chainlink-vrf-on-ethereum-sepolia.js

* Chainlink VRF https://docs.chain.link/getting-started/intermediates-tutorial
* View token balance https://sepolia.etherscan.io/address/0x1dd907abb024e17d196de0d7fe8eb507b6ccaae7
Expand Down Expand Up @@ -211,4 +210,4 @@ Verifying contracts on sourcify

* Other
* https://github.com/HCastano/urban-planning-in-the-paraverse-with-ink
* https://www.rob.tech/blog/hybrid-chains/
* https://www.rob.tech/blog/hybrid-chains/
2 changes: 1 addition & 1 deletion dapps/evm2/flipper/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "flipper",
"version": "1.0.0",
"description": "",
"main": "demo.js",
"main": "demo-chainlink-vrf-on-ethereum-sepolia.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
Expand Down
150 changes: 1 addition & 149 deletions dapps/evm2/flipper/test/test_Flipper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,86 +3,21 @@ const { Web3 } = require('web3');
const BN = require('bn.js');

// Uses Mocha and Ganache
const Randomness = artifacts.require("../build/contracts/Randomness");
const RandomnessConsumer = artifacts.require("../build/contracts/RandomnessConsumer");
const RandomNumber = artifacts.require("../contracts/lib/RandomNumber");
const Flipper = artifacts.require("../contracts/lib/Flipper");

console.log('test_Flipper');

let providerInstance = new Web3.providers.WebsocketProvider(process.env.MOONBASE_BLASTAPI_ENDPOINT, {}, { delay: 500, autoReconnect: true, maxAttempts: 100 });
console.log('providerInstance: ', providerInstance);
let web3 = new Web3(providerInstance);
// when using BlastAPI WSS endpoint I get error `TypeError: Cannot create property 'gasLimit' on string"`
// https://github.com/web3/web3.js/issues/3573
console.log('web3.currentProvider: ', web3.currentProvider);
// Randomness.setProvider(providerInstance);
// RandomnessConsumer.setProvider(providerInstance);
// RandomNumber.setProvider(providerInstance);
// Flipper.setProvider(providerInstance);

// advanceBlock = () => {
// return new Promise((resolve, reject) => {
// web3.currentProvider.send({
// jsonrpc: '2.0',
// method: 'evm_mine',
// id: new Date().getTime()
// }, (err, result) => {
// console.log('result: ', result);
// console.log('err: ', err);
// if (err) { return reject(err) }
// const newBlockHash = web3.eth.getBlock('latest').hash;
// console.log('newBlockHash: ', newBlockHash);

// return resolve(newBlockHash);
// })
// })
// }

contract('Flipper', accounts => {
console.log('accounts: ', accounts);
let randomnessInstance;
let randomNumberInstance;

let flipperInstance;
let fulfillmentFee;
let refundAddress;
let gas;
let gasLimit;
let gasPrice;
let currentBlockNumber;
let nextBlockNumber;
// https://github.com/PureStake/moonbeam/blob/master/precompiles/randomness/Randomness.sol#L17C43-L17C62
// https://docs.web3js.org/api/web3-utils/function/toWei
const requiredDeposit = Web3.utils.toWei('1', 'ether');
const blockTimeout = 1000000;
const initValue = false;
beforeEach(async () => {
console.log('beforeEach');
randomnessInstance = await Randomness.at("0x0000000000000000000000000000000000000809");
// console.log('randomnessInstance.address:', randomnessInstance.address);

// RandomnessConsumer.link(randomnessInstance);
// RandomNumber.link(randomnessInstance);

// gas = Web3.utils.toWei('1000000', 'wei');
// gasLimit = Web3.utils.toWei('600000', 'wei');
// gasPrice = Web3.utils.toWei('2000000', 'wei');
// gas = Web3.utils.toHex(70000);
// gasLimit = Web3.utils.toHex(600000); // gwei
// gasPrice = Web3.utils.toHex(21000);

// Create contract with 1 Ether (contract must be payable)
randomNumberInstance = await RandomNumber.deployed(); //.new({ from: accounts[0], value: requiredDeposit });
// randomNumberInstance = await RandomNumber.new(
// { from: accounts[0], value: requiredDeposit,
// gas: gas, gasLimit: gasLimit, gasPrice: gasPrice, syncWithContext: true }
// );
console.log('randomNumberInstance.address:', randomNumberInstance.address);

// Flipper.link(randomnessInstance);
// Flipper.link(randomNumberInstance);

// Deploy token contract

flipperInstance = await Flipper.deployed(); //.new(initValue, { from: accounts[0] });
// console.log('flipperInstance.address:', flipperInstance.address);
Expand Down Expand Up @@ -112,87 +47,4 @@ contract('Flipper', accounts => {
console.log('error in flip: ', e);
}
});

it("requests randomness", async () => {
try {
fulfillmentFee = await randomNumberInstance.MIN_FEE.call();
console.log('fulfillmentFee: ', fulfillmentFee.toString());
console.log('fulfillmentFee is bn', BN.isBN(fulfillmentFee));
console.log('accounts', accounts);

// console.log('web3.currentProvider: ', web3.currentProvider);
// do not use `.call` when doing state changes to blockchain
// gas = Web3.utils.toWei('1000000', 'wei');
// gasLimit = Web3.utils.toWei('600000', 'wei');
// gasPrice = Web3.utils.toWei('2000000', 'wei');
gas = Web3.utils.toHex(150000);
gasLimit = Web3.utils.toHex(600000);
gasPrice = Web3.utils.toHex(21000);
let roller = '0x1dd907ABb024E17d196de0D7Fe8EB507b6cCaae7';
refundAddress = await randomNumberInstance.requestRandomness(roller, { from: accounts[0], value: fulfillmentFee });
// refundAddress = await randomNumberInstance.requestRandomness(
// {
// from: accounts[0],
// value: fulfillmentFee,
// gas: gas, gasLimit: gasLimit, gasPrice: gasPrice,
// syncWithContext: true
// }
// );
// console.log('refundAddress: ', refundAddress);

const requestId = await randomNumberInstance.requestId.call();
console.log('requestId: ', requestId);
// Check status of request id from the randomness precompile
// https://github.com/PureStake/moonbeam/blob/master/precompiles/randomness/Randomness.sol#L96
const requestStatus = await randomNumberInstance.getRequestStatus.call();
console.log('requestStatus: ', requestStatus.toString());

// Wait for at least MIN_VRF_BLOCKS_DELAY but less than MAX_VRF_BLOCKS_DELAY
// https://github.com/PureStake/moonbeam/blob/master/precompiles/randomness/Randomness.sol#L13
// https://github.com/PureStake/moonbeam/blob/master/precompiles/randomness/Randomness.sol#L15
const MIN_VRF_BLOCKS_DELAY = await randomNumberInstance.VRF_BLOCKS_DELAY.call();
console.log('MIN_VRF_BLOCKS_DELAY: ', MIN_VRF_BLOCKS_DELAY);
// let currentBlock = await web3.eth.getBlock("latest");
currentBlockNumber = await web3.eth.getBlockNumber();
console.log('currentBlockNumber: ', currentBlockNumber.toString());
// remove 'n' character from end of blocknumber
currentBlockNumber = currentBlockNumber.toString().replace(/[^0-9.]/g, '');
let firstBlockNumber = currentBlockNumber;
console.log('firstBlockNumber: ', firstBlockNumber);
assert.equal(requestStatus, 1, 'should still be pending'); // where 1 in enum is 'PENDING'
// evm_mine not defined, since can only do on Ganache not live testnet
// for (i=0; i<MIN_VRF_BLOCKS_DELAY.length; i++) {
// advanceBlock();
// }

// TODO - not sure how to wait for next block number
// while (firstBlockNumber != nextBlockNumber) {
// TODO - wait for at least 2 blocks
// setTimeout(async function(){
// console.log('setTimeout');
// return nextBlockNumber;
// }, 20000);
// nextBlockNumber = await web3.eth.getBlockNumber();
// remove 'n' character from end of blocknumber
// nextBlockNumber = currentBlockNumber.toString().replace(/[^0-9.]/g, '');
// console.log('nextBlockNumber: ', nextBlockNumber);
// }
// console.log('found next block');

// currentBlockNumber = await web3.eth.getBlockNumber();
// // remove 'n' character from end of blocknumber
// currentBlockNumber = currentBlockNumber.toString().replace(/[^0-9.]/g, '');
// let secondBlockNumber = currentBlockNumber;
// console.log('secondBlockNumber: ', secondBlockNumber);

// assert.equal(parseNum(firstBlockNumber), parseNum(secondBlockNumber)+2, 'two blocks should have passed');
// assert.equal(requestStatus, 2, 'not ready as expected'); // where 2 in enum is 'READY'

// await randomNumberInstance.fulfillRequest.call();
// const random = await randomNumberInstance.random.call();
// console.log('random number: ', random[0]);
} catch (e) {
console.log('error in requests randomness: ', e);
}
});
});
Loading