Skip to content

Instantly share code, notes, and snippets.

@earthchie
Last active April 3, 2023 11:30
Show Gist options
  • Save earthchie/e36854ef793eabec775a969d6453f071 to your computer and use it in GitHub Desktop.
Save earthchie/e36854ef793eabec775a969d6453f071 to your computer and use it in GitHub Desktop.
Sphinx31 automatic obtain
// require ethers@5
const Sphinx31Watcher = provider.on('block', async function(){
const provider = new ethers.providers.JsonRpcProvider('https://rpc-l1.jibchain.net');
const wallet = new Wallet('0x...private_key....');
const Sphinx31 = new ethers.Contract('0xd431d826d7a4380b9259612176f00528b88840a7', [
'function supplier(address addr) view returns (bool isCraft, uint256 machineIndex, uint256 laststamp)',
'function machine(uint256 _machineIndex) view returns (bool active, uint256 duration, uint256 miceCost, uint256 cmjCost, uint256 sx31Reward)',
'function obtain(uint256 _machineIndex)'
], provider);
const supplier = await Sphinx31.supplier(wallet.address);
const machine = await Sphinx31.machine(supplier.machineIndex);
const blockTimestamp = (await provider.getBlock()).timestamp;
if(+(supplier.laststamp + machine.duration).toString() >= blockTimestamp){
provider.off('block', Sphinx31Watcher);
const trx = await Sphinx31.connect(wallet).obtain(supplier.machineIndex);
console.log('called obtain() successfully', trx.hash);
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment