-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAddressMapper.cs
95 lines (72 loc) · 2.82 KB
/
AddressMapper.cs
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using Stratis.SmartContracts;
public class AddressMapper : SmartContract
{
public Address GetSecondaryAddress(Address primary) => State.GetAddress($"SecondaryAddress:{primary}");
private void SetSecondaryAddress(Address primary, Address secondary) => State.SetAddress($"SecondaryAddress:{primary}", secondary);
public MappingInfo GetMapping(Address secondary) => State.GetStruct<MappingInfo>($"Mapping:{secondary}");
private void SetMapping(Address secondary, MappingInfo value) => State.SetStruct($"Mapping:{secondary}", value);
private void ClearMappingInfo(Address secondary) => State.Clear($"MappingInfo:{secondary}");
public Address Owner
{
get => State.GetAddress(nameof(Owner));
private set => State.SetAddress(nameof(Owner), value);
}
public AddressMapper(ISmartContractState smartContractState, Address owner) : base(smartContractState)
{
this.Owner = owner;
}
public void MapAddress(Address secondary)
{
Assert(GetMapping(secondary).Status == (int)Status.NoStatus);
SetMapping(secondary, new MappingInfo { Primary = Message.Sender, Status = (int)Status.Pending });
}
public void Approve(Address secondary)
{
EnsureAdminOnly();
var mapping = GetMapping(secondary);
Assert(mapping.Status == (int)Status.Pending, "Mapping is not in pending state.");
SetSecondaryAddress(mapping.Primary, secondary);
SetMapping(secondary, new MappingInfo { Primary = mapping.Primary, Status = (int)Status.Approved });
Log(new AddressMappedLog { Primary = mapping.Primary, Secondary = secondary });
}
public void Reject(Address secondary)
{
EnsureAdminOnly();
var mapping = GetMapping(secondary);
Assert(mapping.Status == (int)Status.Pending, "Mapping is not in pending state.");
ClearMappingInfo(secondary); // same address can be mapped again.
}
public Address GetPrimaryAddress(Address secondary)
{
var mapping = GetMapping(secondary);
Assert(mapping.Status == (int)Status.Approved, "The mapping is not approved.");
return mapping.Primary;
}
public void ChangeOwner(Address owner)
{
EnsureAdminOnly();
Assert(owner != Address.Zero, $"The {nameof(owner)} parameter can not be default(zero) address.");
this.Owner = owner;
}
public void EnsureAdminOnly() => Assert(this.Owner == Message.Sender, "Only contract owner can access.");
public enum Status
{
NoStatus,
Pending,
Approved,
}
public struct MappingInfo
{
[Index]
public Address Primary;
public int Status;
}
public struct AddressMappedLog
{
[Index]
public Address Primary;
[Index]
public Address Secondary;
}
}