Skip to content

Instantly share code, notes, and snippets.

@bonsfi
Created October 2, 2020 19:45
Show Gist options
  • Save bonsfi/1117e4ed39fb625402ab20acbaa4d275 to your computer and use it in GitHub Desktop.
Save bonsfi/1117e4ed39fb625402ab20acbaa4d275 to your computer and use it in GitHub Desktop.
Release the Warchest Bot
require "option_parser"
# Option Parsing
network: String = "betanet"
pool_id: String = ""
account_id: String = ""
dry_run: Bool = false
OptionParser.parse do |parser|
parser.banner = "Usage: warmonger [--dry-run] -n NETWORK -p POOLID -a ACCOUNTID"
parser.on("-d", "--dry-run", "Analyze but do not make any changes.") { dry_run = true }
parser.on("-n NETWORK", "--network=NETWORK", "Specify Target network") { |param| network = param }
parser.on("-p POOLID", "--pool=POOLID", "Specify Pool ID") { |param| pool_id = param }
parser.on("-a ACCOUNTID", "--account=ACCOUNTID", "Specify Account ID") { |param| account_id = param }
parser.on("-h", "--help") do
puts parser
exit
end
end
print "Network: ", network, "\n"
print "Pool: ", pool_id, "\n"
print "Account: ", account_id, "\n"
print "Dry Run: ", dry_run, "\n\n"
proposal_seat_price = ->() do
proposals = `near proposals`
proposals = proposals.split('\n')[0].gsub(/\s+/, ' ')[-20, 20]
proposals = proposals.gsub(/[^0-9]*/, "").strip
return proposals
end.call
my_stake = ->() do
balance = `near view --networkId testnet #{pool_id} get_account '{"account_id":"#{account_id}"}' | grep ' stake'`
balance = balance.gsub(/[^0-9]*/, "").strip
return balance
end.call()
my_stake_ratio=`echo "(#{my_stake} / 1000000000000000000000000)" | bc`.strip
# A bit hacky but easier to shell out for this. Thansk to gaia for this.
total_stake =`curl -s -d '{"jsonrpc": "2.0", "method": "validators", "id": "dontcare", "params": [null]}' -H 'Content-Type: application/json' https://rpc.testnet.near.org | jq '.result.current_validators[] | select(.account_id | contains ("#{pool_id}"))' | jq .stake | sed -r "s/[[:cntrl:]]\[[0-9]{1,3}m//g" | tr -d '"'`.strip
total_ratio = `echo '(#{total_stake} / 1000000000000000000000000)' | bc`.strip
if total_ratio.to_i < proposal_seat_price.to_i
print "Staked Amount too Low for Seat: "
print total_ratio, " < ", proposal_seat_price, "\n"
stake_amount = (proposal_seat_price.to_i - total_ratio.to_i).to_s
print "Upping Stake By: ", stake_amount, "\n"
stake_amount = `echo '(#{stake_amount} * 1000000000000000000000000)' | bc`.strip
if !dry_run
`near call #{pool_id} stake '{\"amount\":\"#{stake_amount}\"}' --accountId #{account_id}`
end
else
print "Staked Amount too High for Seat: "
print total_ratio, " > ", proposal_seat_price, "\n"
stake_amount = (total_ratio.to_i - proposal_seat_price.to_i - 1).to_s
print "Lowering Stake By: ", stake_amount, "\n"
stake_amount = `echo '(#{stake_amount} * 1000000000000000000000000)' | bc`.strip
if !dry_run
`near call #{pool_id} unstake '{\"amount\":\"#{stake_amount}\"}' --accountId #{account_id}`
end
end
print "\n\nPinging Contract..."
result = `near call #{pool_id} ping '{}' --accountId #{account_id}`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment