Class: PolarLoop::Client
- Inherits:
-
Object
- Object
- PolarLoop::Client
- Defined in:
- lib/polarloop/client.rb
Instance Attribute Summary collapse
-
#chain_config ⇒ Object
readonly
Returns the value of attribute chain_config.
-
#contract_caller ⇒ Object
readonly
Returns the value of attribute contract_caller.
-
#event_parser ⇒ Object
readonly
Returns the value of attribute event_parser.
Instance Method Summary collapse
- #batch_charge(mandate_ids) ⇒ Object
- #batch_charge_ready?(mandate_ids) ⇒ Boolean
- #batch_revoke_mandate(mandate_ids) ⇒ Object
-
#charge(mandate_id) ⇒ Object
--- Write methods ---.
- #charge_ready?(mandate_id) ⇒ Boolean
- #events_for_tx(tx_hash) ⇒ Object
-
#get_mandate(mandate_id) ⇒ Object
--- View methods (no gas) ---.
- #get_mandate_by_reference_id(reference_id) ⇒ Object
- #get_payer_mandates(payer) ⇒ Object
- #grant_operator_role(address) ⇒ Object
-
#initialize(chain_config:, key:) ⇒ Client
constructor
A new instance of Client.
- #latest_block_number ⇒ Object
- #mandate_charged_events(from_block:, to_block: "latest") ⇒ Object
-
#mandate_created_events(from_block:, to_block: "latest") ⇒ Object
--- Events ---.
- #mandate_revoked_events(from_block:, to_block: "latest") ⇒ Object
- #min_interval ⇒ Object
-
#normalize_bytes32(hex_str) ⇒ Object
--- Helpers ---.
- #pause ⇒ Object
- #paused? ⇒ Boolean
- #rescue_erc20(token, amount) ⇒ Object
- #rescue_eth ⇒ Object
- #revoke_mandate(mandate_id) ⇒ Object
- #revoke_operator_role(address) ⇒ Object
-
#sign_create_mandate(payer:, merchant:, token:, amount:, interval:, max_total_amount:, reference_id:) ⇒ Object
--- Signature for mandate creation (off-chain, no gas) ---.
- #to_hex_bytes32(binary) ⇒ Object
- #unpause ⇒ Object
Constructor Details
#initialize(chain_config:, key:) ⇒ Client
Returns a new instance of Client.
7 8 9 10 11 12 |
# File 'lib/polarloop/client.rb', line 7 def initialize(chain_config:, key:) @chain_config = chain_config @key = key @contract_caller = ContractCaller.new(chain_config: chain_config, key: key) @event_parser = EventParser.new(@contract_caller) end |
Instance Attribute Details
#chain_config ⇒ Object (readonly)
Returns the value of attribute chain_config.
5 6 7 |
# File 'lib/polarloop/client.rb', line 5 def chain_config @chain_config end |
#contract_caller ⇒ Object (readonly)
Returns the value of attribute contract_caller.
5 6 7 |
# File 'lib/polarloop/client.rb', line 5 def contract_caller @contract_caller end |
#event_parser ⇒ Object (readonly)
Returns the value of attribute event_parser.
5 6 7 |
# File 'lib/polarloop/client.rb', line 5 def event_parser @event_parser end |
Instance Method Details
#batch_charge(mandate_ids) ⇒ Object
34 35 36 |
# File 'lib/polarloop/client.rb', line 34 def batch_charge(mandate_ids) @contract_caller.send_batch_tx("batchCharge", mandate_ids.map { |id| normalize_bytes32(id) }) end |
#batch_charge_ready?(mandate_ids) ⇒ Boolean
94 95 96 97 98 99 100 101 |
# File 'lib/polarloop/client.rb', line 94 def batch_charge_ready?(mandate_ids) result = @contract_caller.call_view("batchIsChargeReady", mandate_ids.map { |id| normalize_bytes32(id) }) ready_arr = result[0] reasons_arr = result[1] ready_arr.zip(reasons_arr).map do |ready, reason| Types::ChargeReadyResult.new(ready: ready, reason: reason) end end |
#batch_revoke_mandate(mandate_ids) ⇒ Object
42 43 44 |
# File 'lib/polarloop/client.rb', line 42 def batch_revoke_mandate(mandate_ids) @contract_caller.send_batch_tx("batchRevokeMandate", mandate_ids.map { |id| normalize_bytes32(id) }) end |
#charge(mandate_id) ⇒ Object
--- Write methods ---
30 31 32 |
# File 'lib/polarloop/client.rb', line 30 def charge(mandate_id) @contract_caller.send_tx("charge", normalize_bytes32(mandate_id)) end |
#charge_ready?(mandate_id) ⇒ Boolean
89 90 91 92 |
# File 'lib/polarloop/client.rb', line 89 def charge_ready?(mandate_id) result = @contract_caller.call_view("isChargeReady", normalize_bytes32(mandate_id)) Types::ChargeReadyResult.new(ready: result[0], reason: result[1]) end |
#events_for_tx(tx_hash) ⇒ Object
129 130 131 |
# File 'lib/polarloop/client.rb', line 129 def events_for_tx(tx_hash) @event_parser.events_for_tx(tx_hash) end |
#get_mandate(mandate_id) ⇒ Object
--- View methods (no gas) ---
72 73 74 75 |
# File 'lib/polarloop/client.rb', line 72 def get_mandate(mandate_id) result = @contract_caller.call_view("getMandate", normalize_bytes32(mandate_id)) Types::Mandate.from_abi(result) end |
#get_mandate_by_reference_id(reference_id) ⇒ Object
77 78 79 80 81 82 |
# File 'lib/polarloop/client.rb', line 77 def get_mandate_by_reference_id(reference_id) result = @contract_caller.call_view("getMandateByReferenceId", normalize_bytes32(reference_id)) mandate_id = to_hex_bytes32(result[0]) mandate = Types::Mandate.from_abi(result[1]) [mandate_id, mandate] end |
#get_payer_mandates(payer) ⇒ Object
84 85 86 87 |
# File 'lib/polarloop/client.rb', line 84 def get_payer_mandates(payer) result = @contract_caller.call_view("getPayerMandates", payer) Array(result).map { |id| to_hex_bytes32(id) } end |
#grant_operator_role(address) ⇒ Object
54 55 56 |
# File 'lib/polarloop/client.rb', line 54 def grant_operator_role(address) @contract_caller.send_tx("grantRole", Abi::OPERATOR_ROLE, address) end |
#latest_block_number ⇒ Object
111 112 113 |
# File 'lib/polarloop/client.rb', line 111 def latest_block_number @contract_caller.latest_block_number end |
#mandate_charged_events(from_block:, to_block: "latest") ⇒ Object
121 122 123 |
# File 'lib/polarloop/client.rb', line 121 def mandate_charged_events(from_block:, to_block: "latest") @event_parser.mandate_charged_events(from_block: from_block, to_block: to_block) end |
#mandate_created_events(from_block:, to_block: "latest") ⇒ Object
--- Events ---
117 118 119 |
# File 'lib/polarloop/client.rb', line 117 def mandate_created_events(from_block:, to_block: "latest") @event_parser.mandate_created_events(from_block: from_block, to_block: to_block) end |
#mandate_revoked_events(from_block:, to_block: "latest") ⇒ Object
125 126 127 |
# File 'lib/polarloop/client.rb', line 125 def mandate_revoked_events(from_block:, to_block: "latest") @event_parser.mandate_revoked_events(from_block: from_block, to_block: to_block) end |
#min_interval ⇒ Object
103 104 105 |
# File 'lib/polarloop/client.rb', line 103 def min_interval @contract_caller.call_view("minInterval").to_i end |
#normalize_bytes32(hex_str) ⇒ Object
--- Helpers ---
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/polarloop/client.rb', line 135 def normalize_bytes32(hex_str) if hex_str.is_a?(String) if hex_str.bytesize == 32 && (hex_str.encoding == Encoding::BINARY || !hex_str.valid_encoding?) return hex_str.b end hex = hex_str.sub(/\A0x/i, "").rjust(64, "0") return [hex].pack("H*") end hex = hex_str.to_s.sub(/\A0x/i, "").rjust(64, "0") [hex].pack("H*") end |
#pause ⇒ Object
46 47 48 |
# File 'lib/polarloop/client.rb', line 46 def pause @contract_caller.send_tx("pause") end |
#paused? ⇒ Boolean
107 108 109 |
# File 'lib/polarloop/client.rb', line 107 def paused? @contract_caller.call_view("paused") end |
#rescue_erc20(token, amount) ⇒ Object
66 67 68 |
# File 'lib/polarloop/client.rb', line 66 def rescue_erc20(token, amount) @contract_caller.send_tx("rescueERC20", token, amount) end |
#rescue_eth ⇒ Object
62 63 64 |
# File 'lib/polarloop/client.rb', line 62 def rescue_eth @contract_caller.send_tx("rescueETH") end |
#revoke_mandate(mandate_id) ⇒ Object
38 39 40 |
# File 'lib/polarloop/client.rb', line 38 def revoke_mandate(mandate_id) @contract_caller.send_tx("revokeMandate", normalize_bytes32(mandate_id)) end |
#revoke_operator_role(address) ⇒ Object
58 59 60 |
# File 'lib/polarloop/client.rb', line 58 def revoke_operator_role(address) @contract_caller.send_tx("revokeRole", Abi::OPERATOR_ROLE, address) end |
#sign_create_mandate(payer:, merchant:, token:, amount:, interval:, max_total_amount:, reference_id:) ⇒ Object
--- Signature for mandate creation (off-chain, no gas) ---
16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/polarloop/client.rb', line 16 def sign_create_mandate(payer:, merchant:, token:, amount:, interval:, max_total_amount:, reference_id:) struct_hash = Eth::Util.keccak256( Eth::Abi.encode( ["address", "address", "address", "uint128", "uint32", "uint128", "bytes32", "uint256", "address"], [payer, merchant, token, amount, interval, max_total_amount, normalize_bytes32(reference_id), @chain_config.chain_id, @chain_config.contract_address] ) ) @key.personal_sign(struct_hash) end |
#to_hex_bytes32(binary) ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/polarloop/client.rb', line 149 def to_hex_bytes32(binary) if binary.is_a?(String) && binary.start_with?("0x") binary.downcase elsif binary.is_a?(String) "0x" + binary.unpack1("H*") else "0x" + binary.to_s(16).rjust(64, "0") end end |
#unpause ⇒ Object
50 51 52 |
# File 'lib/polarloop/client.rb', line 50 def unpause @contract_caller.send_tx("unpause") end |