Skip to content

Commit

Permalink
Merge pull request #2 from GuiGou12358/main
Browse files Browse the repository at this point in the history
V0.0.1
  • Loading branch information
GuiGou12358 authored Apr 9, 2024
2 parents 9b75991 + 4d90e07 commit d4e029a
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 54 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ npm install
Last, under the project directory, run following command to install all the dependency.

```
npx ts-node src/luckyCli.ts [Options]
npx tsx src/luckyCli.ts [Options]
```

```
Options:
--help Show help [boolean]
--help Show help
--dc, --displayConfiguration Display the configuration
--di, --displayInformation Display information from indexer and smart contracts
--ch, --checks Check if the grants and the configuration in the smart contracts have been set
Expand All @@ -40,14 +40,21 @@ Options:
--version Show version number
```

If you want to check the configuration.

```
npx tsx src/luckyCli.ts --network <network> --checks
```

If you want to claim the rewards from dapp staking for all missing era.

```
npx ts-node src/luckyCli.ts --network <network> --claim
npx tsx src/luckyCli.ts --network <network> --claim
```

If you want to run the raffle for the last missing era.
If you want to run the raffle for all missing era.

```
npx ts-node src/luckyCli.ts --network <network> --raffle
```
npx tsx src/luckyCli.ts --network <network> --raffle
```

6 changes: 3 additions & 3 deletions metadata/shibuya/raffle_metadata.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
+{
{
"source": {
"hash": "0x178f2e85d2de1b1afef4d2d599a2d4eb0ae20610d9783e01aec560b83b04565f",
"hash": "0x580bc35dae235016c2e316a7781a89233e3acb93b420d75077b7a170573de0f1",
"language": "ink! 4.3.0",
"compiler": "rustc 1.75.0",
"compiler": "rustc 1.76.0",
"build_info": {
"build_mode": "Debug",
"cargo_contract_version": "3.2.0",
Expand Down
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
{
"dependencies": {
"@polkadot/api": "^10.12.4",
"@polkadot/api-contract": "^10.12.4",
"@polkadot/types": "^10.12.4",
"@phala/sdk": "^0.5.10",
"@phala/sdk": "^0.6.0-beta.19",
"node-fetch": "^3.3.2",
"yargs": "^17.0.22",
"@types/yargs": "^17.0.22",
"node-fetch": "^2",
"yargs": "^17.0.22"
"tsx": "^4.7.2"
},
"devDependencies": {
"@types/node-fetch": "^2.6.11",
"typescript": "^4.9.5"
"typescript": "^5.4.4"
}
}
4 changes: 2 additions & 2 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class ShibuyaConfig implements Config {
dAppStakingDeveloperSmartContractAddress = 'WayJSoeDvHLJ8rXPqrPyQQwznntbxvjwvmq1AKBpu9phYHr';
rewardManagerSmartContractAddress = 'X8nqJsFQWBk137WxetcPdAGLwnJ8xpAQ5tXS1bNsHKaz1q6';
raffleConsumerSmartContractAddress = 'WxB2uwWr1JmUZofp1amiKUcsjtsCZKTUBZg4MWf3HJoMQuY';
rafflePhatContractAddress = "41mr9YDW65QyA5EYF3h4KrAXe9oedRKAXxVtxTDDGRaEAEgM";
rafflePhatContractAttestorAddress = "XpKa1BG9E6WUkCW6FBcwv8maEmZoNWXKoY9BuvSncZMeabS";
rafflePhatContractAddress = "0xb9dd82761d4501791dd284ff97ceb4ffa0b6d928b3b466353ddc1228bd7622dd";
rafflePhatContractAttestorAddress = "ZCYYxa5DAF6UMBVfNT9wDbRNf4imiugXa6jtC8Xdp8T35cM";
subqlUrl = 'https://query.substrate.fi/lucky-subquery-shibuya';
dAppStakingDeveloperSmartContractMetadata = './metadata/shibuya/dapps_staking_developer_metadata.json';
rewardManagerSmartContractMetadata = './metadata/astar/reward_manager_metadata.json';
Expand Down
28 changes: 19 additions & 9 deletions src/luckyCli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {initConnection as initSmartContractConnection} from "./smartContractHelp

const argv = yargs(process.argv.slice(2)).options({
dc: {alias: 'displayConfiguration', desc: 'Display the configuration (contract and http addresses)'},
di: {alias: 'displayInformation', desc: 'DDisplay information from indexer and smart contracts'},
di: {alias: 'displayInformation', desc: 'Display information from indexer and smart contracts'},
ch: {alias: 'checks', desc: 'Check if the grants and the configuration in the smart contracts have been set'},
cl: {alias: 'claim', desc: 'Claim dApp staking developer rewards (for all missing era)'},
r: {alias: 'raffle', desc: 'Start the raffle for the last mist era'},
r: {alias: 'raffle', desc: 'Start the raffle for all missing era'},
net: {alias: 'network', choices:['shibuya', 'shiden', 'astar'], type:'string', desc: 'Specify the network', requiresArg: true},
d: {alias: 'debug', desc: 'Debug mode: display more information'},
}).version('0.1').parseSync();
Expand All @@ -28,7 +28,6 @@ async function claimEra(era: Number) : Promise<void> {

return getEraInfo(era)
.then((eraInfo) => {
console.log("Period %s and sub-period %s for era %s", eraInfo.period, eraInfo.subPeriod, eraInfo.era);
if (eraInfo.subPeriod != "Voting") {
return claimDAppStaking(eraInfo.era);
} else {
Expand Down Expand Up @@ -60,11 +59,24 @@ async function claimAllEras() : Promise<void>{

async function runRaffle() : Promise<void>{

const nextEra = await getNextEraInRaffleConsumer();
let nextEra = await getNextEraInRaffleConsumer();
const currentEra = await getCurrentEra();

if (nextEra < currentEra){
await callRafflePhatContract();
while (nextEra < currentEra){
console.log("Run raffle for era %s", nextEra);
await callRafflePhatContract().then(
() => {
console.log("Successfully run the raffle for era %s", nextEra);
}
).catch( (error) => {
console.log("Error when running the raffle for era %s", nextEra);
return Promise.reject(error);
});

// wait 30 seconds
await new Promise( resolve => setTimeout(resolve, 30000));

nextEra = await getNextEraInRaffleConsumer();
}
}

Expand All @@ -85,9 +97,7 @@ async function run() : Promise<void>{
displayConfiguration();
}

if (argv.displayInformation || argv.checks || argv.claim) {
await initSmartContractConnection();
}
await initSmartContractConnection();

if (argv.displayInformation) {
await getCurrentEra();
Expand Down
55 changes: 32 additions & 23 deletions src/phatContractHelper.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,53 @@
import {ApiPromise, Keyring, WsProvider} from '@polkadot/api';
import {KeyringPair} from '@polkadot/keyring/types';
import {CertificateData, OnChainRegistry, PinkContractPromise, signCertificate} from '@phala/sdk'
import { typeDefinitions } from '@polkadot/types'
import { types } from "@phala/sdk";
import {
OnChainRegistry,
PinkContractPromise,
getClient,
getContract,
} from '@phala/sdk'
import {readFileSync} from 'fs';
import {config} from './config';

export let api : ApiPromise;

export let alice : KeyringPair;
export let aliceCertificate: CertificateData;
export let client : OnChainRegistry;

export let rafflePhatContract : PinkContractPromise;

export async function initConnection(){

if (api){
if (client){
return;
}

api = await ApiPromise.create({
client = await getClient({
transport: config.phatContractRpc
});
/*
api = await ApiPromise.create(options({
provider: new WsProvider(config.phatContractRpc),
noInitWarn: true,
//types: { ...types, ...typeDefinitions }
});
}));
*/

const[chain, nodeName, nodeVersion] = await Promise.all([
api.rpc.system.chain(),
api.rpc.system.name(),
api.rpc.system.version()
client.api.rpc.system.chain(),
client.api.rpc.system.name(),
client.api.rpc.system.version()
]);
console.log('You are connected to chain %s using %s v%s', chain, nodeName, nodeVersion);

alice = new Keyring({ type: 'sr25519' }).addFromUri("//Alice")
aliceCertificate = await signCertificate({ api, pair: alice })
const abi = readFileSync(config.rafflePhatContractMetadata, 'utf-8');

const phatRegistry = await OnChainRegistry.create(api);
const contractKey = await phatRegistry.getContractKey(config.rafflePhatContractAddress) as string;
//const phatRegistry = await OnChainRegistry.create(api);
//const contractKey = await phatRegistry.getContractKey(config.rafflePhatContractAddress) as string;
//rafflePhatContract = new PinkContractPromise(api, phatRegistry, abi, config.rafflePhatContractAddress, contractKey);

const rafflePhatContractMetadata = readFileSync(config.rafflePhatContractMetadata);
const abi = JSON.parse(JSON.stringify(rafflePhatContractMetadata.toString()))
rafflePhatContract = new PinkContractPromise(api, phatRegistry, abi, config.rafflePhatContractAddress, contractKey);
//const suri = '//Alice';
//const provider = await KeyringPairProvider.createFromSURI(client.api, suri);

rafflePhatContract = await getContract({
client,
contractId: config.rafflePhatContractAddress,
abi,
//provider,
}
)
}
16 changes: 12 additions & 4 deletions src/raffle.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import {alice, aliceCertificate, rafflePhatContract} from './phatContractHelper';
import {rafflePhatContract} from './phatContractHelper';
import {Keyring} from "@polkadot/api";
import {signCertificate} from "@phala/sdk";


export async function callRafflePhatContract() : Promise<void>{

console.log('Run raffle for era');
const alice = new Keyring({ type: 'sr25519' }).addFromUri("//Alice")
const aliceCertificate = await signCertificate({ pair: alice })

const {result, output} = await rafflePhatContract.query['runRaffle'](alice.address, {cert: aliceCertificate});

if (result.isOk){
const value : string = output?.toString() ?? '';
const tx = JSON.parse(value).ok;
console.log('Tx for the raffle: %s', tx);
console.log('Result: %s', value);
const res = JSON.parse(value).ok;
if (res.err){
console.log('Error: %s', res.err);
return Promise.reject("ERROR when start the raffle");
}
console.log('Tx for the raffle: %s', res.ok);
return;
}
return Promise.reject("ERROR when start the raffle " + result.asErr);
Expand Down

0 comments on commit d4e029a

Please sign in to comment.