金融供应链智能合约 -- 智能合约实例

迈不过友情╰ 2023-10-14 14:07 129阅读 0赞

前提

Ownable:监管者合约,有一个函数能转让监管者。

SupplyChainFin:供应链金融合约,银行、公司信息上链,公司和银行之间的转账。

发票:记录者交易双方和交易金额等的一种记录数据。如:我在超市买了一瓶水,超市给我开了一张发票。

Ownable

  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.4 <=0.9;
  3. /*
  4. *@title Ownable
  5. *@dev
  6. */
  7. contract Ownable{
  8. address public owner; // 监管者
  9. event OwnershipTransferred( // 监管者转让结构体
  10. address indexed priviousOwner, // indexed表名可以被索引
  11. address indexed newOwner
  12. );
  13. constructor() {
  14. owner = msg.sender;
  15. }
  16. // 判断用户是否是监管者
  17. modifier onlyOwner(){
  18. require(msg.sender == owner,"You cannot owner!");
  19. _;
  20. }
  21. // 转让所有权,必须是原先监管者转让
  22. function transferOwnership(address newOwner) public onlyOwner{
  23. require(newOwner != owner && newOwner != address(0),"newOwner cannot be empty and equal to the priviousOwner");
  24. emit OwnershipTransferred(owner, newOwner);
  25. owner = newOwner;
  26. }
  27. }

SupplyChainFin

  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.4 <=0.9;
  3. import "./Ownable.sol";
  4. /**
  5. *@title SuppluChainFin
  6. *@dev
  7. */
  8. contract SupplyChainFin is Ownable{
  9. // 监管者信息结构体
  10. struct Supervisor{
  11. string supervisorName;
  12. address supervisorAddress;
  13. }
  14. // 公司信息结构体
  15. struct Company{
  16. string companyName;
  17. address companyAddress;
  18. uint creditAsset;
  19. uint[] acceptReceiptIndex;
  20. uint[] sendReceiptIndex;
  21. }
  22. // 银行信息结构体
  23. struct Bank{
  24. string bankName;
  25. address bankAddress;
  26. uint creditAsset;
  27. uint[] acceptReceiptIndex;
  28. uint[] sendReceiptIndex;
  29. }
  30. // 数字发票收据信息
  31. struct Receipt {
  32. address senderAddress;
  33. address accepterAddress;
  34. uint8 receiptType; // 发票类型
  35. uint8 transferType; // 转账类型
  36. uint amount; // 交易额
  37. }
  38. // 公司的map ,用于快速搜索
  39. mapping(address => Company) companyMap;
  40. // 银行map
  41. mapping (address =>Bank) bankMap;
  42. // 发票的map
  43. mapping (uint => Receipt) receiptMap;
  44. //监管者实体
  45. Supervisor public supervisor;
  46. // 公司地址的数组
  47. address[] public companies;
  48. // 银行地址的数组
  49. address[] public banks;
  50. //数组发票索引
  51. uint public receiptIndex;
  52. constructor(string memory name){
  53. supervisor = Supervisor(name,msg.sender); // 初始化监管者信息
  54. }
  55. // 将公司信息添加到智能合约中
  56. function addCompany(
  57. string memory name,
  58. address companyAddress
  59. )public payable returns(bool){
  60. // 初始化公司结构体
  61. // 添加到公司map
  62. // 添加到公司数组
  63. Company memory newCompany = Company(
  64. name,
  65. companyAddress,
  66. msg.value,
  67. new uint[](0),
  68. new uint[](0)
  69. );
  70. companyMap[companyAddress] = newCompany;
  71. companies.push(companyAddress);
  72. return true;
  73. }
  74. // 获取公司信息
  75. function getCompany(
  76. address companyAddress
  77. ) public view returns(string memory,address,uint,uint[] memory,uint[] memory){
  78. // 用地址拿出公司结构体
  79. // 将需要的数据一起返回
  80. Company memory company = companyMap[companyAddress];
  81. return (
  82. company.companyName,
  83. company.companyAddress,
  84. company.creditAsset,
  85. company.acceptReceiptIndex,
  86. company.sendReceiptIndex
  87. );
  88. }
  89. // 添加银行信息上链
  90. function addBank(
  91. string memory bankName,
  92. address bankAddress
  93. ) public payable returns(bool){
  94. Bank memory newBank;
  95. newBank.bankName = bankName;
  96. newBank.bankAddress = bankAddress;
  97. newBank.creditAsset = msg.value;
  98. bankMap[bankAddress] = newBank;
  99. banks.push(bankAddress);
  100. return true;
  101. }
  102. // 获取银行信息
  103. function getBank(
  104. address bankAddress
  105. ) public view returns(string memory,address,uint,uint[] memory,uint[] memory){
  106. Bank memory bank = bankMap[bankAddress];
  107. return (
  108. bank.bankName,
  109. bank.bankAddress,
  110. bank.creditAsset,
  111. bank.acceptReceiptIndex,
  112. bank.sendReceiptIndex
  113. );
  114. }
  115. // 获取公司全部地址
  116. function getAllCompanyAddress() public view returns(address[] memory){
  117. return companies;
  118. }
  119. // 获取银行全部地址
  120. function getAllBankAddress() public view returns(address[] memory){
  121. return banks;
  122. }
  123. // 获取凭证
  124. function getRecipt(
  125. uint index
  126. )public view returns(address,address,uint8,uint8,uint){
  127. Receipt memory receipt = receiptMap[index];
  128. return (
  129. receipt.senderAddress,
  130. receipt.accepterAddress,
  131. receipt.receiptType,
  132. receipt.transferType,
  133. receipt.amount
  134. );
  135. }
  136. //存证交易
  137. // receiptType: 发票类型(存证、现金)
  138. //1: 交易类型为存证
  139. //2:交易类型为现金
  140. // transferType: 交易类型
  141. //1: 银行转账给公司
  142. //2: 公司与公司间转账
  143. //3: 公司转账给银行
  144. // 银行向公司交易(公司颁布凭证):
  145. function bankToCompanyReceipt(
  146. address senderAddress, // 凭证发送方
  147. address accepterAddress, // 凭证接受方
  148. uint amount, // 交易额
  149. uint8 receiptType // 凭证类型
  150. ) public returns(uint){
  151. // 银行转账给公司,银行是发票接受者,只有银行同意要发票,这笔交易才能执行
  152. require(msg.sender == accepterAddress,"The function caller must be accper");
  153. // 拿出银行、公司结构体
  154. Company memory company = companyMap[senderAddress];
  155. Bank memory bank = bankMap[accepterAddress];
  156. // 判断公司银行是否存在
  157. if(keccak256(bytes(bank.bankName)) == keccak256(bytes(""))){
  158. return 404001;
  159. }
  160. if(keccak256(bytes(company.companyName)) == keccak256(bytes(""))){
  161. return 404002;
  162. }
  163. // 判断银行资产是否小于转账额
  164. if(bank.creditAsset < amount){
  165. return 500001;
  166. }
  167. // 初始化凭证
  168. Receipt memory newReceipt = Receipt(
  169. senderAddress,
  170. accepterAddress,
  171. receiptType,
  172. 1,
  173. amount
  174. );
  175. // 发票索引 + 1
  176. receiptIndex += 1;
  177. // 根据转账额,相互的 +-
  178. companyMap[accepterAddress].creditAsset += amount;
  179. bankMap[senderAddress].creditAsset -= amount;
  180. // 存凭证索引,这样我们拿到公司或银行信息,拿到发票索引,在拿到发票结构体
  181. receiptMap[receiptIndex] = newReceipt;
  182. companyMap[accepterAddress].sendReceiptIndex.push(receiptIndex);
  183. bankMap[senderAddress].acceptReceiptIndex.push(receiptIndex);
  184. return 200;
  185. }
  186. //公司向公司交易(接受钱的公司需要颁布凭证)
  187. function companyToCompanyReceipt(
  188. address senderAddress,
  189. address accepterAddress,
  190. uint amount,
  191. uint8 receiptType
  192. ) public returns(uint){
  193. require(msg.sender == accepterAddress);
  194. Company memory senderCompany = companyMap[senderAddress];
  195. Company memory accepterCompany = companyMap[accepterAddress];
  196. if (keccak256(bytes(senderCompany.companyName)) == keccak256(bytes(""))) {
  197. return 404001;
  198. }
  199. //确认接收公司存在
  200. if (keccak256(bytes(accepterCompany.companyName)) == keccak256(bytes(""))) {
  201. return 404002;
  202. }
  203. //如果存证接收的公司资产小于存证数额,那么就不能交易发送存证
  204. if (accepterCompany.creditAsset < amount) {
  205. return 500001;
  206. }
  207. //创建存证
  208. Receipt memory newReceipt = Receipt(
  209. senderAddress,
  210. accepterAddress,
  211. receiptType,
  212. 2,
  213. amount
  214. );
  215. receiptIndex += 1;
  216. //记录存证(存证Map,公司Map对应地址的发送和接收存证列表)
  217. receiptMap[receiptIndex] = newReceipt;
  218. companyMap[senderAddress].sendReceiptIndex.push(receiptIndex);
  219. companyMap[accepterAddress].acceptReceiptIndex.push(receiptIndex);
  220. companyMap[senderAddress].creditAsset += amount;
  221. companyMap[accepterAddress].creditAsset -= amount;
  222. return 200;
  223. }
  224. //公司与银行交易(银行颁布凭证)
  225. function companyToBankReceipt(
  226. address senderAddress,
  227. address accepterAddress,
  228. uint amount,
  229. uint8 receiptType
  230. ) public returns (uint) {
  231. require(msg.sender == accepterAddress);
  232. Bank memory bank = bankMap[senderAddress];
  233. Company memory accepterCompany = companyMap[accepterAddress];
  234. //确认发送公司存在
  235. if (keccak256(bytes(bank.bankName)) == keccak256(bytes(""))) {
  236. return 404001;
  237. }
  238. //确认接收公司存在
  239. if (keccak256(bytes(accepterCompany.companyName)) == keccak256(bytes(""))) {
  240. return 404002;
  241. }
  242. //如果存证接收的公司资产小于存证数额,那么就不能交易发送存证
  243. if (accepterCompany.creditAsset < amount) {
  244. return 500001;
  245. }
  246. //创建存证
  247. Receipt memory newReceipt = Receipt(
  248. senderAddress,
  249. accepterAddress,
  250. receiptType,
  251. 3,
  252. amount
  253. );
  254. receiptIndex += 1;
  255. //记录存证(存证Map,公司Map对应地址的发送和接收存证列表)
  256. receiptMap[receiptIndex] = newReceipt;
  257. bankMap[senderAddress].sendReceiptIndex.push(receiptIndex);
  258. companyMap[accepterAddress].acceptReceiptIndex.push(receiptIndex);
  259. bankMap[senderAddress].creditAsset += amount;
  260. companyMap[accepterAddress].creditAsset -= amount;
  261. return 200;
  262. }
  263. }

发表评论

表情:
评论列表 (有 0 条评论,129人围观)

还没有评论,来说两句吧...

相关阅读

    相关 智能合约

    智能合约(英语:Smart contract )是一种旨在以信息化方式传播、验证或执行合同的计算机协议。智能合约允许在没有第三方的情况下进行可信交易,这些交易可追踪且不可逆转。

    相关 区块 智能合约 简介

    根据谷歌趋势数据显示,目前,程序员对智能合约编程的兴趣已经处于历史最高水平,其中中国高居全球榜首,随着区块链技术的发展,相信日后智能合约将会与我们的生活密切相关,今天就为大家介