Untitled
unknown
c_cpp
2 years ago
2.6 kB
16
Indexable
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract TradeFinance {
struct Escrow {
address alice; // Exporter
address gamma; // Delivery company
uint256 aliceAmount;
uint256 gammaAmount;
uint256 nftId; // For Stretch Flow
bool nftTransferred; // For Stretch Flow
}
mapping(address => mapping(uint256 => Escrow)) public escrows; // Importer's address to escrow ID to Escrow
mapping(uint256 => address) public nftOwners;
uint256 public currentNFTId = 0;
function lockEscrow(
uint256 aliceAmount,
uint256 gammaAmount,
address aliceAddress,
address gammaAddress
) external payable returns (uint256) {
require(
msg.value == aliceAmount + gammaAmount,
"Incorrect amount sent."
);
Escrow memory newEscrow = Escrow({
alice: aliceAddress,
gamma: gammaAddress,
aliceAmount: aliceAmount,
gammaAmount: gammaAmount,
nftId: currentNFTId,
nftTransferred: false
});
uint256 escrowId = currentNFTId;
escrows[msg.sender][escrowId] = newEscrow;
nftOwners[newEscrow.nftId] = aliceAddress; // Mint the NFT to Alice
currentNFTId += 1;
return escrowId;
}
function transferNFTToGamma(uint256 escrowId) external {
Escrow storage escrow = escrows[msg.sender][escrowId];
require(escrow.alice != address(0), "Invalid escrow ID.");
require(
nftOwners[escrow.nftId] == escrow.alice,
"NFT not in Alice's possession."
);
nftOwners[escrow.nftId] = escrow.gamma;
}
function releaseEscrow(uint256 escrowId) external {
Escrow storage escrow = escrows[msg.sender][escrowId];
require(escrow.alice != address(0), "Invalid escrow ID.");
require(
nftOwners[escrow.nftId] == escrow.gamma,
"NFT must be with Gamma Delivery before release."
);
payable(escrow.alice).transfer(escrow.aliceAmount);
payable(escrow.gamma).transfer(escrow.gammaAmount);
nftOwners[escrow.nftId] = msg.sender; // Transfer NFT to Bob
escrow.nftTransferred = true; // Indicate NFT was transferred. (can also be seen as delivered)
// You may also want to delete the escrow once it's complete
// to save storage and gas.
delete escrows[msg.sender][escrowId];
}
function getNFTOwner(uint256 nftId) external view returns (address) {
return nftOwners[nftId];
}
}
Editor is loading...