Authorize an external Smart Contract to operate in a DFO
Sometimes it is necessary for the DFO to provide microservices accessible only from certain addresses (e.g. when for example a SmartContract needs to withdraw funds from the wallet). To do this, the DFO must obviously keep track in its StateHolder of the addresses authorized to call certain microservices. Accesses can be of course granted and revoked via Proposals:
1
contract ManageFixedInflationMicroservice {
2
3
function manageFixedInflation(address sender, uint256, address[] memory tokenAddresses, uint256[] memory transferAmounts, uint256[] memory amountsToMint, address to) public {
4
IMVDProxy proxy = IMVDProxy(msg.sender);
5
IStateHolder stateHolder = IStateHolder(proxy.getStateHolderAddress());
6
/* For each authorized address, the pattern usually used is to save as a key in the stateholder a text string containing the address itself and a reference to the microservice concerned that can avoid false permissions, while the value is a Boolean where true indicates access authorized */
7
require(stateHolder.getBool(_toStateHolderKey("fixedinflation.authorized", _toString(sender))), "Unauthorized action");
8
for(uint256 i = 0; i < tokenAddresses.length; i++) {
9
IERC20 token = IERC20(tokenAddresses[i]);
10
if(amountsToMint[i] > 0) {
11
uint256 lastAmount = token.balanceOf(msg.sender);
12
token.mint(amountsToMint[i]);
13
proxy.flushToWallet(address(token), false, 0);
14
if(lastAmount > 0) {
15
proxy.transfer(msg.sender, lastAmount, address(token));
16
}
17
}
18
proxy.transfer(to, transferAmounts[i] + amountsToMint[i], address(token));
19
}
20
}
21
22
function _toStateHolderKey(string memory a, string memory b) private pure returns(string memory) {
23
return _toLowerCase(string(abi.encodePacked(a, ".", b)));
24
}
25
26
function _toString(address _addr) private pure returns(string memory) {
27
bytes32 value = bytes32(uint256(_addr));
28
bytes memory alphabet = "0123456789abcdef";
29
30
bytes memory str = new bytes(42);
31
str[0] = '0';
32
str[1] = 'x';
33
for (uint i = 0; i < 20; i++) {
34
str[2+i*2] = alphabet[uint(uint8(value[i + 12] >> 4))];
35
str[3+i*2] = alphabet[uint(uint8(value[i + 12] & 0x0f))];
36
}
37
return string(str);
38
}
39
40
function _toLowerCase(string memory str) private pure returns(string memory) {
41
bytes memory bStr = bytes(str);
42
for (uint i = 0; i < bStr.length; i++) {
43
bStr[i] = bStr[i] >= 0x41 && bStr[i] <= 0x5A ? bytes1(uint8(bStr[i]) + 0x20) : bStr[i];
44
}
45
return string(bStr);
46
}
47
}
48
49
interface IMVDProxy {
50
function getStateHolderAddress() external view returns(address);
51
function getMVDWalletAddress() external view returns(address);
52
function transfer(address receiver, uint256 value, address token) external;
53
function flushToWallet(address tokenAddress, bool is721, uint256 tokenId) external;
54
}
55
56
interface IStateHolder {
57
function setUint256(string calldata name, uint256 value) external returns(uint256);
58
function getUint256(string calldata name) external view returns(uint256);
59
function getAddress(string calldata name) external view returns(address);
60
function setAddress(string calldata varName, address val) external returns (address);
61
function getBool(string calldata varName) external view returns (bool);
62
function setBool(string calldata varName, bool val) external returns(bool);
63
function clear(string calldata varName) external returns(string memory oldDataType, bytes memory oldVal);
64
}
65
66
interface IERC20 {
67
function totalSupply() external view returns (uint256);
68
function balanceOf(address account) external view returns (uint256);
69
function transfer(address recipient, uint256 amount) external returns (bool);
70
function allowance(address owner, address spender) external view returns (uint256);
71
function approve(address spender, uint256 amount) external returns (bool);
72
function safeApprove(address spender, uint256 amount) external;
73
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
74
function decimals() external view returns (uint8);
75
function mint(uint256 amount) external;
76
function burn(uint256 amount) external;
77
}
Copied!
Copy link