summaryrefslogtreecommitdiff
path: root/c2ec/simulation-client.go
blob: a6708ecc89ddd9b61f5d8bfd6ecdf689b47dc138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package main

import (
	"bytes"
	"fmt"
	"time"
)

type SimulationTransaction struct {
	ProviderTransaction

	allow bool
}

type SimulationClient struct {
	ProviderClient

	// toggle this to simulate failed transactions.
	AllowNextWithdrawal bool

	// simulates the provider client fetching attestation at the providers backend.
	providerBackendAttestationDelayMs int
}

func (st *SimulationTransaction) AllowWithdrawal() bool {

	return st.allow
}

func (st *SimulationTransaction) AbortWithdrawal() bool {

	return false
}

func (st *SimulationTransaction) Bytes() []byte {

	return bytes.NewBufferString("this is a simulated transaction and therefore has no content.").Bytes()
}

func (sc *SimulationClient) SetupClient(p *Provider) error {

	fmt.Println("setting up simulation client. probably not what you want in production")

	sc.AllowNextWithdrawal = true
	sc.providerBackendAttestationDelayMs = 1000 // one second, might be a lot but for testing this is fine.
	PROVIDER_CLIENTS["Simulation"] = sc
	return nil
}

func (sc *SimulationClient) GetTransaction(transactionId string) (ProviderTransaction, error) {

	fmt.Println("getting transaction from simulation provider")
	time.Sleep(time.Duration(sc.providerBackendAttestationDelayMs) * time.Millisecond)
	st := new(SimulationTransaction)
	st.allow = sc.AllowNextWithdrawal
	return st, nil
}

func (*SimulationClient) Refund(transactionId string) error {

	fmt.Println("refund triggered for simulation provider with transaction id: ", transactionId)
	return nil
}