Class: Remitmd::MockRemit

Inherits:
Object
  • Object
show all
Defined in:
lib/remitmd/mock.rb

Overview

In-memory test double for remit.md. Zero network, zero latency, deterministic.

MockRemit gives you a RemitWallet backed by in-memory state so you can test your agent's payment logic without a live API or spending real USDC.

Examples:

mock   = Remitmd::MockRemit.new
wallet = mock.wallet

wallet.pay("0x0000000000000000000000000000000000000001", 1.50)

mock.was_paid?("0x0000000000000000000000000000000000000001", 1.50) # => true
mock.total_paid_to("0x0000000000000000000000000000000000000001")   # => BigDecimal("1.5")
mock.transaction_count                                              # => 1

Constant Summary collapse

MOCK_ADDRESS =
"0xMockWallet0000000000000000000000000001"
DEFAULT_BALANCE =
BigDecimal("10000")

Instance Method Summary collapse

Constructor Details

#initialize(balance: DEFAULT_BALANCE) ⇒ MockRemit

Returns a new instance of MockRemit.



27
28
29
30
# File 'lib/remitmd/mock.rb', line 27

def initialize(balance: DEFAULT_BALANCE)
  @mutex    = Mutex.new
  @state    = initial_state(BigDecimal(balance.to_s))
end

Instance Method Details

#balanceObject

Current mock balance.



48
49
50
# File 'lib/remitmd/mock.rb', line 48

def balance
  @mutex.synchronize { @state[:balance] }
end

#resetObject

Reset all state. Call between test cases to prevent state leakage.



38
39
40
# File 'lib/remitmd/mock.rb', line 38

def reset
  @mutex.synchronize { @state.replace(initial_state(DEFAULT_BALANCE)) }
end

#set_balance(amount) ⇒ Object

Override the simulated USDC balance.



43
44
45
# File 'lib/remitmd/mock.rb', line 43

def set_balance(amount)
  @mutex.synchronize { @state[:balance] = BigDecimal(amount.to_s) }
end

#total_paid_to(recipient) ⇒ Object

Sum of all USDC sent to recipient.



73
74
75
76
77
78
79
# File 'lib/remitmd/mock.rb', line 73

def total_paid_to(recipient)
  @mutex.synchronize do
    @state[:transactions]
      .select { |tx| tx.to.casecmp(recipient).zero? }
      .sum(BigDecimal("0"), &:amount)
  end
end

#transaction_countObject

Total number of transactions recorded.



58
59
60
# File 'lib/remitmd/mock.rb', line 58

def transaction_count
  @mutex.synchronize { @state[:transactions].length }
end

#transactionsObject

All transactions recorded.



53
54
55
# File 'lib/remitmd/mock.rb', line 53

def transactions
  @mutex.synchronize { @state[:transactions].dup }
end

#walletObject

Return a RemitWallet backed by this mock. No private key required.



33
34
35
# File 'lib/remitmd/mock.rb', line 33

def wallet
  RemitWallet.new(signer: MockSigner.new(MOCK_ADDRESS), transport: MockTransport.new(@state, @mutex))
end

#was_paid?(recipient, amount) ⇒ Boolean

True if a payment of exactly amount USDC was sent to recipient.

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/remitmd/mock.rb', line 63

def was_paid?(recipient, amount)
  d = BigDecimal(amount.to_s)
  @mutex.synchronize do
    @state[:transactions].any? do |tx|
      tx.to.casecmp(recipient).zero? && tx.amount == d
    end
  end
end