Skip to content

Commit

Permalink
尝试调用合约
Browse files Browse the repository at this point in the history
  • Loading branch information
andOneBasketball committed Oct 6, 2024
1 parent 32f1792 commit 0822f0d
Show file tree
Hide file tree
Showing 8 changed files with 800 additions and 1 deletion.
3 changes: 2 additions & 1 deletion account_balance.go → accountBalance.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
}
fmt.Println(balance) // 25893180161173005034

blockNumber := big.NewInt(0)
blockNumber := big.NewInt(37)
balanceAt, err := client.BalanceAt(context.Background(), account, blockNumber)
if err != nil {
log.Fatal(err)
Expand All @@ -36,6 +36,7 @@ func main() {
ethValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18)))
fmt.Println(ethValue) // 25.729324269165216041

// PendingBalanceAt 获取的是挂起的余额
pendingBalance, err := client.PendingBalanceAt(context.Background(), account)
fmt.Println(pendingBalance) // 25729324269165216042
}
1 change: 1 addition & 0 deletions contracts/dutchAuction/DutchAuction.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"address","name":"_nftAddress","type":"address"},{"internalType":"address payable","name":"_seller","type":"address"},{"internalType":"uint256","name":"_tokenId","type":"uint256"},{"internalType":"uint256","name":"_startingPrice","type":"uint256"},{"internalType":"uint256","name":"_discountRate","type":"uint256"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"string","name":"message","type":"string"}],"name":"AuctionEnded","type":"error"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"bool","name":"success","type":"bool"},{"indexed":false,"internalType":"uint256","name":"amount","type":"uint256"}],"name":"Refund","type":"event"},{"inputs":[],"name":"DURATION","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"buy","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"discountRate","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"endTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getBalance","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"getPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"isSold","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"nftAddress","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"seller","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"startTime","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"startingPrice","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"tokenId","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"}]
1 change: 1 addition & 0 deletions contracts/dutchAuction/DutchAuction.bin
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
610120604052348015610010575f5ffd5b50604051610e8f380380610e8f83398181016040528101906100329190610205565b8473ffffffffffffffffffffffffffffffffffffffff1660808173ffffffffffffffffffffffffffffffffffffffff1681525050835f5f6101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508260a081815250508160c081815250508060e0818152505042610100818152505062093a80426100d591906102a9565b60018190555062093a8060e0516100ec91906102dc565b60c0511161012f576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016101269061039d565b60405180910390fd5b50505050506103bb565b5f5ffd5b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6101668261013d565b9050919050565b6101768161015c565b8114610180575f5ffd5b50565b5f815190506101918161016d565b92915050565b5f6101a18261013d565b9050919050565b6101b181610197565b81146101bb575f5ffd5b50565b5f815190506101cc816101a8565b92915050565b5f819050919050565b6101e4816101d2565b81146101ee575f5ffd5b50565b5f815190506101ff816101db565b92915050565b5f5f5f5f5f60a0868803121561021e5761021d610139565b5b5f61022b88828901610183565b955050602061023c888289016101be565b945050604061024d888289016101f1565b935050606061025e888289016101f1565b925050608061026f888289016101f1565b9150509295509295909350565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f6102b3826101d2565b91506102be836101d2565b92508282019050808211156102d6576102d561027c565b5b92915050565b5f6102e6826101d2565b91506102f1836101d2565b92508282026102ff816101d2565b915082820484148315176103165761031561027c565b5b5092915050565b5f82825260208201905092915050565b7f5374617274696e672070726963652073686f756c6420626520677265617465725f8201527f207468616e203000000000000000000000000000000000000000000000000000602082015250565b5f61038760278361031d565b91506103928261032d565b604082019050919050565b5f6020820190508181035f8301526103b48161037b565b9050919050565b60805160a05160c05160e05161010051610a7461041b5f395f818161031b01526103bd01525f818161039c015261069c01525f81816103f3015261067801525f81816102c6015261052c01525f81816102f701526104cd0152610a745ff3fe6080604052600436106100a6575f3560e01c806378e979251161006357806378e97925146101a657806398d5fdca146101d0578063a6f2ae3a146101fa578063d6fbf20214610204578063e6c0e6d51461022e578063e852e74114610258576100a6565b806308551a53146100aa57806312065fe0146100d457806317d70f7c146100fe5780631be05289146101285780633197cbb6146101525780635bf8633a1461017c575b5f5ffd5b3480156100b5575f5ffd5b506100be610282565b6040516100cb919061070f565b60405180910390f35b3480156100df575f5ffd5b506100e86102a6565b6040516100f59190610740565b60405180910390f35b348015610109575f5ffd5b506101126102c4565b60405161011f9190610740565b60405180910390f35b348015610133575f5ffd5b5061013c6102e8565b6040516101499190610740565b60405180910390f35b34801561015d575f5ffd5b506101666102ef565b6040516101739190610740565b60405180910390f35b348015610187575f5ffd5b506101906102f5565b60405161019d9190610779565b60405180910390f35b3480156101b1575f5ffd5b506101ba610319565b6040516101c79190610740565b60405180910390f35b3480156101db575f5ffd5b506101e461033d565b6040516101f19190610740565b60405180910390f35b610202610421565b005b34801561020f575f5ffd5b50610218610676565b6040516102259190610740565b60405180910390f35b348015610239575f5ffd5b5061024261069a565b60405161024f9190610740565b60405180910390f35b348015610263575f5ffd5b5061026c6106be565b60405161027991906107ac565b60405180910390f35b5f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b5f3373ffffffffffffffffffffffffffffffffffffffff1631905090565b7f000000000000000000000000000000000000000000000000000000000000000081565b62093a8081565b60015481565b7f000000000000000000000000000000000000000000000000000000000000000081565b7f000000000000000000000000000000000000000000000000000000000000000081565b5f6001544210158061035a57505f60149054906101000a900460ff165b1561039a576040517f4d9638fc0000000000000000000000000000000000000000000000000000000081526004016103919061081f565b60405180910390fd5b7f00000000000000000000000000000000000000000000000000000000000000007f0000000000000000000000000000000000000000000000000000000000000000426103e7919061086a565b6103f1919061089d565b7f000000000000000000000000000000000000000000000000000000000000000061041c919061086a565b905090565b6001544210158061043d57505f60149054906101000a900460ff165b1561047d576040517f4d9638fc0000000000000000000000000000000000000000000000000000000081526004016104749061081f565b60405180910390fd5b5f61048661033d565b9050803410156104cb576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004016104c290610928565b60405180910390fd5b7f000000000000000000000000000000000000000000000000000000000000000073ffffffffffffffffffffffffffffffffffffffff166323b872dd825f5f9054906101000a900473ffffffffffffffffffffffffffffffffffffffff16337f00000000000000000000000000000000000000000000000000000000000000006040518563ffffffff1660e01b8152600401610569939291906109a1565b5f604051808303818588803b158015610580575f5ffd5b505af1158015610592573d5f5f3e3d5ffd5b50505050505f81346105a4919061086a565b90504260018190555060015f60146101000a81548160ff0219169083151502179055505f811115610672575f3373ffffffffffffffffffffffffffffffffffffffff16826040516105f490610a03565b5f6040518083038185875af1925050503d805f811461062e576040519150601f19603f3d011682016040523d82523d5f602084013e610633565b606091505b505090507fbe74181457391058f21eeb20d41d41f37464c7b5b40f03b4e402e4ea16a1f48e8183604051610668929190610a17565b60405180910390a1505b5050565b7f000000000000000000000000000000000000000000000000000000000000000081565b7f000000000000000000000000000000000000000000000000000000000000000081565b5f60149054906101000a900460ff1681565b5f73ffffffffffffffffffffffffffffffffffffffff82169050919050565b5f6106f9826106d0565b9050919050565b610709816106ef565b82525050565b5f6020820190506107225f830184610700565b92915050565b5f819050919050565b61073a81610728565b82525050565b5f6020820190506107535f830184610731565b92915050565b5f610763826106d0565b9050919050565b61077381610759565b82525050565b5f60208201905061078c5f83018461076a565b92915050565b5f8115159050919050565b6107a681610792565b82525050565b5f6020820190506107bf5f83018461079d565b92915050565b5f82825260208201905092915050565b7f41756374696f6e20656e646564000000000000000000000000000000000000005f82015250565b5f610809600d836107c5565b9150610814826107d5565b602082019050919050565b5f6020820190508181035f830152610836816107fd565b9050919050565b7f4e487b71000000000000000000000000000000000000000000000000000000005f52601160045260245ffd5b5f61087482610728565b915061087f83610728565b92508282039050818111156108975761089661083d565b5b92915050565b5f6108a782610728565b91506108b283610728565b92508282026108c081610728565b915082820484148315176108d7576108d661083d565b5b5092915050565b7f4e6f7420656e6f756768206574686572000000000000000000000000000000005f82015250565b5f6109126010836107c5565b915061091d826108de565b602082019050919050565b5f6020820190508181035f83015261093f81610906565b9050919050565b5f819050919050565b5f61096961096461095f846106d0565b610946565b6106d0565b9050919050565b5f61097a8261094f565b9050919050565b5f61098b82610970565b9050919050565b61099b81610981565b82525050565b5f6060820190506109b45f830186610992565b6109c1602083018561076a565b6109ce6040830184610731565b949350505050565b5f81905092915050565b50565b5f6109ee5f836109d6565b91506109f9826109e0565b5f82019050919050565b5f610a0d826109e3565b9150819050919050565b5f604082019050610a2a5f83018561079d565b610a376020830184610731565b939250505056fea2646970667358221220af00c5966cd77d3f37166227ac431ba0d40b2af3ae6867d84132b5072264f6ce64736f6c634300081b0033
1 change: 1 addition & 0 deletions contracts/dutchAuction/IERC721.abi
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"transferFrom","outputs":[],"stateMutability":"payable","type":"function"}]
Empty file.
64 changes: 64 additions & 0 deletions contracts/dutchAuction/dutchAuction.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.27;

interface IERC721 {
function transferFrom(
address from,
address to,
uint256 tokenId
) external payable;
}

contract DutchAuction {
address immutable public nftAddress;
address payable public seller;
uint256 immutable public tokenId;
bool public isSold;
uint256 immutable public startingPrice;
uint256 immutable public discountRate;
uint256 immutable public startTime;
uint256 constant public DURATION = 7 days;
uint256 public endTime;

error AuctionEnded(string message);
event Refund(bool success, uint256 amount);

modifier auctionEnded() {
if (block.timestamp >= endTime || isSold) {
revert AuctionEnded("Auction ended");
}
_;
}

constructor(address _nftAddress, address payable _seller, uint256 _tokenId, uint256 _startingPrice, uint256 _discountRate) {
nftAddress = _nftAddress;
seller = _seller;
tokenId = _tokenId;
startingPrice = _startingPrice;
discountRate = _discountRate;
startTime = block.timestamp;
endTime = block.timestamp + DURATION;
require(startingPrice > discountRate * DURATION, "Starting price should be greater than 0");
}

function getPrice() public view auctionEnded returns (uint256) {
return startingPrice - (block.timestamp - startTime) * discountRate;
}

function buy() public auctionEnded payable {
uint256 price = getPrice();
require(msg.value >= price, "Not enough ether");
IERC721(nftAddress).transferFrom{value: price}(seller, msg.sender, tokenId);
uint256 refund = msg.value - price;
endTime = block.timestamp;
isSold = true;
if (refund > 0) {
(bool success, ) = payable(msg.sender).call{value: refund}("");
emit Refund(success, refund);
}
}

function getBalance() public view returns (uint256) {
return address(msg.sender).balance;
}
}
Loading

0 comments on commit 0822f0d

Please sign in to comment.