Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
assert_ok!(Crowdfund::fix_deploy_data(
Origin::signed(1),
0,
<Test as system::Trait>::Hash::default(),
vec![0]
));
// Fund crowdfund
assert_ok!(Crowdfund::contribute(Origin::signed(2), 0, 1000));
run_to_block(10);
// Cannot retire fund that is not onboarded
assert_noop!(Crowdfund::begin_retirement(Origin::signed(1), 0), "fund has no parachain");
// Onboard crowdfund
assert_ok!(Crowdfund::onboard(Origin::signed(1), 0, 0.into()));
// Fund is assigned a parachain id
let fund = Crowdfund::funds(0).unwrap();
assert_eq!(fund.parachain, Some(0.into()));
// Cannot retire fund whose deposit has not been returned
assert_noop!(Crowdfund::begin_retirement(Origin::signed(1), 0), "parachain still has deposit");
run_to_block(50);
// Cannot retire invalid fund index
assert_noop!(Crowdfund::begin_retirement(Origin::signed(1), 1), "invalid fund index");
// Cannot retire twice
assert_ok!(Crowdfund::begin_retirement(Origin::signed(1), 0));
assert_noop!(Crowdfund::begin_retirement(Origin::signed(1), 0), "fund has no parachain");
});
}
#[test]
fn withdraw_works() {
new_test_ext().execute_with(|| {
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
// Set up a crowdfund
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
assert_ok!(Crowdfund::create(Origin::signed(1), 1000, 1, 4, 9));
// Transfer fee is taken here
assert_ok!(Crowdfund::contribute(Origin::signed(1), 0, 100));
assert_ok!(Crowdfund::contribute(Origin::signed(2), 0, 200));
assert_ok!(Crowdfund::contribute(Origin::signed(3), 0, 300));
// Skip all the way to the end
run_to_block(50);
// User can withdraw their full balance without fees
assert_ok!(Crowdfund::withdraw(Origin::signed(1), 0));
assert_eq!(Balances::free_balance(1), 989);
assert_ok!(Crowdfund::withdraw(Origin::signed(2), 0));
assert_eq!(Balances::free_balance(2), 1990);
assert_ok!(Crowdfund::withdraw(Origin::signed(3), 0));
assert_eq!(Balances::free_balance(3), 2990);
});
}
#[test]
fn withdraw_handles_basic_errors() {
new_test_ext().execute_with(|| {
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
// Set up a crowdfund
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
assert_ok!(Crowdfund::create(Origin::signed(1), 1000, 1, 4, 9));
// Transfer fee is taken here
assert_ok!(Crowdfund::contribute(Origin::signed(1), 0, 49));
assert_eq!(Balances::free_balance(1), 940);
run_to_block(5);
// Cannot withdraw before fund ends
assert_noop!(Crowdfund::withdraw(Origin::signed(1), 0), "fund has not ended");
run_to_block(10);
// Cannot withdraw if they did not contribute
assert_noop!(Crowdfund::withdraw(Origin::signed(2), 0), "no contributions stored");
// Cannot withdraw from a non-existent fund
assert_noop!(Crowdfund::withdraw(Origin::signed(1), 1), "invalid fund index");
});
}
#[test]
fn dissolve_works() {
new_test_ext().execute_with(|| {
// Set up a crowdfund
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
assert_ok!(Crowdfund::create(Origin::signed(1), 1000, 1, 4, 9));
// Transfer fee is taken here
assert_ok!(Crowdfund::contribute(Origin::signed(1), 0, 100));
assert_ok!(Crowdfund::contribute(Origin::signed(2), 0, 200));
assert_ok!(Crowdfund::contribute(Origin::signed(3), 0, 300));
// Skip all the way to the end
run_to_block(50);
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
// Check current funds (contributions + deposit)
assert_eq!(Balances::free_balance(Crowdfund::fund_account_id(0)), 601);
// Dissolve the crowdfund
assert_ok!(Crowdfund::dissolve(Origin::signed(1), 0));
// Fund account is emptied
assert_eq!(Balances::free_balance(Crowdfund::fund_account_id(0)), 0);
// Deposit is returned
assert_eq!(Balances::free_balance(1), 890);
// Treasury account is filled
assert_eq!(Balances::free_balance(Treasury::account_id()), 600);
// Storage trie is removed
assert_eq!(Crowdfund::contribution_get(0,&0), 0);
// Fund storage is removed
assert_eq!(Crowdfund::funds(0), None);
});
}
#[test]
fn dissolve_handles_basic_errors() {
new_test_ext().execute_with(|| {
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
// Set up a crowdfund
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
assert_ok!(Crowdfund::create(Origin::signed(1), 1000, 1, 4, 9));
// Transfer fee is taken here
assert_ok!(Crowdfund::contribute(Origin::signed(1), 0, 100));
assert_ok!(Crowdfund::contribute(Origin::signed(2), 0, 200));
assert_ok!(Crowdfund::contribute(Origin::signed(3), 0, 300));
// Cannot dissolve an invalid fund index
assert_noop!(Crowdfund::dissolve(Origin::signed(1), 1), "invalid fund index");
// Cannot dissolve a fund in progress
assert_noop!(Crowdfund::dissolve(Origin::signed(1), 0), "retirement period not over");
run_to_block(10);
// Onboard fund
assert_ok!(Crowdfund::fix_deploy_data(
Origin::signed(1),
0,
<Test as system::Trait>::Hash::default(),
vec![0]
));
assert_ok!(Crowdfund::onboard(Origin::signed(1), 0, 0.into()));
// Cannot dissolve an active fund
assert_noop!(Crowdfund::dissolve(Origin::signed(1), 0), "cannot dissolve fund with active parachain");
});
}
#[test]
fn fund_before_auction_works() {
new_test_ext().execute_with(|| {
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
// Create a crowdfund before an auction is created
assert_ok!(Crowdfund::create(Origin::signed(1), 1000, 1, 4, 9));
// Users can already contribute
assert_ok!(Crowdfund::contribute(Origin::signed(1), 0, 49));
// Fund added to NewRaise
assert_eq!(Crowdfund::new_raise(), vec![0]);
// Some blocks later...
run_to_block(2);
// Create an auction
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
// Add deploy data
assert_ok!(Crowdfund::fix_deploy_data(
Origin::signed(1),
0,
<Test as system::Trait>::Hash::default(),
vec![0]
));
// Move to the end of auction...
run_to_block(12);
// Endings count incremented
assert_eq!(Crowdfund::endings_count(), 1);
// Onboard crowdfund
assert_ok!(Crowdfund::onboard(Origin::signed(1), 0, 0.into()));
let fund = Crowdfund::funds(0).unwrap();
// Crowdfund is now assigned a parachain id
assert_eq!(fund.parachain, Some(0.into()));
// This parachain is managed by Slots
assert_eq!(Slots::managed_ids(), vec![0.into()]);
});
}
#[test]
fn fund_across_multiple_auctions_works() {
new_test_ext().execute_with(|| {
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
// Create an auction
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
// Create two competing crowdfunds, with end dates across multiple auctions
// Each crowdfund is competing for the same slots, so only one can win
assert_ok!(Crowdfund::create(Origin::signed(1), 1000, 1, 4, 30));
assert_ok!(Crowdfund::create(Origin::signed(2), 1000, 1, 4, 30));
// Contribute to all, but more money to 0, less to 1
assert_ok!(Crowdfund::contribute(Origin::signed(1), 0, 300));
assert_ok!(Crowdfund::contribute(Origin::signed(1), 1, 200));
// Add deploy data to all
assert_ok!(Crowdfund::fix_deploy_data(
Origin::signed(1),
0,
<Test as system::Trait>::Hash::default(),
vec![0]
));
assert_ok!(Crowdfund::fix_deploy_data(
Origin::signed(2),
1,
<Test as system::Trait>::Hash::default(),
vec![0]
));
// End the current auction, fund 0 wins!
run_to_block(10);
assert_eq!(Crowdfund::endings_count(), 1);
// Onboard crowdfund
assert_ok!(Crowdfund::onboard(Origin::signed(1), 0, 0.into()));
let fund = Crowdfund::funds(0).unwrap();
// Crowdfund is now assigned a parachain id
assert_eq!(fund.parachain, Some(0.into()));
// This parachain is managed by Slots
assert_eq!(Slots::managed_ids(), vec![0.into()]);
// Create a second auction
assert_ok!(Slots::new_auction(Origin::ROOT, 5, 1));
// Contribute to existing funds add to NewRaise
assert_ok!(Crowdfund::contribute(Origin::signed(1), 1, 10));
// End the current auction, fund 1 wins!
run_to_block(20);
assert_eq!(Crowdfund::endings_count(), 2);
// Onboard crowdfund
assert_ok!(Crowdfund::onboard(Origin::signed(2), 1, 1.into()));
let fund = Crowdfund::funds(1).unwrap();
// Crowdfund is now assigned a parachain id
assert_eq!(fund.parachain, Some(1.into()));
// This parachain is managed by Slots
assert_eq!(Slots::managed_ids(), vec![0.into(), 1.into()]);
});
}
}